Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPopupButton.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2007 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2008 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "KisPopupButton.h"
9
10#include <QPointer>
11#include <QApplication>
12#include <QFrame>
13#include <QHBoxLayout>
14#include <QKeyEvent>
15#include <QScreen>
16#include <QStyleOption>
17#include <QStylePainter>
18#include <QWindow>
19#include <QMenu>
20
21#include "kis_global.h"
22#include <kis_debug.h>
23
24// for HAVE_WAYLAND
25#include <KoConfig.h>
26
27#if defined HAVE_WAYLAND && QT_VERSION >= QT_VERSION_CHECK(6, 11, 0)
28#define HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
29#endif
30
31#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
32#include <QtWaylandClient/private/qwaylandwindow_p.h>
33#endif
34
35
36class KisPopupButtonFrame : public QFrame
37{
38 QHBoxLayout* frameLayout {0};
39
40public:
41 KisPopupButtonFrame(QWidget *parent, bool detach, bool popupIsMenu)
42 : QFrame(parent, Qt::Window)
43 {
44 setObjectName("KisPopupButtonFrame");
45 setProperty("_kis_excludeFromLayoutThumbnail", true);
46 frameLayout = new QHBoxLayout(this);
47 frameLayout->setContentsMargins(0, 0, 0, 0);
48
49 setDetached(detach, popupIsMenu);
50 }
51
52 void setDetached(bool detach, bool popupIsMenu)
53 {
54#if defined Q_OS_ANDROID || defined Q_OS_MACOS
55 // for some reason when calling destroy() the platform window isn't
56 // hidden first, this corrupts state of the window stack
57 hide();
58#endif
59
60 // Need to destroy the platform window before changing window flags
61 // so that Qt knows to actually apply the new flags...
62 // At least on Windows, not doing this may result in weird window drop
63 // shadows.
64 destroy();
65
66 if (detach) {
67 setWindowFlags(Qt::Dialog);
68 setFrameStyle(QFrame::NoFrame);
69 }
70 else {
71 setWindowFlags(Qt::Popup);
72 // Menus already have a frame, don't need another.
73 if (popupIsMenu) {
74 setFrameStyle(QFrame::NoFrame);
75 } else {
76 setFrameStyle(QFrame::Box | QFrame::Plain);
77 }
78 }
79
80 updateGeometry();
81 }
82
83protected:
84 void keyPressEvent(QKeyEvent *event) override
85 {
86 if (event->matches(QKeySequence::Cancel)) {
87 event->accept();
88 hide();
89 } else {
90 QFrame::keyPressEvent(event);
91 }
92 }
93
94 bool event(QEvent *e) override
95 {
96 if (e->type() == QEvent::Close) {
97 e->ignore();
98 hide();
99 return true;
100 }
101 return QFrame::event(e);
102 }
103};
104
105
108 // : frameLayout(nullptr)
109 {}
112 bool arrowVisible { true };
113 bool isPopupDetached { false };
114 bool isDetachedGeometrySet { false };
115};
116
118 : QToolButton(parent)
119 , m_d(new Private)
120{
121 setObjectName("KisPopupButton");
122 connect(this, SIGNAL(released()), SLOT(showPopupWidget()));
123}
124
126{
127 delete m_d->frame;
128 delete m_d;
129}
130
132{
133 m_d->isPopupDetached = detach;
134 if (m_d->frame) {
135 bool wasVisible = isPopupWidgetVisible();
136 m_d->frame->setDetached(detach, qobject_cast<QMenu *>(m_d->popupWidget));
137 if (wasVisible) {
138 // Setting the window flags closes the widget, so make it visible again.
140 }
141 }
142}
143
145{
146 if (widget) {
147 delete m_d->frame;
148
149 QWidget *effectiveParent = widget->parentWidget() ? widget->parentWidget() : this->window();
150
151 // Bit wonky to assign a popup menu to a popup widget, so they need
152 // extra coddling to make them work properly.
153 QMenu *menu = qobject_cast<QMenu *>(widget);
154 m_d->frame = new KisPopupButtonFrame(effectiveParent, m_d->isPopupDetached, menu);
155 m_d->frame->setWindowTitle(widget->windowTitle());
156
157 m_d->popupWidget = widget;
158
159 m_d->frame->layout()->addWidget(m_d->popupWidget);
160
161 if (menu) {
162 // The menu may decide to hide itself in response to user input.
163 // Compensate for that by catching the situation where we'd hide
164 // the menu while the frame is still up and show it again.
165 connect(menu, &QMenu::aboutToHide, this, &KisPopupButton::slotMenuAboutToHide);
166 // If the menu still hides itself somehow, make it show up again.
167 connect(this, &KisPopupButton::sigPopupWidgetShown, menu, &QMenu::show);
168 }
169 }
170}
171
176
181
183{
184 if (m_d->popupWidget) {
185 if (visible) {
187 Q_EMIT sigPopupWidgetShown();
188 } else {
189 m_d->frame->setVisible(false);
190 }
191 }
192}
193
195{
196 return m_d->popupWidget && m_d->frame->isVisible();
197}
198
199void KisPopupButton::paintEvent ( QPaintEvent * event )
200{
201 QToolButton::paintEvent(event);
202 if (m_d->arrowVisible) {
204 }
205}
206
208{
209 QStylePainter p(this);
210 QStyleOption option;
211 option.rect = QRect(rect().right() - 15, rect().bottom() - 15, 14, 14);
212 option.palette = palette();
213 option.palette.setBrush(QPalette::ButtonText, Qt::black); // Force color to black
214 option.state = QStyle::State_Enabled;
215 p.setBrush(Qt::black); // work around some theme that don't use QPalette::ButtonText like they should, but instead the QPainter brushes and pen
216 p.setPen(Qt::black);
217 p.drawPrimitive(QStyle::PE_IndicatorArrowDown, option);
218}
219
221{
222 const QSize popupSize = m_d->frame->sizeHint();
223 QRect popupRect(QPoint(), popupSize);
224
225 // resize **must** come before `setParentControlGeometry()`, otherwise
226 // compositor will place the window incorrectly!
227 m_d->frame->resize(popupSize);
228
229 // make sure that all the platform-specific structures for the window
230 // are created (i.e. QWaylandWindow) before trying to request them
231 // via `windowHandle()->handle()`
232 m_d->frame->winId();
233
234#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
235 if (auto waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(m_d->frame->windowHandle()->handle())) {
236 if (m_d->isPopupDetached) {
237 // one cannot control dialog position on Wayland, sorry :(
238 } else {
239 if (this->isVisible()) {
240 QWidget *parentWidget = m_d->frame->parentWidget();
241
242 const QRect gravityRect(parentWidget->window()->mapFromGlobal(mapToGlobal(QPoint())), size());
243
244 waylandWindow->setParentControlGeometry(gravityRect);
245 waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::ComboBox);
246 } else {
247 QWidget *parentWidget = m_d->frame->parentWidget();
248 const QPoint parentPos = parentWidget->window()->mapFromGlobal(QCursor::pos());
249 const QRect gravityRect(parentPos.x() - popupSize.width() / 2, parentPos.y() - popupSize.height() / 2, popupSize.width(), popupSize.height());
250
251 waylandWindow->setParentControlGeometry(gravityRect);
252 waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::Default);
253 }
254 }
255
256 m_d->frame->show();
257 } else
258#endif /* HAVE_QT_PRIVATE_TOOLTIP_POSITION_API */
259
260 {
261 // Get the available geometry of the screen which contains the popup.
262 QScreen *screen = this->parentWidget() ? this->screen() : qApp->screenAt(QCursor::pos());
263 const QRect screenRect = screen ? screen->availableGeometry() : QRect();
264
265 if (m_d->isPopupDetached) {
266
268 popupRect.moveTo(m_d->frame->geometry().topLeft());
269 } else {
270 popupRect.moveTo(this->window()->geometry().center() - QRect(QPoint(0, 0), popupSize).center());
272 }
273 } else {
274 // Attach to the button if it's visible, else attach to the cursor.
275 if (this->isVisible()) {
276 popupRect.moveTo(this->mapToGlobal(rect().bottomLeft()));
277 } else {
278 popupRect.moveCenter(QCursor::pos());
279 }
280 }
281
282 if (!screenRect.isEmpty()) {
283 popupRect = kisEnsureInRect(popupRect, screenRect);
284 }
285
290 m_d->frame->show();
291 m_d->frame->setGeometry(popupRect);
292 }
293}
294
296{
297 if (v) {
298 m_d->arrowVisible = true;
299 } else {
300 m_d->arrowVisible = false;
301 }
302}
303
305{
306 // Menus will hide themselves when clicking on one of their separators,
307 // which just ends up with a blank popup frame. Fight back against this by
308 // showing the menu again if the frame is still up when the menu disappears.
309 if (m_d->popupWidget && m_d->frame->isVisible()) {
310 m_d->popupWidget->show();
311 }
312}
const Params2D p
qreal v
bool event(QEvent *e) override
void setDetached(bool detach, bool popupIsMenu)
QHBoxLayout * frameLayout
void keyPressEvent(QKeyEvent *event) override
KisPopupButtonFrame(QWidget *parent, bool detach, bool popupIsMenu)
void setPopupWidgetVisible(bool visible)
void setPopupWidgetDetached(bool detach)
void setPopupWidget(QWidget *widget)
~KisPopupButton() override
Private *const m_d
void adjustPositionAndShow()
adjustPosition adjusts the position of the popup widget based on the position of this button and the ...
void sigPopupWidgetShown()
void setArrowVisible(bool v)
void paintEvent(QPaintEvent *event) override
KisPopupButton(QWidget *parent)
QRect kisEnsureInRect(QRect rc, const QRect &bounds)
Definition kis_global.h:291
QPointer< QWidget > popupWidget
QPointer< KisPopupButtonFrame > frame