qpushbutton.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qpushbutton.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtWidgets module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qapplication.h"-
41#include "qbitmap.h"-
42#include "qdesktopwidget.h"-
43#include "qdialog.h"-
44#include <private/qdialog_p.h>-
45#include "qdrawutil.h"-
46#include "qevent.h"-
47#include "qfontmetrics.h"-
48#include "qmenu.h"-
49#include "qstylepainter.h"-
50#include "qpixmap.h"-
51#include "qpointer.h"-
52#include "qpushbutton.h"-
53#include "qstyle.h"-
54#include "qstyleoption.h"-
55#include "qtoolbar.h"-
56#include "qdebug.h"-
57#include "qlayoutitem.h"-
58#include "qdialogbuttonbox.h"-
59#ifdef Q_DEAD_CODE_FROM_QT4_MAC-
60#include "private/qmacstyle_mac_p.h"-
61#include "private/qmacstyle_mac_p_p.h"-
62#endif // Q_DEAD_CODE_FROM_QT4_MAC-
63-
64#ifndef QT_NO_ACCESSIBILITY-
65#include "qaccessible.h"-
66#endif-
67-
68#include "private/qmenu_p.h"-
69#include "private/qpushbutton_p.h"-
70-
71QT_BEGIN_NAMESPACE-
72-
73-
74/*!-
75 \class QPushButton-
76 \brief The QPushButton widget provides a command button.-
77-
78 \ingroup basicwidgets-
79 \inmodule QtWidgets-
80-
81 The push button, or command button, is perhaps the most commonly-
82 used widget in any graphical user interface. Push (click) a button-
83 to command the computer to perform some action, or to answer a-
84 question. Typical buttons are OK, Apply, Cancel, Close, Yes, No-
85 and Help.-
86-
87 A command button is rectangular and typically displays a text-
88 label describing its action. A shortcut key can be specified by-
89 preceding the preferred character with an ampersand in the-
90 text. For example:-
91-
92 \snippet code/src_gui_widgets_qpushbutton.cpp 0-
93-
94 In this example the shortcut is \e{Alt+D}. See the \l-
95 {QShortcut#mnemonic}{QShortcut} documentation for details (to-
96 display an actual ampersand, use '&&').-
97-
98 Push buttons display a textual label, and optionally a small-
99 icon. These can be set using the constructors and changed later-
100 using setText() and setIcon(). If the button is disabled, the-
101 appearance of the text and icon will be manipulated with respect-
102 to the GUI style to make the button look "disabled".-
103-
104 A push button emits the signal clicked() when it is activated by-
105 the mouse, the Spacebar or by a keyboard shortcut. Connect to-
106 this signal to perform the button's action. Push buttons also-
107 provide less commonly used signals, for example pressed() and-
108 released().-
109-
110 Command buttons in dialogs are by default auto-default buttons,-
111 i.e., they become the default push button automatically when they-
112 receive the keyboard input focus. A default button is a push-
113 button that is activated when the user presses the Enter or Return-
114 key in a dialog. You can change this with setAutoDefault(). Note-
115 that auto-default buttons reserve a little extra space which is-
116 necessary to draw a default-button indicator. If you do not want-
117 this space around your buttons, call setAutoDefault(false).-
118-
119 Being so central, the button widget has grown to accommodate a-
120 great many variations in the past decade. The Microsoft style-
121 guide now shows about ten different states of Windows push buttons-
122 and the text implies that there are dozens more when all the-
123 combinations of features are taken into consideration.-
124-
125 The most important modes or states are:-
126 \list-
127 \li Available or not (grayed out, disabled).-
128 \li Standard push button, toggling push button or menu button.-
129 \li On or off (only for toggling push buttons).-
130 \li Default or normal. The default button in a dialog can generally-
131 be "clicked" using the Enter or Return key.-
132 \li Auto-repeat or not.-
133 \li Pressed down or not.-
134 \endlist-
135-
136 As a general rule, use a push button when the application or-
137 dialog window performs an action when the user clicks on it (such-
138 as Apply, Cancel, Close and Help) \e and when the widget is-
139 supposed to have a wide, rectangular shape with a text label.-
140 Small, typically square buttons that change the state of the-
141 window rather than performing an action (such as the buttons in-
142 the top-right corner of the QFileDialog) are not command buttons,-
143 but tool buttons. Qt provides a special class (QToolButton) for-
144 these buttons.-
145-
146 If you need toggle behavior (see setCheckable()) or a button-
147 that auto-repeats the activation signal when being pushed down-
148 like the arrows in a scroll bar (see setAutoRepeat()), a command-
149 button is probably not what you want. When in doubt, use a tool-
150 button.-
151-
152 A variation of a command button is a menu button. These provide-
153 not just one command, but several, since when they are clicked-
154 they pop up a menu of options. Use the method setMenu() to-
155 associate a popup menu with a push button.-
156-
157 Other classes of buttons are option buttons (see QRadioButton) and-
158 check boxes (see QCheckBox).-
159-
160 \table 100%-
161 \row \li \inlineimage macintosh-pushbutton.png Screenshot of a Macintosh style push button-
162 \li A push button shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.-
163-
164 Note that when a button's width becomes smaller than 50 or-
165 its height becomes smaller than 30, the button's corners are-
166 changed from round to square. Use the setMinimumSize()-
167 function to prevent this behavior.-
168-
169 \row \li \inlineimage windowsvista-pushbutton.png Screenshot of a Windows Vista style push button-
170 \li A push button shown in the \l{Windows Vista Style Widget Gallery}{Windows Vista widget style}.-
171 \row \li \inlineimage fusion-pushbutton.png Screenshot of a Fusion style push button-
172 \li A push button shown in the \l{Fusion Style Widget Gallery}{Fusion widget style}.-
173 \endtable-
174-
175 In Qt, the QAbstractButton base class provides most of the modes-
176 and other API, and QPushButton provides GUI logic.-
177 See QAbstractButton for more information about the API.-
178-
179 \sa QToolButton, QRadioButton, QCheckBox, {fowler}{GUI Design Handbook: Push Button}-
180*/-
181-
182/*!-
183 \property QPushButton::autoDefault-
184 \brief whether the push button is an auto default button-
185-
186 If this property is set to true then the push button is an auto-
187 default button.-
188-
189 In some GUI styles a default button is drawn with an extra frame-
190 around it, up to 3 pixels or more. Qt automatically keeps this-
191 space free around auto-default buttons, i.e., auto-default buttons-
192 may have a slightly larger size hint.-
193-
194 This property's default is true for buttons that have a QDialog-
195 parent; otherwise it defaults to false.-
196-
197 See the \l default property for details of how \l default and-
198 auto-default interact.-
199*/-
200-
201/*!-
202 \property QPushButton::default-
203 \brief whether the push button is the default button-
204-
205 Default and autodefault buttons decide what happens when the user-
206 presses enter in a dialog.-
207-
208 A button with this property set to true (i.e., the dialog's-
209 \e default button,) will automatically be pressed when the user presses enter,-
210 with one exception: if an \a autoDefault button currently has focus, the autoDefault-
211 button is pressed. When the dialog has \l autoDefault buttons but no default button,-
212 pressing enter will press either the \l autoDefault button that currently has focus, or if no-
213 button has focus, the next \l autoDefault button in the focus chain.-
214-
215 In a dialog, only one push button at a time can be the default-
216 button. This button is then displayed with an additional frame-
217 (depending on the GUI style).-
218-
219 The default button behavior is provided only in dialogs. Buttons-
220 can always be clicked from the keyboard by pressing Spacebar when-
221 the button has focus.-
222-
223 If the default property is set to false on the current default button-
224 while the dialog is visible, a new default will automatically be-
225 assigned the next time a push button in the dialog receives focus.-
226-
227 This property's default is false.-
228*/-
229-
230/*!-
231 \property QPushButton::flat-
232 \brief whether the button border is raised-
233-
234 This property's default is false. If this property is set, most-
235 styles will not paint the button background unless the button is-
236 being pressed. setAutoFillBackground() can be used to ensure that-
237 the background is filled using the QPalette::Button brush.-
238*/-
239-
240/*!-
241 Constructs a push button with no text and a \a parent.-
242*/-
243-
244QPushButton::QPushButton(QWidget *parent)-
245 : QAbstractButton(*new QPushButtonPrivate, parent)-
246{-
247 Q_D(QPushButton);-
248 d->init();-
249}
never executed: end of block
0
250-
251/*!-
252 Constructs a push button with the parent \a parent and the text \a-
253 text.-
254*/-
255-
256QPushButton::QPushButton(const QString &text, QWidget *parent)-
257 : QAbstractButton(*new QPushButtonPrivate, parent)-
258{-
259 Q_D(QPushButton);-
260 setText(text);-
261 d->init();-
262}
never executed: end of block
0
263-
264-
265/*!-
266 Constructs a push button with an \a icon and a \a text, and a \a parent.-
267-
268 Note that you can also pass a QPixmap object as an icon (thanks to-
269 the implicit type conversion provided by C++).-
270-
271*/-
272QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent)-
273 : QAbstractButton(*new QPushButtonPrivate, parent)-
274{-
275 Q_D(QPushButton);-
276 setText(text);-
277 setIcon(icon);-
278 d->init();-
279}
never executed: end of block
0
280-
281/*! \internal-
282 */-
283QPushButton::QPushButton(QPushButtonPrivate &dd, QWidget *parent)-
284 : QAbstractButton(dd, parent)-
285{-
286 Q_D(QPushButton);-
287 d->init();-
288}
never executed: end of block
0
289-
290/*!-
291 Destroys the push button.-
292*/-
293QPushButton::~QPushButton()-
294{-
295}-
296-
297QDialog *QPushButtonPrivate::dialogParent() const-
298{-
299 Q_Q(const QPushButton);-
300 const QWidget *p = q;-
301 while (p && !p->isWindow()) {
pDescription
TRUEnever evaluated
FALSEnever evaluated
!p->isWindow()Description
TRUEnever evaluated
FALSEnever evaluated
0
302 p = p->parentWidget();-
303 if (const QDialog *dialog = qobject_cast<const QDialog *>(p))
const QDialog ... QDialog *>(p)Description
TRUEnever evaluated
FALSEnever evaluated
0
304 return const_cast<QDialog *>(dialog);
never executed: return const_cast<QDialog *>(dialog);
0
305 }
never executed: end of block
0
306 return 0;
never executed: return 0;
0
307}-
308-
309/*!-
310 Initialize \a option with the values from this QPushButton. This method is useful-
311 for subclasses when they need a QStyleOptionButton, but don't want to fill-
312 in all the information themselves.-
313-
314 \sa QStyleOption::initFrom()-
315*/-
316void QPushButton::initStyleOption(QStyleOptionButton *option) const-
317{-
318 if (!option)
!optionDescription
TRUEnever evaluated
FALSEnever evaluated
0
319 return;
never executed: return;
0
320-
321 Q_D(const QPushButton);-
322 option->initFrom(this);-
323 option->features = QStyleOptionButton::None;-
324 if (d->flat)
d->flatDescription
TRUEnever evaluated
FALSEnever evaluated
0
325 option->features |= QStyleOptionButton::Flat;
never executed: option->features |= QStyleOptionButton::Flat;
0
326#ifndef QT_NO_MENU-
327 if (d->menu)
d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
328 option->features |= QStyleOptionButton::HasMenu;
never executed: option->features |= QStyleOptionButton::HasMenu;
0
329#endif-
330 if (autoDefault())
autoDefault()Description
TRUEnever evaluated
FALSEnever evaluated
0
331 option->features |= QStyleOptionButton::AutoDefaultButton;
never executed: option->features |= QStyleOptionButton::AutoDefaultButton;
0
332 if (d->defaultButton)
d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
333 option->features |= QStyleOptionButton::DefaultButton;
never executed: option->features |= QStyleOptionButton::DefaultButton;
0
334 if (d->down || d->menuOpen)
d->downDescription
TRUEnever evaluated
FALSEnever evaluated
d->menuOpenDescription
TRUEnever evaluated
FALSEnever evaluated
0
335 option->state |= QStyle::State_Sunken;
never executed: option->state |= QStyle::State_Sunken;
0
336 if (d->checked)
d->checkedDescription
TRUEnever evaluated
FALSEnever evaluated
0
337 option->state |= QStyle::State_On;
never executed: option->state |= QStyle::State_On;
0
338 if (!d->flat && !d->down)
!d->flatDescription
TRUEnever evaluated
FALSEnever evaluated
!d->downDescription
TRUEnever evaluated
FALSEnever evaluated
0
339 option->state |= QStyle::State_Raised;
never executed: option->state |= QStyle::State_Raised;
0
340 option->text = d->text;-
341 option->icon = d->icon;-
342 option->iconSize = iconSize();-
343}
never executed: end of block
0
344-
345void QPushButton::setAutoDefault(bool enable)-
346{-
347 Q_D(QPushButton);-
348 uint state = enable ? QPushButtonPrivate::On : QPushButtonPrivate::Off;
enableDescription
TRUEnever evaluated
FALSEnever evaluated
0
349 if (d->autoDefault != QPushButtonPrivate::Auto && d->autoDefault == state)
d->autoDefault...nPrivate::AutoDescription
TRUEnever evaluated
FALSEnever evaluated
d->autoDefault == stateDescription
TRUEnever evaluated
FALSEnever evaluated
0
350 return;
never executed: return;
0
351 d->autoDefault = state;-
352 d->sizeHint = QSize();-
353 update();-
354 updateGeometry();-
355}
never executed: end of block
0
356-
357bool QPushButton::autoDefault() const-
358{-
359 Q_D(const QPushButton);-
360 if(d->autoDefault == QPushButtonPrivate::Auto)
d->autoDefault...nPrivate::AutoDescription
TRUEnever evaluated
FALSEnever evaluated
0
361 return ( d->dialogParent() != 0 );
never executed: return ( d->dialogParent() != 0 );
0
362 return d->autoDefault;
never executed: return d->autoDefault;
0
363}-
364-
365void QPushButton::setDefault(bool enable)-
366{-
367 Q_D(QPushButton);-
368 if (d->defaultButton == enable)
d->defaultButton == enableDescription
TRUEnever evaluated
FALSEnever evaluated
0
369 return;
never executed: return;
0
370 d->defaultButton = enable;-
371 if (d->defaultButton) {
d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
372 if (QDialog *dlg = d->dialogParent())
QDialog *dlg =...dialogParent()Description
TRUEnever evaluated
FALSEnever evaluated
0
373 dlg->d_func()->setMainDefault(this);
never executed: dlg->d_func()->setMainDefault(this);
0
374 }
never executed: end of block
0
375 update();-
376#ifndef QT_NO_ACCESSIBILITY-
377 QAccessible::State s;-
378 s.defaultButton = true;-
379 QAccessibleStateChangeEvent event(this, s);-
380 QAccessible::updateAccessibility(&event);-
381#endif-
382}
never executed: end of block
0
383-
384bool QPushButton::isDefault() const-
385{-
386 Q_D(const QPushButton);-
387 return d->defaultButton;
never executed: return d->defaultButton;
0
388}-
389-
390/*!-
391 \reimp-
392*/-
393QSize QPushButton::sizeHint() const-
394{-
395 Q_D(const QPushButton);-
396 if (d->sizeHint.isValid() && d->lastAutoDefault == autoDefault())
d->sizeHint.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
d->lastAutoDef... autoDefault()Description
TRUEnever evaluated
FALSEnever evaluated
0
397 return d->sizeHint;
never executed: return d->sizeHint;
0
398 d->lastAutoDefault = autoDefault();-
399 ensurePolished();-
400-
401 int w = 0, h = 0;-
402-
403 QStyleOptionButton opt;-
404 initStyleOption(&opt);-
405-
406 // calculate contents size...-
407#ifndef QT_NO_ICON-
408-
409 bool showButtonBoxIcons = qobject_cast<QDialogButtonBox*>(parentWidget())
qobject_cast<Q...arentWidget())Description
TRUEnever evaluated
FALSEnever evaluated
0
410 && style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons);
style()->style...tonsHaveIcons)Description
TRUEnever evaluated
FALSEnever evaluated
0
411-
412 if (!icon().isNull() || showButtonBoxIcons) {
!icon().isNull()Description
TRUEnever evaluated
FALSEnever evaluated
showButtonBoxIconsDescription
TRUEnever evaluated
FALSEnever evaluated
0
413 int ih = opt.iconSize.height();-
414 int iw = opt.iconSize.width() + 4;-
415 w += iw;-
416 h = qMax(h, ih);-
417 }
never executed: end of block
0
418#endif-
419 QString s(text());-
420 bool empty = s.isEmpty();-
421 if (empty)
emptyDescription
TRUEnever evaluated
FALSEnever evaluated
0
422 s = QString::fromLatin1("XXXX");
never executed: s = QString::fromLatin1("XXXX");
0
423 QFontMetrics fm = fontMetrics();-
424 QSize sz = fm.size(Qt::TextShowMnemonic, s);-
425 if(!empty || !w)
!emptyDescription
TRUEnever evaluated
FALSEnever evaluated
!wDescription
TRUEnever evaluated
FALSEnever evaluated
0
426 w += sz.width();
never executed: w += sz.width();
0
427 if(!empty || !h)
!emptyDescription
TRUEnever evaluated
FALSEnever evaluated
!hDescription
TRUEnever evaluated
FALSEnever evaluated
0
428 h = qMax(h, sz.height());
never executed: h = qMax(h, sz.height());
0
429 opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height-
430#ifndef QT_NO_MENU-
431 if (menu())
menu()Description
TRUEnever evaluated
FALSEnever evaluated
0
432 w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
never executed: w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
0
433#endif-
434 d->sizeHint = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).-
435 expandedTo(QApplication::globalStrut()));-
436 return d->sizeHint;
never executed: return d->sizeHint;
0
437}-
438-
439/*!-
440 \reimp-
441 */-
442QSize QPushButton::minimumSizeHint() const-
443{-
444 return sizeHint();
never executed: return sizeHint();
0
445}-
446-
447-
448/*!\reimp-
449*/-
450void QPushButton::paintEvent(QPaintEvent *)-
451{-
452 QStylePainter p(this);-
453 QStyleOptionButton option;-
454 initStyleOption(&option);-
455 p.drawControl(QStyle::CE_PushButton, option);-
456}
never executed: end of block
0
457-
458-
459/*! \reimp */-
460void QPushButton::keyPressEvent(QKeyEvent *e)-
461{-
462 Q_D(QPushButton);-
463 switch (e->key()) {-
464 case Qt::Key_Enter:
never executed: case Qt::Key_Enter:
0
465 case Qt::Key_Return:
never executed: case Qt::Key_Return:
0
466 if (autoDefault() || d->defaultButton) {
autoDefault()Description
TRUEnever evaluated
FALSEnever evaluated
d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
467 click();-
468 break;
never executed: break;
0
469 }-
470 // fall through-
471 default:
code before this statement never executed: default:
never executed: default:
0
472 QAbstractButton::keyPressEvent(e);-
473 }
never executed: end of block
0
474}-
475-
476/*!-
477 \reimp-
478*/-
479void QPushButton::focusInEvent(QFocusEvent *e)-
480{-
481 Q_D(QPushButton);-
482 if (e->reason() != Qt::PopupFocusReason && autoDefault() && !d->defaultButton) {
e->reason() !=...pupFocusReasonDescription
TRUEnever evaluated
FALSEnever evaluated
autoDefault()Description
TRUEnever evaluated
FALSEnever evaluated
!d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
483 d->defaultButton = true;-
484 QDialog *dlg = qobject_cast<QDialog*>(window());-
485 if (dlg)
dlgDescription
TRUEnever evaluated
FALSEnever evaluated
0
486 dlg->d_func()->setDefault(this);
never executed: dlg->d_func()->setDefault(this);
0
487 }
never executed: end of block
0
488 QAbstractButton::focusInEvent(e);-
489}
never executed: end of block
0
490-
491/*!-
492 \reimp-
493*/-
494void QPushButton::focusOutEvent(QFocusEvent *e)-
495{-
496 Q_D(QPushButton);-
497 if (e->reason() != Qt::PopupFocusReason && autoDefault() && d->defaultButton) {
e->reason() !=...pupFocusReasonDescription
TRUEnever evaluated
FALSEnever evaluated
autoDefault()Description
TRUEnever evaluated
FALSEnever evaluated
d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
498 QDialog *dlg = qobject_cast<QDialog*>(window());-
499 if (dlg)
dlgDescription
TRUEnever evaluated
FALSEnever evaluated
0
500 dlg->d_func()->setDefault(0);
never executed: dlg->d_func()->setDefault(0);
0
501 else-
502 d->defaultButton = false;
never executed: d->defaultButton = false;
0
503 }-
504-
505 QAbstractButton::focusOutEvent(e);-
506#ifndef QT_NO_MENU-
507 if (d->menu && d->menu->isVisible()) // restore pressed status
d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
d->menu->isVisible()Description
TRUEnever evaluated
FALSEnever evaluated
0
508 setDown(true);
never executed: setDown(true);
0
509#endif-
510}
never executed: end of block
0
511-
512#ifndef QT_NO_MENU-
513/*!-
514 Associates the popup menu \a menu with this push button. This-
515 turns the button into a menu button, which in some styles will-
516 produce a small triangle to the right of the button's text.-
517-
518 Ownership of the menu is \e not transferred to the push button.-
519-
520 \image fusion-pushbutton-menu.png Screenshot of a Fusion style push button with popup menu.-
521 A push button with popup menus shown in the \l{Fusion Style Widget Gallery}{Fusion widget style}.-
522-
523 \sa menu()-
524*/-
525void QPushButton::setMenu(QMenu* menu)-
526{-
527 Q_D(QPushButton);-
528 if (menu == d->menu)
menu == d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
529 return;
never executed: return;
0
530-
531 if (menu && !d->menu) {
menuDescription
TRUEnever evaluated
FALSEnever evaluated
!d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
532 connect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed()), Qt::UniqueConnection);-
533 }
never executed: end of block
0
534 if (d->menu)
d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
535 removeAction(d->menu->menuAction());
never executed: removeAction(d->menu->menuAction());
0
536 d->menu = menu;-
537 if (d->menu)
d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
538 addAction(d->menu->menuAction());
never executed: addAction(d->menu->menuAction());
0
539-
540 d->resetLayoutItemMargins();-
541 d->sizeHint = QSize();-
542 update();-
543 updateGeometry();-
544}
never executed: end of block
0
545-
546/*!-
547 Returns the button's associated popup menu or 0 if no popup menu-
548 has been set.-
549-
550 \sa setMenu()-
551*/-
552QMenu* QPushButton::menu() const-
553{-
554 Q_D(const QPushButton);-
555 return d->menu;
never executed: return d->menu;
0
556}-
557-
558/*!-
559 Shows (pops up) the associated popup menu. If there is no such-
560 menu, this function does nothing. This function does not return-
561 until the popup menu has been closed by the user.-
562*/-
563void QPushButton::showMenu()-
564{-
565 Q_D(QPushButton);-
566 if (!d || !d->menu)
!dDescription
TRUEnever evaluated
FALSEnever evaluated
!d->menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
567 return;
never executed: return;
0
568 setDown(true);-
569 d->_q_popupPressed();-
570}
never executed: end of block
0
571-
572void QPushButtonPrivate::_q_popupPressed()-
573{-
574 Q_Q(QPushButton);-
575 if (!down || !menu)
!downDescription
TRUEnever evaluated
FALSEnever evaluated
!menuDescription
TRUEnever evaluated
FALSEnever evaluated
0
576 return;
never executed: return;
0
577-
578 menu->setNoReplayFor(q);-
579-
580 QPoint menuPos = adjustedMenuPosition();-
581-
582 QPointer<QPushButton> guard(q);-
583 QMenuPrivate::get(menu)->causedPopup.widget = guard;-
584-
585 //Because of a delay in menu effects, we must keep track of the-
586 //menu visibility to avoid flicker on button release-
587 menuOpen = true;-
588 menu->exec(menuPos);-
589 if (guard) {
guardDescription
TRUEnever evaluated
FALSEnever evaluated
0
590 menuOpen = false;-
591 q->setDown(false);-
592 }
never executed: end of block
0
593}
never executed: end of block
0
594-
595QPoint QPushButtonPrivate::adjustedMenuPosition()-
596{-
597 Q_Q(QPushButton);-
598-
599 bool horizontal = true;-
600#if !defined(QT_NO_TOOLBAR)-
601 QToolBar *tb = qobject_cast<QToolBar*>(parent);-
602 if (tb && tb->orientation() == Qt::Vertical)
tbDescription
TRUEnever evaluated
FALSEnever evaluated
tb->orientatio...= Qt::VerticalDescription
TRUEnever evaluated
FALSEnever evaluated
0
603 horizontal = false;
never executed: horizontal = false;
0
604#endif-
605-
606 QWidgetItem item(q);-
607 QRect rect = item.geometry();-
608 rect.setRect(rect.x() - q->x(), rect.y() - q->y(), rect.width(), rect.height());-
609-
610 QSize menuSize = menu->sizeHint();-
611 QPoint globalPos = q->mapToGlobal(rect.topLeft());-
612 int x = globalPos.x();-
613 int y = globalPos.y();-
614 if (horizontal) {
horizontalDescription
TRUEnever evaluated
FALSEnever evaluated
0
615 if (globalPos.y() + rect.height() + menuSize.height() <= QApplication::desktop()->availableGeometry(q).height()) {
globalPos.y() ...ry(q).height()Description
TRUEnever evaluated
FALSEnever evaluated
0
616 y += rect.height();-
617 } else {
never executed: end of block
0
618 y -= menuSize.height();-
619 }
never executed: end of block
0
620 if (q->layoutDirection() == Qt::RightToLeft)
q->layoutDirec...t::RightToLeftDescription
TRUEnever evaluated
FALSEnever evaluated
0
621 x += rect.width() - menuSize.width();
never executed: x += rect.width() - menuSize.width();
0
622 } else {
never executed: end of block
0
623 if (globalPos.x() + rect.width() + menu->sizeHint().width() <= QApplication::desktop()->availableGeometry(q).width())
globalPos.x() ...try(q).width()Description
TRUEnever evaluated
FALSEnever evaluated
0
624 x += rect.width();
never executed: x += rect.width();
0
625 else-
626 x -= menuSize.width();
never executed: x -= menuSize.width();
0
627 }-
628-
629 return QPoint(x,y);
never executed: return QPoint(x,y);
0
630}-
631-
632#endif // QT_NO_MENU-
633-
634void QPushButtonPrivate::resetLayoutItemMargins()-
635{-
636 Q_Q(QPushButton);-
637 QStyleOptionButton opt;-
638 q->initStyleOption(&opt);-
639 setLayoutItemMargins(QStyle::SE_PushButtonLayoutItem, &opt);-
640}
never executed: end of block
0
641-
642void QPushButton::setFlat(bool flat)-
643{-
644 Q_D(QPushButton);-
645 if (d->flat == flat)
d->flat == flatDescription
TRUEnever evaluated
FALSEnever evaluated
0
646 return;
never executed: return;
0
647 d->flat = flat;-
648 d->resetLayoutItemMargins();-
649 d->sizeHint = QSize();-
650 update();-
651 updateGeometry();-
652}
never executed: end of block
0
653-
654bool QPushButton::isFlat() const-
655{-
656 Q_D(const QPushButton);-
657 return d->flat;
never executed: return d->flat;
0
658}-
659-
660/*! \reimp */-
661bool QPushButton::event(QEvent *e)-
662{-
663 Q_D(QPushButton);-
664 if (e->type() == QEvent::ParentChange) {
e->type() == Q...::ParentChangeDescription
TRUEnever evaluated
FALSEnever evaluated
0
665 if (QDialog *dialog = d->dialogParent()) {
QDialog *dialo...dialogParent()Description
TRUEnever evaluated
FALSEnever evaluated
0
666 if (d->defaultButton)
d->defaultButtonDescription
TRUEnever evaluated
FALSEnever evaluated
0
667 dialog->d_func()->setMainDefault(this);
never executed: dialog->d_func()->setMainDefault(this);
0
668 }
never executed: end of block
0
669 } else if (e->type() == QEvent::StyleChange
never executed: end of block
e->type() == Q...t::StyleChangeDescription
TRUEnever evaluated
FALSEnever evaluated
0
670#ifdef Q_OS_MAC-
671 || e->type() == QEvent::MacSizeChange-
672#endif-
673 ) {-
674 d->resetLayoutItemMargins();-
675 updateGeometry();-
676 } else if (e->type() == QEvent::PolishRequest) {
never executed: end of block
e->type() == Q...:PolishRequestDescription
TRUEnever evaluated
FALSEnever evaluated
0
677 updateGeometry();-
678 }
never executed: end of block
0
679 return QAbstractButton::event(e);
never executed: return QAbstractButton::event(e);
0
680}-
681-
682#ifdef Q_DEAD_CODE_FROM_QT4_MAC-
683/*! \reimp */-
684bool QPushButton::hitButton(const QPoint &pos) const-
685{-
686 QStyleOptionButton opt;-
687 initStyleOption(&opt);-
688 if (qt_mac_buttonIsRenderedFlat(this, &opt))-
689 return QAbstractButton::hitButton(pos);-
690-
691 // Now that we know we are using the native style, let's proceed.-
692 Q_D(const QPushButton);-
693 QPushButtonPrivate *nonConst = const_cast<QPushButtonPrivate *>(d);-
694 // In OSX buttons are round, which causes the hit method to be special.-
695 // We cannot simply relay on detecting if something is inside the rect or not,-
696 // we need to check if it is inside the "rounded area" or not. A point might-
697 // be inside the rect but not inside the rounded area.-
698 // Notice this method is only reimplemented for OSX.-
699 return nonConst->hitButton(pos);-
700}-
701-
702bool QPushButtonPrivate::hitButton(const QPoint &pos)-
703{-
704 Q_Q(QPushButton);-
705 QRect roundedRect(q->rect().left() + QMacStylePrivate::PushButtonLeftOffset,-
706 q->rect().top() + QMacStylePrivate::PushButtonContentPadding,-
707 q->rect().width() - QMacStylePrivate::PushButtonRightOffset,-
708 q->rect().height() - QMacStylePrivate::PushButtonBottomOffset);-
709 return roundedRect.contains(pos);-
710}-
711#endif // Q_DEAD_CODE_FROM_QT4_MAC-
712-
713-
714QT_END_NAMESPACE-
715-
716#include "moc_qpushbutton.cpp"-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9