diff options
Diffstat (limited to 'src/widgets/widgets/qmenu.cpp')
-rw-r--r-- | src/widgets/widgets/qmenu.cpp | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp index 865e3b2fb6..64cd87df38 100644 --- a/src/widgets/widgets/qmenu.cpp +++ b/src/widgets/widgets/qmenu.cpp @@ -41,6 +41,7 @@ #include <QtWidgets/private/qtwidgetsglobal_p.h> +#include "qactiongroup.h" #include "qdebug.h" #include "qstyle.h" #include "qevent.h" @@ -72,7 +73,9 @@ #include "qtoolbutton.h" #endif #include "qpushbutton.h" +#if QT_CONFIG(tooltip) #include "qtooltip.h" +#endif #include <qwindow.h> #include <private/qpushbutton_p.h> #include <private/qaction_p.h> @@ -157,10 +160,11 @@ public: Q_D(QTornOffMenu); if(menu != d->causedMenu) return; + auto action = static_cast<QAction *>(act->action()); if (act->type() == QEvent::ActionAdded) { - insertAction(act->before(), act->action()); + insertAction(static_cast<QAction *>(act->before()), action); } else if (act->type() == QEvent::ActionRemoved) - removeAction(act->action()); + removeAction(action); } void actionEvent(QActionEvent *e) override { @@ -197,7 +201,8 @@ void QMenuPrivate::init() #endif q->setAttribute(Qt::WA_X11NetWmWindowTypePopupMenu); defaultMenuAction = menuAction = new QAction(q); - menuAction->d_func()->menu = q; + menuAction->setMenu(q); // this calls setOverrideMenuAction + setOverrideMenuAction(nullptr); QObject::connect(menuAction, &QAction::changed, [this] { if (!tornPopup.isNull()) tornPopup->updateWindowTitle(); @@ -304,7 +309,7 @@ QPlatformMenuItem * QMenuPrivate::insertActionInPlatformMenu(const QAction *acti int QMenuPrivate::scrollerHeight() const { Q_Q(const QMenu); - return qMax(QApplication::globalStrut().height(), q->style()->pixelMetric(QStyle::PM_MenuScrollerHeight, nullptr, q)); + return q->style()->pixelMetric(QStyle::PM_MenuScrollerHeight, nullptr, q); } // Windows and KDE allow menus to cover the taskbar, while GNOME and macOS @@ -403,7 +408,9 @@ void QMenuPrivate::updateActionRects(const QRect &screen) const //calculate size QFontMetrics qfm = q->fontMetrics(); bool previousWasSeparator = true; // this is true to allow removing the leading separators +#if QT_CONFIG(shortcut) const bool contextMenu = isContextMenu(); +#endif for(int i = 0; i <= lastVisibleAction; i++) { QAction *action = actions.at(i); const bool isSection = action->isSeparator() && (!action->text().isEmpty() || !action->icon().isNull()); @@ -434,12 +441,12 @@ void QMenuPrivate::updateActionRects(const QRect &screen) const if (t != -1) { tabWidth = qMax(int(tabWidth), qfm.horizontalAdvance(s.mid(t+1))); s = s.left(t); - #ifndef QT_NO_SHORTCUT +#if QT_CONFIG(shortcut) } else if (action->isShortcutVisibleInContextMenu() || !contextMenu) { QKeySequence seq = action->shortcut(); if (!seq.isEmpty()) tabWidth = qMax(int(tabWidth), qfm.horizontalAdvance(seq.toString(QKeySequence::NativeText))); - #endif +#endif } sz.setWidth(fm.boundingRect(QRect(), Qt::TextSingleLine | Qt::TextShowMnemonic, s).width()); sz.setHeight(qMax(fm.height(), qfm.height())); @@ -471,7 +478,7 @@ void QMenuPrivate::updateActionRects(const QRect &screen) const max_column_width += tabWidth; //finally add in the tab width if (!tornoff || (tornoff && scroll)) { // exclude non-scrollable tear-off menu since the tear-off menu has a fixed size - const int sfcMargin = style->sizeFromContents(QStyle::CT_Menu, &opt, QApplication::globalStrut(), q).width() - QApplication::globalStrut().width(); + const int sfcMargin = style->sizeFromContents(QStyle::CT_Menu, &opt, QSize(0, 0), q).width(); const int min_column_width = q->minimumWidth() - (sfcMargin + leftmargin + rightmargin + 2 * (fw + hmargin)); max_column_width = qMax(min_column_width, max_column_width); } @@ -1770,12 +1777,14 @@ QAction *QMenu::addAction(const QIcon &icon, const QString &text) \sa QWidget::addAction() */ -QAction *QMenu::addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut) +QAction *QMenu::addAction(const QString &text, const QObject *receiver, const char* member +#if QT_CONFIG(shortcut) + , const QKeySequence &shortcut +#endif + ) { QAction *action = new QAction(text, this); -#ifdef QT_NO_SHORTCUT - Q_UNUSED(shortcut); -#else +#if QT_CONFIG(shortcut) action->setShortcut(shortcut); #endif QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); @@ -1863,12 +1872,14 @@ QAction *QMenu::addAction(const QString &text, const QObject *receiver, const ch \sa QWidget::addAction() */ QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, - const char* member, const QKeySequence &shortcut) + const char* member +#if QT_CONFIG(shortcut) + , const QKeySequence &shortcut +#endif + ) { QAction *action = new QAction(icon, text, this); -#ifdef QT_NO_SHORTCUT - Q_UNUSED(shortcut); -#else +#if QT_CONFIG(shortcut) action->setShortcut(shortcut); #endif QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); @@ -2237,7 +2248,7 @@ void QMenu::clear() for(int i = 0; i < acts.size(); i++) { removeAction(acts[i]); - if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty()) + if (acts[i]->parent() == this && acts[i]->d_func()->associatedObjects.isEmpty()) delete acts[i]; } } @@ -2299,8 +2310,7 @@ QSize QMenu::sizeHint() const s.rwidth() += style()->pixelMetric(QStyle::PM_MenuHMargin, &opt, this) + fw + d->rightmargin; s.rheight() += style()->pixelMetric(QStyle::PM_MenuVMargin, &opt, this) + fw + d->bottommargin; - return style()->sizeFromContents(QStyle::CT_Menu, &opt, - s.expandedTo(QApplication::globalStrut()), this); + return style()->sizeFromContents(QStyle::CT_Menu, &opt, s, this); } /*! @@ -3021,7 +3031,7 @@ QMenu::event(QEvent *e) if (d->currentAction) d->popupAction(d->currentAction, 0, false); break; -#ifndef QT_NO_TOOLTIP +#if QT_CONFIG(tooltip) case QEvent::ToolTip: if (d->toolTipsVisible) { const QHelpEvent *ev = static_cast<const QHelpEvent*>(e); @@ -3033,7 +3043,7 @@ QMenu::event(QEvent *e) } } break; -#endif // QT_NO_TOOLTIP +#endif // QT_CONFIG(tooltip) #if QT_CONFIG(whatsthis) case QEvent::QueryWhatsThis: e->setAccepted(d->whatsThis.size()); @@ -3560,15 +3570,16 @@ void QMenu::actionEvent(QActionEvent *e) wa->releaseWidget(widget); } } - d->widgetItems.remove(e->action()); + d->widgetItems.remove(static_cast<QAction *>(e->action())); } if (!d->platformMenu.isNull()) { + auto action = static_cast<QAction *>(e->action()); if (e->type() == QEvent::ActionAdded) { QPlatformMenuItem *beforeItem = e->before() ? d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->before())) : nullptr; - d->insertActionInPlatformMenu(e->action(), beforeItem); + d->insertActionInPlatformMenu(action, beforeItem); } else if (e->type() == QEvent::ActionRemoved) { QPlatformMenuItem *menuItem = d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->action())); d->platformMenu->removeMenuItem(menuItem); @@ -3576,7 +3587,7 @@ void QMenu::actionEvent(QActionEvent *e) } else if (e->type() == QEvent::ActionChanged) { QPlatformMenuItem *menuItem = d->platformMenu->menuItemForTag(reinterpret_cast<quintptr>(e->action())); if (menuItem) { - d->copyActionToPlatformItem(e->action(), menuItem); + d->copyActionToPlatformItem(action, menuItem); d->platformMenu->syncMenuItem(menuItem); } } |