blob: 5f3a111a20393bfb68af3d95af88b0502bf6f61b [file] [log] [blame]
Michael Weghorna6f206c2024-12-20 21:05:20 +01001/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
Michael Meeksd037b812026-03-26 15:06:16 +00003 * This file is part of the Collabora Office project.
Michael Weghorna6f206c2024-12-20 21:05:20 +01004 *
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 Weghorn78872b22024-12-21 00:17:05 +010013#include <QtTools.hxx>
14
15#include <vcl/qt/QtUtils.hxx>
16
17#include <QtWidgets/QMenu>
18
19QtInstanceMenuButton::QtInstanceMenuButton(QToolButton* pButton)
Michael Weghorna6f206c2024-12-20 21:05:20 +010020 : QtInstanceToggleButton(pButton)
Michael Weghorn78872b22024-12-21 00:17:05 +010021 , m_pToolButton(pButton)
Michael Weghorn4108d5f2025-01-21 21:50:10 +010022 , m_pPopover(nullptr)
Michael Weghorna6f206c2024-12-20 21:05:20 +010023{
Michael Weghorn78872b22024-12-21 00:17:05 +010024 assert(m_pToolButton);
Michael Weghorn2ef303282025-01-14 17:30:48 +010025
Michael Weghorn4108d5f2025-01-21 21:50:10 +010026 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 Weghorna6f206c2024-12-20 21:05:20 +010031}
32
Michael Weghorn1b14ace2025-01-14 17:00:53 +010033void QtInstanceMenuButton::insert_item(int nPos, const OUString& rId, const OUString& rStr,
34 const OUString* pIconName, VirtualDevice* pImageSurface,
35 TriState eCheckRadioFalse)
Michael Weghorna6f206c2024-12-20 21:05:20 +010036{
Michael Weghorn1b14ace2025-01-14 17:00:53 +010037 SolarMutexGuard g;
38
39 assert(eCheckRadioFalse == TRISTATE_INDET && "Param not handled yet");
40 (void)eCheckRadioFalse;
41
42 GetQtInstance().RunInMainThread([&] {
Michael Weghorn12c6bf02025-01-27 21:46:17 +010043 QAction* pAction = new QAction(vclToQtStringWithAccelerator(rStr), &getMenu());
Michael Weghorn1b14ace2025-01-14 17:00:53 +010044 pAction->setObjectName(toQString(rId));
45
46 if (pIconName)
47 pAction->setIcon(loadQPixmapIcon(*pIconName));
48 else if (pImageSurface)
49 pAction->setIcon(toQPixmap(*pImageSurface));
Michael Weghorn12c6bf02025-01-27 21:46:17 +010050
51 insertAction(pAction, nPos);
Michael Weghorn1b14ace2025-01-14 17:00:53 +010052 });
Michael Weghorna6f206c2024-12-20 21:05:20 +010053}
54
Michael Weghorn12c6bf02025-01-27 21:46:17 +010055void QtInstanceMenuButton::insert_separator(int nPos, const OUString& rId)
Michael Weghorna6f206c2024-12-20 21:05:20 +010056{
Michael Weghorn12c6bf02025-01-27 21:46:17 +010057 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 Weghorna6f206c2024-12-20 21:05:20 +010066}
67
Michael Weghorn78872b22024-12-21 00:17:05 +010068void QtInstanceMenuButton::remove_item(const OUString& rId)
Michael Weghorna6f206c2024-12-20 21:05:20 +010069{
Michael Weghorn78872b22024-12-21 00:17:05 +010070 SolarMutexGuard g;
71
72 GetQtInstance().RunInMainThread([&] {
73 if (QAction* pAction = getAction(rId))
74 getMenu().removeAction(pAction);
75 });
Michael Weghorna6f206c2024-12-20 21:05:20 +010076}
77
Michael Weghorn78872b22024-12-21 00:17:05 +010078void QtInstanceMenuButton::clear()
Michael Weghorna6f206c2024-12-20 21:05:20 +010079{
Michael Weghorn78872b22024-12-21 00:17:05 +010080 SolarMutexGuard g;
81
82 GetQtInstance().RunInMainThread([&] { getMenu().clear(); });
Michael Weghorna6f206c2024-12-20 21:05:20 +010083}
84
Michael Weghorn78872b22024-12-21 00:17:05 +010085void QtInstanceMenuButton::set_item_sensitive(const OUString& rIdent, bool bSensitive)
Michael Weghorna6f206c2024-12-20 21:05:20 +010086{
Michael Weghorn78872b22024-12-21 00:17:05 +010087 SolarMutexGuard g;
88
89 GetQtInstance().RunInMainThread([&] {
90 if (QAction* pAction = getAction(rIdent))
91 pAction->setEnabled(bSensitive);
92 });
Michael Weghorna6f206c2024-12-20 21:05:20 +010093}
94
Michael Weghorn78872b22024-12-21 00:17:05 +010095void QtInstanceMenuButton::set_item_active(const OUString& rIdent, bool bActive)
Michael Weghorna6f206c2024-12-20 21:05:20 +010096{
Michael Weghorn78872b22024-12-21 00:17:05 +010097 SolarMutexGuard g;
98
99 GetQtInstance().RunInMainThread([&] {
100 if (QAction* pAction = getAction(rIdent))
101 pAction->setChecked(bActive);
102 });
Michael Weghorna6f206c2024-12-20 21:05:20 +0100103}
104
Michael Weghorn78872b22024-12-21 00:17:05 +0100105void QtInstanceMenuButton::set_item_label(const OUString& rIdent, const OUString& rLabel)
Michael Weghorna6f206c2024-12-20 21:05:20 +0100106{
Michael Weghorn78872b22024-12-21 00:17:05 +0100107 SolarMutexGuard g;
108
109 GetQtInstance().RunInMainThread([&] {
110 if (QAction* pAction = getAction(rIdent))
111 pAction->setText(toQString(rLabel));
112 });
113}
114
115OUString 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
128void 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 Weghorna6f206c2024-12-20 21:05:20 +0100136}
137
Michael Weghorn4108d5f2025-01-21 21:50:10 +0100138void QtInstanceMenuButton::set_popover(weld::Widget* pPopover)
139{
140 QtInstanceWidget* pPopoverWidget = dynamic_cast<QtInstanceWidget*>(pPopover);
141 m_pPopover = pPopoverWidget ? pPopoverWidget->getQWidget() : nullptr;
142}
Michael Weghorna6f206c2024-12-20 21:05:20 +0100143
Michael Weghorn78872b22024-12-21 00:17:05 +0100144QMenu& QtInstanceMenuButton::getMenu() const
145{
146 QMenu* pMenu = m_pToolButton->menu();
147 assert(pMenu);
148 return *pMenu;
149}
150
151QAction* QtInstanceMenuButton::getAction(const OUString& rIdent) const
152{
Michael Weghorn5e380592025-01-27 22:06:29 +0100153 const QList<QAction*> aActions = getMenu().actions();
Michael Weghorn78872b22024-12-21 00:17:05 +0100154 for (QAction* pAction : aActions)
155 {
156 if (pAction && pAction->objectName() == toQString(rIdent))
157 return pAction;
158 }
159
160 return nullptr;
161}
162
Michael Weghorn12c6bf02025-01-27 21:46:17 +0100163void 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 Weghorn4108d5f2025-01-21 21:50:10 +0100176void QtInstanceMenuButton::handleButtonClicked()
177{
178 if (m_pPopover)
Michael Weghorn3a9dd7f2025-05-29 18:38:46 +0200179 {
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 Weghorn4108d5f2025-01-21 21:50:10 +0100185 m_pPopover->show();
Michael Weghorn3a9dd7f2025-05-29 18:38:46 +0200186 }
Michael Weghorn4108d5f2025-01-21 21:50:10 +0100187 else
Michael Weghorn3a9dd7f2025-05-29 18:38:46 +0200188 {
Michael Weghorn4108d5f2025-01-21 21:50:10 +0100189 m_pToolButton->showMenu();
Michael Weghorn3a9dd7f2025-05-29 18:38:46 +0200190 }
Michael Weghorn4108d5f2025-01-21 21:50:10 +0100191}
192
Michael Weghorn2ef303282025-01-14 17:30:48 +0100193void QtInstanceMenuButton::handleMenuItemTriggered(QAction* pAction)
194{
195 SolarMutexGuard g;
196
197 assert(pAction);
198 signal_selected(toOUString(pAction->objectName()));
199}
200
Michael Weghorna6f206c2024-12-20 21:05:20 +0100201/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */