From e4d79e1fdeb6b26ba0b12b578daacf7cd672b960 Mon Sep 17 00:00:00 2001
From: Dmitry Shachnev <mitya57@gmail.com>
Date: Sat, 27 Feb 2016 20:55:41 +0300
Subject: [PATCH] Add createMenu() method to QPlatformMenuBar

The D-Bus platform menus are only useful inside menu bars and system
tray icons, and should not be created for other cases (like the context
menus).

This adds a new virtual createMenu() method to QPlatformMenuBar class,
analogous to the already existing QPlatformSystemTrayIcon::createMenu()
method, and adds support for it to QMenuBar.

The D-Bus platform menus are now created from QDBusMenuBar class. As an
additional benefit, we no longer have to check whether the AppMenu
Registrar service is present for every created menu, and check it only
once (this should speed things a bit up).

Change-Id: Ic7d94e58a501ab9d2954aeb342ebd46ef8e62d49
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
---
 src/gui/kernel/qplatformmenu.h                      |  1 +
 src/platformsupport/dbusmenu/qdbusmenubar.cpp       |  5 +++++
 src/platformsupport/dbusmenu/qdbusmenubar_p.h       |  1 +
 .../themes/genericunix/qgenericunixthemes.cpp       | 21 ---------------------
 .../themes/genericunix/qgenericunixthemes_p.h       |  3 ---
 src/widgets/widgets/qmenubar.cpp                    | 21 ++++++++++++++-------
 src/widgets/widgets/qmenubar_p.h                    |  1 +
 7 files changed, 22 insertions(+), 31 deletions(-)

--- a/src/gui/kernel/qplatformmenu.h
+++ b/src/gui/kernel/qplatformmenu.h
@@ -141,6 +141,7 @@ public:
     virtual void handleReparent(QWindow *newParentWindow) = 0;
 
     virtual QPlatformMenu *menuForTag(quintptr tag) const = 0;
+    virtual QPlatformMenu *createMenu() const { return nullptr; }
 };
 
 QT_END_NAMESPACE
--- a/src/platformsupport/dbusmenu/qdbusmenubar.cpp
+++ b/src/platformsupport/dbusmenu/qdbusmenubar.cpp
@@ -133,6 +133,11 @@ QPlatformMenu *QDBusMenuBar::menuForTag(
     return nullptr;
 }
 
+QPlatformMenu *QDBusMenuBar::createMenu() const
+{
+    return new QDBusPlatformMenu;
+}
+
 void QDBusMenuBar::registerMenuBar()
 {
     static uint menuBarId = 0;
--- a/src/platformsupport/dbusmenu/qdbusmenubar_p.h
+++ b/src/platformsupport/dbusmenu/qdbusmenubar_p.h
@@ -72,6 +72,7 @@ public:
     void syncMenu(QPlatformMenu *menu) Q_DECL_OVERRIDE;
     void handleReparent(QWindow *newParentWindow) Q_DECL_OVERRIDE;
     QPlatformMenu *menuForTag(quintptr tag) const Q_DECL_OVERRIDE;
+    QPlatformMenu *createMenu() const Q_DECL_OVERRIDE;
 
 private:
     QDBusPlatformMenu *m_menu;
--- a/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp
+++ b/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp
@@ -185,13 +185,6 @@ QStringList QGenericUnixTheme::xdgIconTh
 }
 
 #ifndef QT_NO_DBUS
-QPlatformMenu *QGenericUnixTheme::createPlatformMenu() const
-{
-    if (isDBusGlobalMenuAvailable())
-        return new QDBusPlatformMenu();
-    return nullptr;
-}
-
 QPlatformMenuBar *QGenericUnixTheme::createPlatformMenuBar() const
 {
     if (isDBusGlobalMenuAvailable())
@@ -588,13 +581,6 @@ QPlatformTheme *QKdeTheme::createKdeThem
 }
 
 #ifndef QT_NO_DBUS
-QPlatformMenu *QKdeTheme::createPlatformMenu() const
-{
-    if (isDBusGlobalMenuAvailable())
-        return new QDBusPlatformMenu();
-    return nullptr;
-}
-
 QPlatformMenuBar *QKdeTheme::createPlatformMenuBar() const
 {
     if (isDBusGlobalMenuAvailable())
@@ -700,13 +686,6 @@ QString QGnomeTheme::gtkFontName() const
 }
 
 #ifndef QT_NO_DBUS
