From 327ed41fd0a01df4f7e6de5e358de1bf4436e72c Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 14 Feb 2022 16:47:32 +0100 Subject: [PATCH] Gui: extend Workbench class to allow to define permanent menu items --- src/Gui/MenuManager.cpp | 17 +++++++++++++++++ src/Gui/MenuManager.h | 2 ++ src/Gui/Workbench.cpp | 33 +++++++++++++++++++++++++++++++++ src/Gui/Workbench.h | 9 +++++++++ 4 files changed, 61 insertions(+) diff --git a/src/Gui/MenuManager.cpp b/src/Gui/MenuManager.cpp index 8393923723..f84058234a 100644 --- a/src/Gui/MenuManager.cpp +++ b/src/Gui/MenuManager.cpp @@ -84,6 +84,23 @@ MenuItem* MenuItem::findItem(const std::string& name) return 0; } +MenuItem* MenuItem::findParentOf(const std::string& name) +{ + for (QList::Iterator it = _items.begin(); it != _items.end(); ++it) + { + if ((*it)->_name == name) + return this; + } + + for (QList::Iterator it = _items.begin(); it != _items.end(); ++it) + { + if ((*it)->findParentOf(name)) + return *it; + } + + return nullptr; +} + MenuItem* MenuItem::copy() const { MenuItem* root = new MenuItem; diff --git a/src/Gui/MenuManager.h b/src/Gui/MenuManager.h index 52bacbbb0c..66296d58bc 100644 --- a/src/Gui/MenuManager.h +++ b/src/Gui/MenuManager.h @@ -26,6 +26,7 @@ #include #include +#include class QAction; class QMenu; @@ -46,6 +47,7 @@ public: bool hasItems() const; MenuItem* findItem(const std::string&); + MenuItem* findParentOf(const std::string&); MenuItem* copy() const; uint count() const; diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index 82164c4ec1..966bef1d1d 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -370,6 +370,38 @@ void Workbench::createLinkMenu(MenuItem *item) { *item << linkMenu; } +std::vector> Workbench::staticMenuItems; + +void Workbench::addPermanentMenuItem(const std::string& cmd, const std::string& after) +{ + staticMenuItems.emplace_back(cmd, after); +} + +void Workbench::removePermanentMenuItem(const std::string& cmd) +{ + auto it = std::find_if(staticMenuItems.begin(), staticMenuItems.end(), [cmd](const std::pair& p) { + return (p.first == cmd); + }); + + if (it != staticMenuItems.end()) + staticMenuItems.erase(it); +} + +void Workbench::addPermanentMenuItems(MenuItem* mb) const +{ + for (const auto& it : staticMenuItems) { + MenuItem* par = mb->findParentOf(it.second); + if (par) { + Gui::MenuItem* item = par->findItem(it.second); + item = par->afterItem(item); + + Gui::MenuItem* add = new Gui::MenuItem(); + add->setCommand(it.first); + par->insertItem(item, add); + } + } +} + void Workbench::activated() { } @@ -395,6 +427,7 @@ bool Workbench::activate() delete dw; MenuItem* mb = setupMenuBar(); + addPermanentMenuItems(mb); MenuManager::getInstance()->setup( mb ); delete mb; diff --git a/src/Gui/Workbench.h b/src/Gui/Workbench.h index 46c70b9eeb..a56ee2d7de 100644 --- a/src/Gui/Workbench.h +++ b/src/Gui/Workbench.h @@ -105,6 +105,12 @@ public: std::list listMenus() const; //// Shows a list of all command bars std::list listCommandbars() const; + /// Add a permanent menu item \a cmd after an existing command \a after. + /// Permanent menu items are always added independent of what the active workbench is. + /// Adding it will only fail if the item \a after doesn't exist. + static void addPermanentMenuItem(const std::string& cmd, const std::string& after); + /// Removes the command \a cmd from the permanent menu items. + static void removePermanentMenuItem(const std::string& cmd); protected: /** Returns a MenuItem tree structure of menus for this workbench. */ @@ -115,6 +121,8 @@ protected: virtual ToolBarItem* setupCommandBars() const=0; /** Returns a DockWindowItems structure of dock windows this workbench. */ virtual DockWindowItems* setupDockWindows() const=0; + /** Add permanent menu items to the structure */ + void addPermanentMenuItems(MenuItem*) const; private: /** @@ -127,6 +135,7 @@ private: private: std::string _name; + static std::vector> staticMenuItems; }; /**