Gui: implement mechanism to set a custom icon name or tooltip of a preferences group

This commit is contained in:
wmayer
2022-07-16 19:27:22 +02:00
parent 2782d09dee
commit 2a41f359f7
3 changed files with 47 additions and 9 deletions

View File

@@ -52,6 +52,8 @@ const int DlgPreferencesImp::GroupNameRole = Qt::UserRole;
/* TRANSLATOR Gui::Dialog::DlgPreferencesImp */
std::list<DlgPreferencesImp::TGroupPages> DlgPreferencesImp::_pages;
std::map<std::string, DlgPreferencesImp::Group> 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.
*/

View File

@@ -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<std::string, std::list<std::string>> TGroupPages;
static std::list<TGroupPages> _pages; /**< Name of all registered preference pages */
struct Group {
std::string iconName;
QString tooltip;
};
static std::map<std::string, Group> _groupMap;
std::unique_ptr<Ui_DlgPreferences> ui;
bool invalidParameter;
bool canEmbedScrollArea;

View File

@@ -20,6 +20,7 @@
#include <Base/PyObjectBase.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/DlgPreferencesImp.h>
#include <Gui/WidgetFactory.h>
#include <Gui/Language/Translator.h>
@@ -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<PartGui::DlgSettingsGeneral>(QT_TRANSLATE_NOOP("QObject", "Part/Part Design"));
(void)new Gui::PrefPageProducer<PartGui::DlgSettings3DViewPart>(QT_TRANSLATE_NOOP("QObject", "Part/Part Design"));
(void)new Gui::PrefPageProducer<PartGui::DlgSettingsObjectColor>(QT_TRANSLATE_NOOP("QObject", "Part/Part Design"));