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 ()
 

Public Member Functions

void adjustPosition ()
 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)
 
void setPopupWidgetWidth (int w)
 
 ~KisPopupButton () override
 

Protected Member Functions

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

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 100 of file KisPopupButton.cpp.

101 : QToolButton(parent)
102 , m_d(new Private)
103{
104 setObjectName("KisPopupButton");
105 connect(this, SIGNAL(released()), SLOT(showPopupWidget()));
106}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
Private *const m_d

References connect(), and showPopupWidget().

◆ ~KisPopupButton()

KisPopupButton::~KisPopupButton ( )
override

Definition at line 108 of file KisPopupButton.cpp.

109{
110 delete m_d->frame;
111 delete m_d;
112}
QPointer< KisPopupButtonFrame > frame

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

Member Function Documentation

◆ adjustPosition()

void KisPopupButton::adjustPosition ( )

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

Definition at line 206 of file KisPopupButton.cpp.

207{
208 // If popup is not detached, or if its detached geometry hasn't been set,
209 // we first move the popup to the "current" screen.
211 QScreen *currentScreen = [this]() {
212 QWindow *mainWinHandle = this->window()->windowHandle();
213 if (mainWinHandle) {
214 return mainWinHandle->screen();
215 }
216 return QApplication::primaryScreen();
217 }();
218 QWindow *winHandle = m_d->frame->windowHandle();
219 if (winHandle) {
220 winHandle->setScreen(currentScreen);
221 }
222 }
223
224 // Attach to the button if it's visible, else attach to the cursor.
225 QPoint pos = this->isVisible() ? this->mapToGlobal(QPoint(0, this->size().height())) : QCursor().pos();
226 QSize popSize = m_d->popupWidget->size();
227 QRect popupRect(pos, popSize);
228
229 // Get the available geometry of the screen which contains the popup.
230 QScreen *screen = [this]() {
231 QWindow *winHandle = m_d->frame->windowHandle();
232 if (winHandle && winHandle->screen()) {
233 return winHandle->screen();
234 }
235 return QApplication::primaryScreen();
236 }();
237 QRect screenRect = screen->availableGeometry();
238 if (m_d->isPopupDetached) {
240 popupRect.moveTo(m_d->frame->geometry().topLeft());
241 } else {
242 popupRect.moveTo(this->window()->geometry().center() - QRect(QPoint(0, 0), popSize).center());
244 }
245 }
246 popupRect = kisEnsureInRect(popupRect, screenRect);
247
248 m_d->frame->setGeometry(popupRect);
249}
QRect kisEnsureInRect(QRect rc, const QRect &bounds)
Definition kis_global.h:267
int size(const Forest< T > &forest)
Definition KisForest.h:1232
QPointer< QWidget > popupWidget

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

◆ hidePopupWidget

void KisPopupButton::hidePopupWidget ( )
slot

Definition at line 158 of file KisPopupButton.cpp.

159{
161}
void setPopupWidgetVisible(bool visible)

References setPopupWidgetVisible().

◆ isPopupWidgetVisible()

bool KisPopupButton::isPopupWidgetVisible ( )

Definition at line 180 of file KisPopupButton.cpp.

181{
182 return m_d->popupWidget && m_d->frame->isVisible();
183}

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

◆ paintEvent()

void KisPopupButton::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 185 of file KisPopupButton.cpp.

186{
187 QToolButton::paintEvent(event);
188 if (m_d->arrowVisible) {
190 }
191}

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

◆ paintPopupArrow()

void KisPopupButton::paintPopupArrow ( )
protected

Definition at line 193 of file KisPopupButton.cpp.

194{
195 QStylePainter p(this);
196 QStyleOption option;
197 option.rect = QRect(rect().right() - 15, rect().bottom() - 15, 14, 14);
198 option.palette = palette();
199 option.palette.setBrush(QPalette::ButtonText, Qt::black); // Force color to black
200 option.state = QStyle::State_Enabled;
201 p.setBrush(Qt::black); // work around some theme that don't use QPalette::ButtonText like they should, but instead the QPainter brushes and pen
202 p.setPen(Qt::black);
203 p.drawPrimitive(QStyle::PE_IndicatorArrowDown, option);
204}
const Params2D p
rgba palette[MAX_PALETTE]
Definition palette.c:35

References p, and palette.

◆ 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 251 of file KisPopupButton.cpp.

252{
253 if (v) {
254 m_d->arrowVisible = true;
255 } else {
256 m_d->arrowVisible = false;
257 }
258}
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.

Definition at line 131 of file KisPopupButton.cpp.

132{
133 if (widget) {
134 delete m_d->frame;
135 m_d->frame = new KisPopupButtonFrame(this->window(), m_d->isPopupDetached);
136 m_d->frame->setWindowTitle(widget->windowTitle());
137
138 m_d->popupWidget = widget;
139
140 m_d->frame->layout()->addWidget(m_d->popupWidget);
141 }
142}

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

◆ 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 114 of file KisPopupButton.cpp.

115{
116 m_d->isPopupDetached = detach;
117 if (m_d->frame) {
118 bool wasVisible = isPopupWidgetVisible();
119 m_d->frame->setDetached(detach);
120 if (wasVisible) {
121 // Setting the window flags closes the widget, so make it visible again.
123 if (detach) {
125 }
127 }
128 }
129}
void adjustPosition()
adjustPosition adjusts the position of the popup widget based on the position of this button and the ...

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

◆ setPopupWidgetVisible

void KisPopupButton::setPopupWidgetVisible ( bool visible)
slot

Definition at line 163 of file KisPopupButton.cpp.

164{
165 if (m_d->popupWidget) {
166 if (visible) {
167 // Force layout sizing before positioning
168 m_d->popupWidget->adjustSize();
169 m_d->frame->adjustSize();
171 m_d->frame->raise();
172 m_d->frame->show();
173 m_d->frame->activateWindow();
174 } else {
175 m_d->frame->setVisible(false);
176 }
177 }
178}

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

◆ setPopupWidgetWidth()

void KisPopupButton::setPopupWidgetWidth ( int w)

Set the width of the popup widget.

Returns
new width of the popup widget

Definition at line 144 of file KisPopupButton.cpp.

145{
146 m_d->frame->resize(w, m_d->frame->height());
147}

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

◆ showPopupWidget

void KisPopupButton::showPopupWidget ( )
slot

Definition at line 149 of file KisPopupButton.cpp.

150{
151 if (m_d->popupWidget && !m_d->frame->isVisible()) {
153 } else {
155 }
156}

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

Member Data Documentation

◆ m_d

Private* const KisPopupButton::m_d
private

Definition at line 75 of file KisPopupButton.h.


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