summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets
diff options
context:
space:
mode:
authorMartin Gräßlin <mgraesslin@kde.org>2014-01-29 10:57:34 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-17 16:47:05 +0100
commit824f080468b02ad0a82e42bbd120b55c0bce1769 (patch)
treeba6a531b43016bb0f31c4e674a7ba2922d7c5623 /src/widgets/widgets
parent6aef733a7a80cc937ba67c982a41a7338379d401 (diff)
Allow QPlatformSystemTrayIcon to create the QPlatformMenu
In case the QPlatformTheme does not provide a QPlatformMenu the QPlatformSystemTrayIcon is not able to forward the menu because the QPlatformMenu pointer passed to updateMenu is always null. Providing a QPlatformMenu in the QPlatformTheme implementation should not be a requirement for having the menu in the system tray icon. There are cases where no QPlatformMenu should be created by the theme, e.g. if the X11 implementation of QSystemTrayIcon gets provided by the xcb plugin. The change adds a virtual method to QPlatformSystemTrayIcon to create a QPlatformMenu. This method is called from the QPA implementation of QSystemTrayIcon if the QMenu's platformMenu is not present. Thus the system tray icon is able to provide a custom implementation of the menu. This gets installed through a new internal method in QMenu to set the platform menu. It creates the required connections and sync the state to the QMenu to the newly created QPlatformMenu. Last but not least QPlatformMenu is extended by a method to create a QPlatformMenuItem. The default implementation delegates to the platform theme. This allows to provide the menu item implementation for the system tray icon without providing the QPlatformMenuItem through the platform theme. Change-Id: I17234bd8bcf8c05f8bd786feff0cf8f860430e82 Reviewed-by: Kevin Krammer <kevin.krammer@kdab.com> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'src/widgets/widgets')
-rw-r--r--src/widgets/widgets/qmenu.cpp48
-rw-r--r--src/widgets/widgets/qmenu.h1
-rw-r--r--src/widgets/widgets/qmenu_p.h2
3 files changed, 48 insertions, 3 deletions
diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp
index d1b0da1a55..edfeb840b1 100644
--- a/src/widgets/widgets/qmenu.cpp
+++ b/src/widgets/widgets/qmenu.cpp
@@ -153,13 +153,48 @@ void QMenuPrivate::init()
scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone;
}
- platformMenu = QGuiApplicationPrivate::platformTheme()->createPlatformMenu();
+ setPlatformMenu(QGuiApplicationPrivate::platformTheme()->createPlatformMenu());
+}
+
+void QMenuPrivate::setPlatformMenu(QPlatformMenu *menu)
+{
+ Q_Q(QMenu);
+ if (!platformMenu.isNull() && !platformMenu->parent())
+ delete platformMenu.data();
+
+ platformMenu = menu;
if (!platformMenu.isNull()) {
QObject::connect(platformMenu, SIGNAL(aboutToShow()), q, SIGNAL(aboutToShow()));
QObject::connect(platformMenu, SIGNAL(aboutToHide()), q, SIGNAL(aboutToHide()));
}
}
+// forward declare function
+static void copyActionToPlatformItem(const QAction *action, QPlatformMenuItem* item);
+
+void QMenuPrivate::syncPlatformMenu()
+{
+ Q_Q(QMenu);
+ if (platformMenu.isNull())
+ return;
+
+ QPlatformMenuItem *beforeItem = Q_NULLPTR;
+ QListIterator<QAction*> it(q->actions());
+ it.toBack();
+ while (it.hasPrevious()) {
+ QPlatformMenuItem *menuItem = platformMenu->createMenuItem();
+ QAction *action = it.previous();
+ menuItem->setTag(reinterpret_cast<quintptr>(action));
+ QObject::connect(menuItem, SIGNAL(activated()), action, SLOT(trigger()));
+ QObject::connect(menuItem, SIGNAL(hovered()), action, SIGNAL(hovered()));
+ copyActionToPlatformItem(action, menuItem);
+ platformMenu->insertMenuItem(menuItem, beforeItem);
+ beforeItem = menuItem;
+ }
+ platformMenu->syncSeparatorsCollapsible(collapsibleSeparators);
+ platformMenu->setEnabled(q->isEnabled());
+}
+
int QMenuPrivate::scrollerHeight() const
{
Q_Q(const QMenu);
@@ -2976,8 +3011,7 @@ void QMenu::actionEvent(QActionEvent *e)
if (!d->platformMenu.isNull()) {
if (e->type() == QEvent::ActionAdded) {
- QPlatformMenuItem *menuItem =
- QGuiApplicationPrivate::platformTheme()->createPlatformMenuItem();
+ QPlatformMenuItem *menuItem = d->platformMenu->createMenuItem();
menuItem->setTag(reinterpret_cast<quintptr>(e->action()));
QObject::connect(menuItem, SIGNAL(activated()), e->action(), SLOT(trigger()));
QObject::connect(menuItem, SIGNAL(hovered()), e->action(), SIGNAL(hovered()));
@@ -3154,6 +3188,14 @@ QPlatformMenu *QMenu::platformMenu()
return d_func()->platformMenu;
}
+/*!\internal
+*/
+void QMenu::setPlatformMenu(QPlatformMenu *platformMenu)
+{
+ d_func()->setPlatformMenu(platformMenu);
+ d_func()->syncPlatformMenu();
+}
+
/*!
\property QMenu::separatorsCollapsible
\since 4.2
diff --git a/src/widgets/widgets/qmenu.h b/src/widgets/widgets/qmenu.h
index 7a128e871c..8a8eaf3bae 100644
--- a/src/widgets/widgets/qmenu.h
+++ b/src/widgets/widgets/qmenu.h
@@ -140,6 +140,7 @@ public:
void setNoReplayFor(QWidget *widget);
QPlatformMenu *platformMenu();
+ void setPlatformMenu(QPlatformMenu *platformMenu);
#ifdef Q_OS_WINCE
HMENU wceMenu();
diff --git a/src/widgets/widgets/qmenu_p.h b/src/widgets/widgets/qmenu_p.h
index 71a3fca237..afd34a5c47 100644
--- a/src/widgets/widgets/qmenu_p.h
+++ b/src/widgets/widgets/qmenu_p.h
@@ -108,6 +108,8 @@ public:
#endif
}
void init();
+ void setPlatformMenu(QPlatformMenu *menu);
+ void syncPlatformMenu();
static QMenuPrivate *get(QMenu *m) { return m->d_func(); }
int scrollerHeight() const;