1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtoolbutton.h"
#ifndef QT_NO_TOOLBUTTON
#include <qapplication.h>
#include <qdesktopwidget.h>
#include <qdrawutil.h>
#include <qevent.h>
#include <qicon.h>
#include <qmenu.h>
#include <qpainter.h>
#include <qpointer.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qtooltip.h>
#include <qmainwindow.h>
#include <qtoolbar.h>
#include <qvariant.h>
#include <qstylepainter.h>
#include <private/qabstractbutton_p.h>
#include <private/qaction_p.h>
#include <private/qmenu_p.h>
QT_BEGIN_NAMESPACE
class QToolButtonPrivate : public QAbstractButtonPrivate
{
Q_DECLARE_PUBLIC(QToolButton)
public:
void init();
#ifndef QT_NO_MENU
void _q_buttonPressed();
void popupTimerDone();
void _q_updateButtonDown();
void _q_menuTriggered(QAction *);
#endif
bool updateHoverControl(const QPoint &pos);
void _q_actionTriggered();
QStyle::SubControl newHoverControl(const QPoint &pos);
QStyle::SubControl hoverControl;
QRect hoverRect;
QPointer<QAction> menuAction; //the menu set by the user (setMenu)
QBasicTimer popupTimer;
int delay;
Qt::ArrowType arrowType;
Qt::ToolButtonStyle toolButtonStyle;
QToolButton::ToolButtonPopupMode popupMode;
enum { NoButtonPressed=0, MenuButtonPressed=1, ToolButtonPressed=2 };
uint buttonPressed : 2;
uint menuButtonDown : 1;
uint autoRaise : 1;
uint repeat : 1;
QAction *defaultAction;
#ifndef QT_NO_MENU
bool hasMenu() const;
//workaround for task 177850
QList<QAction *> actionsCopy;
#endif
#ifdef QT3_SUPPORT
bool userDefinedPopupDelay;
#endif
};
#ifndef QT_NO_MENU
bool QToolButtonPrivate::hasMenu() const
{
Q_Q(const QToolButton);
return ((defaultAction && defaultAction->menu())
|| (menuAction && menuAction->menu())
|| q->actions().size() > (defaultAction ? 1 : 0));
}
#endif
/*!
\class QToolButton
\brief The QToolButton class provides a quick-access button to
commands or options, usually used inside a QToolBar.
\ingroup basicwidgets
\mainclass
A tool button is a special button that provides quick-access to
specific commands or options. As opposed to a normal command
button, a tool button usually doesn't show a text label, but shows
an icon instead.
Tool buttons are normally created when new QAction instances are
created with QToolBar::addAction() or existing actions are added
to a toolbar with QToolBar::addAction(). It is also possible to
construct tool buttons in the same way as any other widget, and
arrange them alongside other widgets in layouts.
One classic use of a tool button is to select tools; for example,
the "pen" tool in a drawing program. This would be implemented
by using a QToolButton as a toggle button (see setToggleButton()).
QToolButton supports auto-raising. In auto-raise mode, the button
draws a 3D frame only when the mouse points at it. The feature is
automatically turned on when a button is used inside a QToolBar.
Change it with setAutoRaise().
A tool button's icon is set as QIcon. This makes it possible to
specify different pixmaps for the disabled and active state. The
disabled pixmap is used when the button's functionality is not
available. The active pixmap is displayed when the button is
auto-raised because the mouse pointer is hovering over it.
The button's look and dimension is adjustable with
setToolButtonStyle() and setIconSize(). When used inside a
QToolBar in a QMainWindow, the button automatically adjusts to
QMainWindow's settings (see QMainWindow::setToolButtonStyle() and
QMainWindow::setIconSize()). Instead of an icon, a tool button can
also display an arrow symbol, specified with
\l{QToolButton::arrowType} {arrowType}.
A tool button can offer additional choices in a popup menu. The
popup menu can be set using setMenu(). Use setPopupMode() to
configure the different modes available for tool buttons with a
menu set. The default mode is DelayedPopupMode which is sometimes
used with the "Back" button in a web browser. After pressing and
holding the button down for a while, a menu pops up showing a list
of possible pages to jump to. The default delay is 600 ms; you can
adjust it with setPopupDelay().
\table 100%
\row \o \inlineimage assistant-toolbar.png Qt Assistant's toolbar with tool buttons
\row \o Qt Assistant's toolbar contains tool buttons that are associated
with actions used in other parts of the main window.
\endtable
\sa QPushButton, QToolBar, QMainWindow, QAction,
{fowler}{GUI Design Handbook: Push Button}
*/
/*!
\fn void QToolButton::triggered(QAction *action)
This signal is emitted when the given \a action is triggered.
The action may also be associated with other parts of the user interface,
such as menu items and keyboard shortcuts. Sharing actions in this
way helps make the user interface more consistent and is often less work
to implement.
*/
/*!
Constructs an empty tool button with parent \a
parent.
*/
QToolButton::QToolButton(QWidget * parent)
: QAbstractButton(*new QToolButtonPrivate, parent)
{
Q_D(QToolButton);
d->init();
}
#ifdef QT3_SUPPORT
/*!
Constructs an empty tool button called \a name, with parent \a
parent.
*/
QToolButton::QToolButton(QWidget * parent, const char *name)
: QAbstractButton(*new QToolButtonPrivate, parent)
{
Q_D(QToolButton);
setObjectName(QString::fromAscii(name));
d->init();
}
/*!
Constructs a tool button called \a name, that is a child of \a
parent.
The tool button will display the given \a icon, with its text
label and tool tip set to \a textLabel and its status bar message
set to \a statusTip. It will be connected to the \a slot in
object \a receiver.
*/
QToolButton::QToolButton(const QIcon& icon, const QString &textLabel,
const QString& statusTip,
QObject * receiver, const char *slot,
QWidget * parent, const char *name)
: QAbstractButton(*new QToolButtonPrivate, parent)
{
Q_D(QToolButton);
setObjectName(QString::fromAscii(name));
d->init();
setIcon(icon);
setText(textLabel);
if (receiver && slot)
connect(this, SIGNAL(clicked()), receiver, slot);
#ifndef QT_NO_TOOLTIP
if (!textLabel.isEmpty())
setToolTip(textLabel);
#endif
#ifndef QT_NO_STATUSTIP
if (!statusTip.isEmpty())
setStatusTip(statusTip);
#else
Q_UNUSED(statusTip);
#endif
}
/*!
Constructs a tool button as an arrow button. The Qt::ArrowType \a
type defines the arrow direction. Possible values are
Qt::LeftArrow, Qt::RightArrow, Qt::UpArrow, and Qt::DownArrow.
An arrow button has auto-repeat turned on by default.
The \a parent and \a name arguments are sent to the QWidget
constructor.
*/
QToolButton::QToolButton(Qt::ArrowType type, QWidget *parent, const char *name)
: QAbstractButton(*new QToolButtonPrivate, parent)
{
Q_D(QToolButton);
setObjectName(QString::fromAscii(name));
d->init();
setAutoRepeat(true);
d->arrowType = type;
}
#endif
/* Set-up code common to all the constructors */
void QToolButtonPrivate::init()
{
Q_Q(QToolButton);
delay = q->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, q);
#ifdef QT3_SUPPORT
userDefinedPopupDelay = false;
#endif
defaultAction = 0;
#ifndef QT_NO_TOOLBAR
if (qobject_cast<QToolBar*>(q->parentWidget()))
autoRaise = true;
else
#endif
autoRaise = false;
arrowType = Qt::NoArrow;
menuButtonDown = false;
popupMode = QToolButton::DelayedPopup;
buttonPressed = QToolButtonPrivate::NoButtonPressed;
toolButtonStyle = Qt::ToolButtonIconOnly;
hoverControl = QStyle::SC_None;
q->setFocusPolicy(Qt::TabFocus);
q->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed,
QSizePolicy::ToolButton));
#ifndef QT_NO_MENU
QObject::connect(q, SIGNAL(pressed()), q, SLOT(_q_buttonPressed()));
#endif
setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem);
}
/*!
Initialize \a option with the values from this QToolButton. This method
is useful for subclasses when they need a QStyleOptionToolButton, but don't want
to fill in all the information themselves.
\sa QStyleOption::initFrom()
*/
void QToolButton::initStyleOption(QStyleOptionToolButton *option) const
{
if (!option)
return;
Q_D(const QToolButton);
option->initFrom(this);
bool forceNoText = false;
option->iconSize = iconSize(); //default value
#ifndef QT_NO_TOOLBAR
if (parentWidget()) {
if (QToolBar *toolBar = qobject_cast<QToolBar *>(parentWidget())) {
option->iconSize = toolBar->iconSize();
}
#ifdef QT3_SUPPORT
else if (parentWidget()->inherits("Q3ToolBar")) {
if (!option->iconSize.isValid()) {
int iconSize = style()->pixelMetric(QStyle::PM_ToolBarIconSize, option, this);
option->iconSize = d->icon.actualSize(QSize(iconSize, iconSize));
}
forceNoText = d->toolButtonStyle == Qt::ToolButtonIconOnly;
}
#endif
}
#endif // QT_NO_TOOLBAR
if (!forceNoText)
option->text = d->text;
option->icon = d->icon;
option->arrowType = d->arrowType;
if (d->down)
option->state |= QStyle::State_Sunken;
if (d->checked)
option->state |= QStyle::State_On;
if (d->autoRaise)
option->state |= QStyle::State_AutoRaise;
if (!d->checked && !d->down)
option->state |= QStyle::State_Raised;
option->subControls = QStyle::SC_ToolButton;
option->activeSubControls = QStyle::SC_None;
option->features = QStyleOptionToolButton::None;
if (d->popupMode == QToolButton::MenuButtonPopup) {
option->subControls |= QStyle::SC_ToolButtonMenu;
option->features |= QStyleOptionToolButton::MenuButtonPopup;
}
if (option->state & QStyle::State_MouseOver) {
option->activeSubControls = d->hoverControl;
}
if (d->menuButtonDown) {
option->state |= QStyle::State_Sunken;
option->activeSubControls |= QStyle::SC_ToolButtonMenu;
}
if (d->down) {
option->state |= QStyle::State_Sunken;
option->activeSubControls |= QStyle::SC_ToolButton;
}
if (d->arrowType != Qt::NoArrow)
option->features |= QStyleOptionToolButton::Arrow;
if (d->popupMode == QToolButton::DelayedPopup)
option->features |= QStyleOptionToolButton::PopupDelay;
#ifndef QT_NO_MENU
if (d->hasMenu())
option->features |= QStyleOptionToolButton::HasMenu;
#endif
option->toolButtonStyle = d->toolButtonStyle;
if (d->icon.isNull() && d->arrowType == Qt::NoArrow && !forceNoText) {
if (!d->text.isEmpty())
option->toolButtonStyle = Qt::ToolButtonTextOnly;
else if (option->toolButtonStyle != Qt::ToolButtonTextOnly)
option->toolButtonStyle = Qt::ToolButtonIconOnly;
} else {
if (d->text.isEmpty() && option->toolButtonStyle != Qt::ToolButtonIconOnly)
option->toolButtonStyle = Qt::ToolButtonIconOnly;
}
option->pos = pos();
option->font = font();
}
/*!
Destroys the object and frees any allocated resources.
*/
QToolButton::~QToolButton()
{
}
/*!
\reimp
*/
QSize QToolButton::sizeHint() const
{
Q_D(const QToolButton);
if (d->sizeHint.isValid())
return d->sizeHint;
ensurePolished();
int w = 0, h = 0;
QStyleOptionToolButton opt;
initStyleOption(&opt);
QFontMetrics fm = fontMetrics();
if (opt.toolButtonStyle != Qt::ToolButtonTextOnly) {
QSize icon = opt.iconSize;
w = icon.width();
h = icon.height();
}
if (opt.toolButtonStyle != Qt::ToolButtonIconOnly) {
QSize textSize = fm.size(Qt::TextShowMnemonic, text());
textSize.setWidth(textSize.width() + fm.width(QLatin1Char(' '))*2);
if (opt.toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
h += 4 + textSize.height();
if (textSize.width() > w)
w = textSize.width();
} else if (opt.toolButtonStyle == Qt::ToolButtonTextBesideIcon) {
w += 4 + textSize.width();
if (textSize.height() > h)
h = textSize.height();
} else { // TextOnly
w = textSize.width();
h = textSize.height();
}
}
opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
if (d->popupMode == MenuButtonPopup)
w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
d->sizeHint = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this).
expandedTo(QApplication::globalStrut());
return d->sizeHint;
}
/*!
\reimp
*/
QSize QToolButton::minimumSizeHint() const
{
return sizeHint();
}
/*!
\enum QToolButton::TextPosition
\compat
This enum describes the position of the tool button's text label in
relation to the tool button's icon.
\value BesideIcon The text appears beside the icon.
\value BelowIcon The text appears below the icon.
\omitvalue Right
\omitvalue Under
*/
/*!
\property QToolButton::toolButtonStyle
\brief whether the tool button displays an icon only, text only,
or text beside/below the icon.
The default is Qt::ToolButtonIconOnly.
QToolButton automatically connects this slot to the relevant
signal in the QMainWindow in which is resides.
*/
/*!
\property QToolButton::arrowType
\brief whether the button displays an arrow instead of a normal icon
This displays an arrow as the icon for the QToolButton.
By default, this property is set to Qt::NoArrow.
*/
Qt::ToolButtonStyle QToolButton::toolButtonStyle() const
{
Q_D(const QToolButton);
return d->toolButtonStyle;
}
Qt::ArrowType QToolButton::arrowType() const
{
Q_D(const QToolButton);
return d->arrowType;
}
void QToolButton::setToolButtonStyle(Qt::ToolButtonStyle style)
{
Q_D(QToolButton);
if (d->toolButtonStyle == style)
return;
d->toolButtonStyle = style;
d->sizeHint = QSize();
updateGeometry();
if (isVisible()) {
update();
}
}
void QToolButton::setArrowType(Qt::ArrowType type)
{
Q_D(QToolButton);
if (d->arrowType == type)
return;
d->arrowType = type;
d->sizeHint = QSize();
updateGeometry();
if (isVisible()) {
update();
}
}
/*!
\fn void QToolButton::paintEvent(QPaintEvent *event)
Paints the button in response to the paint \a event.
*/
void QToolButton::paintEvent(QPaintEvent *)
{
QStylePainter p(this);
QStyleOptionToolButton opt;
initStyleOption(&opt);
p.drawComplexControl(QStyle::CC_ToolButton, opt);
}
/*!
\reimp
*/
void QToolButton::actionEvent(QActionEvent *event)
{
Q_D(QToolButton);
QAction *action = event->action();
switch (event->type()) {
case QEvent::ActionChanged:
if (action == d->defaultAction)
setDefaultAction(action); // update button state
break;
case QEvent::ActionAdded:
connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
break;
case QEvent::ActionRemoved:
if (d->defaultAction == action)
d->defaultAction = 0;
#ifndef QT_NO_MENU
if (action == d->menuAction)
d->menuAction = 0;
#endif
action->disconnect(this);
break;
default:
;
}
QAbstractButton::actionEvent(event);
}
QStyle::SubControl QToolButtonPrivate::newHoverControl(const QPoint &pos)
{
Q_Q(QToolButton);
QStyleOptionToolButton opt;
q->initStyleOption(&opt);
opt.subControls = QStyle::SC_All;
hoverControl = q->style()->hitTestComplexControl(QStyle::CC_ToolButton, &opt, pos, q);
if (hoverControl == QStyle::SC_None)
hoverRect = QRect();
else
hoverRect = q->style()->subControlRect(QStyle::CC_ToolButton, &opt, hoverControl, q);
return hoverControl;
}
bool QToolButtonPrivate::updateHoverControl(const QPoint &pos)
{
Q_Q(QToolButton);
QRect lastHoverRect = hoverRect;
QStyle::SubControl lastHoverControl = hoverControl;
bool doesHover = q->testAttribute(Qt::WA_Hover);
if (lastHoverControl != newHoverControl(pos) && doesHover) {
q->update(lastHoverRect);
q->update(hoverRect);
return true;
}
return !doesHover;
}
void QToolButtonPrivate::_q_actionTriggered()
{
Q_Q(QToolButton);
if (QAction *action = qobject_cast<QAction *>(q->sender()))
emit q->triggered(action);
}
/*!
\reimp
*/
void QToolButton::enterEvent(QEvent * e)
{
Q_D(QToolButton);
if (d->autoRaise)
update();
if (d->defaultAction)
d->defaultAction->hover();
QAbstractButton::enterEvent(e);
}
/*!
\reimp
*/
void QToolButton::leaveEvent(QEvent * e)
{
Q_D(QToolButton);
if (d->autoRaise)
update();
QAbstractButton::leaveEvent(e);
}
/*!
\reimp
*/
void QToolButton::timerEvent(QTimerEvent *e)
{
#ifndef QT_NO_MENU
Q_D(QToolButton);
if (e->timerId() == d->popupTimer.timerId()) {
d->popupTimerDone();
return;
}
#endif
QAbstractButton::timerEvent(e);
}
/*!
\reimp
*/
void QToolButton::changeEvent(QEvent *e)
{
#ifndef QT_NO_TOOLBAR
Q_D(QToolButton);
if (e->type() == QEvent::ParentChange) {
if (qobject_cast<QToolBar*>(parentWidget()))
d->autoRaise = true;
} else if (e->type() == QEvent::StyleChange
#ifdef Q_WS_MAC
|| e->type() == QEvent::MacSizeChange
#endif
) {
#ifdef QT3_SUPPORT
if (!d->userDefinedPopupDelay)
#endif
d->delay = style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, this);
d->setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem);
}
#endif
QAbstractButton::changeEvent(e);
}
/*!
\reimp
*/
void QToolButton::mousePressEvent(QMouseEvent *e)
{
Q_D(QToolButton);
#ifndef QT_NO_MENU
QStyleOptionToolButton opt;
initStyleOption(&opt);
if (e->button() == Qt::LeftButton && (d->popupMode == MenuButtonPopup)) {
QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt,
QStyle::SC_ToolButtonMenu, this);
if (popupr.isValid() && popupr.contains(e->pos())) {
d->buttonPressed = QToolButtonPrivate::MenuButtonPressed;
showMenu();
return;
}
}
#endif
d->buttonPressed = QToolButtonPrivate::ToolButtonPressed;
QAbstractButton::mousePressEvent(e);
}
/*!
\reimp
*/
void QToolButton::mouseReleaseEvent(QMouseEvent *e)
{
Q_D(QToolButton);
QAbstractButton::mouseReleaseEvent(e);
d->buttonPressed = QToolButtonPrivate::NoButtonPressed;
}
/*!
\reimp
*/
bool QToolButton::hitButton(const QPoint &pos) const
{
Q_D(const QToolButton);
if(QAbstractButton::hitButton(pos))
return (d->buttonPressed != QToolButtonPrivate::MenuButtonPressed);
return false;
}
#ifdef QT3_SUPPORT
/*!
Use icon() instead.
*/
QIcon QToolButton::onIconSet() const
{
return icon();
}
/*!
Use icon() instead.
*/
QIcon QToolButton::offIconSet() const
{
return icon();
}
/*!
\obsolete
Use setIcon() instead.
*/
void QToolButton::setOnIconSet(const QIcon& set)
{
setIcon(set);
}
/*!
\obsolete
Use setIcon() instead.
*/
void QToolButton::setOffIconSet(const QIcon& set)
{
setIcon(set);
}
/*! \overload
\obsolete
Since Qt 3.0, QIcon contains both the On and Off icons.
For ease of porting, this function ignores the \a on parameter and
sets the \l{QAbstractButton::icon} {icon} property. If you relied on
the \a on parameter, you probably want to update your code to use
the QIcon On/Off mechanism.
\sa icon QIcon::State
*/
void QToolButton::setIconSet(const QIcon & set, bool /* on */)
{
QAbstractButton::setIcon(set);
}
/*! \overload
\obsolete
Since Qt 3.0, QIcon contains both the On and Off icons.
For ease of porting, this function ignores the \a on parameter and
returns the \l{QAbstractButton::icon} {icon} property. If you relied
on the \a on parameter, you probably want to update your code to use
the QIcon On/Off mechanism.
*/
QIcon QToolButton::iconSet(bool /* on */) const
{
return QAbstractButton::icon();
}
#endif
#ifndef QT_NO_MENU
/*!
Associates the given \a menu with this tool button.
The menu will be shown according to the button's \l popupMode.
Ownership of the menu is not transferred to the tool button.
\sa menu()
*/
void QToolButton::setMenu(QMenu* menu)
{
Q_D(QToolButton);
if (d->menuAction)
removeAction(d->menuAction);
if (menu) {
d->menuAction = menu->menuAction();
addAction(d->menuAction);
} else {
d->menuAction = 0;
}
update();
}
/*!
Returns the associated menu, or 0 if no menu has been defined.
\sa setMenu()
*/
QMenu* QToolButton::menu() const
{
Q_D(const QToolButton);
if (d->menuAction)
return d->menuAction->menu();
return 0;
}
/*!
Shows (pops up) the associated popup menu. If there is no such
menu, this function does nothing. This function does not return
until the popup menu has been closed by the user.
*/
void QToolButton::showMenu()
{
Q_D(QToolButton);
if (!d->hasMenu()) {
d->menuButtonDown = false;
return; // no menu to show
}
d->menuButtonDown = true;
repaint();
d->popupTimer.stop();
d->popupTimerDone();
}
void QToolButtonPrivate::_q_buttonPressed()
{
Q_Q(QToolButton);
if (!hasMenu())
return; // no menu to show
if (delay > 0 && popupMode == QToolButton::DelayedPopup)
popupTimer.start(delay, q);
else if (delay == 0 || popupMode == QToolButton::InstantPopup)
q->showMenu();
}
void QToolButtonPrivate::popupTimerDone()
{
Q_Q(QToolButton);
popupTimer.stop();
if (!menuButtonDown && !down)
return;
menuButtonDown = true;
QPointer<QMenu> actualMenu;
bool mustDeleteActualMenu = false;
if(menuAction) {
actualMenu = menuAction->menu();
} else if (defaultAction && defaultAction->menu()) {
actualMenu = defaultAction->menu();
} else {
actualMenu = new QMenu(q);
mustDeleteActualMenu = true;
QList<QAction*> actions = q->actions();
for(int i = 0; i < actions.size(); i++)
actualMenu->addAction(actions.at(i));
}
repeat = q->autoRepeat();
q->setAutoRepeat(false);
bool horizontal = true;
#if !defined(QT_NO_TOOLBAR)
QToolBar *tb = qobject_cast<QToolBar*>(q->parentWidget());
if (tb && tb->orientation() == Qt::Vertical)
horizontal = false;
#endif
QPoint p;
QRect screen = qApp->desktop()->availableGeometry(q);
QSize sh = ((QToolButton*)(QMenu*)actualMenu)->receivers(SIGNAL(aboutToShow()))? QSize() : actualMenu->sizeHint();
QRect rect = q->rect();
if (horizontal) {
if (q->isRightToLeft()) {
if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
p = q->mapToGlobal(rect.bottomRight());
} else {
p = q->mapToGlobal(rect.topRight() - QPoint(0, sh.height()));
}
p.rx() -= sh.width();
} else {
if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
p = q->mapToGlobal(rect.bottomLeft());
} else {
p = q->mapToGlobal(rect.topLeft() - QPoint(0, sh.height()));
}
}
} else {
if (q->isRightToLeft()) {
if (q->mapToGlobal(QPoint(rect.left(), 0)).x() - sh.width() <= screen.x()) {
p = q->mapToGlobal(rect.topRight());
} else {
p = q->mapToGlobal(rect.topLeft());
p.rx() -= sh.width();
}
} else {
if (q->mapToGlobal(QPoint(rect.right(), 0)).x() + sh.width() <= screen.right()) {
p = q->mapToGlobal(rect.topRight());
} else {
p = q->mapToGlobal(rect.topLeft() - QPoint(sh.width(), 0));
}
}
}
p.rx() = qMax(screen.left(), qMin(p.x(), screen.right() - sh.width()));
p.ry() += 1;
QPointer<QToolButton> that = q;
actualMenu->setNoReplayFor(q);
if (!mustDeleteActualMenu) //only if action are not in this widget
QObject::connect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*)));
QObject::connect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown()));
actualMenu->d_func()->causedPopup.widget = q;
actualMenu->d_func()->causedPopup.action = defaultAction;
actionsCopy = q->actions(); //(the list of action may be modified in slots)
actualMenu->exec(p);
QObject::disconnect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown()));
if (mustDeleteActualMenu)
delete actualMenu;
else
QObject::disconnect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*)));
if (!that)
return;
actionsCopy.clear();
if (repeat)
q->setAutoRepeat(true);
}
void QToolButtonPrivate::_q_updateButtonDown()
{
Q_Q(QToolButton);
menuButtonDown = false;
if (q->isDown())
q->setDown(false);
else
q->repaint();
}
void QToolButtonPrivate::_q_menuTriggered(QAction *action)
{
Q_Q(QToolButton);
if (action && !actionsCopy.contains(action))
emit q->triggered(action);
}
#endif // QT_NO_MENU
#ifdef QT3_SUPPORT
/*!
\fn void QToolButton::setPopupDelay(int delay)
Use the style hint QStyle::SH_ToolButton_PopupDelay instead.
*/
void QToolButton::setPopupDelay(int delay)
{
Q_D(QToolButton);
d->userDefinedPopupDelay = true;
d->delay = delay;
update();
}
/*!
Use the style hint QStyle::SH_ToolButton_PopupDelay instead.
*/
int QToolButton::popupDelay() const
{
Q_D(const QToolButton);
return d->delay;
}
#endif
#ifndef QT_NO_MENU
/*! \enum QToolButton::ToolButtonPopupMode
Describes how a menu should be popped up for tool buttons that has
a menu set or contains a list of actions.
\value DelayedPopup After pressing and holding the tool button
down for a certain amount of time (the timeout is style dependant,
see QStyle::SH_ToolButton_PopupDelay), the menu is displayed. A
typical application example is the "back" button in some web
browsers's tool bars. If the user clicks it, the browser simply
browses back to the previous page. If the user presses and holds
the button down for a while, the tool button shows a menu
containing the current history list
\value MenuButtonPopup In this mode the tool button displays a
special arrow to indicate that a menu is present. The menu is
displayed when the arrow part of the button is pressed.
\value InstantPopup The menu is displayed, without delay, when
the tool button is pressed. In this mode, the button's own action
is not triggered.
*/
/*!
\property QToolButton::popupMode
\brief describes the way that popup menus are used with tool buttons
By default, this property is set to \l DelayedPopup.
*/
void QToolButton::setPopupMode(ToolButtonPopupMode mode)
{
Q_D(QToolButton);
d->popupMode = mode;
}
QToolButton::ToolButtonPopupMode QToolButton::popupMode() const
{
Q_D(const QToolButton);
return d->popupMode;
}
#endif
/*!
\property QToolButton::autoRaise
\brief whether auto-raising is enabled or not.
The default is disabled (i.e. false).
This property is currently ignored on Mac OS X when using QMacStyle.
*/
void QToolButton::setAutoRaise(bool enable)
{
Q_D(QToolButton);
d->autoRaise = enable;
update();
}
bool QToolButton::autoRaise() const
{
Q_D(const QToolButton);
return d->autoRaise;
}
/*!
Sets the default action to \a action.
If a tool button has a default action, the action defines the
button's properties like text, icon, tool tip, etc.
*/
void QToolButton::setDefaultAction(QAction *action)
{
Q_D(QToolButton);
#ifndef QT_NO_MENU
bool hadMenu = false;
hadMenu = d->hasMenu();
#endif
d->defaultAction = action;
if (!action)
return;
if (!actions().contains(action))
addAction(action);
setText(action->iconText());
setIcon(action->icon());
#ifndef QT_NO_TOOLTIP
setToolTip(action->toolTip());
#endif
#ifndef QT_NO_STATUSTIP
setStatusTip(action->statusTip());
#endif
#ifndef QT_NO_WHATSTHIS
setWhatsThis(action->whatsThis());
#endif
#ifndef QT_NO_MENU
if (action->menu() && !hadMenu) {
// new 'default' popup mode defined introduced by tool bar. We
// should have changed QToolButton's default instead. Do that
// in 4.2.
setPopupMode(QToolButton::MenuButtonPopup);
}
#endif
setCheckable(action->isCheckable());
setChecked(action->isChecked());
setEnabled(action->isEnabled());
if (action->d_func()->fontSet)
setFont(action->font());
}
/*!
Returns the default action.
\sa setDefaultAction()
*/
QAction *QToolButton::defaultAction() const
{
Q_D(const QToolButton);
return d->defaultAction;
}
/*!
\reimp
*/
void QToolButton::nextCheckState()
{
Q_D(QToolButton);
if (!d->defaultAction)
QAbstractButton::nextCheckState();
else
d->defaultAction->trigger();
}
/*! \reimp */
bool QToolButton::event(QEvent *event)
{
switch(event->type()) {
case QEvent::HoverEnter:
case QEvent::HoverLeave:
case QEvent::HoverMove:
if (const QHoverEvent *he = static_cast<const QHoverEvent *>(event))
d_func()->updateHoverControl(he->pos());
break;
default:
break;
}
return QAbstractButton::event(event);
}
/*! \internal
*/
QToolButton::QToolButton(QToolButtonPrivate &dd, QWidget *parent)
:QAbstractButton(dd, parent)
{
Q_D(QToolButton);
d->init();
}
/*!
\fn void QToolButton::setPixmap(const QPixmap &pixmap)
Use setIcon(QIcon(pixmap)) instead.
*/
/*!
\fn void QToolButton::setIconSet(const QIcon &icon)
Use setIcon() instead.
*/
/*!
\fn void QToolButton::setTextLabel(const QString &text, bool tooltip)
Use setText() and setToolTip() instead.
*/
/*!
\fn QString QToolButton::textLabel() const
Use text() instead.
*/
/*!
\fn QIcon QToolButton::iconSet() const
Use icon() instead.
*/
/*!
\fn void QToolButton::openPopup()
Use showMenu() instead.
*/
/*!
\fn void QToolButton::setPopup(QMenu* popup)
Use setMenu() instead.
*/
/*!
\fn QMenu* QToolButton::popup() const
Use menu() instead.
*/
/*!
\fn TextPosition QToolButton::textPosition() const
Use toolButtonStyle() instead.
*/
/*!
\fn void QToolButton::setTextPosition(QToolButton::TextPosition pos)
Use setToolButtonStyle() instead.
*/
/*!
\fn bool QToolButton::usesBigPixmap() const
Use iconSize() instead.
*/
/*!
\fn void QToolButton::setUsesBigPixmap(bool enable)
Use setIconSize() instead.
*/
/*!
\fn bool QToolButton::usesTextLabel() const
Use toolButtonStyle() instead.
*/
/*!
\fn void QToolButton::setUsesTextLabel(bool enable)
Use setToolButtonStyle() instead.
*/
QT_END_NAMESPACE
#include "moc_qtoolbutton.cpp"
#endif
|