Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qtoolbutton.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qtoolbutton.h" | - | ||||||||||||||||||
35 | #ifndef QT_NO_TOOLBUTTON | - | ||||||||||||||||||
36 | - | |||||||||||||||||||
37 | #include <qapplication.h> | - | ||||||||||||||||||
38 | #include <qdesktopwidget.h> | - | ||||||||||||||||||
39 | #include <qdrawutil.h> | - | ||||||||||||||||||
40 | #include <qevent.h> | - | ||||||||||||||||||
41 | #include <qicon.h> | - | ||||||||||||||||||
42 | #include <qmenu.h> | - | ||||||||||||||||||
43 | #include <qpainter.h> | - | ||||||||||||||||||
44 | #include <qpointer.h> | - | ||||||||||||||||||
45 | #include <qstyle.h> | - | ||||||||||||||||||
46 | #include <qstyleoption.h> | - | ||||||||||||||||||
47 | #include <qtooltip.h> | - | ||||||||||||||||||
48 | #include <qmainwindow.h> | - | ||||||||||||||||||
49 | #include <qtoolbar.h> | - | ||||||||||||||||||
50 | #include <qvariant.h> | - | ||||||||||||||||||
51 | #include <qstylepainter.h> | - | ||||||||||||||||||
52 | #include <private/qabstractbutton_p.h> | - | ||||||||||||||||||
53 | #include <private/qaction_p.h> | - | ||||||||||||||||||
54 | #include <private/qmenu_p.h> | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | class QToolButtonPrivate : public QAbstractButtonPrivate | - | ||||||||||||||||||
59 | { | - | ||||||||||||||||||
60 | Q_DECLARE_PUBLIC(QToolButton) | - | ||||||||||||||||||
61 | public: | - | ||||||||||||||||||
62 | void init(); | - | ||||||||||||||||||
63 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
64 | void _q_buttonPressed(); | - | ||||||||||||||||||
65 | void _q_buttonReleased(); | - | ||||||||||||||||||
66 | void popupTimerDone(); | - | ||||||||||||||||||
67 | void _q_updateButtonDown(); | - | ||||||||||||||||||
68 | void _q_menuTriggered(QAction *); | - | ||||||||||||||||||
69 | #endif | - | ||||||||||||||||||
70 | bool updateHoverControl(const QPoint &pos); | - | ||||||||||||||||||
71 | void _q_actionTriggered(); | - | ||||||||||||||||||
72 | QStyle::SubControl newHoverControl(const QPoint &pos); | - | ||||||||||||||||||
73 | QStyle::SubControl hoverControl; | - | ||||||||||||||||||
74 | QRect hoverRect; | - | ||||||||||||||||||
75 | QPointer<QAction> menuAction; //the menu set by the user (setMenu) | - | ||||||||||||||||||
76 | QBasicTimer popupTimer; | - | ||||||||||||||||||
77 | int delay; | - | ||||||||||||||||||
78 | Qt::ArrowType arrowType; | - | ||||||||||||||||||
79 | Qt::ToolButtonStyle toolButtonStyle; | - | ||||||||||||||||||
80 | QToolButton::ToolButtonPopupMode popupMode; | - | ||||||||||||||||||
81 | enum { NoButtonPressed=0, MenuButtonPressed=1, ToolButtonPressed=2 }; | - | ||||||||||||||||||
82 | uint buttonPressed : 2; | - | ||||||||||||||||||
83 | uint menuButtonDown : 1; | - | ||||||||||||||||||
84 | uint autoRaise : 1; | - | ||||||||||||||||||
85 | uint repeat : 1; | - | ||||||||||||||||||
86 | QAction *defaultAction; | - | ||||||||||||||||||
87 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
88 | bool hasMenu() const; | - | ||||||||||||||||||
89 | //workaround for task 177850 | - | ||||||||||||||||||
90 | QList<QAction *> actionsCopy; | - | ||||||||||||||||||
91 | #endif | - | ||||||||||||||||||
92 | }; | - | ||||||||||||||||||
93 | - | |||||||||||||||||||
94 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
95 | bool QToolButtonPrivate::hasMenu() const | - | ||||||||||||||||||
96 | { | - | ||||||||||||||||||
97 | return ((defaultAction && defaultAction->menu()) never executed: return ((defaultAction && defaultAction->menu()) || (menuAction && menuAction->menu()) || actions.size() > (defaultAction ? 1 : 0));
| 0 | ||||||||||||||||||
98 | || (menuAction && menuAction->menu()) never executed: return ((defaultAction && defaultAction->menu()) || (menuAction && menuAction->menu()) || actions.size() > (defaultAction ? 1 : 0));
| 0 | ||||||||||||||||||
99 | || actions.size() > (defaultAction ? 1 : 0)); never executed: return ((defaultAction && defaultAction->menu()) || (menuAction && menuAction->menu()) || actions.size() > (defaultAction ? 1 : 0));
| 0 | ||||||||||||||||||
100 | } | - | ||||||||||||||||||
101 | #endif | - | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | /*! | - | ||||||||||||||||||
104 | \class QToolButton | - | ||||||||||||||||||
105 | \brief The QToolButton class provides a quick-access button to | - | ||||||||||||||||||
106 | commands or options, usually used inside a QToolBar. | - | ||||||||||||||||||
107 | - | |||||||||||||||||||
108 | \ingroup basicwidgets | - | ||||||||||||||||||
109 | \inmodule QtWidgets | - | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | A tool button is a special button that provides quick-access to | - | ||||||||||||||||||
112 | specific commands or options. As opposed to a normal command | - | ||||||||||||||||||
113 | button, a tool button usually doesn't show a text label, but shows | - | ||||||||||||||||||
114 | an icon instead. | - | ||||||||||||||||||
115 | - | |||||||||||||||||||
116 | Tool buttons are normally created when new QAction instances are | - | ||||||||||||||||||
117 | created with QToolBar::addAction() or existing actions are added | - | ||||||||||||||||||
118 | to a toolbar with QToolBar::addAction(). It is also possible to | - | ||||||||||||||||||
119 | construct tool buttons in the same way as any other widget, and | - | ||||||||||||||||||
120 | arrange them alongside other widgets in layouts. | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | One classic use of a tool button is to select tools; for example, | - | ||||||||||||||||||
123 | the "pen" tool in a drawing program. This would be implemented | - | ||||||||||||||||||
124 | by using a QToolButton as a toggle button (see setCheckable()). | - | ||||||||||||||||||
125 | - | |||||||||||||||||||
126 | QToolButton supports auto-raising. In auto-raise mode, the button | - | ||||||||||||||||||
127 | draws a 3D frame only when the mouse points at it. The feature is | - | ||||||||||||||||||
128 | automatically turned on when a button is used inside a QToolBar. | - | ||||||||||||||||||
129 | Change it with setAutoRaise(). | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | A tool button's icon is set as QIcon. This makes it possible to | - | ||||||||||||||||||
132 | specify different pixmaps for the disabled and active state. The | - | ||||||||||||||||||
133 | disabled pixmap is used when the button's functionality is not | - | ||||||||||||||||||
134 | available. The active pixmap is displayed when the button is | - | ||||||||||||||||||
135 | auto-raised because the mouse pointer is hovering over it. | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | The button's look and dimension is adjustable with | - | ||||||||||||||||||
138 | setToolButtonStyle() and setIconSize(). When used inside a | - | ||||||||||||||||||
139 | QToolBar in a QMainWindow, the button automatically adjusts to | - | ||||||||||||||||||
140 | QMainWindow's settings (see QMainWindow::setToolButtonStyle() and | - | ||||||||||||||||||
141 | QMainWindow::setIconSize()). Instead of an icon, a tool button can | - | ||||||||||||||||||
142 | also display an arrow symbol, specified with | - | ||||||||||||||||||
143 | \l{QToolButton::arrowType} {arrowType}. | - | ||||||||||||||||||
144 | - | |||||||||||||||||||
145 | A tool button can offer additional choices in a popup menu. The | - | ||||||||||||||||||
146 | popup menu can be set using setMenu(). Use setPopupMode() to | - | ||||||||||||||||||
147 | configure the different modes available for tool buttons with a | - | ||||||||||||||||||
148 | menu set. The default mode is DelayedPopupMode which is sometimes | - | ||||||||||||||||||
149 | used with the "Back" button in a web browser. After pressing and | - | ||||||||||||||||||
150 | holding the button down for a while, a menu pops up showing a list | - | ||||||||||||||||||
151 | of possible pages to jump to. The timeout is style dependent, | - | ||||||||||||||||||
152 | see QStyle::SH_ToolButton_PopupDelay. | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | \table 100% | - | ||||||||||||||||||
155 | \row \li \inlineimage assistant-toolbar.png Qt Assistant's toolbar with tool buttons | - | ||||||||||||||||||
156 | \row \li Qt Assistant's toolbar contains tool buttons that are associated | - | ||||||||||||||||||
157 | with actions used in other parts of the main window. | - | ||||||||||||||||||
158 | \endtable | - | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | \sa QPushButton, QToolBar, QMainWindow, QAction, | - | ||||||||||||||||||
161 | {fowler}{GUI Design Handbook: Push Button} | - | ||||||||||||||||||
162 | */ | - | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | /*! | - | ||||||||||||||||||
165 | \fn void QToolButton::triggered(QAction *action) | - | ||||||||||||||||||
166 | - | |||||||||||||||||||
167 | This signal is emitted when the given \a action is triggered. | - | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | The action may also be associated with other parts of the user interface, | - | ||||||||||||||||||
170 | such as menu items and keyboard shortcuts. Sharing actions in this | - | ||||||||||||||||||
171 | way helps make the user interface more consistent and is often less work | - | ||||||||||||||||||
172 | to implement. | - | ||||||||||||||||||
173 | */ | - | ||||||||||||||||||
174 | - | |||||||||||||||||||
175 | /*! | - | ||||||||||||||||||
176 | Constructs an empty tool button with parent \a | - | ||||||||||||||||||
177 | parent. | - | ||||||||||||||||||
178 | */ | - | ||||||||||||||||||
179 | QToolButton::QToolButton(QWidget * parent) | - | ||||||||||||||||||
180 | : QAbstractButton(*new QToolButtonPrivate, parent) | - | ||||||||||||||||||
181 | { | - | ||||||||||||||||||
182 | Q_D(QToolButton); | - | ||||||||||||||||||
183 | d->init(); | - | ||||||||||||||||||
184 | } never executed: end of block | 0 | ||||||||||||||||||
185 | - | |||||||||||||||||||
186 | - | |||||||||||||||||||
187 | - | |||||||||||||||||||
188 | /* Set-up code common to all the constructors */ | - | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | void QToolButtonPrivate::init() | - | ||||||||||||||||||
191 | { | - | ||||||||||||||||||
192 | Q_Q(QToolButton); | - | ||||||||||||||||||
193 | defaultAction = 0; | - | ||||||||||||||||||
194 | #ifndef QT_NO_TOOLBAR | - | ||||||||||||||||||
195 | if (qobject_cast<QToolBar*>(parent))
| 0 | ||||||||||||||||||
196 | autoRaise = true; never executed: autoRaise = true; | 0 | ||||||||||||||||||
197 | else | - | ||||||||||||||||||
198 | #endif | - | ||||||||||||||||||
199 | autoRaise = false; never executed: autoRaise = false; | 0 | ||||||||||||||||||
200 | arrowType = Qt::NoArrow; | - | ||||||||||||||||||
201 | menuButtonDown = false; | - | ||||||||||||||||||
202 | popupMode = QToolButton::DelayedPopup; | - | ||||||||||||||||||
203 | buttonPressed = QToolButtonPrivate::NoButtonPressed; | - | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | toolButtonStyle = Qt::ToolButtonIconOnly; | - | ||||||||||||||||||
206 | hoverControl = QStyle::SC_None; | - | ||||||||||||||||||
207 | - | |||||||||||||||||||
208 | q->setFocusPolicy(Qt::TabFocus); | - | ||||||||||||||||||
209 | q->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, | - | ||||||||||||||||||
210 | QSizePolicy::ToolButton)); | - | ||||||||||||||||||
211 | - | |||||||||||||||||||
212 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
213 | QObject::connect(q, SIGNAL(pressed()), q, SLOT(_q_buttonPressed())); | - | ||||||||||||||||||
214 | QObject::connect(q, SIGNAL(released()), q, SLOT(_q_buttonReleased())); | - | ||||||||||||||||||
215 | #endif | - | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem); | - | ||||||||||||||||||
218 | delay = q->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, q); | - | ||||||||||||||||||
219 | } never executed: end of block | 0 | ||||||||||||||||||
220 | - | |||||||||||||||||||
221 | /*! | - | ||||||||||||||||||
222 | Initialize \a option with the values from this QToolButton. This method | - | ||||||||||||||||||
223 | is useful for subclasses when they need a QStyleOptionToolButton, but don't want | - | ||||||||||||||||||
224 | to fill in all the information themselves. | - | ||||||||||||||||||
225 | - | |||||||||||||||||||
226 | \sa QStyleOption::initFrom() | - | ||||||||||||||||||
227 | */ | - | ||||||||||||||||||
228 | void QToolButton::initStyleOption(QStyleOptionToolButton *option) const | - | ||||||||||||||||||
229 | { | - | ||||||||||||||||||
230 | if (!option)
| 0 | ||||||||||||||||||
231 | return; never executed: return; | 0 | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | Q_D(const QToolButton); | - | ||||||||||||||||||
234 | option->initFrom(this); | - | ||||||||||||||||||
235 | bool forceNoText = false; | - | ||||||||||||||||||
236 | option->iconSize = iconSize(); //default value | - | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | #ifndef QT_NO_TOOLBAR | - | ||||||||||||||||||
239 | if (parentWidget()) {
| 0 | ||||||||||||||||||
240 | if (QToolBar *toolBar = qobject_cast<QToolBar *>(parentWidget())) {
| 0 | ||||||||||||||||||
241 | option->iconSize = toolBar->iconSize(); | - | ||||||||||||||||||
242 | } never executed: end of block | 0 | ||||||||||||||||||
243 | } never executed: end of block | 0 | ||||||||||||||||||
244 | #endif // QT_NO_TOOLBAR | - | ||||||||||||||||||
245 | - | |||||||||||||||||||
246 | if (!forceNoText)
| 0 | ||||||||||||||||||
247 | option->text = d->text; never executed: option->text = d->text; | 0 | ||||||||||||||||||
248 | option->icon = d->icon; | - | ||||||||||||||||||
249 | option->arrowType = d->arrowType; | - | ||||||||||||||||||
250 | if (d->down)
| 0 | ||||||||||||||||||
251 | option->state |= QStyle::State_Sunken; never executed: option->state |= QStyle::State_Sunken; | 0 | ||||||||||||||||||
252 | if (d->checked)
| 0 | ||||||||||||||||||
253 | option->state |= QStyle::State_On; never executed: option->state |= QStyle::State_On; | 0 | ||||||||||||||||||
254 | if (d->autoRaise)
| 0 | ||||||||||||||||||
255 | option->state |= QStyle::State_AutoRaise; never executed: option->state |= QStyle::State_AutoRaise; | 0 | ||||||||||||||||||
256 | if (!d->checked && !d->down)
| 0 | ||||||||||||||||||
257 | option->state |= QStyle::State_Raised; never executed: option->state |= QStyle::State_Raised; | 0 | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | option->subControls = QStyle::SC_ToolButton; | - | ||||||||||||||||||
260 | option->activeSubControls = QStyle::SC_None; | - | ||||||||||||||||||
261 | - | |||||||||||||||||||
262 | option->features = QStyleOptionToolButton::None; | - | ||||||||||||||||||
263 | if (d->popupMode == QToolButton::MenuButtonPopup) {
| 0 | ||||||||||||||||||
264 | option->subControls |= QStyle::SC_ToolButtonMenu; | - | ||||||||||||||||||
265 | option->features |= QStyleOptionToolButton::MenuButtonPopup; | - | ||||||||||||||||||
266 | } never executed: end of block | 0 | ||||||||||||||||||
267 | if (option->state & QStyle::State_MouseOver) {
| 0 | ||||||||||||||||||
268 | option->activeSubControls = d->hoverControl; | - | ||||||||||||||||||
269 | } never executed: end of block | 0 | ||||||||||||||||||
270 | if (d->menuButtonDown) {
| 0 | ||||||||||||||||||
271 | option->state |= QStyle::State_Sunken; | - | ||||||||||||||||||
272 | option->activeSubControls |= QStyle::SC_ToolButtonMenu; | - | ||||||||||||||||||
273 | } never executed: end of block | 0 | ||||||||||||||||||
274 | if (d->down) {
| 0 | ||||||||||||||||||
275 | option->state |= QStyle::State_Sunken; | - | ||||||||||||||||||
276 | option->activeSubControls |= QStyle::SC_ToolButton; | - | ||||||||||||||||||
277 | } never executed: end of block | 0 | ||||||||||||||||||
278 | - | |||||||||||||||||||
279 | - | |||||||||||||||||||
280 | if (d->arrowType != Qt::NoArrow)
| 0 | ||||||||||||||||||
281 | option->features |= QStyleOptionToolButton::Arrow; never executed: option->features |= QStyleOptionToolButton::Arrow; | 0 | ||||||||||||||||||
282 | if (d->popupMode == QToolButton::DelayedPopup)
| 0 | ||||||||||||||||||
283 | option->features |= QStyleOptionToolButton::PopupDelay; never executed: option->features |= QStyleOptionToolButton::PopupDelay; | 0 | ||||||||||||||||||
284 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
285 | if (d->hasMenu())
| 0 | ||||||||||||||||||
286 | option->features |= QStyleOptionToolButton::HasMenu; never executed: option->features |= QStyleOptionToolButton::HasMenu; | 0 | ||||||||||||||||||
287 | #endif | - | ||||||||||||||||||
288 | if (d->toolButtonStyle == Qt::ToolButtonFollowStyle) {
| 0 | ||||||||||||||||||
289 | option->toolButtonStyle = Qt::ToolButtonStyle(style()->styleHint(QStyle::SH_ToolButtonStyle, option, this)); | - | ||||||||||||||||||
290 | } else never executed: end of block | 0 | ||||||||||||||||||
291 | option->toolButtonStyle = d->toolButtonStyle; never executed: option->toolButtonStyle = d->toolButtonStyle; | 0 | ||||||||||||||||||
292 | - | |||||||||||||||||||
293 | if (option->toolButtonStyle == Qt::ToolButtonTextBesideIcon) {
| 0 | ||||||||||||||||||
294 | // If the action is not prioritized, remove the text label to save space | - | ||||||||||||||||||
295 | if (d->defaultAction && d->defaultAction->priority() < QAction::NormalPriority)
| 0 | ||||||||||||||||||
296 | option->toolButtonStyle = Qt::ToolButtonIconOnly; never executed: option->toolButtonStyle = Qt::ToolButtonIconOnly; | 0 | ||||||||||||||||||
297 | } never executed: end of block | 0 | ||||||||||||||||||
298 | - | |||||||||||||||||||
299 | if (d->icon.isNull() && d->arrowType == Qt::NoArrow && !forceNoText) {
| 0 | ||||||||||||||||||
300 | if (!d->text.isEmpty())
| 0 | ||||||||||||||||||
301 | option->toolButtonStyle = Qt::ToolButtonTextOnly; never executed: option->toolButtonStyle = Qt::ToolButtonTextOnly; | 0 | ||||||||||||||||||
302 | else if (option->toolButtonStyle != Qt::ToolButtonTextOnly)
| 0 | ||||||||||||||||||
303 | option->toolButtonStyle = Qt::ToolButtonIconOnly; never executed: option->toolButtonStyle = Qt::ToolButtonIconOnly; | 0 | ||||||||||||||||||
304 | } never executed: end of block | 0 | ||||||||||||||||||
305 | - | |||||||||||||||||||
306 | option->pos = pos(); | - | ||||||||||||||||||
307 | option->font = font(); | - | ||||||||||||||||||
308 | } never executed: end of block | 0 | ||||||||||||||||||
309 | - | |||||||||||||||||||
310 | /*! | - | ||||||||||||||||||
311 | Destroys the object and frees any allocated resources. | - | ||||||||||||||||||
312 | */ | - | ||||||||||||||||||
313 | - | |||||||||||||||||||
314 | QToolButton::~QToolButton() | - | ||||||||||||||||||
315 | { | - | ||||||||||||||||||
316 | } | - | ||||||||||||||||||
317 | - | |||||||||||||||||||
318 | /*! | - | ||||||||||||||||||
319 | \reimp | - | ||||||||||||||||||
320 | */ | - | ||||||||||||||||||
321 | QSize QToolButton::sizeHint() const | - | ||||||||||||||||||
322 | { | - | ||||||||||||||||||
323 | Q_D(const QToolButton); | - | ||||||||||||||||||
324 | if (d->sizeHint.isValid())
| 0 | ||||||||||||||||||
325 | return d->sizeHint; never executed: return d->sizeHint; | 0 | ||||||||||||||||||
326 | ensurePolished(); | - | ||||||||||||||||||
327 | - | |||||||||||||||||||
328 | int w = 0, h = 0; | - | ||||||||||||||||||
329 | QStyleOptionToolButton opt; | - | ||||||||||||||||||
330 | initStyleOption(&opt); | - | ||||||||||||||||||
331 | - | |||||||||||||||||||
332 | QFontMetrics fm = fontMetrics(); | - | ||||||||||||||||||
333 | if (opt.toolButtonStyle != Qt::ToolButtonTextOnly) {
| 0 | ||||||||||||||||||
334 | QSize icon = opt.iconSize; | - | ||||||||||||||||||
335 | w = icon.width(); | - | ||||||||||||||||||
336 | h = icon.height(); | - | ||||||||||||||||||
337 | } never executed: end of block | 0 | ||||||||||||||||||
338 | - | |||||||||||||||||||
339 | if (opt.toolButtonStyle != Qt::ToolButtonIconOnly) {
| 0 | ||||||||||||||||||
340 | QSize textSize = fm.size(Qt::TextShowMnemonic, text()); | - | ||||||||||||||||||
341 | textSize.setWidth(textSize.width() + fm.width(QLatin1Char(' '))*2); | - | ||||||||||||||||||
342 | if (opt.toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
| 0 | ||||||||||||||||||
343 | h += 4 + textSize.height(); | - | ||||||||||||||||||
344 | if (textSize.width() > w)
| 0 | ||||||||||||||||||
345 | w = textSize.width(); never executed: w = textSize.width(); | 0 | ||||||||||||||||||
346 | } else if (opt.toolButtonStyle == Qt::ToolButtonTextBesideIcon) { never executed: end of block
| 0 | ||||||||||||||||||
347 | w += 4 + textSize.width(); | - | ||||||||||||||||||
348 | if (textSize.height() > h)
| 0 | ||||||||||||||||||
349 | h = textSize.height(); never executed: h = textSize.height(); | 0 | ||||||||||||||||||
350 | } else { // TextOnly never executed: end of block | 0 | ||||||||||||||||||
351 | w = textSize.width(); | - | ||||||||||||||||||
352 | h = textSize.height(); | - | ||||||||||||||||||
353 | } never executed: end of block | 0 | ||||||||||||||||||
354 | } | - | ||||||||||||||||||
355 | - | |||||||||||||||||||
356 | opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height | - | ||||||||||||||||||
357 | if (d->popupMode == MenuButtonPopup)
| 0 | ||||||||||||||||||
358 | w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); never executed: w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); | 0 | ||||||||||||||||||
359 | - | |||||||||||||||||||
360 | d->sizeHint = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this). | - | ||||||||||||||||||
361 | expandedTo(QApplication::globalStrut()); | - | ||||||||||||||||||
362 | return d->sizeHint; never executed: return d->sizeHint; | 0 | ||||||||||||||||||
363 | } | - | ||||||||||||||||||
364 | - | |||||||||||||||||||
365 | /*! | - | ||||||||||||||||||
366 | \reimp | - | ||||||||||||||||||
367 | */ | - | ||||||||||||||||||
368 | QSize QToolButton::minimumSizeHint() const | - | ||||||||||||||||||
369 | { | - | ||||||||||||||||||
370 | return sizeHint(); never executed: return sizeHint(); | 0 | ||||||||||||||||||
371 | } | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | /*! | - | ||||||||||||||||||
374 | \property QToolButton::toolButtonStyle | - | ||||||||||||||||||
375 | \brief whether the tool button displays an icon only, text only, | - | ||||||||||||||||||
376 | or text beside/below the icon. | - | ||||||||||||||||||
377 | - | |||||||||||||||||||
378 | The default is Qt::ToolButtonIconOnly. | - | ||||||||||||||||||
379 | - | |||||||||||||||||||
380 | To have the style of toolbuttons follow the system settings, set this property to Qt::ToolButtonFollowStyle. | - | ||||||||||||||||||
381 | On Unix, the user settings from the desktop environment will be used. | - | ||||||||||||||||||
382 | On other platforms, Qt::ToolButtonFollowStyle means icon only. | - | ||||||||||||||||||
383 | - | |||||||||||||||||||
384 | QToolButton automatically connects this slot to the relevant | - | ||||||||||||||||||
385 | signal in the QMainWindow in which is resides. | - | ||||||||||||||||||
386 | */ | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | /*! | - | ||||||||||||||||||
389 | \property QToolButton::arrowType | - | ||||||||||||||||||
390 | \brief whether the button displays an arrow instead of a normal icon | - | ||||||||||||||||||
391 | - | |||||||||||||||||||
392 | This displays an arrow as the icon for the QToolButton. | - | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | By default, this property is set to Qt::NoArrow. | - | ||||||||||||||||||
395 | */ | - | ||||||||||||||||||
396 | - | |||||||||||||||||||
397 | Qt::ToolButtonStyle QToolButton::toolButtonStyle() const | - | ||||||||||||||||||
398 | { | - | ||||||||||||||||||
399 | Q_D(const QToolButton); | - | ||||||||||||||||||
400 | return d->toolButtonStyle; never executed: return d->toolButtonStyle; | 0 | ||||||||||||||||||
401 | } | - | ||||||||||||||||||
402 | - | |||||||||||||||||||
403 | Qt::ArrowType QToolButton::arrowType() const | - | ||||||||||||||||||
404 | { | - | ||||||||||||||||||
405 | Q_D(const QToolButton); | - | ||||||||||||||||||
406 | return d->arrowType; never executed: return d->arrowType; | 0 | ||||||||||||||||||
407 | } | - | ||||||||||||||||||
408 | - | |||||||||||||||||||
409 | - | |||||||||||||||||||
410 | void QToolButton::setToolButtonStyle(Qt::ToolButtonStyle style) | - | ||||||||||||||||||
411 | { | - | ||||||||||||||||||
412 | Q_D(QToolButton); | - | ||||||||||||||||||
413 | if (d->toolButtonStyle == style)
| 0 | ||||||||||||||||||
414 | return; never executed: return; | 0 | ||||||||||||||||||
415 | - | |||||||||||||||||||
416 | d->toolButtonStyle = style; | - | ||||||||||||||||||
417 | d->sizeHint = QSize(); | - | ||||||||||||||||||
418 | updateGeometry(); | - | ||||||||||||||||||
419 | if (isVisible()) {
| 0 | ||||||||||||||||||
420 | update(); | - | ||||||||||||||||||
421 | } never executed: end of block | 0 | ||||||||||||||||||
422 | } never executed: end of block | 0 | ||||||||||||||||||
423 | - | |||||||||||||||||||
424 | void QToolButton::setArrowType(Qt::ArrowType type) | - | ||||||||||||||||||
425 | { | - | ||||||||||||||||||
426 | Q_D(QToolButton); | - | ||||||||||||||||||
427 | if (d->arrowType == type)
| 0 | ||||||||||||||||||
428 | return; never executed: return; | 0 | ||||||||||||||||||
429 | - | |||||||||||||||||||
430 | d->arrowType = type; | - | ||||||||||||||||||
431 | d->sizeHint = QSize(); | - | ||||||||||||||||||
432 | updateGeometry(); | - | ||||||||||||||||||
433 | if (isVisible()) {
| 0 | ||||||||||||||||||
434 | update(); | - | ||||||||||||||||||
435 | } never executed: end of block | 0 | ||||||||||||||||||
436 | } never executed: end of block | 0 | ||||||||||||||||||
437 | - | |||||||||||||||||||
438 | /*! | - | ||||||||||||||||||
439 | \fn void QToolButton::paintEvent(QPaintEvent *event) | - | ||||||||||||||||||
440 | - | |||||||||||||||||||
441 | Paints the button in response to the paint \a event. | - | ||||||||||||||||||
442 | */ | - | ||||||||||||||||||
443 | void QToolButton::paintEvent(QPaintEvent *) | - | ||||||||||||||||||
444 | { | - | ||||||||||||||||||
445 | QStylePainter p(this); | - | ||||||||||||||||||
446 | QStyleOptionToolButton opt; | - | ||||||||||||||||||
447 | initStyleOption(&opt); | - | ||||||||||||||||||
448 | p.drawComplexControl(QStyle::CC_ToolButton, opt); | - | ||||||||||||||||||
449 | } never executed: end of block | 0 | ||||||||||||||||||
450 | - | |||||||||||||||||||
451 | /*! | - | ||||||||||||||||||
452 | \reimp | - | ||||||||||||||||||
453 | */ | - | ||||||||||||||||||
454 | void QToolButton::actionEvent(QActionEvent *event) | - | ||||||||||||||||||
455 | { | - | ||||||||||||||||||
456 | Q_D(QToolButton); | - | ||||||||||||||||||
457 | QAction *action = event->action(); | - | ||||||||||||||||||
458 | switch (event->type()) { | - | ||||||||||||||||||
459 | case QEvent::ActionChanged: never executed: case QEvent::ActionChanged: | 0 | ||||||||||||||||||
460 | if (action == d->defaultAction)
| 0 | ||||||||||||||||||
461 | setDefaultAction(action); // update button state never executed: setDefaultAction(action); | 0 | ||||||||||||||||||
462 | break; never executed: break; | 0 | ||||||||||||||||||
463 | case QEvent::ActionAdded: never executed: case QEvent::ActionAdded: | 0 | ||||||||||||||||||
464 | connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); | - | ||||||||||||||||||
465 | break; never executed: break; | 0 | ||||||||||||||||||
466 | case QEvent::ActionRemoved: never executed: case QEvent::ActionRemoved: | 0 | ||||||||||||||||||
467 | if (d->defaultAction == action)
| 0 | ||||||||||||||||||
468 | d->defaultAction = 0; never executed: d->defaultAction = 0; | 0 | ||||||||||||||||||
469 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
470 | if (action == d->menuAction)
| 0 | ||||||||||||||||||
471 | d->menuAction = 0; never executed: d->menuAction = 0; | 0 | ||||||||||||||||||
472 | #endif | - | ||||||||||||||||||
473 | action->disconnect(this); | - | ||||||||||||||||||
474 | break; never executed: break; | 0 | ||||||||||||||||||
475 | default: never executed: default: | 0 | ||||||||||||||||||
476 | ; | - | ||||||||||||||||||
477 | } never executed: end of block | 0 | ||||||||||||||||||
478 | QAbstractButton::actionEvent(event); | - | ||||||||||||||||||
479 | } never executed: end of block | 0 | ||||||||||||||||||
480 | - | |||||||||||||||||||
481 | QStyle::SubControl QToolButtonPrivate::newHoverControl(const QPoint &pos) | - | ||||||||||||||||||
482 | { | - | ||||||||||||||||||
483 | Q_Q(QToolButton); | - | ||||||||||||||||||
484 | QStyleOptionToolButton opt; | - | ||||||||||||||||||
485 | q->initStyleOption(&opt); | - | ||||||||||||||||||
486 | opt.subControls = QStyle::SC_All; | - | ||||||||||||||||||
487 | hoverControl = q->style()->hitTestComplexControl(QStyle::CC_ToolButton, &opt, pos, q); | - | ||||||||||||||||||
488 | if (hoverControl == QStyle::SC_None)
| 0 | ||||||||||||||||||
489 | hoverRect = QRect(); never executed: hoverRect = QRect(); | 0 | ||||||||||||||||||
490 | else | - | ||||||||||||||||||
491 | hoverRect = q->style()->subControlRect(QStyle::CC_ToolButton, &opt, hoverControl, q); never executed: hoverRect = q->style()->subControlRect(QStyle::CC_ToolButton, &opt, hoverControl, q); | 0 | ||||||||||||||||||
492 | return hoverControl; never executed: return hoverControl; | 0 | ||||||||||||||||||
493 | } | - | ||||||||||||||||||
494 | - | |||||||||||||||||||
495 | bool QToolButtonPrivate::updateHoverControl(const QPoint &pos) | - | ||||||||||||||||||
496 | { | - | ||||||||||||||||||
497 | Q_Q(QToolButton); | - | ||||||||||||||||||
498 | QRect lastHoverRect = hoverRect; | - | ||||||||||||||||||
499 | QStyle::SubControl lastHoverControl = hoverControl; | - | ||||||||||||||||||
500 | bool doesHover = q->testAttribute(Qt::WA_Hover); | - | ||||||||||||||||||
501 | if (lastHoverControl != newHoverControl(pos) && doesHover) {
| 0 | ||||||||||||||||||
502 | q->update(lastHoverRect); | - | ||||||||||||||||||
503 | q->update(hoverRect); | - | ||||||||||||||||||
504 | return true; never executed: return true; | 0 | ||||||||||||||||||
505 | } | - | ||||||||||||||||||
506 | return !doesHover; never executed: return !doesHover; | 0 | ||||||||||||||||||
507 | } | - | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | void QToolButtonPrivate::_q_actionTriggered() | - | ||||||||||||||||||
510 | { | - | ||||||||||||||||||
511 | Q_Q(QToolButton); | - | ||||||||||||||||||
512 | if (QAction *action = qobject_cast<QAction *>(q->sender()))
| 0 | ||||||||||||||||||
513 | emit q->triggered(action); never executed: q->triggered(action); | 0 | ||||||||||||||||||
514 | } never executed: end of block | 0 | ||||||||||||||||||
515 | - | |||||||||||||||||||
516 | /*! | - | ||||||||||||||||||
517 | \reimp | - | ||||||||||||||||||
518 | */ | - | ||||||||||||||||||
519 | void QToolButton::enterEvent(QEvent * e) | - | ||||||||||||||||||
520 | { | - | ||||||||||||||||||
521 | Q_D(QToolButton); | - | ||||||||||||||||||
522 | if (d->autoRaise)
| 0 | ||||||||||||||||||
523 | update(); never executed: update(); | 0 | ||||||||||||||||||
524 | if (d->defaultAction)
| 0 | ||||||||||||||||||
525 | d->defaultAction->hover(); never executed: d->defaultAction->hover(); | 0 | ||||||||||||||||||
526 | QAbstractButton::enterEvent(e); | - | ||||||||||||||||||
527 | } never executed: end of block | 0 | ||||||||||||||||||
528 | - | |||||||||||||||||||
529 | - | |||||||||||||||||||
530 | /*! | - | ||||||||||||||||||
531 | \reimp | - | ||||||||||||||||||
532 | */ | - | ||||||||||||||||||
533 | void QToolButton::leaveEvent(QEvent * e) | - | ||||||||||||||||||
534 | { | - | ||||||||||||||||||
535 | Q_D(QToolButton); | - | ||||||||||||||||||
536 | if (d->autoRaise)
| 0 | ||||||||||||||||||
537 | update(); never executed: update(); | 0 | ||||||||||||||||||
538 | - | |||||||||||||||||||
539 | QAbstractButton::leaveEvent(e); | - | ||||||||||||||||||
540 | } never executed: end of block | 0 | ||||||||||||||||||
541 | - | |||||||||||||||||||
542 | - | |||||||||||||||||||
543 | /*! | - | ||||||||||||||||||
544 | \reimp | - | ||||||||||||||||||
545 | */ | - | ||||||||||||||||||
546 | void QToolButton::timerEvent(QTimerEvent *e) | - | ||||||||||||||||||
547 | { | - | ||||||||||||||||||
548 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
549 | Q_D(QToolButton); | - | ||||||||||||||||||
550 | if (e->timerId() == d->popupTimer.timerId()) {
| 0 | ||||||||||||||||||
551 | d->popupTimerDone(); | - | ||||||||||||||||||
552 | return; never executed: return; | 0 | ||||||||||||||||||
553 | } | - | ||||||||||||||||||
554 | #endif | - | ||||||||||||||||||
555 | QAbstractButton::timerEvent(e); | - | ||||||||||||||||||
556 | } never executed: end of block | 0 | ||||||||||||||||||
557 | - | |||||||||||||||||||
558 | - | |||||||||||||||||||
559 | /*! | - | ||||||||||||||||||
560 | \reimp | - | ||||||||||||||||||
561 | */ | - | ||||||||||||||||||
562 | void QToolButton::changeEvent(QEvent *e) | - | ||||||||||||||||||
563 | { | - | ||||||||||||||||||
564 | #ifndef QT_NO_TOOLBAR | - | ||||||||||||||||||
565 | Q_D(QToolButton); | - | ||||||||||||||||||
566 | if (e->type() == QEvent::ParentChange) {
| 0 | ||||||||||||||||||
567 | if (qobject_cast<QToolBar*>(parentWidget()))
| 0 | ||||||||||||||||||
568 | d->autoRaise = true; never executed: d->autoRaise = true; | 0 | ||||||||||||||||||
569 | } else if (e->type() == QEvent::StyleChange never executed: end of block
| 0 | ||||||||||||||||||
570 | #ifdef Q_OS_MAC | - | ||||||||||||||||||
571 | || e->type() == QEvent::MacSizeChange | - | ||||||||||||||||||
572 | #endif | - | ||||||||||||||||||
573 | ) { | - | ||||||||||||||||||
574 | d->delay = style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, this); | - | ||||||||||||||||||
575 | d->setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem); | - | ||||||||||||||||||
576 | } never executed: end of block | 0 | ||||||||||||||||||
577 | #endif | - | ||||||||||||||||||
578 | QAbstractButton::changeEvent(e); | - | ||||||||||||||||||
579 | } never executed: end of block | 0 | ||||||||||||||||||
580 | - | |||||||||||||||||||
581 | /*! | - | ||||||||||||||||||
582 | \reimp | - | ||||||||||||||||||
583 | */ | - | ||||||||||||||||||
584 | void QToolButton::mousePressEvent(QMouseEvent *e) | - | ||||||||||||||||||
585 | { | - | ||||||||||||||||||
586 | Q_D(QToolButton); | - | ||||||||||||||||||
587 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
588 | QStyleOptionToolButton opt; | - | ||||||||||||||||||
589 | initStyleOption(&opt); | - | ||||||||||||||||||
590 | if (e->button() == Qt::LeftButton && (d->popupMode == MenuButtonPopup)) {
| 0 | ||||||||||||||||||
591 | QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt, | - | ||||||||||||||||||
592 | QStyle::SC_ToolButtonMenu, this); | - | ||||||||||||||||||
593 | if (popupr.isValid() && popupr.contains(e->pos())) {
| 0 | ||||||||||||||||||
594 | d->buttonPressed = QToolButtonPrivate::MenuButtonPressed; | - | ||||||||||||||||||
595 | showMenu(); | - | ||||||||||||||||||
596 | return; never executed: return; | 0 | ||||||||||||||||||
597 | } | - | ||||||||||||||||||
598 | } never executed: end of block | 0 | ||||||||||||||||||
599 | #endif | - | ||||||||||||||||||
600 | d->buttonPressed = QToolButtonPrivate::ToolButtonPressed; | - | ||||||||||||||||||
601 | QAbstractButton::mousePressEvent(e); | - | ||||||||||||||||||
602 | } never executed: end of block | 0 | ||||||||||||||||||
603 | - | |||||||||||||||||||
604 | /*! | - | ||||||||||||||||||
605 | \reimp | - | ||||||||||||||||||
606 | */ | - | ||||||||||||||||||
607 | void QToolButton::mouseReleaseEvent(QMouseEvent *e) | - | ||||||||||||||||||
608 | { | - | ||||||||||||||||||
609 | Q_D(QToolButton); | - | ||||||||||||||||||
610 | QAbstractButton::mouseReleaseEvent(e); | - | ||||||||||||||||||
611 | d->buttonPressed = QToolButtonPrivate::NoButtonPressed; | - | ||||||||||||||||||
612 | } never executed: end of block | 0 | ||||||||||||||||||
613 | - | |||||||||||||||||||
614 | /*! | - | ||||||||||||||||||
615 | \reimp | - | ||||||||||||||||||
616 | */ | - | ||||||||||||||||||
617 | bool QToolButton::hitButton(const QPoint &pos) const | - | ||||||||||||||||||
618 | { | - | ||||||||||||||||||
619 | Q_D(const QToolButton); | - | ||||||||||||||||||
620 | if(QAbstractButton::hitButton(pos))
| 0 | ||||||||||||||||||
621 | return (d->buttonPressed != QToolButtonPrivate::MenuButtonPressed); never executed: return (d->buttonPressed != QToolButtonPrivate::MenuButtonPressed); | 0 | ||||||||||||||||||
622 | return false; never executed: return false; | 0 | ||||||||||||||||||
623 | } | - | ||||||||||||||||||
624 | - | |||||||||||||||||||
625 | - | |||||||||||||||||||
626 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
627 | /*! | - | ||||||||||||||||||
628 | Associates the given \a menu with this tool button. | - | ||||||||||||||||||
629 | - | |||||||||||||||||||
630 | The menu will be shown according to the button's \l popupMode. | - | ||||||||||||||||||
631 | - | |||||||||||||||||||
632 | Ownership of the menu is not transferred to the tool button. | - | ||||||||||||||||||
633 | - | |||||||||||||||||||
634 | \sa menu() | - | ||||||||||||||||||
635 | */ | - | ||||||||||||||||||
636 | void QToolButton::setMenu(QMenu* menu) | - | ||||||||||||||||||
637 | { | - | ||||||||||||||||||
638 | Q_D(QToolButton); | - | ||||||||||||||||||
639 | - | |||||||||||||||||||
640 | if (d->menuAction == (menu ? menu->menuAction() : 0))
| 0 | ||||||||||||||||||
641 | return; never executed: return; | 0 | ||||||||||||||||||
642 | - | |||||||||||||||||||
643 | if (d->menuAction)
| 0 | ||||||||||||||||||
644 | removeAction(d->menuAction); never executed: removeAction(d->menuAction); | 0 | ||||||||||||||||||
645 | - | |||||||||||||||||||
646 | if (menu) {
| 0 | ||||||||||||||||||
647 | d->menuAction = menu->menuAction(); | - | ||||||||||||||||||
648 | addAction(d->menuAction); | - | ||||||||||||||||||
649 | } else { never executed: end of block | 0 | ||||||||||||||||||
650 | d->menuAction = 0; | - | ||||||||||||||||||
651 | } never executed: end of block | 0 | ||||||||||||||||||
652 | - | |||||||||||||||||||
653 | // changing the menu set may change the size hint, so reset it | - | ||||||||||||||||||
654 | d->sizeHint = QSize(); | - | ||||||||||||||||||
655 | updateGeometry(); | - | ||||||||||||||||||
656 | update(); | - | ||||||||||||||||||
657 | } never executed: end of block | 0 | ||||||||||||||||||
658 | - | |||||||||||||||||||
659 | /*! | - | ||||||||||||||||||
660 | Returns the associated menu, or 0 if no menu has been defined. | - | ||||||||||||||||||
661 | - | |||||||||||||||||||
662 | \sa setMenu() | - | ||||||||||||||||||
663 | */ | - | ||||||||||||||||||
664 | QMenu* QToolButton::menu() const | - | ||||||||||||||||||
665 | { | - | ||||||||||||||||||
666 | Q_D(const QToolButton); | - | ||||||||||||||||||
667 | if (d->menuAction)
| 0 | ||||||||||||||||||
668 | return d->menuAction->menu(); never executed: return d->menuAction->menu(); | 0 | ||||||||||||||||||
669 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
670 | } | - | ||||||||||||||||||
671 | - | |||||||||||||||||||
672 | /*! | - | ||||||||||||||||||
673 | Shows (pops up) the associated popup menu. If there is no such | - | ||||||||||||||||||
674 | menu, this function does nothing. This function does not return | - | ||||||||||||||||||
675 | until the popup menu has been closed by the user. | - | ||||||||||||||||||
676 | */ | - | ||||||||||||||||||
677 | void QToolButton::showMenu() | - | ||||||||||||||||||
678 | { | - | ||||||||||||||||||
679 | Q_D(QToolButton); | - | ||||||||||||||||||
680 | if (!d->hasMenu()) {
| 0 | ||||||||||||||||||
681 | d->menuButtonDown = false; | - | ||||||||||||||||||
682 | return; // no menu to show never executed: return; | 0 | ||||||||||||||||||
683 | } | - | ||||||||||||||||||
684 | // prevent recursions spinning another event loop | - | ||||||||||||||||||
685 | if (d->menuButtonDown)
| 0 | ||||||||||||||||||
686 | return; never executed: return; | 0 | ||||||||||||||||||
687 | - | |||||||||||||||||||
688 | - | |||||||||||||||||||
689 | d->menuButtonDown = true; | - | ||||||||||||||||||
690 | repaint(); | - | ||||||||||||||||||
691 | d->popupTimer.stop(); | - | ||||||||||||||||||
692 | d->popupTimerDone(); | - | ||||||||||||||||||
693 | } never executed: end of block | 0 | ||||||||||||||||||
694 | - | |||||||||||||||||||
695 | void QToolButtonPrivate::_q_buttonPressed() | - | ||||||||||||||||||
696 | { | - | ||||||||||||||||||
697 | Q_Q(QToolButton); | - | ||||||||||||||||||
698 | if (!hasMenu())
| 0 | ||||||||||||||||||
699 | return; // no menu to show never executed: return; | 0 | ||||||||||||||||||
700 | if (popupMode == QToolButton::MenuButtonPopup)
| 0 | ||||||||||||||||||
701 | return; never executed: return; | 0 | ||||||||||||||||||
702 | else if (delay > 0 && popupMode == QToolButton::DelayedPopup)
| 0 | ||||||||||||||||||
703 | popupTimer.start(delay, q); never executed: popupTimer.start(delay, q); | 0 | ||||||||||||||||||
704 | else if (delay == 0 || popupMode == QToolButton::InstantPopup)
| 0 | ||||||||||||||||||
705 | q->showMenu(); never executed: q->showMenu(); | 0 | ||||||||||||||||||
706 | } never executed: end of block | 0 | ||||||||||||||||||
707 | - | |||||||||||||||||||
708 | void QToolButtonPrivate::_q_buttonReleased() | - | ||||||||||||||||||
709 | { | - | ||||||||||||||||||
710 | popupTimer.stop(); | - | ||||||||||||||||||
711 | } never executed: end of block | 0 | ||||||||||||||||||
712 | - | |||||||||||||||||||
713 | void QToolButtonPrivate::popupTimerDone() | - | ||||||||||||||||||
714 | { | - | ||||||||||||||||||
715 | Q_Q(QToolButton); | - | ||||||||||||||||||
716 | popupTimer.stop(); | - | ||||||||||||||||||
717 | if (!menuButtonDown && !down)
| 0 | ||||||||||||||||||
718 | return; never executed: return; | 0 | ||||||||||||||||||
719 | - | |||||||||||||||||||
720 | menuButtonDown = true; | - | ||||||||||||||||||
721 | QPointer<QMenu> actualMenu; | - | ||||||||||||||||||
722 | bool mustDeleteActualMenu = false; | - | ||||||||||||||||||
723 | if(menuAction) {
| 0 | ||||||||||||||||||
724 | actualMenu = menuAction->menu(); | - | ||||||||||||||||||
725 | } else if (defaultAction && defaultAction->menu()) { never executed: end of block
| 0 | ||||||||||||||||||
726 | actualMenu = defaultAction->menu(); | - | ||||||||||||||||||
727 | } else { never executed: end of block | 0 | ||||||||||||||||||
728 | actualMenu = new QMenu(q); | - | ||||||||||||||||||
729 | mustDeleteActualMenu = true; | - | ||||||||||||||||||
730 | for(int i = 0; i < actions.size(); i++)
| 0 | ||||||||||||||||||
731 | actualMenu->addAction(actions.at(i)); never executed: actualMenu->addAction(actions.at(i)); | 0 | ||||||||||||||||||
732 | } never executed: end of block | 0 | ||||||||||||||||||
733 | repeat = q->autoRepeat(); | - | ||||||||||||||||||
734 | q->setAutoRepeat(false); | - | ||||||||||||||||||
735 | bool horizontal = true; | - | ||||||||||||||||||
736 | #if !defined(QT_NO_TOOLBAR) | - | ||||||||||||||||||
737 | QToolBar *tb = qobject_cast<QToolBar*>(parent); | - | ||||||||||||||||||
738 | if (tb && tb->orientation() == Qt::Vertical)
| 0 | ||||||||||||||||||
739 | horizontal = false; never executed: horizontal = false; | 0 | ||||||||||||||||||
740 | #endif | - | ||||||||||||||||||
741 | QPoint p; | - | ||||||||||||||||||
742 | const QRect rect = q->rect(); // Find screen via point in case of QGraphicsProxyWidget. | - | ||||||||||||||||||
743 | QRect screen = QApplication::desktop()->availableGeometry(q->mapToGlobal(rect.center())); | - | ||||||||||||||||||
744 | QSize sh = ((QToolButton*)(QMenu*)actualMenu)->receivers(SIGNAL(aboutToShow()))? QSize() : actualMenu->sizeHint();
| 0 | ||||||||||||||||||
745 | if (horizontal) {
| 0 | ||||||||||||||||||
746 | if (q->isRightToLeft()) {
| 0 | ||||||||||||||||||
747 | if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
| 0 | ||||||||||||||||||
748 | p = q->mapToGlobal(rect.bottomRight()); | - | ||||||||||||||||||
749 | } else { never executed: end of block | 0 | ||||||||||||||||||
750 | p = q->mapToGlobal(rect.topRight() - QPoint(0, sh.height())); | - | ||||||||||||||||||
751 | } never executed: end of block | 0 | ||||||||||||||||||
752 | p.rx() -= sh.width(); | - | ||||||||||||||||||
753 | } else { never executed: end of block | 0 | ||||||||||||||||||
754 | if (q->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
| 0 | ||||||||||||||||||
755 | p = q->mapToGlobal(rect.bottomLeft()); | - | ||||||||||||||||||
756 | } else { never executed: end of block | 0 | ||||||||||||||||||
757 | p = q->mapToGlobal(rect.topLeft() - QPoint(0, sh.height())); | - | ||||||||||||||||||
758 | } never executed: end of block | 0 | ||||||||||||||||||
759 | } | - | ||||||||||||||||||
760 | } else { | - | ||||||||||||||||||
761 | if (q->isRightToLeft()) {
| 0 | ||||||||||||||||||
762 | if (q->mapToGlobal(QPoint(rect.left(), 0)).x() - sh.width() <= screen.x()) {
| 0 | ||||||||||||||||||
763 | p = q->mapToGlobal(rect.topRight()); | - | ||||||||||||||||||
764 | } else { never executed: end of block | 0 | ||||||||||||||||||
765 | p = q->mapToGlobal(rect.topLeft()); | - | ||||||||||||||||||
766 | p.rx() -= sh.width(); | - | ||||||||||||||||||
767 | } never executed: end of block | 0 | ||||||||||||||||||
768 | } else { | - | ||||||||||||||||||
769 | if (q->mapToGlobal(QPoint(rect.right(), 0)).x() + sh.width() <= screen.right()) {
| 0 | ||||||||||||||||||
770 | p = q->mapToGlobal(rect.topRight()); | - | ||||||||||||||||||
771 | } else { never executed: end of block | 0 | ||||||||||||||||||
772 | p = q->mapToGlobal(rect.topLeft() - QPoint(sh.width(), 0)); | - | ||||||||||||||||||
773 | } never executed: end of block | 0 | ||||||||||||||||||
774 | } | - | ||||||||||||||||||
775 | } | - | ||||||||||||||||||
776 | p.rx() = qMax(screen.left(), qMin(p.x(), screen.right() - sh.width())); | - | ||||||||||||||||||
777 | p.ry() += 1; | - | ||||||||||||||||||
778 | QPointer<QToolButton> that = q; | - | ||||||||||||||||||
779 | actualMenu->setNoReplayFor(q); | - | ||||||||||||||||||
780 | if (!mustDeleteActualMenu) //only if action are not in this widget
| 0 | ||||||||||||||||||
781 | QObject::connect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*))); never executed: QObject::connect(actualMenu, qFlagLocation("2""triggered(QAction*)" "\0" __FILE__ ":" "781"), q, qFlagLocation("1""_q_menuTriggered(QAction*)" "\0" __FILE__ ":" "781")); | 0 | ||||||||||||||||||
782 | QObject::connect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown())); | - | ||||||||||||||||||
783 | actualMenu->d_func()->causedPopup.widget = q; | - | ||||||||||||||||||
784 | actualMenu->d_func()->causedPopup.action = defaultAction; | - | ||||||||||||||||||
785 | actionsCopy = q->actions(); //(the list of action may be modified in slots) | - | ||||||||||||||||||
786 | actualMenu->exec(p); | - | ||||||||||||||||||
787 | - | |||||||||||||||||||
788 | if (!that)
| 0 | ||||||||||||||||||
789 | return; never executed: return; | 0 | ||||||||||||||||||
790 | - | |||||||||||||||||||
791 | QObject::disconnect(actualMenu, SIGNAL(aboutToHide()), q, SLOT(_q_updateButtonDown())); | - | ||||||||||||||||||
792 | if (mustDeleteActualMenu)
| 0 | ||||||||||||||||||
793 | delete actualMenu; never executed: delete actualMenu; | 0 | ||||||||||||||||||
794 | else | - | ||||||||||||||||||
795 | QObject::disconnect(actualMenu, SIGNAL(triggered(QAction*)), q, SLOT(_q_menuTriggered(QAction*))); never executed: QObject::disconnect(actualMenu, qFlagLocation("2""triggered(QAction*)" "\0" __FILE__ ":" "795"), q, qFlagLocation("1""_q_menuTriggered(QAction*)" "\0" __FILE__ ":" "795")); | 0 | ||||||||||||||||||
796 | - | |||||||||||||||||||
797 | actionsCopy.clear(); | - | ||||||||||||||||||
798 | - | |||||||||||||||||||
799 | if (repeat)
| 0 | ||||||||||||||||||
800 | q->setAutoRepeat(true); never executed: q->setAutoRepeat(true); | 0 | ||||||||||||||||||
801 | } never executed: end of block | 0 | ||||||||||||||||||
802 | - | |||||||||||||||||||
803 | void QToolButtonPrivate::_q_updateButtonDown() | - | ||||||||||||||||||
804 | { | - | ||||||||||||||||||
805 | Q_Q(QToolButton); | - | ||||||||||||||||||
806 | menuButtonDown = false; | - | ||||||||||||||||||
807 | if (q->isDown())
| 0 | ||||||||||||||||||
808 | q->setDown(false); never executed: q->setDown(false); | 0 | ||||||||||||||||||
809 | else | - | ||||||||||||||||||
810 | q->repaint(); never executed: q->repaint(); | 0 | ||||||||||||||||||
811 | } | - | ||||||||||||||||||
812 | - | |||||||||||||||||||
813 | void QToolButtonPrivate::_q_menuTriggered(QAction *action) | - | ||||||||||||||||||
814 | { | - | ||||||||||||||||||
815 | Q_Q(QToolButton); | - | ||||||||||||||||||
816 | if (action && !actionsCopy.contains(action))
| 0 | ||||||||||||||||||
817 | emit q->triggered(action); never executed: q->triggered(action); | 0 | ||||||||||||||||||
818 | } never executed: end of block | 0 | ||||||||||||||||||
819 | #endif // QT_NO_MENU | - | ||||||||||||||||||
820 | - | |||||||||||||||||||
821 | - | |||||||||||||||||||
822 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
823 | /*! \enum QToolButton::ToolButtonPopupMode | - | ||||||||||||||||||
824 | - | |||||||||||||||||||
825 | Describes how a menu should be popped up for tool buttons that has | - | ||||||||||||||||||
826 | a menu set or contains a list of actions. | - | ||||||||||||||||||
827 | - | |||||||||||||||||||
828 | \value DelayedPopup After pressing and holding the tool button | - | ||||||||||||||||||
829 | down for a certain amount of time (the timeout is style dependent, | - | ||||||||||||||||||
830 | see QStyle::SH_ToolButton_PopupDelay), the menu is displayed. A | - | ||||||||||||||||||
831 | typical application example is the "back" button in some web | - | ||||||||||||||||||
832 | browsers's tool bars. If the user clicks it, the browser simply | - | ||||||||||||||||||
833 | browses back to the previous page. If the user presses and holds | - | ||||||||||||||||||
834 | the button down for a while, the tool button shows a menu | - | ||||||||||||||||||
835 | containing the current history list | - | ||||||||||||||||||
836 | - | |||||||||||||||||||
837 | \value MenuButtonPopup In this mode the tool button displays a | - | ||||||||||||||||||
838 | special arrow to indicate that a menu is present. The menu is | - | ||||||||||||||||||
839 | displayed when the arrow part of the button is pressed. | - | ||||||||||||||||||
840 | - | |||||||||||||||||||
841 | \value InstantPopup The menu is displayed, without delay, when | - | ||||||||||||||||||
842 | the tool button is pressed. In this mode, the button's own action | - | ||||||||||||||||||
843 | is not triggered. | - | ||||||||||||||||||
844 | */ | - | ||||||||||||||||||
845 | - | |||||||||||||||||||
846 | /*! | - | ||||||||||||||||||
847 | \property QToolButton::popupMode | - | ||||||||||||||||||
848 | \brief describes the way that popup menus are used with tool buttons | - | ||||||||||||||||||
849 | - | |||||||||||||||||||
850 | By default, this property is set to \l DelayedPopup. | - | ||||||||||||||||||
851 | */ | - | ||||||||||||||||||
852 | - | |||||||||||||||||||
853 | void QToolButton::setPopupMode(ToolButtonPopupMode mode) | - | ||||||||||||||||||
854 | { | - | ||||||||||||||||||
855 | Q_D(QToolButton); | - | ||||||||||||||||||
856 | d->popupMode = mode; | - | ||||||||||||||||||
857 | } never executed: end of block | 0 | ||||||||||||||||||
858 | - | |||||||||||||||||||
859 | QToolButton::ToolButtonPopupMode QToolButton::popupMode() const | - | ||||||||||||||||||
860 | { | - | ||||||||||||||||||
861 | Q_D(const QToolButton); | - | ||||||||||||||||||
862 | return d->popupMode; never executed: return d->popupMode; | 0 | ||||||||||||||||||
863 | } | - | ||||||||||||||||||
864 | #endif | - | ||||||||||||||||||
865 | - | |||||||||||||||||||
866 | /*! | - | ||||||||||||||||||
867 | \property QToolButton::autoRaise | - | ||||||||||||||||||
868 | \brief whether auto-raising is enabled or not. | - | ||||||||||||||||||
869 | - | |||||||||||||||||||
870 | The default is disabled (i.e. false). | - | ||||||||||||||||||
871 | - | |||||||||||||||||||
872 | This property is currently ignored on \macos when using QMacStyle. | - | ||||||||||||||||||
873 | */ | - | ||||||||||||||||||
874 | void QToolButton::setAutoRaise(bool enable) | - | ||||||||||||||||||
875 | { | - | ||||||||||||||||||
876 | Q_D(QToolButton); | - | ||||||||||||||||||
877 | d->autoRaise = enable; | - | ||||||||||||||||||
878 | - | |||||||||||||||||||
879 | update(); | - | ||||||||||||||||||
880 | } never executed: end of block | 0 | ||||||||||||||||||
881 | - | |||||||||||||||||||
882 | bool QToolButton::autoRaise() const | - | ||||||||||||||||||
883 | { | - | ||||||||||||||||||
884 | Q_D(const QToolButton); | - | ||||||||||||||||||
885 | return d->autoRaise; never executed: return d->autoRaise; | 0 | ||||||||||||||||||
886 | } | - | ||||||||||||||||||
887 | - | |||||||||||||||||||
888 | /*! | - | ||||||||||||||||||
889 | Sets the default action to \a action. | - | ||||||||||||||||||
890 | - | |||||||||||||||||||
891 | If a tool button has a default action, the action defines the | - | ||||||||||||||||||
892 | button's properties like text, icon, tool tip, etc. | - | ||||||||||||||||||
893 | */ | - | ||||||||||||||||||
894 | void QToolButton::setDefaultAction(QAction *action) | - | ||||||||||||||||||
895 | { | - | ||||||||||||||||||
896 | Q_D(QToolButton); | - | ||||||||||||||||||
897 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
898 | bool hadMenu = false; | - | ||||||||||||||||||
899 | hadMenu = d->hasMenu(); | - | ||||||||||||||||||
900 | #endif | - | ||||||||||||||||||
901 | d->defaultAction = action; | - | ||||||||||||||||||
902 | if (!action)
| 0 | ||||||||||||||||||
903 | return; never executed: return; | 0 | ||||||||||||||||||
904 | if (!actions().contains(action))
| 0 | ||||||||||||||||||
905 | addAction(action); never executed: addAction(action); | 0 | ||||||||||||||||||
906 | QString buttonText = action->iconText(); | - | ||||||||||||||||||
907 | // If iconText() is generated from text(), we need to escape any '&'s so they | - | ||||||||||||||||||
908 | // don't turn into shortcuts | - | ||||||||||||||||||
909 | if (QActionPrivate::get(action)->iconText.isEmpty())
| 0 | ||||||||||||||||||
910 | buttonText.replace(QLatin1String("&"), QLatin1String("&&")); never executed: buttonText.replace(QLatin1String("&"), QLatin1String("&&")); | 0 | ||||||||||||||||||
911 | setText(buttonText); | - | ||||||||||||||||||
912 | setIcon(action->icon()); | - | ||||||||||||||||||
913 | #ifndef QT_NO_TOOLTIP | - | ||||||||||||||||||
914 | setToolTip(action->toolTip()); | - | ||||||||||||||||||
915 | #endif | - | ||||||||||||||||||
916 | #ifndef QT_NO_STATUSTIP | - | ||||||||||||||||||
917 | setStatusTip(action->statusTip()); | - | ||||||||||||||||||
918 | #endif | - | ||||||||||||||||||
919 | #ifndef QT_NO_WHATSTHIS | - | ||||||||||||||||||
920 | setWhatsThis(action->whatsThis()); | - | ||||||||||||||||||
921 | #endif | - | ||||||||||||||||||
922 | #ifndef QT_NO_MENU | - | ||||||||||||||||||
923 | if (action->menu() && !hadMenu) {
| 0 | ||||||||||||||||||
924 | // new 'default' popup mode defined introduced by tool bar. We | - | ||||||||||||||||||
925 | // should have changed QToolButton's default instead. Do that | - | ||||||||||||||||||
926 | // in 4.2. | - | ||||||||||||||||||
927 | setPopupMode(QToolButton::MenuButtonPopup); | - | ||||||||||||||||||
928 | } never executed: end of block | 0 | ||||||||||||||||||
929 | #endif | - | ||||||||||||||||||
930 | setCheckable(action->isCheckable()); | - | ||||||||||||||||||
931 | setChecked(action->isChecked()); | - | ||||||||||||||||||
932 | setEnabled(action->isEnabled()); | - | ||||||||||||||||||
933 | if (action->d_func()->fontSet)
| 0 | ||||||||||||||||||
934 | setFont(action->font()); never executed: setFont(action->font()); | 0 | ||||||||||||||||||
935 | } never executed: end of block | 0 | ||||||||||||||||||
936 | - | |||||||||||||||||||
937 | - | |||||||||||||||||||
938 | /*! | - | ||||||||||||||||||
939 | Returns the default action. | - | ||||||||||||||||||
940 | - | |||||||||||||||||||
941 | \sa setDefaultAction() | - | ||||||||||||||||||
942 | */ | - | ||||||||||||||||||
943 | QAction *QToolButton::defaultAction() const | - | ||||||||||||||||||
944 | { | - | ||||||||||||||||||
945 | Q_D(const QToolButton); | - | ||||||||||||||||||
946 | return d->defaultAction; never executed: return d->defaultAction; | 0 | ||||||||||||||||||
947 | } | - | ||||||||||||||||||
948 | - | |||||||||||||||||||
949 | - | |||||||||||||||||||
950 | - | |||||||||||||||||||
951 | /*! | - | ||||||||||||||||||
952 | \reimp | - | ||||||||||||||||||
953 | */ | - | ||||||||||||||||||
954 | void QToolButton::nextCheckState() | - | ||||||||||||||||||
955 | { | - | ||||||||||||||||||
956 | Q_D(QToolButton); | - | ||||||||||||||||||
957 | if (!d->defaultAction)
| 0 | ||||||||||||||||||
958 | QAbstractButton::nextCheckState(); never executed: QAbstractButton::nextCheckState(); | 0 | ||||||||||||||||||
959 | else | - | ||||||||||||||||||
960 | d->defaultAction->trigger(); never executed: d->defaultAction->trigger(); | 0 | ||||||||||||||||||
961 | } | - | ||||||||||||||||||
962 | - | |||||||||||||||||||
963 | /*! \reimp */ | - | ||||||||||||||||||
964 | bool QToolButton::event(QEvent *event) | - | ||||||||||||||||||
965 | { | - | ||||||||||||||||||
966 | switch(event->type()) { | - | ||||||||||||||||||
967 | case QEvent::HoverEnter: never executed: case QEvent::HoverEnter: | 0 | ||||||||||||||||||
968 | case QEvent::HoverLeave: never executed: case QEvent::HoverLeave: | 0 | ||||||||||||||||||
969 | case QEvent::HoverMove: never executed: case QEvent::HoverMove: | 0 | ||||||||||||||||||
970 | if (const QHoverEvent *he = static_cast<const QHoverEvent *>(event))
| 0 | ||||||||||||||||||
971 | d_func()->updateHoverControl(he->pos()); never executed: d_func()->updateHoverControl(he->pos()); | 0 | ||||||||||||||||||
972 | break; never executed: break; | 0 | ||||||||||||||||||
973 | default: never executed: default: | 0 | ||||||||||||||||||
974 | break; never executed: break; | 0 | ||||||||||||||||||
975 | } | - | ||||||||||||||||||
976 | return QAbstractButton::event(event); never executed: return QAbstractButton::event(event); | 0 | ||||||||||||||||||
977 | } | - | ||||||||||||||||||
978 | - | |||||||||||||||||||
979 | QT_END_NAMESPACE | - | ||||||||||||||||||
980 | - | |||||||||||||||||||
981 | #include "moc_qtoolbutton.cpp" | - | ||||||||||||||||||
982 | - | |||||||||||||||||||
983 | #endif | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |