Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPopupButton Class Reference

#include <KisPopupButton.h>

+ Inheritance diagram for KisPopupButton:

Classes

struct  Private
 

Public Slots

void hidePopupWidget ()
 
void setPopupWidgetDetached (bool detach)
 
void setPopupWidgetVisible (bool visible)
 
void showPopupWidget ()
 

Signals

void sigPopupWidgetShown ()
 

Public Member Functions

void adjustPositionAndShow ()
 adjustPosition adjusts the position of the popup widget based on the position of this button and the size of the widget
 
bool isPopupWidgetVisible ()
 
 KisPopupButton (QWidget *parent)
 
void setArrowVisible (bool v)
 
void setPopupWidget (QWidget *widget)
 
 ~KisPopupButton () override
 

Protected Member Functions

void paintEvent (QPaintEvent *event) override
 
void paintPopupArrow ()
 

Private Slots

void slotMenuAboutToHide ()
 

Private Attributes

Private *const m_d
 

Detailed Description

This class is a convenience class for a button that when clicked displays a popup widget.

Definition at line 18 of file KisPopupButton.h.

Constructor & Destructor Documentation

◆ KisPopupButton()

KisPopupButton::KisPopupButton ( QWidget * parent)

Definition at line 117 of file KisPopupButton.cpp.

118 : QToolButton(parent)
119 , m_d(new Private)
120{
121 setObjectName("KisPopupButton");
122 connect(this, SIGNAL(released()), SLOT(showPopupWidget()));
123}
Private *const m_d

References showPopupWidget().

◆ ~KisPopupButton()

KisPopupButton::~KisPopupButton ( )
override

Definition at line 125 of file KisPopupButton.cpp.

126{
127 delete m_d->frame;
128 delete m_d;
129}
QPointer< KisPopupButtonFrame > frame

References KisPopupButton::Private::frame, and m_d.

Member Function Documentation

◆ adjustPositionAndShow()

void KisPopupButton::adjustPositionAndShow ( )

adjustPosition adjusts the position of the popup widget based on the position of this button and the size of the widget

On non-wayland platforms we first show the widget and only then apply geometry, otherwise the settings are not applied properly

Definition at line 220 of file KisPopupButton.cpp.

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}
QRect kisEnsureInRect(QRect rc, const QRect &bounds)
Definition kis_global.h:291
int size(const Forest< T > &forest)
Definition KisForest.h:1232
const NumericSetting< int > popupSize
Definition WGConfig.cpp:245

References KisPopupButton::Private::frame, KisPopupButton::Private::isDetachedGeometrySet, KisPopupButton::Private::isPopupDetached, kisEnsureInRect(), and m_d.

◆ hidePopupWidget

void KisPopupButton::hidePopupWidget ( )
slot

Definition at line 177 of file KisPopupButton.cpp.

178{
180}
void setPopupWidgetVisible(bool visible)

References setPopupWidgetVisible().

◆ isPopupWidgetVisible()

bool KisPopupButton::isPopupWidgetVisible ( )

Definition at line 194 of file KisPopupButton.cpp.

195{
196 return m_d->popupWidget && m_d->frame->isVisible();
197}
QPointer< QWidget > popupWidget

References KisPopupButton::Private::frame, m_d, and KisPopupButton::Private::popupWidget.

◆ paintEvent()

void KisPopupButton::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 199 of file KisPopupButton.cpp.

200{
201 QToolButton::paintEvent(event);
202 if (m_d->arrowVisible) {
204 }
205}

References KisPopupButton::Private::arrowVisible, m_d, and paintPopupArrow().

◆ paintPopupArrow()

void KisPopupButton::paintPopupArrow ( )
protected

Definition at line 207 of file KisPopupButton.cpp.

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}
const Params2D p

References p.

◆ setArrowVisible()

void KisPopupButton::setArrowVisible ( bool v)

This function allows to show or hide the arrow.

Parameters
vset to true to draw the arrow, else set to false

Definition at line 295 of file KisPopupButton.cpp.

296{
297 if (v) {
298 m_d->arrowVisible = true;
299 } else {
300 m_d->arrowVisible = false;
301 }
302}
qreal v

References KisPopupButton::Private::arrowVisible, m_d, and v.

◆ setPopupWidget()

void KisPopupButton::setPopupWidget ( QWidget * widget)

Set the popup widget, the KisPopupButton becomes the owner and parent of the widget.

If a widget does already have a parent, then this parent becomes a parent of the internal popup frame. That might be useful for popup buttons attaches to actions, which may be not visible or attached to anything, e.g the "Docker Box" action.

Definition at line 144 of file KisPopupButton.cpp.

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}
void sigPopupWidgetShown()

References KisPopupButton::Private::frame, KisPopupButton::Private::isPopupDetached, m_d, KisPopupButton::Private::popupWidget, sigPopupWidgetShown(), and slotMenuAboutToHide().

◆ setPopupWidgetDetached

void KisPopupButton::setPopupWidgetDetached ( bool detach)
slot

Set whether the popup is detached as a dialog.

Parameters
vset to true to cause the popup to be detached

Definition at line 131 of file KisPopupButton.cpp.

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}

References KisPopupButton::Private::frame, KisPopupButton::Private::isPopupDetached, isPopupWidgetVisible(), m_d, KisPopupButton::Private::popupWidget, and setPopupWidgetVisible().

◆ setPopupWidgetVisible

void KisPopupButton::setPopupWidgetVisible ( bool visible)
slot

Definition at line 182 of file KisPopupButton.cpp.

183{
184 if (m_d->popupWidget) {
185 if (visible) {
187 Q_EMIT sigPopupWidgetShown();
188 } else {
189 m_d->frame->setVisible(false);
190 }
191 }
192}
void adjustPositionAndShow()
adjustPosition adjusts the position of the popup widget based on the position of this button and the ...

References adjustPositionAndShow(), KisPopupButton::Private::frame, m_d, KisPopupButton::Private::popupWidget, and sigPopupWidgetShown().

◆ showPopupWidget

void KisPopupButton::showPopupWidget ( )
slot

Definition at line 172 of file KisPopupButton.cpp.

173{
174 setPopupWidgetVisible(!m_d->frame->isVisible());
175}

References KisPopupButton::Private::frame, m_d, and setPopupWidgetVisible().

◆ sigPopupWidgetShown

void KisPopupButton::sigPopupWidgetShown ( )
signal

◆ slotMenuAboutToHide

void KisPopupButton::slotMenuAboutToHide ( )
privateslot

Definition at line 304 of file KisPopupButton.cpp.

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}

References KisPopupButton::Private::frame, m_d, and KisPopupButton::Private::popupWidget.

Member Data Documentation

◆ m_d

Private* const KisPopupButton::m_d
private

Definition at line 82 of file KisPopupButton.h.


The documentation for this class was generated from the following files: