| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 2 | /* |
| Michael Meeks | d037b81 | 2026-03-26 15:06:16 +0000 | [diff] [blame] | 3 | * This file is part of the Collabora Office project. |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include <QtInstanceMenuButton.hxx> |
| 11 | #include <QtInstanceMenuButton.moc> |
| 12 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 13 | #include <QtTools.hxx> |
| 14 | |
| 15 | #include <vcl/qt/QtUtils.hxx> |
| 16 | |
| 17 | #include <QtWidgets/QMenu> |
| 18 | |
| 19 | QtInstanceMenuButton::QtInstanceMenuButton(QToolButton* pButton) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 20 | : QtInstanceToggleButton(pButton) |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 21 | , m_pToolButton(pButton) |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 22 | , m_pPopover(nullptr) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 23 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 24 | assert(m_pToolButton); |
| Michael Weghorn | 2ef30328 | 2025-01-14 17:30:48 +0100 | [diff] [blame] | 25 | |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 26 | if (m_pToolButton->menu()) |
| 27 | connect(m_pToolButton->menu(), &QMenu::triggered, this, |
| 28 | &QtInstanceMenuButton::handleMenuItemTriggered); |
| 29 | |
| 30 | connect(m_pToolButton, &QToolButton::clicked, this, &QtInstanceMenuButton::handleButtonClicked); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| Michael Weghorn | 1b14ace | 2025-01-14 17:00:53 +0100 | [diff] [blame] | 33 | void QtInstanceMenuButton::insert_item(int nPos, const OUString& rId, const OUString& rStr, |
| 34 | const OUString* pIconName, VirtualDevice* pImageSurface, |
| 35 | TriState eCheckRadioFalse) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 36 | { |
| Michael Weghorn | 1b14ace | 2025-01-14 17:00:53 +0100 | [diff] [blame] | 37 | SolarMutexGuard g; |
| 38 | |
| 39 | assert(eCheckRadioFalse == TRISTATE_INDET && "Param not handled yet"); |
| 40 | (void)eCheckRadioFalse; |
| 41 | |
| 42 | GetQtInstance().RunInMainThread([&] { |
| Michael Weghorn | 12c6bf0 | 2025-01-27 21:46:17 +0100 | [diff] [blame] | 43 | QAction* pAction = new QAction(vclToQtStringWithAccelerator(rStr), &getMenu()); |
| Michael Weghorn | 1b14ace | 2025-01-14 17:00:53 +0100 | [diff] [blame] | 44 | pAction->setObjectName(toQString(rId)); |
| 45 | |
| 46 | if (pIconName) |
| 47 | pAction->setIcon(loadQPixmapIcon(*pIconName)); |
| 48 | else if (pImageSurface) |
| 49 | pAction->setIcon(toQPixmap(*pImageSurface)); |
| Michael Weghorn | 12c6bf0 | 2025-01-27 21:46:17 +0100 | [diff] [blame] | 50 | |
| 51 | insertAction(pAction, nPos); |
| Michael Weghorn | 1b14ace | 2025-01-14 17:00:53 +0100 | [diff] [blame] | 52 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| Michael Weghorn | 12c6bf0 | 2025-01-27 21:46:17 +0100 | [diff] [blame] | 55 | void QtInstanceMenuButton::insert_separator(int nPos, const OUString& rId) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 56 | { |
| Michael Weghorn | 12c6bf0 | 2025-01-27 21:46:17 +0100 | [diff] [blame] | 57 | SolarMutexGuard g; |
| 58 | |
| 59 | GetQtInstance().RunInMainThread([&] { |
| 60 | QAction* pAction = new QAction(&getMenu()); |
| 61 | pAction->setSeparator(true); |
| 62 | pAction->setObjectName(toQString(rId)); |
| 63 | |
| 64 | insertAction(pAction, nPos); |
| 65 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 68 | void QtInstanceMenuButton::remove_item(const OUString& rId) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 69 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 70 | SolarMutexGuard g; |
| 71 | |
| 72 | GetQtInstance().RunInMainThread([&] { |
| 73 | if (QAction* pAction = getAction(rId)) |
| 74 | getMenu().removeAction(pAction); |
| 75 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 78 | void QtInstanceMenuButton::clear() |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 79 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 80 | SolarMutexGuard g; |
| 81 | |
| 82 | GetQtInstance().RunInMainThread([&] { getMenu().clear(); }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 85 | void QtInstanceMenuButton::set_item_sensitive(const OUString& rIdent, bool bSensitive) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 86 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 87 | SolarMutexGuard g; |
| 88 | |
| 89 | GetQtInstance().RunInMainThread([&] { |
| 90 | if (QAction* pAction = getAction(rIdent)) |
| 91 | pAction->setEnabled(bSensitive); |
| 92 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 95 | void QtInstanceMenuButton::set_item_active(const OUString& rIdent, bool bActive) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 96 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 97 | SolarMutexGuard g; |
| 98 | |
| 99 | GetQtInstance().RunInMainThread([&] { |
| 100 | if (QAction* pAction = getAction(rIdent)) |
| 101 | pAction->setChecked(bActive); |
| 102 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 105 | void QtInstanceMenuButton::set_item_label(const OUString& rIdent, const OUString& rLabel) |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 106 | { |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 107 | SolarMutexGuard g; |
| 108 | |
| 109 | GetQtInstance().RunInMainThread([&] { |
| 110 | if (QAction* pAction = getAction(rIdent)) |
| 111 | pAction->setText(toQString(rLabel)); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | OUString QtInstanceMenuButton::get_item_label(const OUString& rIdent) const |
| 116 | { |
| 117 | SolarMutexGuard g; |
| 118 | |
| 119 | OUString sLabel; |
| 120 | GetQtInstance().RunInMainThread([&] { |
| 121 | if (QAction* pAction = getAction(rIdent)) |
| 122 | sLabel = toOUString(pAction->text()); |
| 123 | }); |
| 124 | |
| 125 | return sLabel; |
| 126 | } |
| 127 | |
| 128 | void QtInstanceMenuButton::set_item_visible(const OUString& rIdent, bool bVisible) |
| 129 | { |
| 130 | SolarMutexGuard g; |
| 131 | |
| 132 | GetQtInstance().RunInMainThread([&] { |
| 133 | if (QAction* pAction = getAction(rIdent)) |
| 134 | pAction->setVisible(bVisible); |
| 135 | }); |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 138 | void QtInstanceMenuButton::set_popover(weld::Widget* pPopover) |
| 139 | { |
| 140 | QtInstanceWidget* pPopoverWidget = dynamic_cast<QtInstanceWidget*>(pPopover); |
| 141 | m_pPopover = pPopoverWidget ? pPopoverWidget->getQWidget() : nullptr; |
| 142 | } |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 143 | |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 144 | QMenu& QtInstanceMenuButton::getMenu() const |
| 145 | { |
| 146 | QMenu* pMenu = m_pToolButton->menu(); |
| 147 | assert(pMenu); |
| 148 | return *pMenu; |
| 149 | } |
| 150 | |
| 151 | QAction* QtInstanceMenuButton::getAction(const OUString& rIdent) const |
| 152 | { |
| Michael Weghorn | 5e38059 | 2025-01-27 22:06:29 +0100 | [diff] [blame] | 153 | const QList<QAction*> aActions = getMenu().actions(); |
| Michael Weghorn | 78872b2 | 2024-12-21 00:17:05 +0100 | [diff] [blame] | 154 | for (QAction* pAction : aActions) |
| 155 | { |
| 156 | if (pAction && pAction->objectName() == toQString(rIdent)) |
| 157 | return pAction; |
| 158 | } |
| 159 | |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| Michael Weghorn | 12c6bf0 | 2025-01-27 21:46:17 +0100 | [diff] [blame] | 163 | void QtInstanceMenuButton::insertAction(QAction* pAction, int nPos) |
| 164 | { |
| 165 | SolarMutexGuard g; |
| 166 | |
| 167 | GetQtInstance().RunInMainThread([&] { |
| 168 | QAction* pNextAction = nullptr; |
| 169 | QList<QAction*> pActions = getMenu().actions(); |
| 170 | if (nPos >= 0 && nPos < pActions.count()) |
| 171 | pNextAction = pActions.at(nPos); |
| 172 | getMenu().insertAction(pNextAction, pAction); |
| 173 | }); |
| 174 | } |
| 175 | |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 176 | void QtInstanceMenuButton::handleButtonClicked() |
| 177 | { |
| 178 | if (m_pPopover) |
| Michael Weghorn | 3a9dd7f | 2025-05-29 18:38:46 +0200 | [diff] [blame] | 179 | { |
| 180 | // show popup horizontally centered below the button |
| 181 | m_pPopover->adjustSize(); |
| 182 | QPoint aPos = m_pToolButton->mapToGlobal(QPoint(0, m_pToolButton->height())); |
| 183 | aPos.setX(aPos.x() + (m_pToolButton->width() - m_pPopover->width() / 2)); |
| 184 | m_pPopover->move(aPos); |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 185 | m_pPopover->show(); |
| Michael Weghorn | 3a9dd7f | 2025-05-29 18:38:46 +0200 | [diff] [blame] | 186 | } |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 187 | else |
| Michael Weghorn | 3a9dd7f | 2025-05-29 18:38:46 +0200 | [diff] [blame] | 188 | { |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 189 | m_pToolButton->showMenu(); |
| Michael Weghorn | 3a9dd7f | 2025-05-29 18:38:46 +0200 | [diff] [blame] | 190 | } |
| Michael Weghorn | 4108d5f | 2025-01-21 21:50:10 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| Michael Weghorn | 2ef30328 | 2025-01-14 17:30:48 +0100 | [diff] [blame] | 193 | void QtInstanceMenuButton::handleMenuItemTriggered(QAction* pAction) |
| 194 | { |
| 195 | SolarMutexGuard g; |
| 196 | |
| 197 | assert(pAction); |
| 198 | signal_selected(toOUString(pAction->objectName())); |
| 199 | } |
| 200 | |
| Michael Weghorn | a6f206c | 2024-12-20 21:05:20 +0100 | [diff] [blame] | 201 | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |