widgets/qpushbutton.cpp

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

Generated by Squish Coco Non-Commercial