Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qmenu.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qmenu.h" | - | ||||||
41 | - | |||||||
42 | #ifndef QT_NO_MENU | - | ||||||
43 | - | |||||||
44 | #include "qdebug.h" | - | ||||||
45 | #include "qstyle.h" | - | ||||||
46 | #include "qevent.h" | - | ||||||
47 | #include "qtimer.h" | - | ||||||
48 | #include "qlayout.h" | - | ||||||
49 | #include "qpainter.h" | - | ||||||
50 | #include <qpa/qplatformtheme.h> | - | ||||||
51 | #ifdef Q_OS_OSX | - | ||||||
52 | #include "qmacnativewidget_mac.h" | - | ||||||
53 | #endif | - | ||||||
54 | #include "qapplication.h" | - | ||||||
55 | #include "qdesktopwidget.h" | - | ||||||
56 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||
57 | # include "qaccessible.h" | - | ||||||
58 | #endif | - | ||||||
59 | #ifndef QT_NO_EFFECTS | - | ||||||
60 | # include <private/qeffects_p.h> | - | ||||||
61 | #endif | - | ||||||
62 | #ifndef QT_NO_WHATSTHIS | - | ||||||
63 | # include <qwhatsthis.h> | - | ||||||
64 | #endif | - | ||||||
65 | - | |||||||
66 | #include "qmenu_p.h" | - | ||||||
67 | #include "qmenubar_p.h" | - | ||||||
68 | #include "qwidgetaction.h" | - | ||||||
69 | #include "qtoolbutton.h" | - | ||||||
70 | #include "qpushbutton.h" | - | ||||||
71 | #include "qtooltip.h" | - | ||||||
72 | #include <private/qpushbutton_p.h> | - | ||||||
73 | #include <private/qaction_p.h> | - | ||||||
74 | #include <private/qguiapplication_p.h> | - | ||||||
75 | - | |||||||
76 | QT_BEGIN_NAMESPACE | - | ||||||
77 | - | |||||||
78 | QMenu *QMenuPrivate::mouseDown = 0; | - | ||||||
79 | - | |||||||
80 | /* QMenu code */ | - | ||||||
81 | // internal class used for the torn off popup | - | ||||||
82 | class QTornOffMenu : public QMenu | - | ||||||
83 | { | - | ||||||
84 | Q_OBJECT | - | ||||||
85 | class QTornOffMenuPrivate : public QMenuPrivate | - | ||||||
86 | { | - | ||||||
87 | Q_DECLARE_PUBLIC(QMenu) | - | ||||||
88 | public: | - | ||||||
89 | QTornOffMenuPrivate(QMenu *p) : causedMenu(p) { | - | ||||||
90 | tornoff = 1; | - | ||||||
91 | causedPopup.widget = 0; | - | ||||||
92 | causedPopup.action = ((QTornOffMenu*)p)->d_func()->causedPopup.action; | - | ||||||
93 | causedStack = ((QTornOffMenu*)p)->d_func()->calcCausedStack(); | - | ||||||
94 | } | - | ||||||
95 | QVector<QPointer<QWidget> > calcCausedStack() const Q_DECL_OVERRIDE { return causedStack; } | - | ||||||
96 | QPointer<QMenu> causedMenu; | - | ||||||
97 | QVector<QPointer<QWidget> > causedStack; | - | ||||||
98 | }; | - | ||||||
99 | public: | - | ||||||
100 | QTornOffMenu(QMenu *p) : QMenu(*(new QTornOffMenuPrivate(p))) | - | ||||||
101 | { | - | ||||||
102 | Q_D(QTornOffMenu); | - | ||||||
103 | // make the torn-off menu a sibling of p (instead of a child) | - | ||||||
104 | QWidget *parentWidget = d->causedStack.isEmpty() ? p : d->causedStack.lastconstLast();
| 0 | ||||||
105 | if (parentWidget->parentWidget())
| 0 | ||||||
106 | parentWidget = parentWidget->parentWidget(); never executed: parentWidget = parentWidget->parentWidget(); | 0 | ||||||
107 | setParent(parentWidget, Qt::Window | Qt::Tool); | - | ||||||
108 | setAttribute(Qt::WA_DeleteOnClose, true); | - | ||||||
109 | setAttribute(Qt::WA_X11NetWmWindowTypeMenu, true); | - | ||||||
110 | setWindowTitle(p->windowTitle()); | - | ||||||
111 | setEnabled(p->isEnabled()); | - | ||||||
112 | //QObject::connect(this, SIGNAL(triggered(QAction*)), this, SLOT(onTrigger(QAction*))); | - | ||||||
113 | //QObject::connect(this, SIGNAL(hovered(QAction*)), this, SLOT(onHovered(QAction*))); | - | ||||||
114 | QList<QAction*> items = p->actions(); | - | ||||||
115 | for(int i = 0; i < items.count(); i++)
| 0 | ||||||
116 | addAction(items.at(i)); never executed: addAction(items.at(i)); | 0 | ||||||
117 | } never executed: end of block | 0 | ||||||
118 | void syncWithMenu(QMenu *menu, QActionEvent *act) | - | ||||||
119 | { | - | ||||||
120 | Q_D(QTornOffMenu); | - | ||||||
121 | if(menu != d->causedMenu) | - | ||||||
122 | return; | - | ||||||
123 | if (act->type() == QEvent::ActionAdded) { | - | ||||||
124 | insertAction(act->before(), act->action()); | - | ||||||
125 | } else if (act->type() == QEvent::ActionRemoved) | - | ||||||
126 | removeAction(act->action()); | - | ||||||
127 | } | - | ||||||
128 | void actionEvent(QActionEvent *e) Q_DECL_OVERRIDE | - | ||||||
129 | { | - | ||||||
130 | QMenu::actionEvent(e); | - | ||||||
131 | setFixedSize(sizeHint()); | - | ||||||
132 | } | - | ||||||
133 | public slots: | - | ||||||
134 | void onTrigger(QAction *action) { d_func()->activateAction(action, QAction::Trigger, false); } | - | ||||||
135 | void onHovered(QAction *action) { d_func()->activateAction(action, QAction::Hover, false); } | - | ||||||
136 | private: | - | ||||||
137 | Q_DECLARE_PRIVATE(QTornOffMenu) | - | ||||||
138 | friend class QMenuPrivate; | - | ||||||
139 | }; | - | ||||||
140 | - | |||||||
141 | void QMenuPrivate::init() | - | ||||||
142 | { | - | ||||||
143 | Q_Q(QMenu); | - | ||||||
144 | #ifndef QT_NO_WHATSTHIS | - | ||||||
145 | q->setAttribute(Qt::WA_CustomWhatsThis); | - | ||||||
146 | #endif | - | ||||||
147 | q->setAttribute(Qt::WA_X11NetWmWindowTypePopupMenu); | - | ||||||
148 | defaultMenuAction = menuAction = new QAction(q); | - | ||||||
149 | menuAction->d_func()->menu = q; | - | ||||||
150 | q->setMouseTracking(q->style()->styleHint(QStyle::SH_Menu_MouseTracking, 0, q)); | - | ||||||
151 | if (q->style()->styleHint(QStyle::SH_Menu_Scrollable, 0, q)) { | - | ||||||
152 | scroll = new QMenuPrivate::QMenuScroller; | - | ||||||
153 | scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone; | - | ||||||
154 | } | - | ||||||
155 | - | |||||||
156 | setPlatformMenu(QGuiApplicationPrivate::platformTheme()->createPlatformMenu()); | - | ||||||
157 | sloppyState.initialize(q); | - | ||||||
158 | delayState.initialize(q); | - | ||||||
159 | mousePopupDelay = q->style()->styleHint(QStyle::SH_Menu_SubMenuPopupDelay, 0, q); | - | ||||||
160 | } | - | ||||||
161 | - | |||||||
162 | void QMenuPrivate::setPlatformMenu(QPlatformMenu *menu) | - | ||||||
163 | { | - | ||||||
164 | Q_Q(QMenu); | - | ||||||
165 | if (!platformMenu.isNull() && !platformMenu->parent()) | - | ||||||
166 | delete platformMenu.data(); | - | ||||||
167 | - | |||||||
168 | platformMenu = menu; | - | ||||||
169 | if (!platformMenu.isNull()) { | - | ||||||
170 | QObject::connect(platformMenu, SIGNAL(aboutToShow()), q, SLOT(_q_platformMenuAboutToShow())); | - | ||||||
171 | QObject::connect(platformMenu, SIGNAL(aboutToHide()), q, SIGNAL(aboutToHide())); | - | ||||||
172 | } | - | ||||||
173 | } | - | ||||||
174 | - | |||||||
175 | // forward declare function | - | ||||||
176 | static void copyActionToPlatformItem(const QAction *action, QPlatformMenuItem *item, QPlatformMenu *itemsMenu); | - | ||||||
177 | - | |||||||
178 | void QMenuPrivate::syncPlatformMenu() | - | ||||||
179 | { | - | ||||||
180 | Q_Q(QMenu); | - | ||||||
181 | if (platformMenu.isNull()) | - | ||||||
182 | return; | - | ||||||
183 | - | |||||||
184 | QPlatformMenuItem *beforeItem = Q_NULLPTR; | - | ||||||
185 | const QList<QAction*> actions = q->actions(); | - | ||||||
186 | for (QList<QAction*>::const_reverse_iterator it = actions.rbegin(), end = actions.rend(); it != end; ++it) { | - | ||||||
187 | QPlatformMenuItem *menuItem = platformMenu->createMenuItem(); | - | ||||||
188 | QAction *action = *it; | - | ||||||
189 | menuItem->setTag(reinterpret_cast<quintptr>(action)); | - | ||||||
190 | QObject::connect(menuItem, SIGNAL(activated()), action, SLOT(trigger()), Qt::QueuedConnection); | - | ||||||
191 | QObject::connect(menuItem, SIGNAL(hovered()), action, SIGNAL(hovered()), Qt::QueuedConnection); | - | ||||||
192 | copyActionToPlatformItem(action, menuItem, platformMenu.data()); | - | ||||||
193 | platformMenu->insertMenuItem(menuItem, beforeItem); | - | ||||||
194 | beforeItem = menuItem; | - | ||||||
195 | } | - | ||||||
196 | platformMenu->syncSeparatorsCollapsible(collapsibleSeparators); | - | ||||||
197 | platformMenu->setEnabled(q->isEnabled()); | - | ||||||
198 | } | - | ||||||
199 | - | |||||||
200 | int QMenuPrivate::scrollerHeight() const | - | ||||||
201 | { | - | ||||||
202 | Q_Q(const QMenu); | - | ||||||
203 | return qMax(QApplication::globalStrut().height(), q->style()->pixelMetric(QStyle::PM_MenuScrollerHeight, 0, q)); | - | ||||||
204 | } | - | ||||||
205 | - | |||||||
206 | //Windows and KDE allows menus to cover the taskbar, while GNOME and Mac don't | - | ||||||
207 | QRect QMenuPrivate::popupGeometry(const QWidget *widget) const | - | ||||||
208 | { | - | ||||||
209 | if (QGuiApplicationPrivate::platformTheme() && | - | ||||||
210 | QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::UseFullScreenForPopupMenu).toBool()) { | - | ||||||
211 | return QApplication::desktop()->screenGeometry(widget); | - | ||||||
212 | } else { | - | ||||||
213 | return QApplication::desktop()->availableGeometry(widget); | - | ||||||
214 | } | - | ||||||
215 | } | - | ||||||
216 | - | |||||||
217 | //Windows and KDE allows menus to cover the taskbar, while GNOME and Mac don't | - | ||||||
218 | QRect QMenuPrivate::popupGeometry(int screen) const | - | ||||||
219 | { | - | ||||||
220 | if (QGuiApplicationPrivate::platformTheme() && | - | ||||||
221 | QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::UseFullScreenForPopupMenu).toBool()) { | - | ||||||
222 | return QApplication::desktop()->screenGeometry(screen); | - | ||||||
223 | } else { | - | ||||||
224 | return QApplication::desktop()->availableGeometry(screen); | - | ||||||
225 | } | - | ||||||
226 | } | - | ||||||
227 | - | |||||||
228 | QVector<QPointer<QWidget> > QMenuPrivate::calcCausedStack() const | - | ||||||
229 | { | - | ||||||
230 | QVector<QPointer<QWidget> > ret; | - | ||||||
231 | for(QWidget *widget = causedPopup.widget; widget; ) { | - | ||||||
232 | ret.append(widget); | - | ||||||
233 | if (QTornOffMenu *qtmenu = qobject_cast<QTornOffMenu*>(widget)) | - | ||||||
234 | ret += qtmenu->d_func()->causedStack; | - | ||||||
235 | if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) | - | ||||||
236 | widget = qmenu->d_func()->causedPopup.widget; | - | ||||||
237 | else | - | ||||||
238 | break; | - | ||||||
239 | } | - | ||||||
240 | return ret; | - | ||||||
241 | } | - | ||||||
242 | - | |||||||
243 | void QMenuPrivate::updateActionRects() const | - | ||||||
244 | { | - | ||||||
245 | Q_Q(const QMenu); | - | ||||||
246 | updateActionRects(popupGeometry(q)); | - | ||||||
247 | } | - | ||||||
248 | - | |||||||
249 | void QMenuPrivate::updateActionRects(const QRect &screen) const | - | ||||||
250 | { | - | ||||||
251 | Q_Q(const QMenu); | - | ||||||
252 | if (!itemsDirty) | - | ||||||
253 | return; | - | ||||||
254 | - | |||||||
255 | q->ensurePolished(); | - | ||||||
256 | - | |||||||
257 | //let's reinitialize the buffer | - | ||||||
258 | actionRects.resize(actions.count()); | - | ||||||
259 | actionRects.fill(QRect()); | - | ||||||
260 | - | |||||||
261 | int lastVisibleAction = getLastVisibleAction(); | - | ||||||
262 | - | |||||||
263 | int max_column_width = 0, | - | ||||||
264 | dh = screen.height(), | - | ||||||
265 | y = 0; | - | ||||||
266 | QStyle *style = q->style(); | - | ||||||
267 | QStyleOption opt; | - | ||||||
268 | opt.init(q); | - | ||||||
269 | const int hmargin = style->pixelMetric(QStyle::PM_MenuHMargin, &opt, q), | - | ||||||
270 | vmargin = style->pixelMetric(QStyle::PM_MenuVMargin, &opt, q), | - | ||||||
271 | icone = style->pixelMetric(QStyle::PM_SmallIconSize, &opt, q); | - | ||||||
272 | const int fw = style->pixelMetric(QStyle::PM_MenuPanelWidth, &opt, q); | - | ||||||
273 | const int deskFw = style->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, &opt, q); | - | ||||||
274 | const int tearoffHeight = tearoff ? style->pixelMetric(QStyle::PM_MenuTearoffHeight, &opt, q) : 0; | - | ||||||
275 | - | |||||||
276 | //for compatibility now - will have to refactor this away | - | ||||||
277 | tabWidth = 0; | - | ||||||
278 | maxIconWidth = 0; | - | ||||||
279 | hasCheckableItems = false; | - | ||||||
280 | ncols = 1; | - | ||||||
281 | - | |||||||
282 | for (int i = 0; i < actions.count(); ++i) { | - | ||||||
283 | QAction *action = actions.at(i); | - | ||||||
284 | if (action->isSeparator() || !action->isVisible() || widgetItems.contains(action)) | - | ||||||
285 | continue; | - | ||||||
286 | //..and some members | - | ||||||
287 | hasCheckableItems |= action->isCheckable(); | - | ||||||
288 | QIcon is = action->icon(); | - | ||||||
289 | if (!is.isNull()) { | - | ||||||
290 | maxIconWidth = qMax<uint>(maxIconWidth, icone + 4); | - | ||||||
291 | } | - | ||||||
292 | } | - | ||||||
293 | - | |||||||
294 | //calculate size | - | ||||||
295 | QFontMetrics qfm = q->fontMetrics(); | - | ||||||
296 | bool previousWasSeparator = true; // this is true to allow removing the leading separators | - | ||||||
297 | for(int i = 0; i <= lastVisibleAction; i++) { | - | ||||||
298 | QAction *action = actions.at(i); | - | ||||||
299 | const bool isSection = action->isSeparator() && (!action->text().isEmpty() || !action->icon().isNull()); | - | ||||||
300 | const bool isPlainSeparator = (isSection && !q->style()->styleHint(QStyle::SH_Menu_SupportsSections)) | - | ||||||
301 | || (action->isSeparator() && !isSection); | - | ||||||
302 | - | |||||||
303 | if (!action->isVisible() || | - | ||||||
304 | (collapsibleSeparators && previousWasSeparator && isPlainSeparator)) | - | ||||||
305 | continue; // we continue, this action will get an empty QRect | - | ||||||
306 | - | |||||||
307 | previousWasSeparator = isPlainSeparator; | - | ||||||
308 | - | |||||||
309 | //let the style modify the above size.. | - | ||||||
310 | QStyleOptionMenuItem opt; | - | ||||||
311 | q->initStyleOption(&opt, action); | - | ||||||
312 | const QFontMetrics &fm = opt.fontMetrics; | - | ||||||
313 | - | |||||||
314 | QSize sz; | - | ||||||
315 | if (QWidget *w = widgetItems.value(action)) { | - | ||||||
316 | sz = w->sizeHint().expandedTo(w->minimumSize()).expandedTo(w->minimumSizeHint()).boundedTo(w->maximumSize()); | - | ||||||
317 | } else { | - | ||||||
318 | //calc what I think the size is.. | - | ||||||
319 | if (action->isSeparator()) { | - | ||||||
320 | sz = QSize(2, 2); | - | ||||||
321 | } else { | - | ||||||
322 | QString s = action->text(); | - | ||||||
323 | int t = s.indexOf(QLatin1Char('\t')); | - | ||||||
324 | if (t != -1) { | - | ||||||
325 | tabWidth = qMax(int(tabWidth), qfm.width(s.mid(t+1))); | - | ||||||
326 | s = s.left(t); | - | ||||||
327 | #ifndef QT_NO_SHORTCUT | - | ||||||
328 | } else { | - | ||||||
329 | QKeySequence seq = action->shortcut(); | - | ||||||
330 | if (!seq.isEmpty()) | - | ||||||
331 | tabWidth = qMax(int(tabWidth), qfm.width(seq.toString(QKeySequence::NativeText))); | - | ||||||
332 | #endif | - | ||||||
333 | } | - | ||||||
334 | sz.setWidth(fm.boundingRect(QRect(), Qt::TextSingleLine | Qt::TextShowMnemonic, s).width()); | - | ||||||
335 | sz.setHeight(qMax(fm.height(), qfm.height())); | - | ||||||
336 | - | |||||||
337 | QIcon is = action->icon(); | - | ||||||
338 | if (!is.isNull()) { | - | ||||||
339 | QSize is_sz = QSize(icone, icone); | - | ||||||
340 | if (is_sz.height() > sz.height()) | - | ||||||
341 | sz.setHeight(is_sz.height()); | - | ||||||
342 | } | - | ||||||
343 | } | - | ||||||
344 | sz = style->sizeFromContents(QStyle::CT_MenuItem, &opt, sz, q); | - | ||||||
345 | } | - | ||||||
346 | - | |||||||
347 | - | |||||||
348 | if (!sz.isEmpty()) { | - | ||||||
349 | max_column_width = qMax(max_column_width, sz.width()); | - | ||||||
350 | //wrapping | - | ||||||
351 | if (!scroll && | - | ||||||
352 | y+sz.height()+vmargin > dh - (deskFw * 2)) { | - | ||||||
353 | ncols++; | - | ||||||
354 | y = vmargin; | - | ||||||
355 | } | - | ||||||
356 | y += sz.height(); | - | ||||||
357 | //update the item | - | ||||||
358 | actionRects[i] = QRect(0, 0, sz.width(), sz.height()); | - | ||||||
359 | } | - | ||||||
360 | } | - | ||||||
361 | - | |||||||
362 | max_column_width += tabWidth; //finally add in the tab width | - | ||||||
363 | const int sfcMargin = style->sizeFromContents(QStyle::CT_Menu, &opt, QApplication::globalStrut(), q).width() - QApplication::globalStrut().width(); | - | ||||||
364 | const int min_column_width = q->minimumWidth() - (sfcMargin + leftmargin + rightmargin + 2 * (fw + hmargin)); | - | ||||||
365 | max_column_width = qMax(min_column_width, max_column_width); | - | ||||||
366 | - | |||||||
367 | //calculate position | - | ||||||
368 | const int base_y = vmargin + fw + topmargin + | - | ||||||
369 | (scroll ? scroll->scrollOffset : 0) + | - | ||||||
370 | tearoffHeight; | - | ||||||
371 | int x = hmargin + fw + leftmargin; | - | ||||||
372 | y = base_y; | - | ||||||
373 | - | |||||||
374 | for(int i = 0; i < actions.count(); i++) { | - | ||||||
375 | QRect &rect = actionRects[i]; | - | ||||||
376 | if (rect.isNull()) | - | ||||||
377 | continue; | - | ||||||
378 | if (!scroll && | - | ||||||
379 | y+rect.height() > dh - deskFw * 2) { | - | ||||||
380 | x += max_column_width + hmargin; | - | ||||||
381 | y = base_y; | - | ||||||
382 | } | - | ||||||
383 | rect.translate(x, y); //move | - | ||||||
384 | rect.setWidth(max_column_width); //uniform width | - | ||||||
385 | - | |||||||
386 | //we need to update the widgets geometry | - | ||||||
387 | if (QWidget *widget = widgetItems.value(actions.at(i))) { | - | ||||||
388 | widget->setGeometry(rect); | - | ||||||
389 | widget->setVisible(actions.at(i)->isVisible()); | - | ||||||
390 | } | - | ||||||
391 | - | |||||||
392 | y += rect.height(); | - | ||||||
393 | } | - | ||||||
394 | itemsDirty = 0; | - | ||||||
395 | } | - | ||||||
396 | - | |||||||
397 | QSize QMenuPrivate::adjustMenuSizeForScreen(const QRect &screen) | - | ||||||
398 | { | - | ||||||
399 | Q_Q(QMenu); | - | ||||||
400 | QSize ret = screen.size(); | - | ||||||
401 | itemsDirty = true; | - | ||||||
402 | updateActionRects(screen); | - | ||||||
403 | const int fw = q->style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, q); | - | ||||||
404 | ret.setWidth(actionRects.at(getLastVisibleAction()).right() + fw); | - | ||||||
405 | return ret; | - | ||||||
406 | } | - | ||||||
407 | - | |||||||
408 | int QMenuPrivate::getLastVisibleAction() const | - | ||||||
409 | { | - | ||||||
410 | //let's try to get the last visible action | - | ||||||
411 | int lastVisibleAction = actions.count() - 1; | - | ||||||
412 | for (;lastVisibleAction >= 0; --lastVisibleAction) { | - | ||||||
413 | const QAction *action = actions.at(lastVisibleAction); | - | ||||||
414 | if (action->isVisible()) { | - | ||||||
415 | //removing trailing separators | - | ||||||
416 | if (action->isSeparator() && collapsibleSeparators) | - | ||||||
417 | continue; | - | ||||||
418 | break; | - | ||||||
419 | } | - | ||||||
420 | } | - | ||||||
421 | return lastVisibleAction; | - | ||||||
422 | } | - | ||||||
423 | - | |||||||
424 | - | |||||||
425 | QRect QMenuPrivate::actionRect(QAction *act) const | - | ||||||
426 | { | - | ||||||
427 | int index = actions.indexOf(act); | - | ||||||
428 | if (index == -1) | - | ||||||
429 | return QRect(); | - | ||||||
430 | - | |||||||
431 | updateActionRects(); | - | ||||||
432 | - | |||||||
433 | //we found the action | - | ||||||
434 | return actionRects.at(index); | - | ||||||
435 | } | - | ||||||
436 | - | |||||||
437 | void QMenuPrivate::hideUpToMenuBar() | - | ||||||
438 | { | - | ||||||
439 | Q_Q(QMenu); | - | ||||||
440 | bool fadeMenus = q->style()->styleHint(QStyle::SH_Menu_FadeOutOnHide); | - | ||||||
441 | if (!tornoff) { | - | ||||||
442 | QWidget *caused = causedPopup.widget; | - | ||||||
443 | hideMenu(q); //hide after getting causedPopup | - | ||||||
444 | while(caused) { | - | ||||||
445 | #ifndef QT_NO_MENUBAR | - | ||||||
446 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(caused)) { | - | ||||||
447 | mb->d_func()->setCurrentAction(0); | - | ||||||
448 | mb->d_func()->setKeyboardMode(false); | - | ||||||
449 | caused = 0; | - | ||||||
450 | } else | - | ||||||
451 | #endif | - | ||||||
452 | if (QMenu *m = qobject_cast<QMenu*>(caused)) { | - | ||||||
453 | caused = m->d_func()->causedPopup.widget; | - | ||||||
454 | if (!m->d_func()->tornoff) | - | ||||||
455 | hideMenu(m); | - | ||||||
456 | if (!fadeMenus) // Mac doesn't clear the action until after hidden. | - | ||||||
457 | m->d_func()->setCurrentAction(0); | - | ||||||
458 | } else { caused = 0; | - | ||||||
459 | } | - | ||||||
460 | } | - | ||||||
461 | } | - | ||||||
462 | setCurrentAction(0); | - | ||||||
463 | } | - | ||||||
464 | - | |||||||
465 | void QMenuPrivate::hideMenu(QMenu *menu) | - | ||||||
466 | { | - | ||||||
467 | if (!menu) | - | ||||||
468 | return; | - | ||||||
469 | #if !defined(QT_NO_EFFECTS) | - | ||||||
470 | QSignalBlocker blocker(menu); | - | ||||||
471 | aboutToHide = true; | - | ||||||
472 | // Flash item which is about to trigger (if any). | - | ||||||
473 | if (menu->style()->styleHint(QStyle::SH_Menu_FlashTriggeredItem) | - | ||||||
474 | && currentAction && currentAction == actionAboutToTrigger | - | ||||||
475 | && menu->actions().contains(currentAction)) { | - | ||||||
476 | QEventLoop eventLoop; | - | ||||||
477 | QAction *activeAction = currentAction; | - | ||||||
478 | - | |||||||
479 | menu->setActiveAction(0); | - | ||||||
480 | QTimer::singleShot(60, &eventLoop, SLOT(quit())); | - | ||||||
481 | eventLoop.exec(); | - | ||||||
482 | - | |||||||
483 | // Select and wait 20 ms. | - | ||||||
484 | menu->setActiveAction(activeAction); | - | ||||||
485 | QTimer::singleShot(20, &eventLoop, SLOT(quit())); | - | ||||||
486 | eventLoop.exec(); | - | ||||||
487 | } | - | ||||||
488 | - | |||||||
489 | aboutToHide = false; | - | ||||||
490 | blocker.unblock(); | - | ||||||
491 | #endif // QT_NO_EFFECTS | - | ||||||
492 | if (activeMenu == menu) | - | ||||||
493 | activeMenu = 0; | - | ||||||
494 | menu->d_func()->causedPopup.action = 0; | - | ||||||
495 | menu->close(); | - | ||||||
496 | menu->d_func()->causedPopup.widget = 0; | - | ||||||
497 | } | - | ||||||
498 | - | |||||||
499 | void QMenuPrivate::popupAction(QAction *action, int delay, bool activateFirst) | - | ||||||
500 | { | - | ||||||
501 | Q_Q(QMenu); | - | ||||||
502 | if (action) { | - | ||||||
503 | if (action->isEnabled()) { | - | ||||||
504 | if (!delay) | - | ||||||
505 | q->internalDelayedPopup(); | - | ||||||
506 | else if (action->menu() && !action->menu()->isVisible()) | - | ||||||
507 | delayState.start(delay, action); | - | ||||||
508 | else if (!action->menu()) | - | ||||||
509 | delayState.stop(); | - | ||||||
510 | if (activateFirst && action->menu()) | - | ||||||
511 | action->menu()->d_func()->setFirstActionActive(); | - | ||||||
512 | } | - | ||||||
513 | } else if (QMenu *menu = activeMenu) { //hide the current item | - | ||||||
514 | hideMenu(menu); | - | ||||||
515 | } | - | ||||||
516 | } | - | ||||||
517 | - | |||||||
518 | void QMenuPrivate::setSyncAction() | - | ||||||
519 | { | - | ||||||
520 | Q_Q(QMenu); | - | ||||||
521 | QAction *current = currentAction; | - | ||||||
522 | if(current && (!current->isEnabled() || current->menu() || current->isSeparator())) | - | ||||||
523 | current = 0; | - | ||||||
524 | for(QWidget *caused = q; caused;) { | - | ||||||
525 | if (QMenu *m = qobject_cast<QMenu*>(caused)) { | - | ||||||
526 | caused = m->d_func()->causedPopup.widget; | - | ||||||
527 | if (m->d_func()->eventLoop) | - | ||||||
528 | m->d_func()->syncAction = current; // synchronous operation | - | ||||||
529 | } else { | - | ||||||
530 | break; | - | ||||||
531 | } | - | ||||||
532 | } | - | ||||||
533 | } | - | ||||||
534 | - | |||||||
535 | - | |||||||
536 | void QMenuPrivate::setFirstActionActive() | - | ||||||
537 | { | - | ||||||
538 | Q_Q(QMenu); | - | ||||||
539 | updateActionRects(); | - | ||||||
540 | for(int i = 0, saccum = 0; i < actions.count(); i++) { | - | ||||||
541 | const QRect &rect = actionRects.at(i); | - | ||||||
542 | if (rect.isNull()) | - | ||||||
543 | continue; | - | ||||||
544 | if (scroll && scroll->scrollFlags & QMenuScroller::ScrollUp) { | - | ||||||
545 | saccum -= rect.height(); | - | ||||||
546 | if (saccum > scroll->scrollOffset - scrollerHeight()) | - | ||||||
547 | continue; | - | ||||||
548 | } | - | ||||||
549 | QAction *act = actions.at(i); | - | ||||||
550 | if (!act->isSeparator() && | - | ||||||
551 | (q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q) | - | ||||||
552 | || act->isEnabled())) { | - | ||||||
553 | setCurrentAction(act); | - | ||||||
554 | break; | - | ||||||
555 | } | - | ||||||
556 | } | - | ||||||
557 | } | - | ||||||
558 | - | |||||||
559 | // popup == -1 means do not popup, 0 means immediately, others mean use a timer | - | ||||||
560 | void QMenuPrivate::setCurrentAction(QAction *action, int popup, SelectionReason reason, bool activateFirst) | - | ||||||
561 | { | - | ||||||
562 | Q_Q(QMenu); | - | ||||||
563 | tearoffHighlighted = 0; | - | ||||||
564 | - | |||||||
565 | if (action | - | ||||||
566 | && (action->isSeparator() | - | ||||||
567 | || (!action->isEnabled() && !q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q)))) | - | ||||||
568 | action = Q_NULLPTR; | - | ||||||
569 | - | |||||||
570 | // Reselect the currently active action in case mouse moved over other menu items when | - | ||||||
571 | // moving from sub menu action to sub menu (QTBUG-20094). | - | ||||||
572 | if (reason != SelectedFromKeyboard) { | - | ||||||
573 | if (QMenu *menu = qobject_cast<QMenu*>(causedPopup.widget)) { | - | ||||||
574 | if (causedPopup.action && menu->d_func()->activeMenu == q) | - | ||||||
575 | // Reselect parent menu action only if mouse is over a menu and parent menu action is not already selected (QTBUG-47987) | - | ||||||
576 | if (hasReceievedEnter && menu->d_func()->currentAction != causedPopup.action) | - | ||||||
577 | menu->d_func()->setCurrentAction(causedPopup.action, 0, reason, false); | - | ||||||
578 | } | - | ||||||
579 | } | - | ||||||
580 | - | |||||||
581 | if (currentAction) | - | ||||||
582 | q->update(actionRect(currentAction)); | - | ||||||
583 | - | |||||||
584 | QMenu *hideActiveMenu = activeMenu; | - | ||||||
585 | QAction *previousAction = currentAction; | - | ||||||
586 | - | |||||||
587 | currentAction = action; | - | ||||||
588 | if (action) { | - | ||||||
589 | if (!action->isSeparator()) { | - | ||||||
590 | activateAction(action, QAction::Hover); | - | ||||||
591 | if (popup != -1) { | - | ||||||
592 | // if the menu is visible then activate the required action, | - | ||||||
593 | // otherwise we just mark the action as currentAction | - | ||||||
594 | // and activate it when the menu will be popuped. | - | ||||||
595 | if (q->isVisible()) | - | ||||||
596 | popupAction(currentAction, popup, activateFirst); | - | ||||||
597 | } | - | ||||||
598 | q->update(actionRect(action)); | - | ||||||
599 | - | |||||||
600 | if (reason == SelectedFromKeyboard) { | - | ||||||
601 | QWidget *widget = widgetItems.value(action); | - | ||||||
602 | if (widget) { | - | ||||||
603 | if (widget->focusPolicy() != Qt::NoFocus) | - | ||||||
604 | widget->setFocus(Qt::TabFocusReason); | - | ||||||
605 | } else { | - | ||||||
606 | //when the action has no QWidget, the QMenu itself should | - | ||||||
607 | // get the focus | - | ||||||
608 | // Since the menu is a pop-up, it uses the popup reason. | - | ||||||
609 | if (!q->hasFocus()) { | - | ||||||
610 | q->setFocus(Qt::PopupFocusReason); | - | ||||||
611 | } | - | ||||||
612 | } | - | ||||||
613 | } | - | ||||||
614 | } | - | ||||||
615 | #ifndef QT_NO_STATUSTIP | - | ||||||
616 | } else if (previousAction) { | - | ||||||
617 | previousAction->d_func()->showStatusText(topCausedWidget(), QString()); | - | ||||||
618 | #endif | - | ||||||
619 | } | - | ||||||
620 | if (hideActiveMenu && previousAction != currentAction) { | - | ||||||
621 | if (popup == -1) { | - | ||||||
622 | #ifndef QT_NO_EFFECTS | - | ||||||
623 | // kill any running effect | - | ||||||
624 | qFadeEffect(0); | - | ||||||
625 | qScrollEffect(0); | - | ||||||
626 | #endif | - | ||||||
627 | hideMenu(hideActiveMenu); | - | ||||||
628 | } else if (!currentAction || !currentAction->menu()) { | - | ||||||
629 | sloppyState.startTimerIfNotRunning(); | - | ||||||
630 | } | - | ||||||
631 | } | - | ||||||
632 | } | - | ||||||
633 | - | |||||||
634 | void QMenuSloppyState::reset() | - | ||||||
635 | { | - | ||||||
636 | m_enabled = false; | - | ||||||
637 | m_first_mouse = true; | - | ||||||
638 | m_init_guard = false; | - | ||||||
639 | m_uni_dir_discarded_count = 0; | - | ||||||
640 | m_time.stop(); | - | ||||||
641 | m_reset_action = Q_NULLPTR; | - | ||||||
642 | m_origin_action = Q_NULLPTR; | - | ||||||
643 | m_action_rect = QRect(); | - | ||||||
644 | m_previous_point = QPointF(); | - | ||||||
645 | if (m_sub_menu) { | - | ||||||
646 | QMenuPrivate::get(m_sub_menu)->sloppyState.m_parent = Q_NULLPTR; | - | ||||||
647 | m_sub_menu = Q_NULLPTR; | - | ||||||
648 | } | - | ||||||
649 | } | - | ||||||
650 | void QMenuSloppyState::enter() | - | ||||||
651 | { | - | ||||||
652 | QMenuPrivate *menuPriv = QMenuPrivate::get(m_menu); | - | ||||||
653 | - | |||||||
654 | if (m_discard_state_when_entering_parent && m_sub_menu == menuPriv->activeMenu) { | - | ||||||
655 | menuPriv->hideMenu(m_sub_menu); | - | ||||||
656 | reset(); | - | ||||||
657 | } | - | ||||||
658 | if (m_parent) | - | ||||||
659 | m_parent->childEnter(); | - | ||||||
660 | } | - | ||||||
661 | - | |||||||
662 | void QMenuSloppyState::childEnter() | - | ||||||
663 | { | - | ||||||
664 | stopTimer(); | - | ||||||
665 | if (m_parent) | - | ||||||
666 | m_parent->childEnter(); | - | ||||||
667 | } | - | ||||||
668 | - | |||||||
669 | void QMenuSloppyState::leave() | - | ||||||
670 | { | - | ||||||
671 | if (!m_dont_start_time_on_leave) { | - | ||||||
672 | if (m_parent) | - | ||||||
673 | m_parent->childLeave(); | - | ||||||
674 | startTimerIfNotRunning(); | - | ||||||
675 | } | - | ||||||
676 | } | - | ||||||
677 | - | |||||||
678 | void QMenuSloppyState::childLeave() | - | ||||||
679 | { | - | ||||||
680 | if (m_enabled && !QMenuPrivate::get(m_menu)->hasReceievedEnter) { | - | ||||||
681 | startTimerIfNotRunning(); | - | ||||||
682 | if (m_parent) | - | ||||||
683 | m_parent->childLeave(); | - | ||||||
684 | } | - | ||||||
685 | } | - | ||||||
686 | - | |||||||
687 | void QMenuSloppyState::setSubMenuPopup(const QRect &actionRect, QAction *resetAction, QMenu *subMenu) | - | ||||||
688 | { | - | ||||||
689 | m_enabled = true; | - | ||||||
690 | m_init_guard = true; | - | ||||||
691 | m_time.stop(); | - | ||||||
692 | m_action_rect = actionRect; | - | ||||||
693 | m_sub_menu = subMenu; | - | ||||||
694 | QMenuPrivate::get(subMenu)->sloppyState.m_parent = this; | - | ||||||
695 | m_reset_action = resetAction; | - | ||||||
696 | m_origin_action = resetAction; | - | ||||||
697 | } | - | ||||||
698 | - | |||||||
699 | bool QMenuSloppyState::hasParentActiveDelayTimer() const | - | ||||||
700 | { | - | ||||||
701 | return m_parent && m_parent->m_menu && QMenuPrivate::get(m_parent->m_menu)->delayState.timer.isActive(); | - | ||||||
702 | } | - | ||||||
703 | - | |||||||
704 | class ResetOnDestroy | - | ||||||
705 | { | - | ||||||
706 | public: | - | ||||||
707 | ResetOnDestroy(QMenuSloppyState *sloppyState, bool *guard) | - | ||||||
708 | : toReset(sloppyState) | - | ||||||
709 | , guard(guard) | - | ||||||
710 | { | - | ||||||
711 | *guard = false; | - | ||||||
712 | } | - | ||||||
713 | - | |||||||
714 | ~ResetOnDestroy() | - | ||||||
715 | { | - | ||||||
716 | if (!*guard) | - | ||||||
717 | toReset->reset(); | - | ||||||
718 | } | - | ||||||
719 | - | |||||||
720 | QMenuSloppyState *toReset; | - | ||||||
721 | bool *guard; | - | ||||||
722 | }; | - | ||||||
723 | - | |||||||
724 | void QMenuSloppyState::timeout() | - | ||||||
725 | { | - | ||||||
726 | QMenuPrivate *menu_priv = QMenuPrivate::get(m_menu); | - | ||||||
727 | - | |||||||
728 | bool reallyHasMouse = menu_priv->hasReceievedEnter; | - | ||||||
729 | if (!reallyHasMouse) { | - | ||||||
730 | // Check whether the menu really has a mouse, because only active popup | - | ||||||
731 | // menu gets the enter/leave events. Currently Cocoa is an exception. | - | ||||||
732 | const QPoint lastCursorPos = QGuiApplicationPrivate::lastCursorPosition.toPoint(); | - | ||||||
733 | reallyHasMouse = m_menu->frameGeometry().contains(lastCursorPos); | - | ||||||
734 | } | - | ||||||
735 | - | |||||||
736 | if (menu_priv->currentAction == m_reset_action | - | ||||||
737 | && reallyHasMouse | - | ||||||
738 | && (menu_priv->currentAction | - | ||||||
739 | && menu_priv->currentAction->menu() == menu_priv->activeMenu)) { | - | ||||||
740 | return; | - | ||||||
741 | } | - | ||||||
742 | - | |||||||
743 | ResetOnDestroy resetState(this, &m_init_guard); | - | ||||||
744 | - | |||||||
745 | if (hasParentActiveDelayTimer() || !m_menu->isVisible()) | - | ||||||
746 | return; | - | ||||||
747 | - | |||||||
748 | if (m_sub_menu) | - | ||||||
749 | menu_priv->hideMenu(m_sub_menu); | - | ||||||
750 | - | |||||||
751 | if (reallyHasMouse) | - | ||||||
752 | menu_priv->setCurrentAction(m_reset_action,0); | - | ||||||
753 | else | - | ||||||
754 | menu_priv->setCurrentAction(Q_NULLPTR, 0); | - | ||||||
755 | } | - | ||||||
756 | - | |||||||
757 | //return the top causedPopup.widget that is not a QMenu | - | ||||||
758 | QWidget *QMenuPrivate::topCausedWidget() const | - | ||||||
759 | { | - | ||||||
760 | QWidget* top = causedPopup.widget; | - | ||||||
761 | while (QMenu* m = qobject_cast<QMenu *>(top)) | - | ||||||
762 | top = m->d_func()->causedPopup.widget; | - | ||||||
763 | return top; | - | ||||||
764 | } | - | ||||||
765 | - | |||||||
766 | QAction *QMenuPrivate::actionAt(QPoint p) const | - | ||||||
767 | { | - | ||||||
768 | if (!q_func()->rect().contains(p)) //sanity check | - | ||||||
769 | return 0; | - | ||||||
770 | - | |||||||
771 | for(int i = 0; i < actionRects.count(); i++) { | - | ||||||
772 | if (actionRects.at(i).contains(p)) | - | ||||||
773 | return actions.at(i); | - | ||||||
774 | } | - | ||||||
775 | return 0; | - | ||||||
776 | } | - | ||||||
777 | - | |||||||
778 | void QMenuPrivate::setOverrideMenuAction(QAction *a) | - | ||||||
779 | { | - | ||||||
780 | Q_Q(QMenu); | - | ||||||
781 | QObject::disconnect(menuAction, SIGNAL(destroyed()), q, SLOT(_q_overrideMenuActionDestroyed())); | - | ||||||
782 | if (a) { | - | ||||||
783 | menuAction = a; | - | ||||||
784 | QObject::connect(a, SIGNAL(destroyed()), q, SLOT(_q_overrideMenuActionDestroyed())); | - | ||||||
785 | } else { //we revert back to the default action created by the QMenu itself | - | ||||||
786 | menuAction = defaultMenuAction; | - | ||||||
787 | } | - | ||||||
788 | } | - | ||||||
789 | - | |||||||
790 | void QMenuPrivate::_q_overrideMenuActionDestroyed() | - | ||||||
791 | { | - | ||||||
792 | menuAction=defaultMenuAction; | - | ||||||
793 | } | - | ||||||
794 | - | |||||||
795 | - | |||||||
796 | void QMenuPrivate::updateLayoutDirection() | - | ||||||
797 | { | - | ||||||
798 | Q_Q(QMenu); | - | ||||||
799 | //we need to mimic the cause of the popup's layout direction | - | ||||||
800 | //to allow setting it on a mainwindow for example | - | ||||||
801 | //we call setLayoutDirection_helper to not overwrite a user-defined value | - | ||||||
802 | if (!q->testAttribute(Qt::WA_SetLayoutDirection)) { | - | ||||||
803 | if (QWidget *w = causedPopup.widget) | - | ||||||
804 | setLayoutDirection_helper(w->layoutDirection()); | - | ||||||
805 | else if (QWidget *w = q->parentWidget()) | - | ||||||
806 | setLayoutDirection_helper(w->layoutDirection()); | - | ||||||
807 | else | - | ||||||
808 | setLayoutDirection_helper(QApplication::layoutDirection()); | - | ||||||
809 | } | - | ||||||
810 | } | - | ||||||
811 | - | |||||||
812 | - | |||||||
813 | /*! | - | ||||||
814 | Returns the action associated with this menu. | - | ||||||
815 | */ | - | ||||||
816 | QAction *QMenu::menuAction() const | - | ||||||
817 | { | - | ||||||
818 | return d_func()->menuAction; | - | ||||||
819 | } | - | ||||||
820 | - | |||||||
821 | /*! | - | ||||||
822 | \property QMenu::title | - | ||||||
823 | \brief The title of the menu | - | ||||||
824 | - | |||||||
825 | This is equivalent to the QAction::text property of the menuAction(). | - | ||||||
826 | - | |||||||
827 | By default, this property contains an empty string. | - | ||||||
828 | */ | - | ||||||
829 | QString QMenu::title() const | - | ||||||
830 | { | - | ||||||
831 | return d_func()->menuAction->text(); | - | ||||||
832 | } | - | ||||||
833 | - | |||||||
834 | void QMenu::setTitle(const QString &text) | - | ||||||
835 | { | - | ||||||
836 | d_func()->menuAction->setText(text); | - | ||||||
837 | } | - | ||||||
838 | - | |||||||
839 | /*! | - | ||||||
840 | \property QMenu::icon | - | ||||||
841 | - | |||||||
842 | \brief The icon of the menu | - | ||||||
843 | - | |||||||
844 | This is equivalent to the QAction::icon property of the menuAction(). | - | ||||||
845 | - | |||||||
846 | By default, if no icon is explicitly set, this property contains a null icon. | - | ||||||
847 | */ | - | ||||||
848 | QIcon QMenu::icon() const | - | ||||||
849 | { | - | ||||||
850 | return d_func()->menuAction->icon(); | - | ||||||
851 | } | - | ||||||
852 | - | |||||||
853 | void QMenu::setIcon(const QIcon &icon) | - | ||||||
854 | { | - | ||||||
855 | d_func()->menuAction->setIcon(icon); | - | ||||||
856 | } | - | ||||||
857 | - | |||||||
858 | - | |||||||
859 | //actually performs the scrolling | - | ||||||
860 | void QMenuPrivate::scrollMenu(QAction *action, QMenuScroller::ScrollLocation location, bool active) | - | ||||||
861 | { | - | ||||||
862 | Q_Q(QMenu); | - | ||||||
863 | if (!scroll || !scroll->scrollFlags) | - | ||||||
864 | return; | - | ||||||
865 | updateActionRects(); | - | ||||||
866 | int newOffset = 0; | - | ||||||
867 | const int topScroll = (scroll->scrollFlags & QMenuScroller::ScrollUp) ? scrollerHeight() : 0; | - | ||||||
868 | const int botScroll = (scroll->scrollFlags & QMenuScroller::ScrollDown) ? scrollerHeight() : 0; | - | ||||||
869 | const int vmargin = q->style()->pixelMetric(QStyle::PM_MenuVMargin, 0, q); | - | ||||||
870 | const int fw = q->style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, q); | - | ||||||
871 | - | |||||||
872 | if (location == QMenuScroller::ScrollTop) { | - | ||||||
873 | for(int i = 0, saccum = 0; i < actions.count(); i++) { | - | ||||||
874 | if (actions.at(i) == action) { | - | ||||||
875 | newOffset = topScroll - saccum; | - | ||||||
876 | break; | - | ||||||
877 | } | - | ||||||
878 | saccum += actionRects.at(i).height(); | - | ||||||
879 | } | - | ||||||
880 | } else { | - | ||||||
881 | for(int i = 0, saccum = 0; i < actions.count(); i++) { | - | ||||||
882 | saccum += actionRects.at(i).height(); | - | ||||||
883 | if (actions.at(i) == action) { | - | ||||||
884 | if (location == QMenuScroller::ScrollCenter) | - | ||||||
885 | newOffset = ((q->height() / 2) - botScroll) - (saccum - topScroll); | - | ||||||
886 | else | - | ||||||
887 | newOffset = (q->height() - botScroll) - saccum; | - | ||||||
888 | break; | - | ||||||
889 | } | - | ||||||
890 | } | - | ||||||
891 | if(newOffset) | - | ||||||
892 | newOffset -= fw * 2; | - | ||||||
893 | } | - | ||||||
894 | - | |||||||
895 | //figure out which scroll flags | - | ||||||
896 | uint newScrollFlags = QMenuScroller::ScrollNone; | - | ||||||
897 | if (newOffset < 0) //easy and cheap one | - | ||||||
898 | newScrollFlags |= QMenuScroller::ScrollUp; | - | ||||||
899 | int saccum = newOffset; | - | ||||||
900 | for(int i = 0; i < actionRects.count(); i++) { | - | ||||||
901 | saccum += actionRects.at(i).height(); | - | ||||||
902 | if (saccum > q->height()) { | - | ||||||
903 | newScrollFlags |= QMenuScroller::ScrollDown; | - | ||||||
904 | break; | - | ||||||
905 | } | - | ||||||
906 | } | - | ||||||
907 | - | |||||||
908 | if (!(newScrollFlags & QMenuScroller::ScrollDown) && (scroll->scrollFlags & QMenuScroller::ScrollDown)) { | - | ||||||
909 | newOffset = q->height() - (saccum - newOffset) - fw*2 - vmargin; //last item at bottom | - | ||||||
910 | } | - | ||||||
911 | - | |||||||
912 | if (!(newScrollFlags & QMenuScroller::ScrollUp) && (scroll->scrollFlags & QMenuScroller::ScrollUp)) { | - | ||||||
913 | newOffset = 0; //first item at top | - | ||||||
914 | } | - | ||||||
915 | - | |||||||
916 | if (newScrollFlags & QMenuScroller::ScrollUp) | - | ||||||
917 | newOffset -= vmargin; | - | ||||||
918 | - | |||||||
919 | QRect screen = popupGeometry(q); | - | ||||||
920 | const int desktopFrame = q->style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, q); | - | ||||||
921 | if (q->height() < screen.height()-(desktopFrame*2)-1) { | - | ||||||
922 | QRect geom = q->geometry(); | - | ||||||
923 | if (newOffset > scroll->scrollOffset && (scroll->scrollFlags & newScrollFlags & QMenuScroller::ScrollUp)) { //scroll up | - | ||||||
924 | const int newHeight = geom.height()-(newOffset-scroll->scrollOffset); | - | ||||||
925 | if(newHeight > geom.height()) | - | ||||||
926 | geom.setHeight(newHeight); | - | ||||||
927 | } else if(scroll->scrollFlags & newScrollFlags & QMenuScroller::ScrollDown) { | - | ||||||
928 | int newTop = geom.top() + (newOffset-scroll->scrollOffset); | - | ||||||
929 | if (newTop < desktopFrame+screen.top()) | - | ||||||
930 | newTop = desktopFrame+screen.top(); | - | ||||||
931 | if (newTop < geom.top()) { | - | ||||||
932 | geom.setTop(newTop); | - | ||||||
933 | newOffset = 0; | - | ||||||
934 | newScrollFlags &= ~QMenuScroller::ScrollUp; | - | ||||||
935 | } | - | ||||||
936 | } | - | ||||||
937 | if (geom.bottom() > screen.bottom() - desktopFrame) | - | ||||||
938 | geom.setBottom(screen.bottom() - desktopFrame); | - | ||||||
939 | if (geom.top() < desktopFrame+screen.top()) | - | ||||||
940 | geom.setTop(desktopFrame+screen.top()); | - | ||||||
941 | if (geom != q->geometry()) { | - | ||||||
942 | #if 0 | - | ||||||
943 | if (newScrollFlags & QMenuScroller::ScrollDown && | - | ||||||
944 | q->geometry().top() - geom.top() >= -newOffset) | - | ||||||
945 | newScrollFlags &= ~QMenuScroller::ScrollDown; | - | ||||||
946 | #endif | - | ||||||
947 | q->setGeometry(geom); | - | ||||||
948 | } | - | ||||||
949 | } | - | ||||||
950 | - | |||||||
951 | //actually update flags | - | ||||||
952 | const int delta = qMin(0, newOffset) - scroll->scrollOffset; //make sure the new offset is always negative | - | ||||||
953 | if (!itemsDirty && delta) { | - | ||||||
954 | //we've scrolled so we need to update the action rects | - | ||||||
955 | for (int i = 0; i < actionRects.count(); ++i) { | - | ||||||
956 | QRect ¤t = actionRects[i]; | - | ||||||
957 | current.moveTop(current.top() + delta); | - | ||||||
958 | - | |||||||
959 | //we need to update the widgets geometry | - | ||||||
960 | if (QWidget *w = widgetItems.value(actions.at(i))) | - | ||||||
961 | w->setGeometry(current); | - | ||||||
962 | } | - | ||||||
963 | } | - | ||||||
964 | scroll->scrollOffset += delta; | - | ||||||
965 | scroll->scrollFlags = newScrollFlags; | - | ||||||
966 | if (active) | - | ||||||
967 | setCurrentAction(action); | - | ||||||
968 | - | |||||||
969 | q->update(); //issue an update so we see all the new state.. | - | ||||||
970 | } | - | ||||||
971 | - | |||||||
972 | void QMenuPrivate::scrollMenu(QMenuScroller::ScrollLocation location, bool active) | - | ||||||
973 | { | - | ||||||
974 | Q_Q(QMenu); | - | ||||||
975 | updateActionRects(); | - | ||||||
976 | if(location == QMenuScroller::ScrollBottom) { | - | ||||||
977 | for(int i = actions.size()-1; i >= 0; --i) { | - | ||||||
978 | QAction *act = actions.at(i); | - | ||||||
979 | if (actionRects.at(i).isNull()) | - | ||||||
980 | continue; | - | ||||||
981 | if (!act->isSeparator() && | - | ||||||
982 | (q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q) | - | ||||||
983 | || act->isEnabled())) { | - | ||||||
984 | if(scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown) | - | ||||||
985 | scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollBottom, active); | - | ||||||
986 | else if(active) | - | ||||||
987 | setCurrentAction(act, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard); | - | ||||||
988 | break; | - | ||||||
989 | } | - | ||||||
990 | } | - | ||||||
991 | } else if(location == QMenuScroller::ScrollTop) { | - | ||||||
992 | for(int i = 0; i < actions.size(); ++i) { | - | ||||||
993 | QAction *act = actions.at(i); | - | ||||||
994 | if (actionRects.at(i).isNull()) | - | ||||||
995 | continue; | - | ||||||
996 | if (!act->isSeparator() && | - | ||||||
997 | (q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, q) | - | ||||||
998 | || act->isEnabled())) { | - | ||||||
999 | if(scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) | - | ||||||
1000 | scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollTop, active); | - | ||||||
1001 | else if(active) | - | ||||||
1002 | setCurrentAction(act, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard); | - | ||||||
1003 | break; | - | ||||||
1004 | } | - | ||||||
1005 | } | - | ||||||
1006 | } | - | ||||||
1007 | } | - | ||||||
1008 | - | |||||||
1009 | //only directional | - | ||||||
1010 | void QMenuPrivate::scrollMenu(QMenuScroller::ScrollDirection direction, bool page, bool active) | - | ||||||
1011 | { | - | ||||||
1012 | Q_Q(QMenu); | - | ||||||
1013 | if (!scroll || !(scroll->scrollFlags & direction)) //not really possible... | - | ||||||
1014 | return; | - | ||||||
1015 | updateActionRects(); | - | ||||||
1016 | const int topScroll = (scroll->scrollFlags & QMenuScroller::ScrollUp) ? scrollerHeight() : 0; | - | ||||||
1017 | const int botScroll = (scroll->scrollFlags & QMenuScroller::ScrollDown) ? scrollerHeight() : 0; | - | ||||||
1018 | const int vmargin = q->style()->pixelMetric(QStyle::PM_MenuVMargin, 0, q); | - | ||||||
1019 | const int fw = q->style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, q); | - | ||||||
1020 | const int offset = topScroll ? topScroll-vmargin : 0; | - | ||||||
1021 | if (direction == QMenuScroller::ScrollUp) { | - | ||||||
1022 | for(int i = 0, saccum = 0; i < actions.count(); i++) { | - | ||||||
1023 | saccum -= actionRects.at(i).height(); | - | ||||||
1024 | if (saccum <= scroll->scrollOffset-offset) { | - | ||||||
1025 | scrollMenu(actions.at(i), page ? QMenuScroller::ScrollBottom : QMenuScroller::ScrollTop, active); | - | ||||||
1026 | break; | - | ||||||
1027 | } | - | ||||||
1028 | } | - | ||||||
1029 | } else if (direction == QMenuScroller::ScrollDown) { | - | ||||||
1030 | bool scrolled = false; | - | ||||||
1031 | for(int i = 0, saccum = 0; i < actions.count(); i++) { | - | ||||||
1032 | const int iHeight = actionRects.at(i).height(); | - | ||||||
1033 | saccum -= iHeight; | - | ||||||
1034 | if (saccum <= scroll->scrollOffset-offset) { | - | ||||||
1035 | const int scrollerArea = q->height() - botScroll - fw*2; | - | ||||||
1036 | int visible = (scroll->scrollOffset-offset) - saccum; | - | ||||||
1037 | for(i++ ; i < actions.count(); i++) { | - | ||||||
1038 | visible += actionRects.at(i).height(); | - | ||||||
1039 | if (visible > scrollerArea - topScroll) { | - | ||||||
1040 | scrolled = true; | - | ||||||
1041 | scrollMenu(actions.at(i), page ? QMenuScroller::ScrollTop : QMenuScroller::ScrollBottom, active); | - | ||||||
1042 | break; | - | ||||||
1043 | } | - | ||||||
1044 | } | - | ||||||
1045 | break; | - | ||||||
1046 | } | - | ||||||
1047 | } | - | ||||||
1048 | if(!scrolled) { | - | ||||||
1049 | scroll->scrollFlags &= ~QMenuScroller::ScrollDown; | - | ||||||
1050 | q->update(); | - | ||||||
1051 | } | - | ||||||
1052 | } | - | ||||||
1053 | } | - | ||||||
1054 | - | |||||||
1055 | /* This is poor-mans eventfilters. This avoids the use of | - | ||||||
1056 | eventFilter (which can be nasty for users of QMenuBar's). */ | - | ||||||
1057 | bool QMenuPrivate::mouseEventTaken(QMouseEvent *e) | - | ||||||
1058 | { | - | ||||||
1059 | Q_Q(QMenu); | - | ||||||
1060 | QPoint pos = q->mapFromGlobal(e->globalPos()); | - | ||||||
1061 | if (scroll && !activeMenu) { //let the scroller "steal" the event | - | ||||||
1062 | bool isScroll = false; | - | ||||||
1063 | if (pos.x() >= 0 && pos.x() < q->width()) { | - | ||||||
1064 | for(int dir = QMenuScroller::ScrollUp; dir <= QMenuScroller::ScrollDown; dir = dir << 1) { | - | ||||||
1065 | if (scroll->scrollFlags & dir) { | - | ||||||
1066 | if (dir == QMenuScroller::ScrollUp) | - | ||||||
1067 | isScroll = (pos.y() <= scrollerHeight()); | - | ||||||
1068 | else if (dir == QMenuScroller::ScrollDown) | - | ||||||
1069 | isScroll = (pos.y() >= q->height() - scrollerHeight()); | - | ||||||
1070 | if (isScroll) { | - | ||||||
1071 | scroll->scrollDirection = dir; | - | ||||||
1072 | break; | - | ||||||
1073 | } | - | ||||||
1074 | } | - | ||||||
1075 | } | - | ||||||
1076 | } | - | ||||||
1077 | if (isScroll) { | - | ||||||
1078 | scroll->scrollTimer.start(50, q); | - | ||||||
1079 | return true; | - | ||||||
1080 | } else { | - | ||||||
1081 | scroll->scrollTimer.stop(); | - | ||||||
1082 | } | - | ||||||
1083 | } | - | ||||||
1084 | - | |||||||
1085 | if (tearoff) { //let the tear off thingie "steal" the event.. | - | ||||||
1086 | QRect tearRect(0, 0, q->width(), q->style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, q)); | - | ||||||
1087 | if (scroll && scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) | - | ||||||
1088 | tearRect.translate(0, scrollerHeight()); | - | ||||||
1089 | q->update(tearRect); | - | ||||||
1090 | if (tearRect.contains(pos) && hasMouseMoved(e->globalPos())) { | - | ||||||
1091 | setCurrentAction(0); | - | ||||||
1092 | tearoffHighlighted = 1; | - | ||||||
1093 | if (e->type() == QEvent::MouseButtonRelease) { | - | ||||||
1094 | if (!tornPopup) | - | ||||||
1095 | tornPopup = new QTornOffMenu(q); | - | ||||||
1096 | tornPopup->setGeometry(q->geometry()); | - | ||||||
1097 | tornPopup->show(); | - | ||||||
1098 | hideUpToMenuBar(); | - | ||||||
1099 | } | - | ||||||
1100 | return true; | - | ||||||
1101 | } | - | ||||||
1102 | tearoffHighlighted = 0; | - | ||||||
1103 | } | - | ||||||
1104 | - | |||||||
1105 | if (q->frameGeometry().contains(e->globalPos())) | - | ||||||
1106 | return false; //otherwise if the event is in our rect we want it.. | - | ||||||
1107 | - | |||||||
1108 | for(QWidget *caused = causedPopup.widget; caused;) { | - | ||||||
1109 | bool passOnEvent = false; | - | ||||||
1110 | QWidget *next_widget = 0; | - | ||||||
1111 | QPoint cpos = caused->mapFromGlobal(e->globalPos()); | - | ||||||
1112 | #ifndef QT_NO_MENUBAR | - | ||||||
1113 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(caused)) { | - | ||||||
1114 | passOnEvent = mb->rect().contains(cpos); | - | ||||||
1115 | } else | - | ||||||
1116 | #endif | - | ||||||
1117 | if (QMenu *m = qobject_cast<QMenu*>(caused)) { | - | ||||||
1118 | passOnEvent = m->rect().contains(cpos); | - | ||||||
1119 | next_widget = m->d_func()->causedPopup.widget; | - | ||||||
1120 | } | - | ||||||
1121 | if (passOnEvent) { | - | ||||||
1122 | if (e->type() != QEvent::MouseButtonRelease || mouseDown == caused) { | - | ||||||
1123 | QMouseEvent new_e(e->type(), cpos, caused->mapTo(caused->topLevelWidget(), cpos), e->screenPos(), | - | ||||||
1124 | e->button(), e->buttons(), e->modifiers(), e->source()); | - | ||||||
1125 | QApplication::sendEvent(caused, &new_e); | - | ||||||
1126 | return true; | - | ||||||
1127 | } | - | ||||||
1128 | } | - | ||||||
1129 | caused = next_widget; | - | ||||||
1130 | if (!caused) | - | ||||||
1131 | sloppyState.leave(); // Start timers | - | ||||||
1132 | } | - | ||||||
1133 | return false; | - | ||||||
1134 | } | - | ||||||
1135 | - | |||||||
1136 | void QMenuPrivate::activateCausedStack(const QVector<QPointer<QWidget> > &causedStack, QAction *action, QAction::ActionEvent action_e, bool self) | - | ||||||
1137 | { | - | ||||||
1138 | QBoolBlocker guard(activationRecursionGuard); | - | ||||||
1139 | if(self) | - | ||||||
1140 | action->activate(action_e); | - | ||||||
1141 | - | |||||||
1142 | for(int i = 0; i < causedStack.size(); ++i) { | - | ||||||
1143 | QPointer<QWidget> widget = causedStack.at(i); | - | ||||||
1144 | if (!widget) | - | ||||||
1145 | continue; | - | ||||||
1146 | //fire | - | ||||||
1147 | if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) { | - | ||||||
1148 | widget = qmenu->d_func()->causedPopup.widget; | - | ||||||
1149 | if (action_e == QAction::Trigger) { | - | ||||||
1150 | emit qmenu->triggered(action); | - | ||||||
1151 | } else if (action_e == QAction::Hover) { | - | ||||||
1152 | emit qmenu->hovered(action); | - | ||||||
1153 | } | - | ||||||
1154 | #ifndef QT_NO_MENUBAR | - | ||||||
1155 | } else if (QMenuBar *qmenubar = qobject_cast<QMenuBar*>(widget)) { | - | ||||||
1156 | if (action_e == QAction::Trigger) { | - | ||||||
1157 | emit qmenubar->triggered(action); | - | ||||||
1158 | } else if (action_e == QAction::Hover) { | - | ||||||
1159 | emit qmenubar->hovered(action); | - | ||||||
1160 | } | - | ||||||
1161 | break; //nothing more.. | - | ||||||
1162 | #endif | - | ||||||
1163 | } | - | ||||||
1164 | } | - | ||||||
1165 | } | - | ||||||
1166 | - | |||||||
1167 | void QMenuPrivate::activateAction(QAction *action, QAction::ActionEvent action_e, bool self) | - | ||||||
1168 | { | - | ||||||
1169 | Q_Q(QMenu); | - | ||||||
1170 | #ifndef QT_NO_WHATSTHIS | - | ||||||
1171 | bool inWhatsThisMode = QWhatsThis::inWhatsThisMode(); | - | ||||||
1172 | #endif | - | ||||||
1173 | if (!action || !q->isEnabled() | - | ||||||
1174 | || (action_e == QAction::Trigger | - | ||||||
1175 | #ifndef QT_NO_WHATSTHIS | - | ||||||
1176 | && !inWhatsThisMode | - | ||||||
1177 | #endif | - | ||||||
1178 | && (action->isSeparator() ||!action->isEnabled()))) | - | ||||||
1179 | return; | - | ||||||
1180 | - | |||||||
1181 | /* I have to save the caused stack here because it will be undone after popup execution (ie in the hide). | - | ||||||
1182 | Then I iterate over the list to actually send the events. --Sam | - | ||||||
1183 | */ | - | ||||||
1184 | const QVector<QPointer<QWidget> > causedStack = calcCausedStack(); | - | ||||||
1185 | if (action_e == QAction::Trigger) { | - | ||||||
1186 | #ifndef QT_NO_WHATSTHIS | - | ||||||
1187 | if (!inWhatsThisMode) | - | ||||||
1188 | actionAboutToTrigger = action; | - | ||||||
1189 | #endif | - | ||||||
1190 | - | |||||||
1191 | if (q->testAttribute(Qt::WA_DontShowOnScreen)) { | - | ||||||
1192 | hideUpToMenuBar(); | - | ||||||
1193 | } else { | - | ||||||
1194 | for(QWidget *widget = QApplication::activePopupWidget(); widget; ) { | - | ||||||
1195 | if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) { | - | ||||||
1196 | if(qmenu == q) | - | ||||||
1197 | hideUpToMenuBar(); | - | ||||||
1198 | widget = qmenu->d_func()->causedPopup.widget; | - | ||||||
1199 | } else { | - | ||||||
1200 | break; | - | ||||||
1201 | } | - | ||||||
1202 | } | - | ||||||
1203 | } | - | ||||||
1204 | - | |||||||
1205 | #ifndef QT_NO_WHATSTHIS | - | ||||||
1206 | if (inWhatsThisMode) { | - | ||||||
1207 | QString s = action->whatsThis(); | - | ||||||
1208 | if (s.isEmpty()) | - | ||||||
1209 | s = whatsThis; | - | ||||||
1210 | QWhatsThis::showText(q->mapToGlobal(actionRect(action).center()), s, q); | - | ||||||
1211 | return; | - | ||||||
1212 | } | - | ||||||
1213 | #endif | - | ||||||
1214 | } | - | ||||||
1215 | - | |||||||
1216 | - | |||||||
1217 | activateCausedStack(causedStack, action, action_e, self); | - | ||||||
1218 | - | |||||||
1219 | - | |||||||
1220 | if (action_e == QAction::Hover) { | - | ||||||
1221 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||
1222 | if (QAccessible::isActive()) { | - | ||||||
1223 | int actionIndex = indexOf(action); | - | ||||||
1224 | QAccessibleEvent focusEvent(q, QAccessible::Focus); | - | ||||||
1225 | focusEvent.setChild(actionIndex); | - | ||||||
1226 | QAccessible::updateAccessibility(&focusEvent); | - | ||||||
1227 | } | - | ||||||
1228 | #endif | - | ||||||
1229 | action->showStatusText(topCausedWidget()); | - | ||||||
1230 | } else { | - | ||||||
1231 | actionAboutToTrigger = 0; | - | ||||||
1232 | } | - | ||||||
1233 | } | - | ||||||
1234 | - | |||||||
1235 | void QMenuPrivate::_q_actionTriggered() | - | ||||||
1236 | { | - | ||||||
1237 | Q_Q(QMenu); | - | ||||||
1238 | if (QAction *action = qobject_cast<QAction *>(q->sender())) { | - | ||||||
1239 | QPointer<QAction> actionGuard = action; | - | ||||||
1240 | if (platformMenu && widgetItems.value(action)) | - | ||||||
1241 | platformMenu->dismiss(); | - | ||||||
1242 | emit q->triggered(action); | - | ||||||
1243 | if (!activationRecursionGuard && actionGuard) { | - | ||||||
1244 | //in case the action has not been activated by the mouse | - | ||||||
1245 | //we check the parent hierarchy | - | ||||||
1246 | QVector< QPointer<QWidget> > list; | - | ||||||
1247 | for(QWidget *widget = q->parentWidget(); widget; ) { | - | ||||||
1248 | if (qobject_cast<QMenu*>(widget) | - | ||||||
1249 | #ifndef QT_NO_MENUBAR | - | ||||||
1250 | || qobject_cast<QMenuBar*>(widget) | - | ||||||
1251 | #endif | - | ||||||
1252 | ) { | - | ||||||
1253 | list.append(widget); | - | ||||||
1254 | widget = widget->parentWidget(); | - | ||||||
1255 | } else { | - | ||||||
1256 | break; | - | ||||||
1257 | } | - | ||||||
1258 | } | - | ||||||
1259 | activateCausedStack(list, action, QAction::Trigger, false); | - | ||||||
1260 | } | - | ||||||
1261 | } | - | ||||||
1262 | } | - | ||||||
1263 | - | |||||||
1264 | void QMenuPrivate::_q_actionHovered() | - | ||||||
1265 | { | - | ||||||
1266 | Q_Q(QMenu); | - | ||||||
1267 | if (QAction * action = qobject_cast<QAction *>(q->sender())) { | - | ||||||
1268 | emit q->hovered(action); | - | ||||||
1269 | } | - | ||||||
1270 | } | - | ||||||
1271 | - | |||||||
1272 | void QMenuPrivate::_q_platformMenuAboutToShow() | - | ||||||
1273 | { | - | ||||||
1274 | Q_Q(QMenu); | - | ||||||
1275 | - | |||||||
1276 | #ifdef Q_OS_OSX | - | ||||||
1277 | if (platformMenu) Q_FOREACH{ | - | ||||||
1278 | const auto actions = q->actions(); | - | ||||||
1279 | for (QAction *action , q->: actions())) { | - | ||||||
1280 | if (QWidget *widget = widgetItems.value(action)) | - | ||||||
1281 | if (widget->parent() == q) { | - | ||||||
1282 | QPlatformMenuItem *menuItem = platformMenu->menuItemForTag(reinterpret_cast<quintptr>(action)); | - | ||||||
1283 | moveWidgetToPlatformItem(widget, menuItem); | - | ||||||
1284 | platformMenu->syncMenuItem(menuItem); | - | ||||||
1285 | } | - | ||||||
1286 | } | - | ||||||
1287 | } | - | ||||||
1288 | #endif | - | ||||||
1289 | - | |||||||
1290 | emit q->aboutToShow(); | - | ||||||
1291 | } never executed: end of block | 0 | ||||||
1292 | - | |||||||
1293 | bool QMenuPrivate::hasMouseMoved(const QPoint &globalPos) | - | ||||||
1294 | { | - | ||||||
1295 | //determines if the mouse has moved (ie its initial position has | - | ||||||
1296 | //changed by more than QApplication::startDragDistance() | - | ||||||
1297 | //or if there were at least 6 mouse motions) | - | ||||||
1298 | return motions > 6 || | - | ||||||
1299 | QApplication::startDragDistance() < (mousePopupPos - globalPos).manhattanLength(); | - | ||||||
1300 | } | - | ||||||
1301 | - | |||||||
1302 | - | |||||||
1303 | /*! | - | ||||||
1304 | Initialize \a option with the values from this menu and information from \a action. This method | - | ||||||
1305 | is useful for subclasses when they need a QStyleOptionMenuItem, but don't want | - | ||||||
1306 | to fill in all the information themselves. | - | ||||||
1307 | - | |||||||
1308 | \sa QStyleOption::initFrom(), QMenuBar::initStyleOption() | - | ||||||
1309 | */ | - | ||||||
1310 | void QMenu::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const | - | ||||||
1311 | { | - | ||||||
1312 | if (!option || !action) | - | ||||||
1313 | return; | - | ||||||
1314 | - | |||||||
1315 | Q_D(const QMenu); | - | ||||||
1316 | option->initFrom(this); | - | ||||||
1317 | option->palette = palette(); | - | ||||||
1318 | option->state = QStyle::State_None; | - | ||||||
1319 | - | |||||||
1320 | if (window()->isActiveWindow()) | - | ||||||
1321 | option->state |= QStyle::State_Active; | - | ||||||
1322 | if (isEnabled() && action->isEnabled() | - | ||||||
1323 | && (!action->menu() || action->menu()->isEnabled())) | - | ||||||
1324 | option->state |= QStyle::State_Enabled; | - | ||||||
1325 | else | - | ||||||
1326 | option->palette.setCurrentColorGroup(QPalette::Disabled); | - | ||||||
1327 | - | |||||||
1328 | option->font = action->font().resolve(font()); | - | ||||||
1329 | option->fontMetrics = QFontMetrics(option->font); | - | ||||||
1330 | - | |||||||
1331 | if (d->currentAction && d->currentAction == action && !d->currentAction->isSeparator()) { | - | ||||||
1332 | option->state |= QStyle::State_Selected | - | ||||||
1333 | | (d->mouseDown ? QStyle::State_Sunken : QStyle::State_None); | - | ||||||
1334 | } | - | ||||||
1335 | - | |||||||
1336 | option->menuHasCheckableItems = d->hasCheckableItems; | - | ||||||
1337 | if (!action->isCheckable()) { | - | ||||||
1338 | option->checkType = QStyleOptionMenuItem::NotCheckable; | - | ||||||
1339 | } else { | - | ||||||
1340 | option->checkType = (action->actionGroup() && action->actionGroup()->isExclusive()) | - | ||||||
1341 | ? QStyleOptionMenuItem::Exclusive : QStyleOptionMenuItem::NonExclusive; | - | ||||||
1342 | option->checked = action->isChecked(); | - | ||||||
1343 | } | - | ||||||
1344 | if (action->menu()) | - | ||||||
1345 | option->menuItemType = QStyleOptionMenuItem::SubMenu; | - | ||||||
1346 | else if (action->isSeparator()) | - | ||||||
1347 | option->menuItemType = QStyleOptionMenuItem::Separator; | - | ||||||
1348 | else if (d->defaultAction == action) | - | ||||||
1349 | option->menuItemType = QStyleOptionMenuItem::DefaultItem; | - | ||||||
1350 | else | - | ||||||
1351 | option->menuItemType = QStyleOptionMenuItem::Normal; | - | ||||||
1352 | if (action->isIconVisibleInMenu()) | - | ||||||
1353 | option->icon = action->icon(); | - | ||||||
1354 | QString textAndAccel = action->text(); | - | ||||||
1355 | #ifndef QT_NO_SHORTCUT | - | ||||||
1356 | if (textAndAccel.indexOf(QLatin1Char('\t')) == -1) { | - | ||||||
1357 | QKeySequence seq = action->shortcut(); | - | ||||||
1358 | if (!seq.isEmpty()) | - | ||||||
1359 | textAndAccel += QLatin1Char('\t') + seq.toString(QKeySequence::NativeText); | - | ||||||
1360 | } | - | ||||||
1361 | #endif | - | ||||||
1362 | option->text = textAndAccel; | - | ||||||
1363 | option->tabWidth = d->tabWidth; | - | ||||||
1364 | option->maxIconWidth = d->maxIconWidth; | - | ||||||
1365 | option->menuRect = rect(); | - | ||||||
1366 | } | - | ||||||
1367 | - | |||||||
1368 | /*! | - | ||||||
1369 | \class QMenu | - | ||||||
1370 | \brief The QMenu class provides a menu widget for use in menu | - | ||||||
1371 | bars, context menus, and other popup menus. | - | ||||||
1372 | - | |||||||
1373 | \ingroup mainwindow-classes | - | ||||||
1374 | \ingroup basicwidgets | - | ||||||
1375 | \inmodule QtWidgets | - | ||||||
1376 | - | |||||||
1377 | A menu widget is a selection menu. It can be either a pull-down | - | ||||||
1378 | menu in a menu bar or a standalone context menu. Pull-down menus | - | ||||||
1379 | are shown by the menu bar when the user clicks on the respective | - | ||||||
1380 | item or presses the specified shortcut key. Use | - | ||||||
1381 | QMenuBar::addMenu() to insert a menu into a menu bar. Context | - | ||||||
1382 | menus are usually invoked by some special keyboard key or by | - | ||||||
1383 | right-clicking. They can be executed either asynchronously with | - | ||||||
1384 | popup() or synchronously with exec(). Menus can also be invoked in | - | ||||||
1385 | response to button presses; these are just like context menus | - | ||||||
1386 | except for how they are invoked. | - | ||||||
1387 | - | |||||||
1388 | \table 100% | - | ||||||
1389 | \row | - | ||||||
1390 | \li \inlineimage fusion-menu.png | - | ||||||
1391 | \li \inlineimage windowsxp-menu.png | - | ||||||
1392 | \li \inlineimage macintosh-menu.png | - | ||||||
1393 | \endtable | - | ||||||
1394 | \caption Fig. A menu shown in \l{Fusion Style Widget Gallery}{Fusion widget style}, | - | ||||||
1395 | \l{Windows XP Style Widget Gallery}{Windows XP widget style}, | - | ||||||
1396 | and \l{Macintosh Style Widget Gallery}{Macintosh widget style}. | - | ||||||
1397 | - | |||||||
1398 | \section1 Actions | - | ||||||
1399 | - | |||||||
1400 | A menu consists of a list of action items. Actions are added with | - | ||||||
1401 | the addAction(), addActions() and insertAction() functions. An action | - | ||||||
1402 | is represented vertically and rendered by QStyle. In addition, actions | - | ||||||
1403 | can have a text label, an optional icon drawn on the very left side, | - | ||||||
1404 | and shortcut key sequence such as "Ctrl+X". | - | ||||||
1405 | - | |||||||
1406 | The existing actions held by a menu can be found with actions(). | - | ||||||
1407 | - | |||||||
1408 | There are four kinds of action items: separators, actions that | - | ||||||
1409 | show a submenu, widgets, and actions that perform an action. | - | ||||||
1410 | Separators are inserted with addSeparator(), submenus with addMenu(), | - | ||||||
1411 | and all other items are considered action items. | - | ||||||
1412 | - | |||||||
1413 | When inserting action items you usually specify a receiver and a | - | ||||||
1414 | slot. The receiver will be notifed whenever the item is | - | ||||||
1415 | \l{QAction::triggered()}{triggered()}. In addition, QMenu provides | - | ||||||
1416 | two signals, activated() and highlighted(), which signal the | - | ||||||
1417 | QAction that was triggered from the menu. | - | ||||||
1418 | - | |||||||
1419 | You clear a menu with clear() and remove individual action items | - | ||||||
1420 | with removeAction(). | - | ||||||
1421 | - | |||||||
1422 | A QMenu can also provide a tear-off menu. A tear-off menu is a | - | ||||||
1423 | top-level window that contains a copy of the menu. This makes it | - | ||||||
1424 | possible for the user to "tear off" frequently used menus and | - | ||||||
1425 | position them in a convenient place on the screen. If you want | - | ||||||
1426 | this functionality for a particular menu, insert a tear-off handle | - | ||||||
1427 | with setTearOffEnabled(). When using tear-off menus, bear in mind | - | ||||||
1428 | that the concept isn't typically used on Microsoft Windows so | - | ||||||
1429 | some users may not be familiar with it. Consider using a QToolBar | - | ||||||
1430 | instead. | - | ||||||
1431 | - | |||||||
1432 | Widgets can be inserted into menus with the QWidgetAction class. | - | ||||||
1433 | Instances of this class are used to hold widgets, and are inserted | - | ||||||
1434 | into menus with the addAction() overload that takes a QAction. | - | ||||||
1435 | - | |||||||
1436 | Conversely, actions can be added to widgets with the addAction(), | - | ||||||
1437 | addActions() and insertAction() functions. | - | ||||||
1438 | - | |||||||
1439 | \warning To make QMenu visible on the screen, exec() or popup() should be | - | ||||||
1440 | used instead of show(). | - | ||||||
1441 | - | |||||||
1442 | \section1 QMenu on \macos with Qt Build Against Cocoa | - | ||||||
1443 | - | |||||||
1444 | QMenu can be inserted only once in a menu/menubar. Subsequent insertions will | - | ||||||
1445 | have no effect or will result in a disabled menu item. | - | ||||||
1446 | - | |||||||
1447 | See the \l{mainwindows/menus}{Menus} example for an example of how | - | ||||||
1448 | to use QMenuBar and QMenu in your application. | - | ||||||
1449 | - | |||||||
1450 | \b{Important inherited functions:} addAction(), removeAction(), clear(), | - | ||||||
1451 | addSeparator(), and addMenu(). | - | ||||||
1452 | - | |||||||
1453 | \sa QMenuBar, {fowler}{GUI Design Handbook: Menu, Drop-Down and Pop-Up}, | - | ||||||
1454 | {Application Example}, {Menus Example} | - | ||||||
1455 | */ | - | ||||||
1456 | - | |||||||
1457 | - | |||||||
1458 | /*! | - | ||||||
1459 | Constructs a menu with parent \a parent. | - | ||||||
1460 | - | |||||||
1461 | Although a popup menu is always a top-level widget, if a parent is | - | ||||||
1462 | passed the popup menu will be deleted when that parent is | - | ||||||
1463 | destroyed (as with any other QObject). | - | ||||||
1464 | */ | - | ||||||
1465 | QMenu::QMenu(QWidget *parent) | - | ||||||
1466 | : QWidget(*new QMenuPrivate, parent, Qt::Popup) | - | ||||||
1467 | { | - | ||||||
1468 | Q_D(QMenu); | - | ||||||
1469 | d->init(); | - | ||||||
1470 | } | - | ||||||
1471 | - | |||||||
1472 | /*! | - | ||||||
1473 | Constructs a menu with a \a title and a \a parent. | - | ||||||
1474 | - | |||||||
1475 | Although a popup menu is always a top-level widget, if a parent is | - | ||||||
1476 | passed the popup menu will be deleted when that parent is | - | ||||||
1477 | destroyed (as with any other QObject). | - | ||||||
1478 | - | |||||||
1479 | \sa title | - | ||||||
1480 | */ | - | ||||||
1481 | QMenu::QMenu(const QString &title, QWidget *parent) | - | ||||||
1482 | : QWidget(*new QMenuPrivate, parent, Qt::Popup) | - | ||||||
1483 | { | - | ||||||
1484 | Q_D(QMenu); | - | ||||||
1485 | d->init(); | - | ||||||
1486 | d->menuAction->setText(title); | - | ||||||
1487 | } | - | ||||||
1488 | - | |||||||
1489 | /*! \internal | - | ||||||
1490 | */ | - | ||||||
1491 | QMenu::QMenu(QMenuPrivate &dd, QWidget *parent) | - | ||||||
1492 | : QWidget(dd, parent, Qt::Popup) | - | ||||||
1493 | { | - | ||||||
1494 | Q_D(QMenu); | - | ||||||
1495 | d->init(); | - | ||||||
1496 | } | - | ||||||
1497 | - | |||||||
1498 | /*! | - | ||||||
1499 | Destroys the menu. | - | ||||||
1500 | */ | - | ||||||
1501 | QMenu::~QMenu() | - | ||||||
1502 | { | - | ||||||
1503 | Q_D(QMenu); | - | ||||||
1504 | if (!d->widgetItems.isEmpty()) { // avoid detach on shared null hash | - | ||||||
1505 | QHash<QAction *, QWidget *>::iterator it = d->widgetItems.begin(); | - | ||||||
1506 | for (; it != d->widgetItems.end(); ++it) { | - | ||||||
1507 | if (QWidget *widget = it.value()) { | - | ||||||
1508 | QWidgetAction *action = static_cast<QWidgetAction *>(it.key()); | - | ||||||
1509 | action->releaseWidget(widget); | - | ||||||
1510 | *it = 0; | - | ||||||
1511 | } | - | ||||||
1512 | } | - | ||||||
1513 | } | - | ||||||
1514 | - | |||||||
1515 | if (d->eventLoop) | - | ||||||
1516 | d->eventLoop->exit(); | - | ||||||
1517 | hideTearOffMenu(); | - | ||||||
1518 | } | - | ||||||
1519 | - | |||||||
1520 | /*! | - | ||||||
1521 | \overload | - | ||||||
1522 | - | |||||||
1523 | This convenience function creates a new action with \a text. | - | ||||||
1524 | The function adds the newly created action to the menu's | - | ||||||
1525 | list of actions, and returns it. | - | ||||||
1526 | - | |||||||
1527 | QMenu takes ownership of the returned QAction. | - | ||||||
1528 | - | |||||||
1529 | \sa QWidget::addAction() | - | ||||||
1530 | */ | - | ||||||
1531 | QAction *QMenu::addAction(const QString &text) | - | ||||||
1532 | { | - | ||||||
1533 | QAction *ret = new QAction(text, this); | - | ||||||
1534 | addAction(ret); | - | ||||||
1535 | return ret; | - | ||||||
1536 | } | - | ||||||
1537 | - | |||||||
1538 | /*! | - | ||||||
1539 | \overload | - | ||||||
1540 | - | |||||||
1541 | This convenience function creates a new action with an \a icon | - | ||||||
1542 | and some \a text. The function adds the newly created action to | - | ||||||
1543 | the menu's list of actions, and returns it. | - | ||||||
1544 | - | |||||||
1545 | QMenu takes ownership of the returned QAction. | - | ||||||
1546 | - | |||||||
1547 | \sa QWidget::addAction() | - | ||||||
1548 | */ | - | ||||||
1549 | QAction *QMenu::addAction(const QIcon &icon, const QString &text) | - | ||||||
1550 | { | - | ||||||
1551 | QAction *ret = new QAction(icon, text, this); | - | ||||||
1552 | addAction(ret); | - | ||||||
1553 | return ret; | - | ||||||
1554 | } | - | ||||||
1555 | - | |||||||
1556 | /*! | - | ||||||
1557 | \overload | - | ||||||
1558 | - | |||||||
1559 | This convenience function creates a new action with the text \a | - | ||||||
1560 | text and an optional shortcut \a shortcut. The action's | - | ||||||
1561 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1562 | \a receiver's \a member slot. The function adds the newly created | - | ||||||
1563 | action to the menu's list of actions and returns it. | - | ||||||
1564 | - | |||||||
1565 | QMenu takes ownership of the returned QAction. | - | ||||||
1566 | - | |||||||
1567 | \sa QWidget::addAction() | - | ||||||
1568 | */ | - | ||||||
1569 | QAction *QMenu::addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut) | - | ||||||
1570 | { | - | ||||||
1571 | QAction *action = new QAction(text, this); | - | ||||||
1572 | #ifdef QT_NO_SHORTCUT | - | ||||||
1573 | Q_UNUSED(shortcut); | - | ||||||
1574 | #else | - | ||||||
1575 | action->setShortcut(shortcut); | - | ||||||
1576 | #endif | - | ||||||
1577 | QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); | - | ||||||
1578 | addAction(action); | - | ||||||
1579 | return action; | - | ||||||
1580 | } | - | ||||||
1581 | - | |||||||
1582 | /*!\fn QAction *QMenu::addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0) | - | ||||||
1583 | - | |||||||
1584 | \since 5.6 | - | ||||||
1585 | - | |||||||
1586 | \overload | - | ||||||
1587 | - | |||||||
1588 | This convenience function creates a new action with the text \a | - | ||||||
1589 | text and an optional shortcut \a shortcut. The action's | - | ||||||
1590 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1591 | \a method of the \a receiver. The function adds the newly created | - | ||||||
1592 | action to the menu's list of actions and returns it. | - | ||||||
1593 | - | |||||||
1594 | QMenu takes ownership of the returned QAction. | - | ||||||
1595 | */ | - | ||||||
1596 | - | |||||||
1597 | /*!\fn QAction *QMenu::addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0) | - | ||||||
1598 | - | |||||||
1599 | \since 5.6 | - | ||||||
1600 | - | |||||||
1601 | \overload | - | ||||||
1602 | - | |||||||
1603 | This convenience function creates a new action with the text \a | - | ||||||
1604 | text and an optional shortcut \a shortcut. The action's | - | ||||||
1605 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1606 | \a functor. The function adds the newly created | - | ||||||
1607 | action to the menu's list of actions and returns it. | - | ||||||
1608 | - | |||||||
1609 | QMenu takes ownership of the returned QAction. | - | ||||||
1610 | */ | - | ||||||
1611 | - | |||||||
1612 | /*!\fn QAction *QMenu::addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0) | - | ||||||
1613 | - | |||||||
1614 | \since 5.6 | - | ||||||
1615 | - | |||||||
1616 | \overload | - | ||||||
1617 | - | |||||||
1618 | This convenience function creates a new action with the text \a | - | ||||||
1619 | text and an optional shortcut \a shortcut. The action's | - | ||||||
1620 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1621 | \a functor. The function adds the newly created | - | ||||||
1622 | action to the menu's list of actions and returns it. | - | ||||||
1623 | - | |||||||
1624 | If \a context is destroyed, the functor will not be called. | - | ||||||
1625 | - | |||||||
1626 | QMenu takes ownership of the returned QAction. | - | ||||||
1627 | */ | - | ||||||
1628 | - | |||||||
1629 | /*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0) | - | ||||||
1630 | - | |||||||
1631 | \since 5.6 | - | ||||||
1632 | - | |||||||
1633 | \overload | - | ||||||
1634 | - | |||||||
1635 | This convenience function creates a new action with an \a icon | - | ||||||
1636 | and some \a text and an optional shortcut \a shortcut. The action's | - | ||||||
1637 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1638 | \a method of the \a receiver. The function adds the newly created | - | ||||||
1639 | action to the menu's list of actions and returns it. | - | ||||||
1640 | - | |||||||
1641 | QMenu takes ownership of the returned QAction. | - | ||||||
1642 | */ | - | ||||||
1643 | - | |||||||
1644 | /*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0) | - | ||||||
1645 | - | |||||||
1646 | \since 5.6 | - | ||||||
1647 | - | |||||||
1648 | \overload | - | ||||||
1649 | - | |||||||
1650 | This convenience function creates a new action with an \a icon | - | ||||||
1651 | and some \a text and an optional shortcut \a shortcut. The action's | - | ||||||
1652 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1653 | \a functor. The function adds the newly created | - | ||||||
1654 | action to the menu's list of actions and returns it. | - | ||||||
1655 | - | |||||||
1656 | QMenu takes ownership of the returned QAction. | - | ||||||
1657 | */ | - | ||||||
1658 | - | |||||||
1659 | /*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0) | - | ||||||
1660 | - | |||||||
1661 | \since 5.6 | - | ||||||
1662 | - | |||||||
1663 | \overload | - | ||||||
1664 | - | |||||||
1665 | This convenience function creates a new action with an \a icon | - | ||||||
1666 | and some \a text and an optional shortcut \a shortcut. The action's | - | ||||||
1667 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1668 | \a functor. The function adds the newly created | - | ||||||
1669 | action to the menu's list of actions and returns it. | - | ||||||
1670 | - | |||||||
1671 | If \a context is destroyed, the functor will not be called. | - | ||||||
1672 | - | |||||||
1673 | QMenu takes ownership of the returned QAction. | - | ||||||
1674 | */ | - | ||||||
1675 | - | |||||||
1676 | /*! | - | ||||||
1677 | \overload | - | ||||||
1678 | - | |||||||
1679 | This convenience function creates a new action with an \a icon and | - | ||||||
1680 | some \a text and an optional shortcut \a shortcut. The action's | - | ||||||
1681 | \l{QAction::triggered()}{triggered()} signal is connected to the | - | ||||||
1682 | \a member slot of the \a receiver object. The function adds the | - | ||||||
1683 | newly created action to the menu's list of actions, and returns it. | - | ||||||
1684 | - | |||||||
1685 | QMenu takes ownership of the returned QAction. | - | ||||||
1686 | - | |||||||
1687 | \sa QWidget::addAction() | - | ||||||
1688 | */ | - | ||||||
1689 | QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, | - | ||||||
1690 | const char* member, const QKeySequence &shortcut) | - | ||||||
1691 | { | - | ||||||
1692 | QAction *action = new QAction(icon, text, this); | - | ||||||
1693 | #ifdef QT_NO_SHORTCUT | - | ||||||
1694 | Q_UNUSED(shortcut); | - | ||||||
1695 | #else | - | ||||||
1696 | action->setShortcut(shortcut); | - | ||||||
1697 | #endif | - | ||||||
1698 | QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); | - | ||||||
1699 | addAction(action); | - | ||||||
1700 | return action; | - | ||||||
1701 | } | - | ||||||
1702 | - | |||||||
1703 | /*! | - | ||||||
1704 | This convenience function adds \a menu as a submenu to this menu. | - | ||||||
1705 | It returns \a menu's menuAction(). This menu does not take | - | ||||||
1706 | ownership of \a menu. | - | ||||||
1707 | - | |||||||
1708 | \sa QWidget::addAction(), QMenu::menuAction() | - | ||||||
1709 | */ | - | ||||||
1710 | QAction *QMenu::addMenu(QMenu *menu) | - | ||||||
1711 | { | - | ||||||
1712 | QAction *action = menu->menuAction(); | - | ||||||
1713 | addAction(action); | - | ||||||
1714 | return action; | - | ||||||
1715 | } | - | ||||||
1716 | - | |||||||
1717 | /*! | - | ||||||
1718 | Appends a new QMenu with \a title to the menu. The menu | - | ||||||
1719 | takes ownership of the menu. Returns the new menu. | - | ||||||
1720 | - | |||||||
1721 | \sa QWidget::addAction(), QMenu::menuAction() | - | ||||||
1722 | */ | - | ||||||
1723 | QMenu *QMenu::addMenu(const QString &title) | - | ||||||
1724 | { | - | ||||||
1725 | QMenu *menu = new QMenu(title, this); | - | ||||||
1726 | addAction(menu->menuAction()); | - | ||||||
1727 | return menu; | - | ||||||
1728 | } | - | ||||||
1729 | - | |||||||
1730 | /*! | - | ||||||
1731 | Appends a new QMenu with \a icon and \a title to the menu. The menu | - | ||||||
1732 | takes ownership of the menu. Returns the new menu. | - | ||||||
1733 | - | |||||||
1734 | \sa QWidget::addAction(), QMenu::menuAction() | - | ||||||
1735 | */ | - | ||||||
1736 | QMenu *QMenu::addMenu(const QIcon &icon, const QString &title) | - | ||||||
1737 | { | - | ||||||
1738 | QMenu *menu = new QMenu(title, this); | - | ||||||
1739 | menu->setIcon(icon); | - | ||||||
1740 | addAction(menu->menuAction()); | - | ||||||
1741 | return menu; | - | ||||||
1742 | } | - | ||||||
1743 | - | |||||||
1744 | /*! | - | ||||||
1745 | This convenience function creates a new separator action, i.e. an | - | ||||||
1746 | action with QAction::isSeparator() returning true, and adds the new | - | ||||||
1747 | action to this menu's list of actions. It returns the newly | - | ||||||
1748 | created action. | - | ||||||
1749 | - | |||||||
1750 | QMenu takes ownership of the returned QAction. | - | ||||||
1751 | - | |||||||
1752 | \sa QWidget::addAction() | - | ||||||
1753 | */ | - | ||||||
1754 | QAction *QMenu::addSeparator() | - | ||||||
1755 | { | - | ||||||
1756 | QAction *action = new QAction(this); | - | ||||||
1757 | action->setSeparator(true); | - | ||||||
1758 | addAction(action); | - | ||||||
1759 | return action; | - | ||||||
1760 | } | - | ||||||
1761 | - | |||||||
1762 | /*! | - | ||||||
1763 | \since 5.1 | - | ||||||
1764 | - | |||||||
1765 | This convenience function creates a new section action, i.e. an | - | ||||||
1766 | action with QAction::isSeparator() returning true but also | - | ||||||
1767 | having \a text hint, and adds the new action to this menu's list | - | ||||||
1768 | of actions. It returns the newly created action. | - | ||||||
1769 | - | |||||||
1770 | The rendering of the hint is style and platform dependent. Widget | - | ||||||
1771 | styles can use the text information in the rendering for sections, | - | ||||||
1772 | or can choose to ignore it and render sections like simple separators. | - | ||||||
1773 | - | |||||||
1774 | QMenu takes ownership of the returned QAction. | - | ||||||
1775 | - | |||||||
1776 | \sa QWidget::addAction() | - | ||||||
1777 | */ | - | ||||||
1778 | QAction *QMenu::addSection(const QString &text) | - | ||||||
1779 | { | - | ||||||
1780 | QAction *action = new QAction(text, this); | - | ||||||
1781 | action->setSeparator(true); | - | ||||||
1782 | addAction(action); | - | ||||||
1783 | return action; | - | ||||||
1784 | } | - | ||||||
1785 | - | |||||||
1786 | /*! | - | ||||||
1787 | \since 5.1 | - | ||||||
1788 | - | |||||||
1789 | This convenience function creates a new section action, i.e. an | - | ||||||
1790 | action with QAction::isSeparator() returning true but also | - | ||||||
1791 | having \a text and \a icon hints, and adds the new action to this menu's | - | ||||||
1792 | list of actions. It returns the newly created action. | - | ||||||
1793 | - | |||||||
1794 | The rendering of the hints is style and platform dependent. Widget | - | ||||||
1795 | styles can use the text and icon information in the rendering for sections, | - | ||||||
1796 | or can choose to ignore them and render sections like simple separators. | - | ||||||
1797 | - | |||||||
1798 | QMenu takes ownership of the returned QAction. | - | ||||||
1799 | - | |||||||
1800 | \sa QWidget::addAction() | - | ||||||
1801 | */ | - | ||||||
1802 | QAction *QMenu::addSection(const QIcon &icon, const QString &text) | - | ||||||
1803 | { | - | ||||||
1804 | QAction *action = new QAction(icon, text, this); | - | ||||||
1805 | action->setSeparator(true); | - | ||||||
1806 | addAction(action); | - | ||||||
1807 | return action; | - | ||||||
1808 | } | - | ||||||
1809 | - | |||||||
1810 | /*! | - | ||||||
1811 | This convenience function inserts \a menu before action \a before | - | ||||||
1812 | and returns the menus menuAction(). | - | ||||||
1813 | - | |||||||
1814 | \sa QWidget::insertAction(), addMenu() | - | ||||||
1815 | */ | - | ||||||
1816 | QAction *QMenu::insertMenu(QAction *before, QMenu *menu) | - | ||||||
1817 | { | - | ||||||
1818 | QAction *action = menu->menuAction(); | - | ||||||
1819 | insertAction(before, action); | - | ||||||
1820 | return action; | - | ||||||
1821 | } | - | ||||||
1822 | - | |||||||
1823 | /*! | - | ||||||
1824 | This convenience function creates a new separator action, i.e. an | - | ||||||
1825 | action with QAction::isSeparator() returning true. The function inserts | - | ||||||
1826 | the newly created action into this menu's list of actions before | - | ||||||
1827 | action \a before and returns it. | - | ||||||
1828 | - | |||||||
1829 | QMenu takes ownership of the returned QAction. | - | ||||||
1830 | - | |||||||
1831 | \sa QWidget::insertAction(), addSeparator() | - | ||||||
1832 | */ | - | ||||||
1833 | QAction *QMenu::insertSeparator(QAction *before) | - | ||||||
1834 | { | - | ||||||
1835 | QAction *action = new QAction(this); | - | ||||||
1836 | action->setSeparator(true); | - | ||||||
1837 | insertAction(before, action); | - | ||||||
1838 | return action; | - | ||||||
1839 | } | - | ||||||
1840 | - | |||||||
1841 | /*! | - | ||||||
1842 | \since 5.1 | - | ||||||
1843 | - | |||||||
1844 | This convenience function creates a new title action, i.e. an | - | ||||||
1845 | action with QAction::isSeparator() returning true but also having | - | ||||||
1846 | \a text hint. The function inserts the newly created action | - | ||||||
1847 | into this menu's list of actions before action \a before and | - | ||||||
1848 | returns it. | - | ||||||
1849 | - | |||||||
1850 | The rendering of the hint is style and platform dependent. Widget | - | ||||||
1851 | styles can use the text information in the rendering for sections, | - | ||||||
1852 | or can choose to ignore it and render sections like simple separators. | - | ||||||
1853 | - | |||||||
1854 | QMenu takes ownership of the returned QAction. | - | ||||||
1855 | - | |||||||
1856 | \sa QWidget::insertAction(), addSection() | - | ||||||
1857 | */ | - | ||||||
1858 | QAction *QMenu::insertSection(QAction *before, const QString &text) | - | ||||||
1859 | { | - | ||||||
1860 | QAction *action = new QAction(text, this); | - | ||||||
1861 | action->setSeparator(true); | - | ||||||
1862 | insertAction(before, action); | - | ||||||
1863 | return action; | - | ||||||
1864 | } | - | ||||||
1865 | - | |||||||
1866 | /*! | - | ||||||
1867 | \since 5.1 | - | ||||||
1868 | - | |||||||
1869 | This convenience function creates a new title action, i.e. an | - | ||||||
1870 | action with QAction::isSeparator() returning true but also having | - | ||||||
1871 | \a text and \a icon hints. The function inserts the newly created action | - | ||||||
1872 | into this menu's list of actions before action \a before and returns it. | - | ||||||
1873 | - | |||||||
1874 | The rendering of the hints is style and platform dependent. Widget | - | ||||||
1875 | styles can use the text and icon information in the rendering for sections, | - | ||||||
1876 | or can choose to ignore them and render sections like simple separators. | - | ||||||
1877 | - | |||||||
1878 | QMenu takes ownership of the returned QAction. | - | ||||||
1879 | - | |||||||
1880 | \sa QWidget::insertAction(), addSection() | - | ||||||
1881 | */ | - | ||||||
1882 | QAction *QMenu::insertSection(QAction *before, const QIcon &icon, const QString &text) | - | ||||||
1883 | { | - | ||||||
1884 | QAction *action = new QAction(icon, text, this); | - | ||||||
1885 | action->setSeparator(true); | - | ||||||
1886 | insertAction(before, action); | - | ||||||
1887 | return action; | - | ||||||
1888 | } | - | ||||||
1889 | - | |||||||
1890 | /*! | - | ||||||
1891 | This sets the default action to \a act. The default action may have | - | ||||||
1892 | a visual cue, depending on the current QStyle. A default action | - | ||||||
1893 | usually indicates what will happen by default when a drop occurs. | - | ||||||
1894 | - | |||||||
1895 | \sa defaultAction() | - | ||||||
1896 | */ | - | ||||||
1897 | void QMenu::setDefaultAction(QAction *act) | - | ||||||
1898 | { | - | ||||||
1899 | d_func()->defaultAction = act; | - | ||||||
1900 | } | - | ||||||
1901 | - | |||||||
1902 | /*! | - | ||||||
1903 | Returns the current default action. | - | ||||||
1904 | - | |||||||
1905 | \sa setDefaultAction() | - | ||||||
1906 | */ | - | ||||||
1907 | QAction *QMenu::defaultAction() const | - | ||||||
1908 | { | - | ||||||
1909 | return d_func()->defaultAction; | - | ||||||
1910 | } | - | ||||||
1911 | - | |||||||
1912 | /*! | - | ||||||
1913 | \property QMenu::tearOffEnabled | - | ||||||
1914 | \brief whether the menu supports being torn off | - | ||||||
1915 | - | |||||||
1916 | When true, the menu contains a special tear-off item (often shown as a dashed | - | ||||||
1917 | line at the top of the menu) that creates a copy of the menu when it is | - | ||||||
1918 | triggered. | - | ||||||
1919 | - | |||||||
1920 | This "torn-off" copy lives in a separate window. It contains the same menu | - | ||||||
1921 | items as the original menu, with the exception of the tear-off handle. | - | ||||||
1922 | - | |||||||
1923 | By default, this property is \c false. | - | ||||||
1924 | */ | - | ||||||
1925 | void QMenu::setTearOffEnabled(bool b) | - | ||||||
1926 | { | - | ||||||
1927 | Q_D(QMenu); | - | ||||||
1928 | if (d->tearoff == b) | - | ||||||
1929 | return; | - | ||||||
1930 | if (!b) | - | ||||||
1931 | hideTearOffMenu(); | - | ||||||
1932 | d->tearoff = b; | - | ||||||
1933 | - | |||||||
1934 | d->itemsDirty = true; | - | ||||||
1935 | if (isVisible()) | - | ||||||
1936 | resize(sizeHint()); | - | ||||||
1937 | } | - | ||||||
1938 | - | |||||||
1939 | bool QMenu::isTearOffEnabled() const | - | ||||||
1940 | { | - | ||||||
1941 | return d_func()->tearoff; | - | ||||||
1942 | } | - | ||||||
1943 | - | |||||||
1944 | /*! | - | ||||||
1945 | When a menu is torn off a second menu is shown to display the menu | - | ||||||
1946 | contents in a new window. When the menu is in this mode and the menu | - | ||||||
1947 | is visible returns \c true; otherwise false. | - | ||||||
1948 | - | |||||||
1949 | \sa showTearOffMenu(), hideTearOffMenu(), isTearOffEnabled() | - | ||||||
1950 | */ | - | ||||||
1951 | bool QMenu::isTearOffMenuVisible() const | - | ||||||
1952 | { | - | ||||||
1953 | if (d_func()->tornPopup) | - | ||||||
1954 | return d_func()->tornPopup->isVisible(); | - | ||||||
1955 | return false; | - | ||||||
1956 | } | - | ||||||
1957 | - | |||||||
1958 | /*! | - | ||||||
1959 | \since 5.7 | - | ||||||
1960 | - | |||||||
1961 | This function will forcibly show the torn off menu making it | - | ||||||
1962 | appear on the user's desktop at the specified \e global position \a pos. | - | ||||||
1963 | - | |||||||
1964 | \sa hideTearOffMenu(), isTearOffMenuVisible(), isTearOffEnabled() | - | ||||||
1965 | */ | - | ||||||
1966 | void QMenu::showTearOffMenu(const QPoint &pos) | - | ||||||
1967 | { | - | ||||||
1968 | Q_D(QMenu); | - | ||||||
1969 | if (!d->tornPopup)
| 0 | ||||||
1970 | d->tornPopup = new QTornOffMenu(this); never executed: d->tornPopup = new QTornOffMenu(this); | 0 | ||||||
1971 | const QSize &s = sizeHint(); | - | ||||||
1972 | d->tornPopup->setGeometry(pos.x(), pos.y(), s.width(), s.height()); | - | ||||||
1973 | d->tornPopup->show(); | - | ||||||
1974 | } never executed: end of block | 0 | ||||||
1975 | - | |||||||
1976 | /*! | - | ||||||
1977 | \overload | - | ||||||
1978 | \since 5.7 | - | ||||||
1979 | - | |||||||
1980 | This function will forcibly show the torn off menu making it | - | ||||||
1981 | appear on the user's desktop under the mouse currsor. | - | ||||||
1982 | - | |||||||
1983 | \sa hideTearOffMenu(), isTearOffMenuVisible(), isTearOffEnabled() | - | ||||||
1984 | */ | - | ||||||
1985 | void QMenu::showTearOffMenu() | - | ||||||
1986 | { | - | ||||||
1987 | showTearOffMenu(QCursor::pos()); | - | ||||||
1988 | } never executed: end of block | 0 | ||||||
1989 | - | |||||||
1990 | /*! | - | ||||||
1991 | This function will forcibly hide the torn off menu making it | - | ||||||
1992 | disappear from the user's desktop. | - | ||||||
1993 | - | |||||||
1994 | \sa showTearOffMenu(), isTearOffMenuVisible(), isTearOffEnabled() | - | ||||||
1995 | */ | - | ||||||
1996 | void QMenu::hideTearOffMenu() | - | ||||||
1997 | { | - | ||||||
1998 | Q_D(QMenu); | - | ||||||
1999 | if (QWidget *w = d_func()->d->tornPopup) w{
| 0 | ||||||
2000 | d->tornPopup->close(); | - | ||||||
2001 | // QTornOffMenu sets WA_DeleteOnClose, so we | - | ||||||
2002 | // should consider the torn-off menu deleted. | - | ||||||
2003 | // This way showTearOffMenu() will not try to | - | ||||||
2004 | // reuse the dying torn-off menu. | - | ||||||
2005 | d->tornPopup = Q_NULLPTR; | - | ||||||
2006 | } never executed: end of block | 0 | ||||||
2007 | } never executed: end of block | 0 | ||||||
2008 | - | |||||||
2009 | - | |||||||
2010 | /*! | - | ||||||
2011 | Sets the currently highlighted action to \a act. | - | ||||||
2012 | */ | - | ||||||
2013 | void QMenu::setActiveAction(QAction *act) | - | ||||||
2014 | { | - | ||||||
2015 | Q_D(QMenu); | - | ||||||
2016 | d->setCurrentAction(act, 0); | - | ||||||
2017 | if (d->scroll) | - | ||||||
2018 | d->scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollCenter); | - | ||||||
2019 | } | - | ||||||
2020 | - | |||||||
2021 | - | |||||||
2022 | /*! | - | ||||||
2023 | Returns the currently highlighted action, or 0 if no | - | ||||||
2024 | action is currently highlighted. | - | ||||||
2025 | */ | - | ||||||
2026 | QAction *QMenu::activeAction() const | - | ||||||
2027 | { | - | ||||||
2028 | return d_func()->currentAction; | - | ||||||
2029 | } | - | ||||||
2030 | - | |||||||
2031 | /*! | - | ||||||
2032 | \since 4.2 | - | ||||||
2033 | - | |||||||
2034 | Returns \c true if there are no visible actions inserted into the menu, false | - | ||||||
2035 | otherwise. | - | ||||||
2036 | - | |||||||
2037 | \sa QWidget::actions() | - | ||||||
2038 | */ | - | ||||||
2039 | - | |||||||
2040 | bool QMenu::isEmpty() const | - | ||||||
2041 | { | - | ||||||
2042 | bool ret = true; | - | ||||||
2043 | for(int i = 0; ret && i < actions().count(); ++i) { | - | ||||||
2044 | const QAction *action = actions().at(i); | - | ||||||
2045 | if (!action->isSeparator() && action->isVisible()) { | - | ||||||
2046 | ret = false; | - | ||||||
2047 | } | - | ||||||
2048 | } | - | ||||||
2049 | return ret; | - | ||||||
2050 | } | - | ||||||
2051 | - | |||||||
2052 | /*! | - | ||||||
2053 | Removes all the menu's actions. Actions owned by the menu and not | - | ||||||
2054 | shown in any other widget are deleted. | - | ||||||
2055 | - | |||||||
2056 | \sa removeAction() | - | ||||||
2057 | */ | - | ||||||
2058 | void QMenu::clear() | - | ||||||
2059 | { | - | ||||||
2060 | QList<QAction*> acts = actions(); | - | ||||||
2061 | - | |||||||
2062 | for(int i = 0; i < acts.size(); i++) { | - | ||||||
2063 | removeAction(acts[i]); | - | ||||||
2064 | if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty()) | - | ||||||
2065 | delete acts[i]; | - | ||||||
2066 | } | - | ||||||
2067 | } | - | ||||||
2068 | - | |||||||
2069 | /*! | - | ||||||
2070 | If a menu does not fit on the screen it lays itself out so that it | - | ||||||
2071 | does fit. It is style dependent what layout means (for example, on | - | ||||||
2072 | Windows it will use multiple columns). | - | ||||||
2073 | - | |||||||
2074 | This functions returns the number of columns necessary. | - | ||||||
2075 | */ | - | ||||||
2076 | int QMenu::columnCount() const | - | ||||||
2077 | { | - | ||||||
2078 | return d_func()->ncols; | - | ||||||
2079 | } | - | ||||||
2080 | - | |||||||
2081 | /*! | - | ||||||
2082 | Returns the item at \a pt; returns 0 if there is no item there. | - | ||||||
2083 | */ | - | ||||||
2084 | QAction *QMenu::actionAt(const QPoint &pt) const | - | ||||||
2085 | { | - | ||||||
2086 | if (QAction *ret = d_func()->actionAt(pt)) | - | ||||||
2087 | return ret; | - | ||||||
2088 | return 0; | - | ||||||
2089 | } | - | ||||||
2090 | - | |||||||
2091 | /*! | - | ||||||
2092 | Returns the geometry of action \a act. | - | ||||||
2093 | */ | - | ||||||
2094 | QRect QMenu::actionGeometry(QAction *act) const | - | ||||||
2095 | { | - | ||||||
2096 | return d_func()->actionRect(act); | - | ||||||
2097 | } | - | ||||||
2098 | - | |||||||
2099 | /*! | - | ||||||
2100 | \reimp | - | ||||||
2101 | */ | - | ||||||
2102 | QSize QMenu::sizeHint() const | - | ||||||
2103 | { | - | ||||||
2104 | Q_D(const QMenu); | - | ||||||
2105 | d->updateActionRects(); | - | ||||||
2106 | - | |||||||
2107 | QSize s; | - | ||||||
2108 | for (int i = 0; i < d->actionRects.count(); ++i) { | - | ||||||
2109 | const QRect &rect = d->actionRects.at(i); | - | ||||||
2110 | if (rect.isNull()) | - | ||||||
2111 | continue; | - | ||||||
2112 | if (rect.bottom() >= s.height()) | - | ||||||
2113 | s.setHeight(rect.y() + rect.height()); | - | ||||||
2114 | if (rect.right() >= s.width()) | - | ||||||
2115 | s.setWidth(rect.x() + rect.width()); | - | ||||||
2116 | } | - | ||||||
2117 | // Note that the action rects calculated above already include | - | ||||||
2118 | // the top and left margins, so we only need to add margins for | - | ||||||
2119 | // the bottom and right. | - | ||||||
2120 | QStyleOption opt(0); | - | ||||||
2121 | opt.init(this); | - | ||||||
2122 | const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, &opt, this); | - | ||||||
2123 | s.rwidth() += style()->pixelMetric(QStyle::PM_MenuHMargin, &opt, this) + fw + d->rightmargin; | - | ||||||
2124 | s.rheight() += style()->pixelMetric(QStyle::PM_MenuVMargin, &opt, this) + fw + d->bottommargin; | - | ||||||
2125 | - | |||||||
2126 | return style()->sizeFromContents(QStyle::CT_Menu, &opt, | - | ||||||
2127 | s.expandedTo(QApplication::globalStrut()), this); | - | ||||||
2128 | } | - | ||||||
2129 | - | |||||||
2130 | /*! | - | ||||||
2131 | Displays the menu so that the action \a atAction will be at the | - | ||||||
2132 | specified \e global position \a p. To translate a widget's local | - | ||||||
2133 | coordinates into global coordinates, use QWidget::mapToGlobal(). | - | ||||||
2134 | - | |||||||
2135 | When positioning a menu with exec() or popup(), bear in mind that | - | ||||||
2136 | you cannot rely on the menu's current size(). For performance | - | ||||||
2137 | reasons, the menu adapts its size only when necessary, so in many | - | ||||||
2138 | cases, the size before and after the show is different. Instead, | - | ||||||
2139 | use sizeHint() which calculates the proper size depending on the | - | ||||||
2140 | menu's current contents. | - | ||||||
2141 | - | |||||||
2142 | \sa QWidget::mapToGlobal(), exec() | - | ||||||
2143 | */ | - | ||||||
2144 | void QMenu::popup(const QPoint &p, QAction *atAction) | - | ||||||
2145 | { | - | ||||||
2146 | Q_D(QMenu); | - | ||||||
2147 | if (d->scroll) { // reset scroll state from last popup | - | ||||||
2148 | if (d->scroll->scrollOffset) | - | ||||||
2149 | d->itemsDirty = 1; // sizeHint will be incorrect if there is previous scroll | - | ||||||
2150 | d->scroll->scrollOffset = 0; | - | ||||||
2151 | d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone; | - | ||||||
2152 | } | - | ||||||
2153 | d->tearoffHighlighted = 0; | - | ||||||
2154 | d->motions = 0; | - | ||||||
2155 | d->doChildEffects = true; | - | ||||||
2156 | d->updateLayoutDirection(); | - | ||||||
2157 | - | |||||||
2158 | #ifndef QT_NO_MENUBAR | - | ||||||
2159 | // if this menu is part of a chain attached to a QMenuBar, set the | - | ||||||
2160 | // _NET_WM_WINDOW_TYPE_DROPDOWN_MENU X11 window type | - | ||||||
2161 | setAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu, qobject_cast<QMenuBar *>(d->topCausedWidget()) != 0); | - | ||||||
2162 | #endif | - | ||||||
2163 | - | |||||||
2164 | ensurePolished(); // Get the right font | - | ||||||
2165 | emit aboutToShow(); | - | ||||||
2166 | const bool actionListChanged = d->itemsDirty; | - | ||||||
2167 | d->updateActionRects(); | - | ||||||
2168 | QPoint pos; | - | ||||||
2169 | QPushButton *causedButton = qobject_cast<QPushButton*>(d->causedPopup.widget); | - | ||||||
2170 | if (actionListChanged && causedButton) | - | ||||||
2171 | pos = QPushButtonPrivate::get(causedButton)->adjustedMenuPosition(); | - | ||||||
2172 | else | - | ||||||
2173 | pos = p; | - | ||||||
2174 | - | |||||||
2175 | QSize size = sizeHint(); | - | ||||||
2176 | QRect screen; | - | ||||||
2177 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||
2178 | bool isEmbedded = !bypassGraphicsProxyWidget(this) && d->nearestGraphicsProxyWidget(this); | - | ||||||
2179 | if (isEmbedded) | - | ||||||
2180 | screen = d->popupGeometry(this); | - | ||||||
2181 | else | - | ||||||
2182 | #endif | - | ||||||
2183 | screen = d->popupGeometry(QApplication::desktop()->screenNumber(p)); | - | ||||||
2184 | const int desktopFrame = style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, this); | - | ||||||
2185 | bool adjustToDesktop = !window()->testAttribute(Qt::WA_DontShowOnScreen); | - | ||||||
2186 | - | |||||||
2187 | // if the screens have very different geometries and the menu is too big, we have to recalculate | - | ||||||
2188 | if (size.height() > screen.height() || size.width() > screen.width()) { | - | ||||||
2189 | size = d->adjustMenuSizeForScreen(screen); | - | ||||||
2190 | adjustToDesktop = true; | - | ||||||
2191 | } | - | ||||||
2192 | // Layout is not right, we might be able to save horizontal space | - | ||||||
2193 | if (d->ncols >1 && size.height() < screen.height()) { | - | ||||||
2194 | size = d->adjustMenuSizeForScreen(screen); | - | ||||||
2195 | adjustToDesktop = true; | - | ||||||
2196 | } | - | ||||||
2197 | - | |||||||
2198 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||
2199 | if (!atAction && QApplication::keypadNavigationEnabled()) { | - | ||||||
2200 | // Try to have one item activated | - | ||||||
2201 | if (d->defaultAction && d->defaultAction->isEnabled()) { | - | ||||||
2202 | atAction = d->defaultAction; | - | ||||||
2203 | // TODO: This works for first level menus, not yet sub menus | - | ||||||
2204 | } else { | - | ||||||
2205 | foreach (QAction *action, d->actions) | - | ||||||
2206 | if (action->isEnabled()) { | - | ||||||
2207 | atAction = action; | - | ||||||
2208 | break; | - | ||||||
2209 | } | - | ||||||
2210 | } | - | ||||||
2211 | d->currentAction = atAction; | - | ||||||
2212 | } | - | ||||||
2213 | #endif | - | ||||||
2214 | if (d->ncols > 1) { | - | ||||||
2215 | pos.setY(screen.top() + desktopFrame); | - | ||||||
2216 | } else if (atAction) { | - | ||||||
2217 | for (int i = 0, above_height = 0; i < d->actions.count(); i++) { | - | ||||||
2218 | QAction *action = d->actions.at(i); | - | ||||||
2219 | if (action == atAction) { | - | ||||||
2220 | int newY = pos.y() - above_height; | - | ||||||
2221 | if (d->scroll && newY < desktopFrame) { | - | ||||||
2222 | d->scroll->scrollFlags = d->scroll->scrollFlags | - | ||||||
2223 | | QMenuPrivate::QMenuScroller::ScrollUp; | - | ||||||
2224 | d->scroll->scrollOffset = newY; | - | ||||||
2225 | newY = desktopFrame; | - | ||||||
2226 | } | - | ||||||
2227 | pos.setY(newY); | - | ||||||
2228 | - | |||||||
2229 | if (d->scroll && d->scroll->scrollFlags != QMenuPrivate::QMenuScroller::ScrollNone | - | ||||||
2230 | && !style()->styleHint(QStyle::SH_Menu_FillScreenWithScroll, 0, this)) { | - | ||||||
2231 | int below_height = above_height + d->scroll->scrollOffset; | - | ||||||
2232 | for (int i2 = i; i2 < d->actionRects.count(); i2++) | - | ||||||
2233 | below_height += d->actionRects.at(i2).height(); | - | ||||||
2234 | size.setHeight(below_height); | - | ||||||
2235 | } | - | ||||||
2236 | break; | - | ||||||
2237 | } else { | - | ||||||
2238 | above_height += d->actionRects.at(i).height(); | - | ||||||
2239 | } | - | ||||||
2240 | } | - | ||||||
2241 | } | - | ||||||
2242 | - | |||||||
2243 | QPoint mouse = QCursor::pos(); | - | ||||||
2244 | d->mousePopupPos = mouse; | - | ||||||
2245 | const bool snapToMouse = !d->causedPopup.widget && (QRect(p.x() - 3, p.y() - 3, 6, 6).contains(mouse)); | - | ||||||
2246 | - | |||||||
2247 | const QSize menuSize(sizeHint()); | - | ||||||
2248 | if (adjustToDesktop) { | - | ||||||
2249 | // handle popup falling "off screen" | - | ||||||
2250 | if (isRightToLeft()) { | - | ||||||
2251 | if (snapToMouse) // position flowing left from the mouse | - | ||||||
2252 | pos.setX(mouse.x() - size.width()); | - | ||||||
2253 | - | |||||||
2254 | #ifndef QT_NO_MENUBAR | - | ||||||
2255 | // if the menu is in a menubar or is a submenu, it should be right-aligned | - | ||||||
2256 | if (qobject_cast<QMenuBar*>(d->causedPopup.widget) || qobject_cast<QMenu*>(d->causedPopup.widget)) | - | ||||||
2257 | pos.rx() -= size.width(); | - | ||||||
2258 | #endif //QT_NO_MENUBAR | - | ||||||
2259 | - | |||||||
2260 | if (pos.x() < screen.left() + desktopFrame) | - | ||||||
2261 | pos.setX(qMax(p.x(), screen.left() + desktopFrame)); | - | ||||||
2262 | if (pos.x() + size.width() - 1 > screen.right() - desktopFrame) | - | ||||||
2263 | pos.setX(qMax(p.x() - size.width(), screen.right() - desktopFrame - size.width() + 1)); | - | ||||||
2264 | } else { | - | ||||||
2265 | if (pos.x() + size.width() - 1 > screen.right() - desktopFrame) | - | ||||||
2266 | pos.setX(screen.right() - desktopFrame - size.width() + 1); | - | ||||||
2267 | if (pos.x() < screen.left() + desktopFrame) | - | ||||||
2268 | pos.setX(screen.left() + desktopFrame); | - | ||||||
2269 | } | - | ||||||
2270 | if (pos.y() + size.height() - 1 > screen.bottom() - desktopFrame) { | - | ||||||
2271 | if(snapToMouse) | - | ||||||
2272 | pos.setY(qMin(mouse.y() - (size.height() + desktopFrame), screen.bottom()-desktopFrame-size.height()+1)); | - | ||||||
2273 | else | - | ||||||
2274 | pos.setY(qMax(p.y() - (size.height() + desktopFrame), screen.bottom()-desktopFrame-size.height()+1)); | - | ||||||
2275 | } else if (pos.y() < screen.top() + desktopFrame) { | - | ||||||
2276 | pos.setY(screen.top() + desktopFrame); | - | ||||||
2277 | } | - | ||||||
2278 | - | |||||||
2279 | if (pos.y() < screen.top() + desktopFrame) | - | ||||||
2280 | pos.setY(screen.top() + desktopFrame); | - | ||||||
2281 | if (pos.y() + menuSize.height() - 1 > screen.bottom() - desktopFrame) { | - | ||||||
2282 | if (d->scroll) { | - | ||||||
2283 | d->scroll->scrollFlags |= uint(QMenuPrivate::QMenuScroller::ScrollDown); | - | ||||||
2284 | int y = qMax(screen.y(),pos.y()); | - | ||||||
2285 | size.setHeight(screen.bottom() - (desktopFrame * 2) - y); | - | ||||||
2286 | } else { | - | ||||||
2287 | // Too big for screen, bias to see bottom of menu (for some reason) | - | ||||||
2288 | pos.setY(screen.bottom() - size.height() + 1); | - | ||||||
2289 | } | - | ||||||
2290 | } | - | ||||||
2291 | } | - | ||||||
2292 | const int subMenuOffset = style()->pixelMetric(QStyle::PM_SubMenuOverlap, 0, this); | - | ||||||
2293 | QMenu *caused = qobject_cast<QMenu*>(d_func()->causedPopup.widget); | - | ||||||
2294 | if (caused && caused->geometry().width() + menuSize.width() + subMenuOffset < screen.width()) { | - | ||||||
2295 | QRect parentActionRect(caused->d_func()->actionRect(caused->d_func()->currentAction)); | - | ||||||
2296 | const QPoint actionTopLeft = caused->mapToGlobal(parentActionRect.topLeft()); | - | ||||||
2297 | parentActionRect.moveTopLeft(actionTopLeft); | - | ||||||
2298 | if (isRightToLeft()) { | - | ||||||
2299 | if ((pos.x() + menuSize.width() > parentActionRect.left() - subMenuOffset) | - | ||||||
2300 | && (pos.x() < parentActionRect.right())) | - | ||||||
2301 | { | - | ||||||
2302 | pos.rx() = parentActionRect.left() - menuSize.width(); | - | ||||||
2303 | if (pos.x() < screen.x()) | - | ||||||
2304 | pos.rx() = parentActionRect.right(); | - | ||||||
2305 | if (pos.x() + menuSize.width() > screen.x() + screen.width()) | - | ||||||
2306 | pos.rx() = screen.x(); | - | ||||||
2307 | } | - | ||||||
2308 | } else { | - | ||||||
2309 | if ((pos.x() < parentActionRect.right() + subMenuOffset) | - | ||||||
2310 | && (pos.x() + menuSize.width() > parentActionRect.left())) | - | ||||||
2311 | { | - | ||||||
2312 | pos.rx() = parentActionRect.right(); | - | ||||||
2313 | if (pos.x() + menuSize.width() > screen.x() + screen.width()) | - | ||||||
2314 | pos.rx() = parentActionRect.left() - menuSize.width(); | - | ||||||
2315 | if (pos.x() < screen.x()) | - | ||||||
2316 | pos.rx() = screen.x() + screen.width() - menuSize.width(); | - | ||||||
2317 | } | - | ||||||
2318 | } | - | ||||||
2319 | } | - | ||||||
2320 | setGeometry(QRect(pos, size)); | - | ||||||
2321 | #ifndef QT_NO_EFFECTS | - | ||||||
2322 | int hGuess = isRightToLeft() ? QEffects::LeftScroll : QEffects::RightScroll; | - | ||||||
2323 | int vGuess = QEffects::DownScroll; | - | ||||||
2324 | if (isRightToLeft()) { | - | ||||||
2325 | if ((snapToMouse && (pos.x() + size.width() / 2 > mouse.x())) || | - | ||||||
2326 | (qobject_cast<QMenu*>(d->causedPopup.widget) && pos.x() + size.width() / 2 > d->causedPopup.widget->x())) | - | ||||||
2327 | hGuess = QEffects::RightScroll; | - | ||||||
2328 | } else { | - | ||||||
2329 | if ((snapToMouse && (pos.x() + size.width() / 2 < mouse.x())) || | - | ||||||
2330 | (qobject_cast<QMenu*>(d->causedPopup.widget) && pos.x() + size.width() / 2 < d->causedPopup.widget->x())) | - | ||||||
2331 | hGuess = QEffects::LeftScroll; | - | ||||||
2332 | } | - | ||||||
2333 | - | |||||||
2334 | #ifndef QT_NO_MENUBAR | - | ||||||
2335 | if ((snapToMouse && (pos.y() + size.height() / 2 < mouse.y())) || | - | ||||||
2336 | (qobject_cast<QMenuBar*>(d->causedPopup.widget) && | - | ||||||
2337 | pos.y() + size.width() / 2 < d->causedPopup.widget->mapToGlobal(d->causedPopup.widget->pos()).y())) | - | ||||||
2338 | vGuess = QEffects::UpScroll; | - | ||||||
2339 | #endif | - | ||||||
2340 | if (QApplication::isEffectEnabled(Qt::UI_AnimateMenu)) { | - | ||||||
2341 | bool doChildEffects = true; | - | ||||||
2342 | #ifndef QT_NO_MENUBAR | - | ||||||
2343 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->causedPopup.widget)) { | - | ||||||
2344 | doChildEffects = mb->d_func()->doChildEffects; | - | ||||||
2345 | mb->d_func()->doChildEffects = false; | - | ||||||
2346 | } else | - | ||||||
2347 | #endif | - | ||||||
2348 | if (QMenu *m = qobject_cast<QMenu*>(d->causedPopup.widget)) { | - | ||||||
2349 | doChildEffects = m->d_func()->doChildEffects; | - | ||||||
2350 | m->d_func()->doChildEffects = false; | - | ||||||
2351 | } | - | ||||||
2352 | - | |||||||
2353 | if (doChildEffects) { | - | ||||||
2354 | if (QApplication::isEffectEnabled(Qt::UI_FadeMenu)) | - | ||||||
2355 | qFadeEffect(this); | - | ||||||
2356 | else if (d->causedPopup.widget) | - | ||||||
2357 | qScrollEffect(this, qobject_cast<QMenu*>(d->causedPopup.widget) ? hGuess : vGuess); | - | ||||||
2358 | else | - | ||||||
2359 | qScrollEffect(this, hGuess | vGuess); | - | ||||||
2360 | } else { | - | ||||||
2361 | // kill any running effect | - | ||||||
2362 | qFadeEffect(0); | - | ||||||
2363 | qScrollEffect(0); | - | ||||||
2364 | - | |||||||
2365 | show(); | - | ||||||
2366 | } | - | ||||||
2367 | } else | - | ||||||
2368 | #endif | - | ||||||
2369 | { | - | ||||||
2370 | show(); | - | ||||||
2371 | } | - | ||||||
2372 | - | |||||||
2373 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||
2374 | QAccessibleEvent event(this, QAccessible::PopupMenuStart); | - | ||||||
2375 | QAccessible::updateAccessibility(&event); | - | ||||||
2376 | #endif | - | ||||||
2377 | } | - | ||||||
2378 | - | |||||||
2379 | /*! | - | ||||||
2380 | Executes this menu synchronously. | - | ||||||
2381 | - | |||||||
2382 | This is equivalent to \c{exec(pos())}. | - | ||||||
2383 | - | |||||||
2384 | This returns the triggered QAction in either the popup menu or one | - | ||||||
2385 | of its submenus, or 0 if no item was triggered (normally because | - | ||||||
2386 | the user pressed Esc). | - | ||||||
2387 | - | |||||||
2388 | In most situations you'll want to specify the position yourself, | - | ||||||
2389 | for example, the current mouse position: | - | ||||||
2390 | \snippet code/src_gui_widgets_qmenu.cpp 0 | - | ||||||
2391 | or aligned to a widget: | - | ||||||
2392 | \snippet code/src_gui_widgets_qmenu.cpp 1 | - | ||||||
2393 | or in reaction to a QMouseEvent *e: | - | ||||||
2394 | \snippet code/src_gui_widgets_qmenu.cpp 2 | - | ||||||
2395 | */ | - | ||||||
2396 | QAction *QMenu::exec() | - | ||||||
2397 | { | - | ||||||
2398 | return exec(pos()); | - | ||||||
2399 | } | - | ||||||
2400 | - | |||||||
2401 | - | |||||||
2402 | /*! | - | ||||||
2403 | \overload | - | ||||||
2404 | - | |||||||
2405 | Executes this menu synchronously. | - | ||||||
2406 | - | |||||||
2407 | Pops up the menu so that the action \a action will be at the | - | ||||||
2408 | specified \e global position \a p. To translate a widget's local | - | ||||||
2409 | coordinates into global coordinates, use QWidget::mapToGlobal(). | - | ||||||
2410 | - | |||||||
2411 | This returns the triggered QAction in either the popup menu or one | - | ||||||
2412 | of its submenus, or 0 if no item was triggered (normally because | - | ||||||
2413 | the user pressed Esc). | - | ||||||
2414 | - | |||||||
2415 | Note that all signals are emitted as usual. If you connect a | - | ||||||
2416 | QAction to a slot and call the menu's exec(), you get the result | - | ||||||
2417 | both via the signal-slot connection and in the return value of | - | ||||||
2418 | exec(). | - | ||||||
2419 | - | |||||||
2420 | Common usage is to position the menu at the current mouse | - | ||||||
2421 | position: | - | ||||||
2422 | \snippet code/src_gui_widgets_qmenu.cpp 3 | - | ||||||
2423 | or aligned to a widget: | - | ||||||
2424 | \snippet code/src_gui_widgets_qmenu.cpp 4 | - | ||||||
2425 | or in reaction to a QMouseEvent *e: | - | ||||||
2426 | \snippet code/src_gui_widgets_qmenu.cpp 5 | - | ||||||
2427 | - | |||||||
2428 | When positioning a menu with exec() or popup(), bear in mind that | - | ||||||
2429 | you cannot rely on the menu's current size(). For performance | - | ||||||
2430 | reasons, the menu adapts its size only when necessary. So in many | - | ||||||
2431 | cases, the size before and after the show is different. Instead, | - | ||||||
2432 | use sizeHint() which calculates the proper size depending on the | - | ||||||
2433 | menu's current contents. | - | ||||||
2434 | - | |||||||
2435 | \sa popup(), QWidget::mapToGlobal() | - | ||||||
2436 | */ | - | ||||||
2437 | QAction *QMenu::exec(const QPoint &p, QAction *action) | - | ||||||
2438 | { | - | ||||||
2439 | Q_D(QMenu); | - | ||||||
2440 | ensurePolished(); | - | ||||||
2441 | createWinId(); | - | ||||||
2442 | QEventLoop eventLoop; | - | ||||||
2443 | d->eventLoop = &eventLoop; | - | ||||||
2444 | popup(p, action); | - | ||||||
2445 | - | |||||||
2446 | QPointer<QObject> guard = this; | - | ||||||
2447 | (void) eventLoop.exec(); | - | ||||||
2448 | if (guard.isNull()) | - | ||||||
2449 | return 0; | - | ||||||
2450 | - | |||||||
2451 | action = d->syncAction; | - | ||||||
2452 | d->syncAction = 0; | - | ||||||
2453 | d->eventLoop = 0; | - | ||||||
2454 | return action; | - | ||||||
2455 | } | - | ||||||
2456 | - | |||||||
2457 | /*! | - | ||||||
2458 | \overload | - | ||||||
2459 | - | |||||||
2460 | Executes a menu synchronously. | - | ||||||
2461 | - | |||||||
2462 | The menu's actions are specified by the list of \a actions. The menu will | - | ||||||
2463 | pop up so that the specified action, \a at, appears at global position \a | - | ||||||
2464 | pos. If \a at is not specified then the menu appears at position \a | - | ||||||
2465 | pos. \a parent is the menu's parent widget; specifying the parent will | - | ||||||
2466 | provide context when \a pos alone is not enough to decide where the menu | - | ||||||
2467 | should go (e.g., with multiple desktops or when the parent is embedded in | - | ||||||
2468 | QGraphicsView). | - | ||||||
2469 | - | |||||||
2470 | The function returns the triggered QAction in either the popup | - | ||||||
2471 | menu or one of its submenus, or 0 if no item was triggered | - | ||||||
2472 | (normally because the user pressed Esc). | - | ||||||
2473 | - | |||||||
2474 | This is equivalent to: | - | ||||||
2475 | \snippet code/src_gui_widgets_qmenu.cpp 6 | - | ||||||
2476 | - | |||||||
2477 | \sa popup(), QWidget::mapToGlobal() | - | ||||||
2478 | */ | - | ||||||
2479 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) | - | ||||||
2480 | QAction *QMenu::exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at, QWidget *parent) | - | ||||||
2481 | #else | - | ||||||
2482 | QAction *QMenu::exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent) | - | ||||||
2483 | #endif | - | ||||||
2484 | { | - | ||||||
2485 | QMenu menu(parent); | - | ||||||
2486 | menu.addActions(actions); | - | ||||||
2487 | return menu.exec(pos, at); | - | ||||||
2488 | } | - | ||||||
2489 | - | |||||||
2490 | /*! | - | ||||||
2491 | \reimp | - | ||||||
2492 | */ | - | ||||||
2493 | void QMenu::hideEvent(QHideEvent *) | - | ||||||
2494 | { | - | ||||||
2495 | Q_D(QMenu); | - | ||||||
2496 | emit aboutToHide(); | - | ||||||
2497 | if (d->eventLoop) | - | ||||||
2498 | d->eventLoop->exit(); | - | ||||||
2499 | d->setCurrentAction(0); | - | ||||||
2500 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||
2501 | QAccessibleEvent event(this, QAccessible::PopupMenuEnd); | - | ||||||
2502 | QAccessible::updateAccessibility(&event); | - | ||||||
2503 | #endif | - | ||||||
2504 | #ifndef QT_NO_MENUBAR | - | ||||||
2505 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->causedPopup.widget)) | - | ||||||
2506 | mb->d_func()->setCurrentAction(0); | - | ||||||
2507 | #endif | - | ||||||
2508 | d->mouseDown = 0; | - | ||||||
2509 | d->hasHadMouse = false; | - | ||||||
2510 | if (d->activeMenu) | - | ||||||
2511 | d->hideMenu(d->activeMenu); | - | ||||||
2512 | d->causedPopup.widget = 0; | - | ||||||
2513 | d->causedPopup.action = 0; | - | ||||||
2514 | if (d->scroll) | - | ||||||
2515 | d->scroll->scrollTimer.stop(); //make sure the timer stops | - | ||||||
2516 | } | - | ||||||
2517 | - | |||||||
2518 | /*! | - | ||||||
2519 | \reimp | - | ||||||
2520 | */ | - | ||||||
2521 | void QMenu::paintEvent(QPaintEvent *e) | - | ||||||
2522 | { | - | ||||||
2523 | Q_D(QMenu); | - | ||||||
2524 | d->updateActionRects(); | - | ||||||
2525 | QPainter p(this); | - | ||||||
2526 | QRegion emptyArea = QRegion(rect()); | - | ||||||
2527 | - | |||||||
2528 | QStyleOptionMenuItem menuOpt; | - | ||||||
2529 | menuOpt.initFrom(this); | - | ||||||
2530 | menuOpt.state = QStyle::State_None; | - | ||||||
2531 | menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; | - | ||||||
2532 | menuOpt.maxIconWidth = 0; | - | ||||||
2533 | menuOpt.tabWidth = 0; | - | ||||||
2534 | style()->drawPrimitive(QStyle::PE_PanelMenu, &menuOpt, &p, this); | - | ||||||
2535 | - | |||||||
2536 | //draw the items that need updating.. | - | ||||||
2537 | for (int i = 0; i < d->actions.count(); ++i) { | - | ||||||
2538 | QAction *action = d->actions.at(i); | - | ||||||
2539 | QRect adjustedActionRect = d->actionRects.at(i); | - | ||||||
2540 | if (!e->rect().intersects(adjustedActionRect) | - | ||||||
2541 | || d->widgetItems.value(action)) | - | ||||||
2542 | continue; | - | ||||||
2543 | //set the clip region to be extra safe (and adjust for the scrollers) | - | ||||||
2544 | QRegion adjustedActionReg(adjustedActionRect); | - | ||||||
2545 | emptyArea -= adjustedActionReg; | - | ||||||
2546 | p.setClipRegion(adjustedActionReg); | - | ||||||
2547 | - | |||||||
2548 | QStyleOptionMenuItem opt; | - | ||||||
2549 | initStyleOption(&opt, action); | - | ||||||
2550 | opt.rect = adjustedActionRect; | - | ||||||
2551 | style()->drawControl(QStyle::CE_MenuItem, &opt, &p, this); | - | ||||||
2552 | } | - | ||||||
2553 | - | |||||||
2554 | const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this); | - | ||||||
2555 | //draw the scroller regions.. | - | ||||||
2556 | if (d->scroll) { | - | ||||||
2557 | menuOpt.menuItemType = QStyleOptionMenuItem::Scroller; | - | ||||||
2558 | menuOpt.state |= QStyle::State_Enabled; | - | ||||||
2559 | if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) { | - | ||||||
2560 | menuOpt.rect.setRect(fw, fw, width() - (fw * 2), d->scrollerHeight()); | - | ||||||
2561 | emptyArea -= QRegion(menuOpt.rect); | - | ||||||
2562 | p.setClipRect(menuOpt.rect); | - | ||||||
2563 | style()->drawControl(QStyle::CE_MenuScroller, &menuOpt, &p, this); | - | ||||||
2564 | } | - | ||||||
2565 | if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown) { | - | ||||||
2566 | menuOpt.rect.setRect(fw, height() - d->scrollerHeight() - fw, width() - (fw * 2), | - | ||||||
2567 | d->scrollerHeight()); | - | ||||||
2568 | emptyArea -= QRegion(menuOpt.rect); | - | ||||||
2569 | menuOpt.state |= QStyle::State_DownArrow; | - | ||||||
2570 | p.setClipRect(menuOpt.rect); | - | ||||||
2571 | style()->drawControl(QStyle::CE_MenuScroller, &menuOpt, &p, this); | - | ||||||
2572 | } | - | ||||||
2573 | } | - | ||||||
2574 | //paint the tear off.. | - | ||||||
2575 | if (d->tearoff) { | - | ||||||
2576 | menuOpt.menuItemType = QStyleOptionMenuItem::TearOff; | - | ||||||
2577 | menuOpt.rect.setRect(fw, fw, width() - (fw * 2), | - | ||||||
2578 | style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this)); | - | ||||||
2579 | if (d->scroll && d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) | - | ||||||
2580 | menuOpt.rect.translate(0, d->scrollerHeight()); | - | ||||||
2581 | emptyArea -= QRegion(menuOpt.rect); | - | ||||||
2582 | p.setClipRect(menuOpt.rect); | - | ||||||
2583 | menuOpt.state = QStyle::State_None; | - | ||||||
2584 | if (d->tearoffHighlighted) | - | ||||||
2585 | menuOpt.state |= QStyle::State_Selected; | - | ||||||
2586 | style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this); | - | ||||||
2587 | } | - | ||||||
2588 | //draw border | - | ||||||
2589 | if (fw) { | - | ||||||
2590 | QRegion borderReg; | - | ||||||
2591 | borderReg += QRect(0, 0, fw, height()); //left | - | ||||||
2592 | borderReg += QRect(width()-fw, 0, fw, height()); //right | - | ||||||
2593 | borderReg += QRect(0, 0, width(), fw); //top | - | ||||||
2594 | borderReg += QRect(0, height()-fw, width(), fw); //bottom | - | ||||||
2595 | p.setClipRegion(borderReg); | - | ||||||
2596 | emptyArea -= borderReg; | - | ||||||
2597 | QStyleOptionFrame frame; | - | ||||||
2598 | frame.rect = rect(); | - | ||||||
2599 | frame.palette = palette(); | - | ||||||
2600 | frame.state = QStyle::State_None; | - | ||||||
2601 | frame.lineWidth = style()->pixelMetric(QStyle::PM_MenuPanelWidth); | - | ||||||
2602 | frame.midLineWidth = 0; | - | ||||||
2603 | style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this); | - | ||||||
2604 | } | - | ||||||
2605 | - | |||||||
2606 | //finally the rest of the space | - | ||||||
2607 | p.setClipRegion(emptyArea); | - | ||||||
2608 | menuOpt.state = QStyle::State_None; | - | ||||||
2609 | menuOpt.menuItemType = QStyleOptionMenuItem::EmptyArea; | - | ||||||
2610 | menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; | - | ||||||
2611 | menuOpt.rect = rect(); | - | ||||||
2612 | menuOpt.menuRect = rect(); | - | ||||||
2613 | style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this); | - | ||||||
2614 | } | - | ||||||
2615 | - | |||||||
2616 | #ifndef QT_NO_WHEELEVENT | - | ||||||
2617 | /*! | - | ||||||
2618 | \reimp | - | ||||||
2619 | */ | - | ||||||
2620 | void QMenu::wheelEvent(QWheelEvent *e) | - | ||||||
2621 | { | - | ||||||
2622 | Q_D(QMenu); | - | ||||||
2623 | if (d->scroll && rect().contains(e->pos())) | - | ||||||
2624 | d->scrollMenu(e->delta() > 0 ? | - | ||||||
2625 | QMenuPrivate::QMenuScroller::ScrollUp : QMenuPrivate::QMenuScroller::ScrollDown); | - | ||||||
2626 | } | - | ||||||
2627 | #endif | - | ||||||
2628 | - | |||||||
2629 | /*! | - | ||||||
2630 | \reimp | - | ||||||
2631 | */ | - | ||||||
2632 | void QMenu::mousePressEvent(QMouseEvent *e) | - | ||||||
2633 | { | - | ||||||
2634 | Q_D(QMenu); | - | ||||||
2635 | if (d->aboutToHide || d->mouseEventTaken(e)) | - | ||||||
2636 | return; | - | ||||||
2637 | // Workaround for XCB on multiple screens which doesn't have offset. If the menu is open on one screen | - | ||||||
2638 | // and mouse clicks on second screen, e->pos() is QPoint(0,0) and the menu doesn't hide. This trick makes | - | ||||||
2639 | // possible to hide the menu when mouse clicks on another screen (e->screenPos() returns correct value). | - | ||||||
2640 | // Only when mouse clicks in QPoint(0,0) on second screen, the menu doesn't hide. | - | ||||||
2641 | if ((e->pos().isNull() && !e->screenPos().isNull()) || !rect().contains(e->pos())) { | - | ||||||
2642 | if (d->noReplayFor | - | ||||||
2643 | && QRect(d->noReplayFor->mapToGlobal(QPoint()), d->noReplayFor->size()).contains(e->globalPos())) | - | ||||||
2644 | setAttribute(Qt::WA_NoMouseReplay); | - | ||||||
2645 | if (d->eventLoop) // synchronous operation | - | ||||||
2646 | d->syncAction = 0; | - | ||||||
2647 | d->hideUpToMenuBar(); | - | ||||||
2648 | return; | - | ||||||
2649 | } | - | ||||||
2650 | d->mouseDown = this; | - | ||||||
2651 | - | |||||||
2652 | QAction *action = d->actionAt(e->pos()); | - | ||||||
2653 | d->setCurrentAction(action, 20); | - | ||||||
2654 | update(); | - | ||||||
2655 | } | - | ||||||
2656 | - | |||||||
2657 | /*! | - | ||||||
2658 | \reimp | - | ||||||
2659 | */ | - | ||||||
2660 | void QMenu::mouseReleaseEvent(QMouseEvent *e) | - | ||||||
2661 | { | - | ||||||
2662 | Q_D(QMenu); | - | ||||||
2663 | if (d->aboutToHide || d->mouseEventTaken(e)) | - | ||||||
2664 | return; | - | ||||||
2665 | if(d->mouseDown != this) { | - | ||||||
2666 | d->mouseDown = 0; | - | ||||||
2667 | return; | - | ||||||
2668 | } | - | ||||||
2669 | - | |||||||
2670 | d->mouseDown = 0; | - | ||||||
2671 | d->setSyncAction(); | - | ||||||
2672 | QAction *action = d->actionAt(e->pos()); | - | ||||||
2673 | - | |||||||
2674 | if (action && action == d->currentAction) { | - | ||||||
2675 | if (!action->menu()){ | - | ||||||
2676 | #if defined(Q_OS_WIN) | - | ||||||
2677 | //On Windows only context menus can be activated with the right button | - | ||||||
2678 | if (e->button() == Qt::LeftButton || d->topCausedWidget() == 0) | - | ||||||
2679 | #endif | - | ||||||
2680 | d->activateAction(action, QAction::Trigger); | - | ||||||
2681 | } | - | ||||||
2682 | } else if ((!action || action->isEnabled()) && d->hasMouseMoved(e->globalPos())) { | - | ||||||
2683 | d->hideUpToMenuBar(); | - | ||||||
2684 | } | - | ||||||
2685 | } | - | ||||||
2686 | - | |||||||
2687 | /*! | - | ||||||
2688 | \reimp | - | ||||||
2689 | */ | - | ||||||
2690 | void QMenu::changeEvent(QEvent *e) | - | ||||||
2691 | { | - | ||||||
2692 | Q_D(QMenu); | - | ||||||
2693 | if (e->type() == QEvent::StyleChange || e->type() == QEvent::FontChange || | - | ||||||
2694 | e->type() == QEvent::LayoutDirectionChange) { | - | ||||||
2695 | d->itemsDirty = 1; | - | ||||||
2696 | setMouseTracking(style()->styleHint(QStyle::SH_Menu_MouseTracking, 0, this)); | - | ||||||
2697 | if (isVisible()) | - | ||||||
2698 | resize(sizeHint()); | - | ||||||
2699 | if (!style()->styleHint(QStyle::SH_Menu_Scrollable, 0, this)) { | - | ||||||
2700 | delete d->scroll; | - | ||||||
2701 | d->scroll = 0; | - | ||||||
2702 | } else if (!d->scroll) { | - | ||||||
2703 | d->scroll = new QMenuPrivate::QMenuScroller; | - | ||||||
2704 | d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone; | - | ||||||
2705 | } | - | ||||||
2706 | } else if (e->type() == QEvent::EnabledChange) { | - | ||||||
2707 | if (d->tornPopup) // torn-off menu | - | ||||||
2708 | d->tornPopup->setEnabled(isEnabled()); | - | ||||||
2709 | d->menuAction->setEnabled(isEnabled()); | - | ||||||
2710 | if (!d->platformMenu.isNull()) | - | ||||||
2711 | d->platformMenu->setEnabled(isEnabled()); | - | ||||||
2712 | } | - | ||||||
2713 | QWidget::changeEvent(e); | - | ||||||
2714 | } | - | ||||||
2715 | - | |||||||
2716 | - | |||||||
2717 | /*! | - | ||||||
2718 | \reimp | - | ||||||
2719 | */ | - | ||||||
2720 | bool | - | ||||||
2721 | QMenu::event(QEvent *e) | - | ||||||
2722 | { | - | ||||||
2723 | Q_D(QMenu); | - | ||||||
2724 | switch (e->type()) { | - | ||||||
2725 | case QEvent::Polish: | - | ||||||
2726 | d->updateLayoutDirection(); | - | ||||||
2727 | break; | - | ||||||
2728 | case QEvent::ShortcutOverride: { | - | ||||||
2729 | QKeyEvent *kev = static_cast<QKeyEvent*>(e); | - | ||||||
2730 | if (kev->key() == Qt::Key_Up || kev->key() == Qt::Key_Down | - | ||||||
2731 | || kev->key() == Qt::Key_Left || kev->key() == Qt::Key_Right | - | ||||||
2732 | || kev->key() == Qt::Key_Enter || kev->key() == Qt::Key_Return | - | ||||||
2733 | || kev->matches(QKeySequence::Cancel)) { | - | ||||||
2734 | e->accept(); | - | ||||||
2735 | return true; | - | ||||||
2736 | } | - | ||||||
2737 | } | - | ||||||
2738 | break; | - | ||||||
2739 | case QEvent::KeyPress: { | - | ||||||
2740 | QKeyEvent *ke = (QKeyEvent*)e; | - | ||||||
2741 | if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) { | - | ||||||
2742 | keyPressEvent(ke); | - | ||||||
2743 | return true; | - | ||||||
2744 | } | - | ||||||
2745 | } break; | - | ||||||
2746 | case QEvent::MouseButtonPress: | - | ||||||
2747 | case QEvent::ContextMenu: { | - | ||||||
2748 | bool canPopup = true; | - | ||||||
2749 | if (e->type() == QEvent::MouseButtonPress) | - | ||||||
2750 | canPopup = (static_cast<QMouseEvent*>(e)->button() == Qt::LeftButton); | - | ||||||
2751 | if (canPopup && d->delayState.timer.isActive()) { | - | ||||||
2752 | d->delayState.stop(); | - | ||||||
2753 | internalDelayedPopup(); | - | ||||||
2754 | } | - | ||||||
2755 | } | - | ||||||
2756 | break; | - | ||||||
2757 | case QEvent::Resize: { | - | ||||||
2758 | QStyleHintReturnMask menuMask; | - | ||||||
2759 | QStyleOption option; | - | ||||||
2760 | option.initFrom(this); | - | ||||||
2761 | if (style()->styleHint(QStyle::SH_Menu_Mask, &option, this, &menuMask)) { | - | ||||||
2762 | setMask(menuMask.region); | - | ||||||
2763 | } | - | ||||||
2764 | d->itemsDirty = 1; | - | ||||||
2765 | d->updateActionRects(); | - | ||||||
2766 | break; } | - | ||||||
2767 | case QEvent::Show: | - | ||||||
2768 | d->mouseDown = 0; | - | ||||||
2769 | d->updateActionRects(); | - | ||||||
2770 | d->sloppyState.reset(); | - | ||||||
2771 | if (d->currentAction) | - | ||||||
2772 | d->popupAction(d->currentAction, 0, false); | - | ||||||
2773 | break; | - | ||||||
2774 | #ifndef QT_NO_TOOLTIP | - | ||||||
2775 | case QEvent::ToolTip: | - | ||||||
2776 | if (d->toolTipsVisible) { | - | ||||||
2777 | const QHelpEvent *ev = static_cast<const QHelpEvent*>(e); | - | ||||||
2778 | if (const QAction *action = actionAt(ev->pos())) { | - | ||||||
2779 | const QString toolTip = action->d_func()->tooltip; | - | ||||||
2780 | if (!toolTip.isEmpty()) | - | ||||||
2781 | QToolTip::showText(ev->globalPos(), toolTip, this); | - | ||||||
2782 | return true; | - | ||||||
2783 | } | - | ||||||
2784 | } | - | ||||||
2785 | break; | - | ||||||
2786 | #endif // QT_NO_TOOLTIP | - | ||||||
2787 | #ifndef QT_NO_WHATSTHIS | - | ||||||
2788 | case QEvent::QueryWhatsThis: | - | ||||||
2789 | e->setAccepted(d->whatsThis.size()); | - | ||||||
2790 | if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) { | - | ||||||
2791 | if (action->whatsThis().size() || action->menu()) | - | ||||||
2792 | e->accept(); | - | ||||||
2793 | } | - | ||||||
2794 | return true; | - | ||||||
2795 | #endif | - | ||||||
2796 | default: | - | ||||||
2797 | break; | - | ||||||
2798 | } | - | ||||||
2799 | return QWidget::event(e); | - | ||||||
2800 | } | - | ||||||
2801 | - | |||||||
2802 | /*! | - | ||||||
2803 | \reimp | - | ||||||
2804 | */ | - | ||||||
2805 | bool QMenu::focusNextPrevChild(bool next) | - | ||||||
2806 | { | - | ||||||
2807 | setFocus(); | - | ||||||
2808 | QKeyEvent ev(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier); | - | ||||||
2809 | keyPressEvent(&ev); | - | ||||||
2810 | return true; | - | ||||||
2811 | } | - | ||||||
2812 | - | |||||||
2813 | /*! | - | ||||||
2814 | \reimp | - | ||||||
2815 | */ | - | ||||||
2816 | void QMenu::keyPressEvent(QKeyEvent *e) | - | ||||||
2817 | { | - | ||||||
2818 | Q_D(QMenu); | - | ||||||
2819 | d->updateActionRects(); | - | ||||||
2820 | int key = e->key(); | - | ||||||
2821 | if (isRightToLeft()) { // in reverse mode open/close key for submenues are reversed | - | ||||||
2822 | if (key == Qt::Key_Left) | - | ||||||
2823 | key = Qt::Key_Right; | - | ||||||
2824 | else if (key == Qt::Key_Right) | - | ||||||
2825 | key = Qt::Key_Left; | - | ||||||
2826 | } | - | ||||||
2827 | #ifndef Q_OS_MAC | - | ||||||
2828 | if (key == Qt::Key_Tab) //means down | - | ||||||
2829 | key = Qt::Key_Down; | - | ||||||
2830 | if (key == Qt::Key_Backtab) //means up | - | ||||||
2831 | key = Qt::Key_Up; | - | ||||||
2832 | #endif | - | ||||||
2833 | - | |||||||
2834 | bool key_consumed = false; | - | ||||||
2835 | switch(key) { | - | ||||||
2836 | case Qt::Key_Home: | - | ||||||
2837 | key_consumed = true; | - | ||||||
2838 | if (d->scroll) | - | ||||||
2839 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollTop, true); | - | ||||||
2840 | break; | - | ||||||
2841 | case Qt::Key_End: | - | ||||||
2842 | key_consumed = true; | - | ||||||
2843 | if (d->scroll) | - | ||||||
2844 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollBottom, true); | - | ||||||
2845 | break; | - | ||||||
2846 | case Qt::Key_PageUp: | - | ||||||
2847 | key_consumed = true; | - | ||||||
2848 | if (d->currentAction && d->scroll) { | - | ||||||
2849 | if(d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) | - | ||||||
2850 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollUp, true, true); | - | ||||||
2851 | else | - | ||||||
2852 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollTop, true); | - | ||||||
2853 | } | - | ||||||
2854 | break; | - | ||||||
2855 | case Qt::Key_PageDown: | - | ||||||
2856 | key_consumed = true; | - | ||||||
2857 | if (d->currentAction && d->scroll) { | - | ||||||
2858 | if(d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown) | - | ||||||
2859 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollDown, true, true); | - | ||||||
2860 | else | - | ||||||
2861 | d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollBottom, true); | - | ||||||
2862 | } | - | ||||||
2863 | break; | - | ||||||
2864 | case Qt::Key_Up: | - | ||||||
2865 | case Qt::Key_Down: { | - | ||||||
2866 | key_consumed = true; | - | ||||||
2867 | QAction *nextAction = 0; | - | ||||||
2868 | QMenuPrivate::QMenuScroller::ScrollLocation scroll_loc = QMenuPrivate::QMenuScroller::ScrollStay; | - | ||||||
2869 | if (!d->currentAction) { | - | ||||||
2870 | if(key == Qt::Key_Down) { | - | ||||||
2871 | for(int i = 0; i < d->actions.count(); ++i) { | - | ||||||
2872 | QAction *act = d->actions.at(i); | - | ||||||
2873 | if (d->actionRects.at(i).isNull()) | - | ||||||
2874 | continue; | - | ||||||
2875 | if (!act->isSeparator() && | - | ||||||
2876 | (style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, this) | - | ||||||
2877 | || act->isEnabled())) { | - | ||||||
2878 | nextAction = act; | - | ||||||
2879 | break; | - | ||||||
2880 | } | - | ||||||
2881 | } | - | ||||||
2882 | } else { | - | ||||||
2883 | for(int i = d->actions.count()-1; i >= 0; --i) { | - | ||||||
2884 | QAction *act = d->actions.at(i); | - | ||||||
2885 | if (d->actionRects.at(i).isNull()) | - | ||||||
2886 | continue; | - | ||||||
2887 | if (!act->isSeparator() && | - | ||||||
2888 | (style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, this) | - | ||||||
2889 | || act->isEnabled())) { | - | ||||||
2890 | nextAction = act; | - | ||||||
2891 | break; | - | ||||||
2892 | } | - | ||||||
2893 | } | - | ||||||
2894 | } | - | ||||||
2895 | } else { | - | ||||||
2896 | for(int i = 0, y = 0; !nextAction && i < d->actions.count(); i++) { | - | ||||||
2897 | QAction *act = d->actions.at(i); | - | ||||||
2898 | if (act == d->currentAction) { | - | ||||||
2899 | if (key == Qt::Key_Up) { | - | ||||||
2900 | for(int next_i = i-1; true; next_i--) { | - | ||||||
2901 | if (next_i == -1) { | - | ||||||
2902 | if(!style()->styleHint(QStyle::SH_Menu_SelectionWrap, 0, this)) | - | ||||||
2903 | break; | - | ||||||
2904 | if (d->scroll) | - | ||||||
2905 | scroll_loc = QMenuPrivate::QMenuScroller::ScrollBottom; | - | ||||||
2906 | next_i = d->actionRects.count()-1; | - | ||||||
2907 | } | - | ||||||
2908 | QAction *next = d->actions.at(next_i); | - | ||||||
2909 | if (next == d->currentAction) | - | ||||||
2910 | break; | - | ||||||
2911 | if (d->actionRects.at(next_i).isNull()) | - | ||||||
2912 | continue; | - | ||||||
2913 | if (next->isSeparator() || | - | ||||||
2914 | (!next->isEnabled() && | - | ||||||
2915 | !style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, this))) | - | ||||||
2916 | continue; | - | ||||||
2917 | nextAction = next; | - | ||||||
2918 | if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)) { | - | ||||||
2919 | int topVisible = d->scrollerHeight(); | - | ||||||
2920 | if (d->tearoff) | - | ||||||
2921 | topVisible += style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this); | - | ||||||
2922 | if (((y + d->scroll->scrollOffset) - topVisible) <= d->actionRects.at(next_i).height()) | - | ||||||
2923 | scroll_loc = QMenuPrivate::QMenuScroller::ScrollTop; | - | ||||||
2924 | } | - | ||||||
2925 | break; | - | ||||||
2926 | } | - | ||||||
2927 | if (!nextAction && d->tearoff) | - | ||||||
2928 | d->tearoffHighlighted = 1; | - | ||||||
2929 | } else { | - | ||||||
2930 | y += d->actionRects.at(i).height(); | - | ||||||
2931 | for(int next_i = i+1; true; next_i++) { | - | ||||||
2932 | if (next_i == d->actionRects.count()) { | - | ||||||
2933 | if(!style()->styleHint(QStyle::SH_Menu_SelectionWrap, 0, this)) | - | ||||||
2934 | break; | - | ||||||
2935 | if (d->scroll) | - | ||||||
2936 | scroll_loc = QMenuPrivate::QMenuScroller::ScrollTop; | - | ||||||
2937 | next_i = 0; | - | ||||||
2938 | } | - | ||||||
2939 | QAction *next = d->actions.at(next_i); | - | ||||||
2940 | if (next == d->currentAction) | - | ||||||
2941 | break; | - | ||||||
2942 | if (d->actionRects.at(next_i).isNull()) | - | ||||||
2943 | continue; | - | ||||||
2944 | if (next->isSeparator() || | - | ||||||
2945 | (!next->isEnabled() && | - | ||||||
2946 | !style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, 0, this))) | - | ||||||
2947 | continue; | - | ||||||
2948 | nextAction = next; | - | ||||||
2949 | if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)) { | - | ||||||
2950 | int bottomVisible = height() - d->scrollerHeight(); | - | ||||||
2951 | if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) | - | ||||||
2952 | bottomVisible -= d->scrollerHeight(); | - | ||||||
2953 | if (d->tearoff) | - | ||||||
2954 | bottomVisible -= style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this); | - | ||||||
2955 | if ((y + d->scroll->scrollOffset + d->actionRects.at(next_i).height()) > bottomVisible) | - | ||||||
2956 | scroll_loc = QMenuPrivate::QMenuScroller::ScrollBottom; | - | ||||||
2957 | } | - | ||||||
2958 | break; | - | ||||||
2959 | } | - | ||||||
2960 | } | - | ||||||
2961 | break; | - | ||||||
2962 | } | - | ||||||
2963 | y += d->actionRects.at(i).height(); | - | ||||||
2964 | } | - | ||||||
2965 | } | - | ||||||
2966 | if (nextAction) { | - | ||||||
2967 | if (d->scroll && scroll_loc != QMenuPrivate::QMenuScroller::ScrollStay) { | - | ||||||
2968 | d->scroll->scrollTimer.stop(); | - | ||||||
2969 | d->scrollMenu(nextAction, scroll_loc); | - | ||||||
2970 | } | - | ||||||
2971 | d->setCurrentAction(nextAction, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard); | - | ||||||
2972 | } | - | ||||||
2973 | break; } | - | ||||||
2974 | - | |||||||
2975 | case Qt::Key_Right: | - | ||||||
2976 | if (d->currentAction && d->currentAction->isEnabled() && d->currentAction->menu()) { | - | ||||||
2977 | d->popupAction(d->currentAction, 0, true); | - | ||||||
2978 | key_consumed = true; | - | ||||||
2979 | break; | - | ||||||
2980 | } | - | ||||||
2981 | //FALL THROUGH | - | ||||||
2982 | case Qt::Key_Left: { | - | ||||||
2983 | if (d->currentAction && !d->scroll) { | - | ||||||
2984 | QAction *nextAction = 0; | - | ||||||
2985 | if (key == Qt::Key_Left) { | - | ||||||
2986 | QRect actionR = d->actionRect(d->currentAction); | - | ||||||
2987 | for(int x = actionR.left()-1; !nextAction && x >= 0; x--) | - | ||||||
2988 | nextAction = d->actionAt(QPoint(x, actionR.center().y())); | - | ||||||
2989 | } else { | - | ||||||
2990 | QRect actionR = d->actionRect(d->currentAction); | - | ||||||
2991 | for(int x = actionR.right()+1; !nextAction && x < width(); x++) | - | ||||||
2992 | nextAction = d->actionAt(QPoint(x, actionR.center().y())); | - | ||||||
2993 | } | - | ||||||
2994 | if (nextAction) { | - | ||||||
2995 | d->setCurrentAction(nextAction, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard); | - | ||||||
2996 | key_consumed = true; | - | ||||||
2997 | } | - | ||||||
2998 | } | - | ||||||
2999 | if (!key_consumed && key == Qt::Key_Left && qobject_cast<QMenu*>(d->causedPopup.widget)) { | - | ||||||
3000 | QPointer<QWidget> caused = d->causedPopup.widget; | - | ||||||
3001 | d->hideMenu(this); | - | ||||||
3002 | if (caused) | - | ||||||
3003 | caused->setFocus(); | - | ||||||
3004 | key_consumed = true; | - | ||||||
3005 | } | - | ||||||
3006 | break; } | - | ||||||
3007 | - | |||||||
3008 | case Qt::Key_Alt: | - | ||||||
3009 | if (d->tornoff) | - | ||||||
3010 | break; | - | ||||||
3011 | - | |||||||
3012 | key_consumed = true; | - | ||||||
3013 | if (style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation, 0, this)) | - | ||||||
3014 | { | - | ||||||
3015 | d->hideMenu(this); | - | ||||||
3016 | #ifndef QT_NO_MENUBAR | - | ||||||
3017 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(QApplication::focusWidget())) { | - | ||||||
3018 | mb->d_func()->setKeyboardMode(false); | - | ||||||
3019 | } | - | ||||||
3020 | #endif | - | ||||||
3021 | } | - | ||||||
3022 | break; | - | ||||||
3023 | - | |||||||
3024 | case Qt::Key_Space: | - | ||||||
3025 | if (!style()->styleHint(QStyle::SH_Menu_SpaceActivatesItem, 0, this)) | - | ||||||
3026 | break; | - | ||||||
3027 | // for motif, fall through | - | ||||||
3028 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||
3029 | case Qt::Key_Select: | - | ||||||
3030 | #endif | - | ||||||
3031 | case Qt::Key_Return: | - | ||||||
3032 | case Qt::Key_Enter: { | - | ||||||
3033 | if (!d->currentAction) { | - | ||||||
3034 | d->setFirstActionActive(); | - | ||||||
3035 | key_consumed = true; | - | ||||||
3036 | break; | - | ||||||
3037 | } | - | ||||||
3038 | - | |||||||
3039 | d->setSyncAction(); | - | ||||||
3040 | - | |||||||
3041 | if (d->currentAction->menu()) | - | ||||||
3042 | d->popupAction(d->currentAction, 0, true); | - | ||||||
3043 | else | - | ||||||
3044 | d->activateAction(d->currentAction, QAction::Trigger); | - | ||||||
3045 | key_consumed = true; | - | ||||||
3046 | break; } | - | ||||||
3047 | - | |||||||
3048 | #ifndef QT_NO_WHATSTHIS | - | ||||||
3049 | case Qt::Key_F1: | - | ||||||
3050 | if (!d->currentAction || d->currentAction->whatsThis().isNull()) | - | ||||||
3051 | break; | - | ||||||
3052 | QWhatsThis::enterWhatsThisMode(); | - | ||||||
3053 | d->activateAction(d->currentAction, QAction::Trigger); | - | ||||||
3054 | return; | - | ||||||
3055 | #endif | - | ||||||
3056 | default: | - | ||||||
3057 | key_consumed = false; | - | ||||||
3058 | } | - | ||||||
3059 | - | |||||||
3060 | if (!key_consumed && (e->matches(QKeySequence::Cancel) | - | ||||||
3061 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||
3062 | || e->key() == Qt::Key_Back | - | ||||||
3063 | #endif | - | ||||||
3064 | )) { | - | ||||||
3065 | key_consumed = true; | - | ||||||
3066 | if (d->tornoff) { | - | ||||||
3067 | close(); | - | ||||||
3068 | return; | - | ||||||
3069 | } | - | ||||||
3070 | { | - | ||||||
3071 | QPointer<QWidget> caused = d->causedPopup.widget; | - | ||||||
3072 | d->hideMenu(this); // hide after getting causedPopup | - | ||||||
3073 | #ifndef QT_NO_MENUBAR | - | ||||||
3074 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(caused)) { | - | ||||||
3075 | mb->d_func()->setCurrentAction(d->menuAction); | - | ||||||
3076 | mb->d_func()->setKeyboardMode(true); | - | ||||||
3077 | } | - | ||||||
3078 | #endif | - | ||||||
3079 | } | - | ||||||
3080 | } | - | ||||||
3081 | - | |||||||
3082 | if (!key_consumed) { // send to menu bar | - | ||||||
3083 | if ((!e->modifiers() || e->modifiers() == Qt::AltModifier || e->modifiers() == Qt::ShiftModifier) && | - | ||||||
3084 | e->text().length()==1) { | - | ||||||
3085 | bool activateAction = false; | - | ||||||
3086 | QAction *nextAction = 0; | - | ||||||
3087 | if (style()->styleHint(QStyle::SH_Menu_KeyboardSearch, 0, this) && !e->modifiers()) { | - | ||||||
3088 | int best_match_count = 0; | - | ||||||
3089 | d->searchBufferTimer.start(2000, this); | - | ||||||
3090 | d->searchBuffer += e->text(); | - | ||||||
3091 | for(int i = 0; i < d->actions.size(); ++i) { | - | ||||||
3092 | int match_count = 0; | - | ||||||
3093 | if (d->actionRects.at(i).isNull()) | - | ||||||
3094 | continue; | - | ||||||
3095 | QAction *act = d->actions.at(i); | - | ||||||
3096 | const QString act_text = act->text(); | - | ||||||
3097 | for(int c = 0; c < d->searchBuffer.size(); ++c) { | - | ||||||
3098 | if(act_text.indexOf(d->searchBuffer.at(c), 0, Qt::CaseInsensitive) != -1) | - | ||||||
3099 | ++match_count; | - | ||||||
3100 | } | - | ||||||
3101 | if(match_count > best_match_count) { | - | ||||||
3102 | best_match_count = match_count; | - | ||||||
3103 | nextAction = act; | - | ||||||
3104 | } | - | ||||||
3105 | } | - | ||||||
3106 | } | - | ||||||
3107 | #ifndef QT_NO_SHORTCUT | - | ||||||
3108 | else { | - | ||||||
3109 | int clashCount = 0; | - | ||||||
3110 | QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0; | - | ||||||
3111 | QChar c = e->text().at(0).toUpper(); | - | ||||||
3112 | for(int i = 0; i < d->actions.size(); ++i) { | - | ||||||
3113 | if (d->actionRects.at(i).isNull()) | - | ||||||
3114 | continue; | - | ||||||
3115 | QAction *act = d->actions.at(i); | - | ||||||
3116 | QKeySequence sequence = QKeySequence::mnemonic(act->text()); | - | ||||||
3117 | int key = sequence[0] & 0xffff; | - | ||||||
3118 | if (key == c.unicode()) { | - | ||||||
3119 | clashCount++; | - | ||||||
3120 | if (!first) | - | ||||||
3121 | first = act; | - | ||||||
3122 | if (act == d->currentAction) | - | ||||||
3123 | currentSelected = act; | - | ||||||
3124 | else if (!firstAfterCurrent && currentSelected) | - | ||||||
3125 | firstAfterCurrent = act; | - | ||||||
3126 | } | - | ||||||
3127 | } | - | ||||||
3128 | if (clashCount == 1) | - | ||||||
3129 | activateAction = true; | - | ||||||
3130 | if (clashCount >= 1) { | - | ||||||
3131 | if (clashCount == 1 || !currentSelected || !firstAfterCurrent) | - | ||||||
3132 | nextAction = first; | - | ||||||
3133 | else | - | ||||||
3134 | nextAction = firstAfterCurrent; | - | ||||||
3135 | } | - | ||||||
3136 | } | - | ||||||
3137 | #endif | - | ||||||
3138 | if (nextAction) { | - | ||||||
3139 | key_consumed = true; | - | ||||||
3140 | if(d->scroll) | - | ||||||
3141 | d->scrollMenu(nextAction, QMenuPrivate::QMenuScroller::ScrollCenter, false); | - | ||||||
3142 | d->setCurrentAction(nextAction, 0, QMenuPrivate::SelectedFromElsewhere, true); | - | ||||||
3143 | if (!nextAction->menu() && activateAction) { | - | ||||||
3144 | d->setSyncAction(); | - | ||||||
3145 | d->activateAction(nextAction, QAction::Trigger); | - | ||||||
3146 | } | - | ||||||
3147 | } | - | ||||||
3148 | } | - | ||||||
3149 | if (!key_consumed) { | - | ||||||
3150 | #ifndef QT_NO_MENUBAR | - | ||||||
3151 | if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->topCausedWidget())) { | - | ||||||
3152 | QAction *oldAct = mb->d_func()->currentAction; | - | ||||||
3153 | QApplication::sendEvent(mb, e); | - | ||||||
3154 | if (mb->d_func()->currentAction != oldAct) | - | ||||||
3155 | key_consumed = true; | - | ||||||
3156 | } | - | ||||||
3157 | #endif | - | ||||||
3158 | } | - | ||||||
3159 | - | |||||||
3160 | #ifdef Q_OS_WIN32 | - | ||||||
3161 | if (key_consumed && (e->key() == Qt::Key_Control || e->key() == Qt::Key_Shift || e->key() == Qt::Key_Meta)) | - | ||||||
3162 | QApplication::beep(); | - | ||||||
3163 | #endif // Q_OS_WIN32 | - | ||||||
3164 | } | - | ||||||
3165 | if (key_consumed) | - | ||||||
3166 | e->accept(); | - | ||||||
3167 | else | - | ||||||
3168 | e->ignore(); | - | ||||||
3169 | } | - | ||||||
3170 | - | |||||||
3171 | /*! | - | ||||||
3172 | \reimp | - | ||||||
3173 | */ | - | ||||||
3174 | void QMenu::mouseMoveEvent(QMouseEvent *e) | - | ||||||
3175 | { | - | ||||||
3176 | Q_D(QMenu); | - | ||||||
3177 | if (!isVisible() || d->aboutToHide || d->mouseEventTaken(e)) | - | ||||||
3178 | return; | - | ||||||
3179 | - | |||||||
3180 | d->motions++; | - | ||||||
3181 | if (d->motions == 0) | - | ||||||
3182 | return; | - | ||||||
3183 | - | |||||||
3184 | d->hasHadMouse = d->hasHadMouse || rect().contains(e->pos()); | - | ||||||
3185 | - | |||||||
3186 | QAction *action = d->actionAt(e->pos()); | - | ||||||
3187 | if ((!action || action->isSeparator()) && !d->sloppyState.enabled()) { | - | ||||||
3188 | if (d->hasHadMouse | - | ||||||
3189 | || (!d->currentAction || !d->currentAction->menu() || !d->currentAction->menu()->isVisible())) { | - | ||||||
3190 | d->setCurrentAction(action); | - | ||||||
3191 | } | - | ||||||
3192 | return; | - | ||||||
3193 | } | - | ||||||
3194 | - | |||||||
3195 | if (e->buttons()) | - | ||||||
3196 | d->mouseDown = this; | - | ||||||
3197 | - | |||||||
3198 | if (d->activeMenu) | - | ||||||
3199 | d->activeMenu->d_func()->setCurrentAction(0); | - | ||||||
3200 | - | |||||||
3201 | QMenuSloppyState::MouseEventResult sloppyEventResult = d->sloppyState.processMouseEvent(e->localPos(), action, d->currentAction); | - | ||||||
3202 | if (sloppyEventResult == QMenuSloppyState::EventShouldBePropagated) { | - | ||||||
3203 | d->setCurrentAction(action, d->mousePopupDelay); | - | ||||||
3204 | } else if (sloppyEventResult == QMenuSloppyState::EventDiscardsSloppyState) { | - | ||||||
3205 | d->sloppyState.reset(); | - | ||||||
3206 | d->hideMenu(d->activeMenu); | - | ||||||
3207 | } | - | ||||||
3208 | } | - | ||||||
3209 | - | |||||||
3210 | /*! | - | ||||||
3211 | \reimp | - | ||||||
3212 | */ | - | ||||||
3213 | void QMenu::enterEvent(QEvent *) | - | ||||||
3214 | { | - | ||||||
3215 | Q_D(QMenu); | - | ||||||
3216 | d->hasReceievedEnter = true; | - | ||||||
3217 | d->sloppyState.enter(); | - | ||||||
3218 | d->motions = -1; // force us to ignore the generate mouse move in mouseMoveEvent() | - | ||||||
3219 | } | - | ||||||
3220 | - | |||||||
3221 | /*! | - | ||||||
3222 | \reimp | - | ||||||
3223 | */ | - | ||||||
3224 | void QMenu::leaveEvent(QEvent *) | - | ||||||
3225 | { | - | ||||||
3226 | Q_D(QMenu); | - | ||||||
3227 | d->hasReceievedEnter = false; | - | ||||||
3228 | if (!d->activeMenu && d->currentAction) | - | ||||||
3229 | setActiveAction(0); | - | ||||||
3230 | } | - | ||||||
3231 | - | |||||||
3232 | /*! | - | ||||||
3233 | \reimp | - | ||||||
3234 | */ | - | ||||||
3235 | void | - | ||||||
3236 | QMenu::timerEvent(QTimerEvent *e) | - | ||||||
3237 | { | - | ||||||
3238 | Q_D(QMenu); | - | ||||||
3239 | if (d->scroll && d->scroll->scrollTimer.timerId() == e->timerId()) { | - | ||||||
3240 | d->scrollMenu((QMenuPrivate::QMenuScroller::ScrollDirection)d->scroll->scrollDirection); | - | ||||||
3241 | if (d->scroll->scrollFlags == QMenuPrivate::QMenuScroller::ScrollNone) | - | ||||||
3242 | d->scroll->scrollTimer.stop(); | - | ||||||
3243 | } else if (d->delayState.timer.timerId() == e->timerId()) { | - | ||||||
3244 | if (d->currentAction && !d->currentAction->menu()) | - | ||||||
3245 | return; | - | ||||||
3246 | d->delayState.stop(); | - | ||||||
3247 | d->sloppyState.stopTimer(); | - | ||||||
3248 | internalDelayedPopup(); | - | ||||||
3249 | } else if (d->sloppyState.isTimerId(e->timerId())) { | - | ||||||
3250 | d->sloppyState.timeout(); | - | ||||||
3251 | } else if(d->searchBufferTimer.timerId() == e->timerId()) { | - | ||||||
3252 | d->searchBuffer.clear(); | - | ||||||
3253 | } | - | ||||||
3254 | } | - | ||||||
3255 | - | |||||||
3256 | static void copyActionToPlatformItem(const QAction *action, QPlatformMenuItem *item, QPlatformMenu *itemsMenu) | - | ||||||
3257 | { | - | ||||||
3258 | item->setText(action->text()); | - | ||||||
3259 | item->setIsSeparator(action->isSeparator()); | - | ||||||
3260 | if (action->isIconVisibleInMenu()) {
| 0 | ||||||
3261 | item->setIcon(action->icon()); | - | ||||||
3262 | if (QWidget *w = action->parentWidget()) {
| 0 | ||||||
3263 | QStyleOption opt; | - | ||||||
3264 | opt.init(w); | - | ||||||
3265 | item->setIconSize(w->style()->pixelMetric(QStyle::PM_SmallIconSize, &opt, w)); | - | ||||||
3266 | } else { never executed: end of block | 0 | ||||||
3267 | QStyleOption opt; | - | ||||||
3268 | item->setIconSize(qApp->style()->pixelMetric(QStyle::PM_SmallIconSize, &opt, 0)); | - | ||||||
3269 | } never executed: end of block | 0 | ||||||
3270 | } else { | - | ||||||
3271 | item->setIcon(QIcon()); | - | ||||||
3272 | } never executed: end of block | 0 | ||||||
3273 | item->setVisible(action->isVisible()); | - | ||||||
3274 | item->setShortcut(action->shortcut()); | - | ||||||
3275 | item->setCheckable(action->isCheckable()); | - | ||||||
3276 | item->setChecked(action->isChecked()); | - | ||||||
3277 | item->setHasExclusiveGroup(action->actionGroup() && action->actionGroup()->isExclusive()); | - | ||||||
3278 | item->setFont(action->font()); | - | ||||||
3279 | item->setRole((QPlatformMenuItem::MenuRole) action->menuRole()); | - | ||||||
3280 | item->setEnabled(action->isEnabled()); | - | ||||||
3281 | - | |||||||
3282 | if (action->menu()) {
| 0 | ||||||
3283 | if (!action->menu()->platformMenu())
| 0 | ||||||
3284 | action->menu()->setPlatformMenu(itemsMenu->createSubMenu()); never executed: action->menu()->setPlatformMenu(itemsMenu->createSubMenu()); | 0 | ||||||
3285 | item->setMenu(action->menu()->platformMenu()); | - | ||||||
3286 | } else { never executed: end of block | 0 | ||||||
3287 | item->setMenu(0); | - | ||||||
3288 | } never executed: end of block | 0 | ||||||
3289 | } | - | ||||||
3290 | - | |||||||
3291 | /*! | - | ||||||
3292 | \reimp | - | ||||||
3293 | */ | - | ||||||
3294 | void QMenu::actionEvent(QActionEvent *e) | - | ||||||
3295 | { | - | ||||||
3296 | Q_D(QMenu); | - | ||||||
3297 | d->itemsDirty = 1; | - | ||||||
3298 | setAttribute(Qt::WA_Resized, false); | - | ||||||
3299 | if (d->tornPopup) | - | ||||||
3300 | d->tornPopup->syncWithMenu(this, e); | - | ||||||
3301 | if (e->type() == QEvent::ActionAdded) { | - | ||||||
3302 | if(!d->tornoff) { | - | ||||||
3303 | connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); | - | ||||||
3304 | connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered())); | - | ||||||
3305 | } | - | ||||||
3306 | if (QWidgetAction *wa = qobject_cast<QWidgetAction *>(e->action())) { | - | ||||||
3307 | QWidget *widget = wa->requestWidget(this); | - | ||||||
3308 | if (widget) | - | ||||||
3309 | d->widgetItems.insert(wa, widget); | - | ||||||
3310 | } | - | ||||||
3311 | } else if (e->type() == QEvent::ActionRemoved) { | - | ||||||
3312 | e->action()->disconnect(this); | - | ||||||
3313 | if (e->action() == d->currentAction) | - | ||||||
3314 | d->currentAction = 0; | - | ||||||
3315 | if (QWidgetAction *wa = qobject_cast<QWidgetAction *>(e->action())) { | - | ||||||
3316 | if (QWidget *widget = d->widgetItems.value(wa)) { | - | ||||||
3317 | #ifdef Q_OS_OSX | - | ||||||
3318 | QWidget *p = widget->parentWidget(); | - | ||||||
3319 | if (p != this && qobject_cast<QMacNativeWidget *>(p)) { | - | ||||||
3320 | // This widget was reparented into a native Mac view | - | ||||||
3321 | // (see QMenuPrivate::moveWidgetToPlatformItem). | - | ||||||
3322 | // Reset the parent and delete the native widget. | - | ||||||
3323 | widget->setParent(this); | - | ||||||
3324 | p->deleteLater(); | - | ||||||
3325 | } | - | ||||||
3326 | #endif | - | ||||||
3327 | wa->releaseWidget(widget); | - | ||||||
3328 | } | - | ||||||
3329 | } | - | ||||||
3330 | d->widgetItems.remove(e->action()); | - | ||||||
3331 | } | - | ||||||
3332 | - | |||||||
3333 | if (!d->platformMenu.isNull()) { | - | ||||||
3334 | if (e->type() == QEvent::ActionAdded) { | - | ||||||
3335 | QPlatformMenuItem *menuItem = d->platformMenu->createMenuItem(); | - | ||||||
3336 | menuItem->setTag(reinterpret_cast<quintptr>(e->action())); | - | ||||||
3337 | QObject::connect(menuItem, SIGNAL(activated()), e->action(), SLOT(trigger())); | - | ||||||
3338 | QObject::connect(menuItem, SIGNAL(hovered()), e->action(), SIGNAL(hovered())); | - | ||||||
3339 | copyActionToPlatformItem(e->action(), menuItem, d->platformMenu); | - | ||||||
3340 | QPlatformMenuItem* beforeItem = d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->before())); | - | ||||||
3341 | d->platformMenu->insertMenuItem(menuItem, beforeItem); | - | ||||||
3342 | } else if (e->type() == QEvent::ActionRemoved) { | - | ||||||
3343 | QPlatformMenuItem *menuItem = d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->action())); | - | ||||||
3344 | d->platformMenu->removeMenuItem(menuItem); | - | ||||||
3345 | delete menuItem; | - | ||||||
3346 | } else if (e->type() == QEvent::ActionChanged) { | - | ||||||
3347 | QPlatformMenuItem *menuItem = d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->action())); | - | ||||||
3348 | if (menuItem) { | - | ||||||
3349 | copyActionToPlatformItem(e->action(), menuItem, d->platformMenu); | - | ||||||
3350 | d->platformMenu->syncMenuItem(menuItem); | - | ||||||
3351 | } | - | ||||||
3352 | } | - | ||||||
3353 | - | |||||||
3354 | d->platformMenu->syncSeparatorsCollapsible(d->collapsibleSeparators); | - | ||||||
3355 | } | - | ||||||
3356 | - | |||||||
3357 | #if defined(Q_OS_WINCE) && !defined(QT_NO_MENUBAR) | - | ||||||
3358 | if (!d->wce_menu) | - | ||||||
3359 | d->wce_menu = new QMenuPrivate::QWceMenuPrivate; | - | ||||||
3360 | if (e->type() == QEvent::ActionAdded) | - | ||||||
3361 | d->wce_menu->addAction(e->action(), d->wce_menu->findAction(e->before())); | - | ||||||
3362 | else if (e->type() == QEvent::ActionRemoved) | - | ||||||
3363 | d->wce_menu->removeAction(e->action()); | - | ||||||
3364 | else if (e->type() == QEvent::ActionChanged) | - | ||||||
3365 | d->wce_menu->syncAction(e->action()); | - | ||||||
3366 | #endif | - | ||||||
3367 | - | |||||||
3368 | if (isVisible()) { | - | ||||||
3369 | d->updateActionRects(); | - | ||||||
3370 | resize(sizeHint()); | - | ||||||
3371 | update(); | - | ||||||
3372 | } | - | ||||||
3373 | } | - | ||||||
3374 | - | |||||||
3375 | /*! | - | ||||||
3376 | \internal | - | ||||||
3377 | */ | - | ||||||
3378 | void QMenu::internalDelayedPopup() | - | ||||||
3379 | { | - | ||||||
3380 | Q_D(QMenu); | - | ||||||
3381 | //hide the current item | - | ||||||
3382 | if (QMenu *menu = d->activeMenu) { | - | ||||||
3383 | if (d->activeMenu->menuAction() != d->currentAction) | - | ||||||
3384 | d->hideMenu(menu); | - | ||||||
3385 | } | - | ||||||
3386 | - | |||||||
3387 | if (!d->currentAction || !d->currentAction->isEnabled() || !d->currentAction->menu() || | - | ||||||
3388 | !d->currentAction->menu()->isEnabled() || d->currentAction->menu()->isVisible()) | - | ||||||
3389 | return; | - | ||||||
3390 | - | |||||||
3391 | //setup | - | ||||||
3392 | d->activeMenu = d->currentAction->menu(); | - | ||||||
3393 | d->activeMenu->d_func()->causedPopup.widget = this; | - | ||||||
3394 | d->activeMenu->d_func()->causedPopup.action = d->currentAction; | - | ||||||
3395 | - | |||||||
3396 | int subMenuOffset = style()->pixelMetric(QStyle::PM_SubMenuOverlap, 0, this); | - | ||||||
3397 | const QRect actionRect(d->actionRect(d->currentAction)); | - | ||||||
3398 | const QPoint rightPos(mapToGlobal(QPoint(actionRect.right() + subMenuOffset + 1, actionRect.top()))); | - | ||||||
3399 | - | |||||||
3400 | d->activeMenu->popup(rightPos); | - | ||||||
3401 | d->sloppyState.setSubMenuPopup(actionRect, d->currentAction, d->activeMenu); | - | ||||||
3402 | - | |||||||
3403 | #if !defined(Q_OS_DARWIN) | - | ||||||
3404 | // Send the leave event to the current menu - only active popup menu gets | - | ||||||
3405 | // mouse enter/leave events. Currently Cocoa is an exception, so disable | - | ||||||
3406 | // it there to avoid event duplication. | - | ||||||
3407 | if (underMouse()) { | - | ||||||
3408 | QEvent leaveEvent(QEvent::Leave); | - | ||||||
3409 | QCoreApplication::sendEvent(this, &leaveEvent); | - | ||||||
3410 | } | - | ||||||
3411 | #endif | - | ||||||
3412 | } | - | ||||||
3413 | - | |||||||
3414 | /*! | - | ||||||
3415 | \fn void QMenu::aboutToHide() | - | ||||||
3416 | \since 4.2 | - | ||||||
3417 | - | |||||||
3418 | This signal is emitted just before the menu is hidden from the user. | - | ||||||
3419 | - | |||||||
3420 | \sa aboutToShow(), hide() | - | ||||||
3421 | */ | - | ||||||
3422 | - | |||||||
3423 | /*! | - | ||||||
3424 | \fn void QMenu::aboutToShow() | - | ||||||
3425 | - | |||||||
3426 | This signal is emitted just before the menu is shown to the user. | - | ||||||
3427 | - | |||||||
3428 | \sa aboutToHide(), show() | - | ||||||
3429 | */ | - | ||||||
3430 | - | |||||||
3431 | /*! | - | ||||||
3432 | \fn void QMenu::triggered(QAction *action) | - | ||||||
3433 | - | |||||||
3434 | This signal is emitted when an action in this menu is triggered. | - | ||||||
3435 | - | |||||||
3436 | \a action is the action that caused the signal to be emitted. | - | ||||||
3437 | - | |||||||
3438 | Normally, you connect each menu action's \l{QAction::}{triggered()} signal | - | ||||||
3439 | to its own custom slot, but sometimes you will want to connect several | - | ||||||
3440 | actions to a single slot, for example, when you have a group of closely | - | ||||||
3441 | related actions, such as "left justify", "center", "right justify". | - | ||||||
3442 | - | |||||||
3443 | \note This signal is emitted for the main parent menu in a hierarchy. | - | ||||||
3444 | Hence, only the parent menu needs to be connected to a slot; sub-menus need | - | ||||||
3445 | not be connected. | - | ||||||
3446 | - | |||||||
3447 | \sa hovered(), QAction::triggered() | - | ||||||
3448 | */ | - | ||||||
3449 | - | |||||||
3450 | /*! | - | ||||||
3451 | \fn void QMenu::hovered(QAction *action) | - | ||||||
3452 | - | |||||||
3453 | This signal is emitted when a menu action is highlighted; \a action | - | ||||||
3454 | is the action that caused the signal to be emitted. | - | ||||||
3455 | - | |||||||
3456 | Often this is used to update status information. | - | ||||||
3457 | - | |||||||
3458 | \sa triggered(), QAction::hovered() | - | ||||||
3459 | */ | - | ||||||
3460 | - | |||||||
3461 | - | |||||||
3462 | /*!\internal | - | ||||||
3463 | */ | - | ||||||
3464 | void QMenu::setNoReplayFor(QWidget *noReplayFor) | - | ||||||
3465 | { | - | ||||||
3466 | d_func()->noReplayFor = noReplayFor; | - | ||||||
3467 | } | - | ||||||
3468 | - | |||||||
3469 | /*!\internal | - | ||||||
3470 | */ | - | ||||||
3471 | QPlatformMenu *QMenu::platformMenu() | - | ||||||
3472 | { | - | ||||||
3473 | - | |||||||
3474 | return d_func()->platformMenu; | - | ||||||
3475 | } | - | ||||||
3476 | - | |||||||
3477 | /*!\internal | - | ||||||
3478 | */ | - | ||||||
3479 | void QMenu::setPlatformMenu(QPlatformMenu *platformMenu) | - | ||||||
3480 | { | - | ||||||
3481 | d_func()->setPlatformMenu(platformMenu); | - | ||||||
3482 | d_func()->syncPlatformMenu(); | - | ||||||
3483 | } | - | ||||||
3484 | - | |||||||
3485 | /*! | - | ||||||
3486 | \property QMenu::separatorsCollapsible | - | ||||||
3487 | \since 4.2 | - | ||||||
3488 | - | |||||||
3489 | \brief whether consecutive separators should be collapsed | - | ||||||
3490 | - | |||||||
3491 | This property specifies whether consecutive separators in the menu | - | ||||||
3492 | should be visually collapsed to a single one. Separators at the | - | ||||||
3493 | beginning or the end of the menu are also hidden. | - | ||||||
3494 | - | |||||||
3495 | By default, this property is \c true. | - | ||||||
3496 | */ | - | ||||||
3497 | bool QMenu::separatorsCollapsible() const | - | ||||||
3498 | { | - | ||||||
3499 | Q_D(const QMenu); | - | ||||||
3500 | return d->collapsibleSeparators; | - | ||||||
3501 | } | - | ||||||
3502 | - | |||||||
3503 | void QMenu::setSeparatorsCollapsible(bool collapse) | - | ||||||
3504 | { | - | ||||||
3505 | Q_D(QMenu); | - | ||||||
3506 | if (d->collapsibleSeparators == collapse) | - | ||||||
3507 | return; | - | ||||||
3508 | - | |||||||
3509 | d->collapsibleSeparators = collapse; | - | ||||||
3510 | d->itemsDirty = 1; | - | ||||||
3511 | if (isVisible()) { | - | ||||||
3512 | d->updateActionRects(); | - | ||||||
3513 | update(); | - | ||||||
3514 | } | - | ||||||
3515 | if (!d->platformMenu.isNull()) | - | ||||||
3516 | d->platformMenu->syncSeparatorsCollapsible(collapse); | - | ||||||
3517 | } | - | ||||||
3518 | - | |||||||
3519 | /*! | - | ||||||
3520 | \property QMenu::toolTipsVisible | - | ||||||
3521 | \since 5.1 | - | ||||||
3522 | - | |||||||
3523 | \brief whether tooltips of menu actions should be visible | - | ||||||
3524 | - | |||||||
3525 | This property specifies whether action menu entries show | - | ||||||
3526 | their tooltip. | - | ||||||
3527 | - | |||||||
3528 | By default, this property is \c false. | - | ||||||
3529 | */ | - | ||||||
3530 | bool QMenu::toolTipsVisible() const | - | ||||||
3531 | { | - | ||||||
3532 | Q_D(const QMenu); | - | ||||||
3533 | return d->toolTipsVisible; | - | ||||||
3534 | } | - | ||||||
3535 | - | |||||||
3536 | void QMenu::setToolTipsVisible(bool visible) | - | ||||||
3537 | { | - | ||||||
3538 | Q_D(QMenu); | - | ||||||
3539 | if (d->toolTipsVisible == visible) | - | ||||||
3540 | return; | - | ||||||
3541 | - | |||||||
3542 | d->toolTipsVisible = visible; | - | ||||||
3543 | } | - | ||||||
3544 | - | |||||||
3545 | QT_END_NAMESPACE | - | ||||||
3546 | - | |||||||
3547 | // for private slots | - | ||||||
3548 | #include "moc_qmenu.cpp" | - | ||||||
3549 | #include "qmenu.moc" | - | ||||||
3550 | - | |||||||
3551 | #endif // QT_NO_MENU | - | ||||||
Source code | Switch to Preprocessed file |