Gui: extend Workbench class to allow to define permanent menu items

This commit is contained in:
wmayer
2022-02-14 16:47:32 +01:00
parent 20f31f409f
commit 327ed41fd0
4 changed files with 61 additions and 0 deletions

View File

@@ -84,6 +84,23 @@ MenuItem* MenuItem::findItem(const std::string& name)
return 0;
}
MenuItem* MenuItem::findParentOf(const std::string& name)
{
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it)
{
if ((*it)->_name == name)
return this;
}
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it)
{
if ((*it)->findParentOf(name))
return *it;
}
return nullptr;
}
MenuItem* MenuItem::copy() const
{
MenuItem* root = new MenuItem;

View File

@@ -26,6 +26,7 @@
#include <string>
#include <QStringList>
#include <FCGlobal.h>
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;

View File

@@ -370,6 +370,38 @@ void Workbench::createLinkMenu(MenuItem *item) {
*item << linkMenu;
}
std::vector<std::pair<std::string, std::string>> 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<std::string, std::string>& 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;

View File

@@ -105,6 +105,12 @@ public:
std::list<std::string> listMenus() const;
//// Shows a list of all command bars
std::list<std::string> 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<std::pair<std::string, std::string>> staticMenuItems;
};
/**