-QPlatformMenu *QGnomeTheme::createPlatformMenu() const
-{
-    if (isDBusGlobalMenuAvailable())
-        return new QDBusPlatformMenu();
-    return nullptr;
-}
-
 QPlatformMenuBar *QGnomeTheme::createPlatformMenuBar() const
 {
     if (isDBusGlobalMenuAvailable())
--- a/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h
+++ b/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h
@@ -80,7 +80,6 @@ public:
 
     static QStringList xdgIconThemePaths();
 #ifndef QT_NO_DBUS
-    QPlatformMenu *createPlatformMenu() const Q_DECL_OVERRIDE;
     QPlatformMenuBar *createPlatformMenuBar() const Q_DECL_OVERRIDE;
 #endif
 #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON)
@@ -106,7 +105,6 @@ public:
 
     const QFont *font(Font type) const Q_DECL_OVERRIDE;
 #ifndef QT_NO_DBUS
-    QPlatformMenu *createPlatformMenu() const Q_DECL_OVERRIDE;
     QPlatformMenuBar *createPlatformMenuBar() const Q_DECL_OVERRIDE;
 #endif
 #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON)
@@ -130,7 +128,6 @@ public:
 
     virtual QString gtkFontName() const;
 #ifndef QT_NO_DBUS
-    QPlatformMenu *createPlatformMenu() const Q_DECL_OVERRIDE;
     QPlatformMenuBar *createPlatformMenuBar() const Q_DECL_OVERRIDE;
 #endif
 #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON)
--- a/src/widgets/widgets/qmenubar.cpp
+++ b/src/widgets/widgets/qmenubar.cpp
@@ -1204,12 +1204,19 @@ void QMenuBar::leaveEvent(QEvent *)
         d->setCurrentAction(0);
 }
 
-QPlatformMenu *getPlatformMenu(QAction *action)
+QPlatformMenu *QMenuBarPrivate::getPlatformMenu(QAction *action)
 {
     if (!action || !action->menu())
         return 0;
 
-    return action->menu()->platformMenu();
+    QPlatformMenu *platformMenu = action->menu()->platformMenu();
+    if (!platformMenu && platformMenuBar) {
+        platformMenu = platformMenuBar->createMenu();
+        if (platformMenu)
+            action->menu()->setPlatformMenu(platformMenu);
+    }
+
+    return platformMenu;
 }
 
 /*!
@@ -1230,14 +1237,14 @@ void QMenuBar::actionEvent(QActionEvent
             return;
 
         if (e->type() == QEvent::ActionAdded) {
-            QPlatformMenu *menu = getPlatformMenu(e->action());
+            QPlatformMenu *menu = d->getPlatformMenu(e->action());
             if (menu) {
                 QPlatformMenu* beforeMenu = NULL;
                 for (int beforeIndex = d->indexOf(e->action()) + 1;
                      !beforeMenu && (beforeIndex < actions().size());
                      ++beforeIndex)
                 {
-                    beforeMenu = getPlatformMenu(actions().at(beforeIndex));
+                    beforeMenu = d->getPlatformMenu(actions().at(beforeIndex));
                 }
 
                 menu->setTag(reinterpret_cast<quintptr>(e->action()));
@@ -1245,12 +1252,12 @@ void QMenuBar::actionEvent(QActionEvent
                 d->platformMenuBar->insertMenu(menu, beforeMenu);
             }
         } else if (e->type() == QEvent::ActionRemoved) {
-            QPlatformMenu *menu = getPlatformMenu(e->action());
+            QPlatformMenu *menu = d->getPlatformMenu(e->action());
             if (menu)
                 d->platformMenuBar->removeMenu(menu);
         } else if (e->type() == QEvent::ActionChanged) {
             QPlatformMenu* cur = d->platformMenuBar->menuForTag(reinterpret_cast<quintptr>(e->action()));
-            QPlatformMenu *menu = getPlatformMenu(e->action());
+            QPlatformMenu *menu = d->getPlatformMenu(e->action());
 
             // the menu associated with the action can change, need to
             // remove and/or insert the new platform menu
@@ -1265,7 +1272,7 @@ void QMenuBar::actionEvent(QActionEvent
                          !beforeMenu && (beforeIndex < actions().size());
                          ++beforeIndex)
                     {
-                        beforeMenu = getPlatformMenu(actions().at(beforeIndex));
+                        beforeMenu = d->getPlatformMenu(actions().at(beforeIndex));
                     }
                     d->platformMenuBar->insertMenu(menu, beforeMenu);
                 }
--- a/src/widgets/widgets/qmenubar_p.h
+++ b/src/widgets/widgets/qmenubar_p.h
@@ -137,6 +137,7 @@ public:
 
     QBasicTimer autoReleaseTimer;
     QPlatformMenuBar *platformMenuBar;
+    QPlatformMenu *getPlatformMenu(QAction *action);
 
     inline int indexOf(QAction *act) const { return q_func()->actions().indexOf(act); }
 
