From 2a41f359f71e69309e294a2803a8399ed1e4a4d6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 16 Jul 2022 19:27:22 +0200 Subject: [PATCH] Gui: implement mechanism to set a custom icon name or tooltip of a preferences group --- src/Gui/DlgPreferencesImp.cpp | 47 ++++++++++++++++++++++++++------- src/Gui/DlgPreferencesImp.h | 7 +++++ src/Mod/Part/Gui/AppPartGui.cpp | 2 ++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/Gui/DlgPreferencesImp.cpp b/src/Gui/DlgPreferencesImp.cpp index 64c3818033..6d2cb77f58 100644 --- a/src/Gui/DlgPreferencesImp.cpp +++ b/src/Gui/DlgPreferencesImp.cpp @@ -52,6 +52,8 @@ const int DlgPreferencesImp::GroupNameRole = Qt::UserRole; /* TRANSLATOR Gui::Dialog::DlgPreferencesImp */ std::list DlgPreferencesImp::_pages; +std::map DlgPreferencesImp::_groupMap; + DlgPreferencesImp* DlgPreferencesImp::_activeDialog = nullptr; /** @@ -116,6 +118,10 @@ QTabWidget* DlgPreferencesImp::createTabForGroup(const std::string &groupName) { QString groupNameQString = QString::fromStdString(groupName); + std::string fileName = groupName; + QString tooltip; + getGroupData(groupName, fileName, tooltip); + QTabWidget* tabWidget = new QTabWidget; ui->tabWidgetStack->addWidget(tabWidget); tabWidget->setProperty("GroupName", QVariant(groupNameQString)); @@ -123,15 +129,8 @@ QTabWidget* DlgPreferencesImp::createTabForGroup(const std::string &groupName) QListWidgetItem* item = new QListWidgetItem(ui->listBox); item->setData(GroupNameRole, QVariant(groupNameQString)); item->setText(QObject::tr(groupNameQString.toLatin1())); - // for Part/PD we need another tooltip since this group is for 2 WBs - if (groupName.compare("Part/Part Design") == 0) - item->setToolTip(QObject::tr(QString::fromStdString("Part and Part Design workbench").toLatin1())); - else - item->setToolTip(QObject::tr(groupNameQString.toLatin1())); - std::string fileName = groupName; - // for Part/PD the filename cannot be groupName since this group is for 2 WBs - if (groupName.compare("Part/Part Design") == 0) - fileName = "Part design"; + item->setToolTip(tooltip); + for (auto &ch : fileName) { if (ch == ' ') ch = '_'; @@ -238,6 +237,36 @@ void DlgPreferencesImp::removePage(const std::string& className, const std::stri } } +/** + * Sets a custom icon name or tool tip for a given group. + */ +void DlgPreferencesImp::setGroupData(const std::string& name, const std::string& icon, const QString& tip) +{ + Group group; + group.iconName = icon; + group.tooltip = tip; + _groupMap[name] = group; +} + +/** + * Gets the icon name or tool tip for a given group. If no custom name or tool tip is given + * they are determined from the group name. + */ +void DlgPreferencesImp::getGroupData(const std::string& group, std::string& icon, QString& tip) +{ + auto it = _groupMap.find(group); + if (it != _groupMap.end()) { + icon = it->second.iconName; + tip = it->second.tooltip; + } + + if (icon.empty()) + icon = group; + + if (tip.isEmpty()) + tip = QObject::tr(group.c_str()); +} + /** * Activates the page at position \a index of the group with name \a group. */ diff --git a/src/Gui/DlgPreferencesImp.h b/src/Gui/DlgPreferencesImp.h index c7aa1e44d2..d59385bac5 100644 --- a/src/Gui/DlgPreferencesImp.h +++ b/src/Gui/DlgPreferencesImp.h @@ -114,6 +114,8 @@ class GuiExport DlgPreferencesImp : public QDialog public: static void addPage(const std::string& className, const std::string& group); static void removePage(const std::string& className, const std::string& group); + static void setGroupData(const std::string& group, const std::string& icon, const QString& tip); + static void getGroupData(const std::string& group, std::string& icon, QString& tip); static void reloadSettings(); DlgPreferencesImp(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags()); @@ -148,6 +150,11 @@ private: private: typedef std::pair> TGroupPages; static std::list _pages; /**< Name of all registered preference pages */ + struct Group { + std::string iconName; + QString tooltip; + }; + static std::map _groupMap; std::unique_ptr ui; bool invalidParameter; bool canEmbedScrollArea; diff --git a/src/Mod/Part/Gui/AppPartGui.cpp b/src/Mod/Part/Gui/AppPartGui.cpp index cfc6f4d05b..d410f28c44 100644 --- a/src/Mod/Part/Gui/AppPartGui.cpp +++ b/src/Mod/Part/Gui/AppPartGui.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -207,6 +208,7 @@ PyMOD_INIT_FUNC(PartGui) } // register preferences pages + Gui::Dialog::DlgPreferencesImp::setGroupData("Part/Part Design", "Part design", QObject::tr("Part and Part Design workbench")); (void)new Gui::PrefPageProducer(QT_TRANSLATE_NOOP("QObject", "Part/Part Design")); (void)new Gui::PrefPageProducer(QT_TRANSLATE_NOOP("QObject", "Part/Part Design")); (void)new Gui::PrefPageProducer(QT_TRANSLATE_NOOP("QObject", "Part/Part Design"));