From 4407387ab43f31dab4957fc9b64d588f859ed0dc Mon Sep 17 00:00:00 2001 From: Paddle Date: Fri, 31 Mar 2023 14:12:01 +0200 Subject: [PATCH] Pref: wb: change to disabled + ordered lists. --- src/Gui/Action.cpp | 11 --- src/Gui/DlgSettingsWorkbenchesImp.cpp | 117 ++++++++++++++------------ src/Gui/DlgSettingsWorkbenchesImp.h | 3 +- 3 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/Gui/Action.cpp b/src/Gui/Action.cpp index f2289df944..32f5ecb0aa 100644 --- a/src/Gui/Action.cpp +++ b/src/Gui/Action.cpp @@ -800,19 +800,8 @@ void WorkbenchGroup::setWorkbenchData(int index, const QString& wb) void WorkbenchGroup::refreshWorkbenchList() { - QStringList items = Application::Instance->workbenches(); QStringList enabled_wbs_list = DlgSettingsWorkbenchesImp::getEnabledWorkbenches(); - // Go through the list of enabled workbenches and verify that they really exist because - // it might be possible that a workbench has been removed after setting up the list of - // enabled workbenches. - for (int i = 0; i < enabled_wbs_list.size(); i++) { - if (items.indexOf(enabled_wbs_list[i]) < 0) { - enabled_wbs_list.removeAt(i); - i--; - } - } - // Resize the action group. QList workbenches = groupAction()->actions(); int numActions = workbenches.size(); diff --git a/src/Gui/DlgSettingsWorkbenchesImp.cpp b/src/Gui/DlgSettingsWorkbenchesImp.cpp index ffc0064732..c6e5807f33 100644 --- a/src/Gui/DlgSettingsWorkbenchesImp.cpp +++ b/src/Gui/DlgSettingsWorkbenchesImp.cpp @@ -253,7 +253,7 @@ DlgSettingsWorkbenchesImp::~DlgSettingsWorkbenchesImp() void DlgSettingsWorkbenchesImp::saveSettings() { - std::ostringstream enabledStr, autoloadStr; + std::ostringstream orderedStr, disabledStr, autoloadStr; auto addStrToOss = [](std::string wbName, std::ostringstream& oss) { if (!oss.str().empty()) @@ -268,7 +268,10 @@ void DlgSettingsWorkbenchesImp::saveSettings() std::string wbName = wbItem->objectName().toStdString(); if (wbItem->isEnabled()) { - addStrToOss(wbName, enabledStr); + addStrToOss(wbName, orderedStr); + } + else { + addStrToOss(wbName, disabledStr); } if (wbItem->isAutoLoading()) { @@ -276,11 +279,18 @@ void DlgSettingsWorkbenchesImp::saveSettings() } } - if (enabledStr.str().empty()) //make sure that we have at least one enabled workbench. - enabledStr << "NoneWorkbench"; + if (orderedStr.str().empty()) //make sure that we have at least one enabled workbench. This should not be necessary because startup wb cannot be disabled. + orderedStr << "NoneWorkbench"; + else { + if (!disabledStr.str().empty()) + disabledStr << ","; + disabledStr << "NoneWorkbench"; + } + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches"); - hGrp->SetASCII("Enabled", enabledStr.str().c_str()); + hGrp->SetASCII("Ordered", orderedStr.str().c_str()); + hGrp->SetASCII("Disabled", disabledStr.str().c_str()); App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")-> SetASCII("BackgroundAutoloadModules", autoloadStr.str().c_str()); @@ -335,23 +345,16 @@ void DlgSettingsWorkbenchesImp::buildWorkbenchList() { QSignalBlocker sigblk(ui->wbList); - QStringList workbenches = Application::Instance->workbenches(); - workbenches.sort(); //This will sort alphabetically the disabled wb. - QStringList enabledWbs = getEnabledWorkbenches(); + QStringList disabledWbs = getDisabledWorkbenches(); //First we add the enabled wbs in their saved order. for (const auto& wbName : enabledWbs) { - if (workbenches.contains(wbName)) { - addWorkbench(wbName, true); - } - else { - Base::Console().Log("Ignoring unknown %s workbench found in user preferences.\n", wbName.toStdString().c_str()); - } + addWorkbench(wbName, true); } //Second we add workbenches that are disabled in alphabetical order. - for (const auto& wbName : workbenches) { - if (!enabledWbs.contains(wbName) && wbName.toStdString() != "NoneWorkbench") { + for (const auto& wbName : disabledWbs) { + if (wbName.toStdString() != "NoneWorkbench") { addWorkbench(wbName, false); } } @@ -372,63 +375,71 @@ void DlgSettingsWorkbenchesImp::addWorkbench(const QString& wbName, bool enabled QStringList DlgSettingsWorkbenchesImp::getEnabledWorkbenches() { - QString enabled_wbs; + QStringList disabled_wbs_list = getDisabledWorkbenches(); QStringList enabled_wbs_list; + QStringList wbs_ordered_list; + QString wbs_ordered; ParameterGrp::handle hGrp; - QString allWorkbenches = QString::fromLatin1("ALL"); hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches"); - enabled_wbs = QString::fromStdString(hGrp->GetASCII("Enabled", allWorkbenches.toStdString().c_str())); + wbs_ordered = QString::fromStdString(hGrp->GetASCII("Ordered", "")); #if QT_VERSION >= QT_VERSION_CHECK(5,15,0) - enabled_wbs_list = enabled_wbs.split(QLatin1String(","), Qt::SkipEmptyParts); + wbs_ordered_list = wbs_ordered.split(QLatin1String(","), Qt::SkipEmptyParts); #else - enabled_wbs_list = enabled_wbs.split(QLatin1String(","), QString::SkipEmptyParts); + wbs_ordered_list = wbs_ordered.split(QLatin1String(","), QString::SkipEmptyParts); #endif - if (enabled_wbs_list.at(0) == allWorkbenches) { - enabled_wbs_list.removeFirst(); - QStringList workbenches = Application::Instance->workbenches(); - for(auto& wbName : workbenches) { - if (wbName.toStdString() != "NoneWorkbench" && wbName.toStdString() != "TestWorkbench") - enabled_wbs_list.append(wbName); + QStringList workbenches = Application::Instance->workbenches(); + workbenches.sort(); + + //First we add the wb that are ordered. + for(auto& wbName : wbs_ordered_list) { + if (workbenches.contains(wbName) && !disabled_wbs_list.contains(wbName)) { //Some wb may have been removed + enabled_wbs_list.append(wbName); + } + else { + Base::Console().Log("Ignoring unknown %s workbench found in user preferences.\n", wbName.toStdString().c_str()); } - enabled_wbs_list.sort(); } + + //Then we add the wbs that are not ordered and not disabled in alphabetical order + for(auto& wbName : workbenches) { + if (!enabled_wbs_list.contains(wbName) && !disabled_wbs_list.contains(wbName)) + enabled_wbs_list.append(wbName); + } + return enabled_wbs_list; } -void DlgSettingsWorkbenchesImp::addToEnabledWorkenches(const QString wbToAddName) +QStringList DlgSettingsWorkbenchesImp::getDisabledWorkbenches() { - QStringList enabled_wbs_list = getEnabledWorkbenches(); + QString disabled_wbs; + QStringList unfiltered_disabled_wbs_list; + QStringList disabled_wbs_list; + ParameterGrp::handle hGrp; - enabled_wbs_list.push_back(wbToAddName); + hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches"); + disabled_wbs = QString::fromStdString(hGrp->GetASCII("Disabled", "NoneWorkbench,TestWorkbench")); +#if QT_VERSION >= QT_VERSION_CHECK(5,15,0) + unfiltered_disabled_wbs_list = disabled_wbs.split(QLatin1String(","), Qt::SkipEmptyParts); +#else + unfiltered_disabled_wbs_list = disabled_wbs.split(QLatin1String(","), QString::SkipEmptyParts); +#endif - std::ostringstream enabledStr; - for (auto& wbName : enabled_wbs_list) { - if (!enabledStr.str().empty()) - enabledStr << ","; - enabledStr << wbName.toStdString(); - } + QStringList workbenches = Application::Instance->workbenches(); - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches"); - hGrp->SetASCII("Enabled", enabledStr.str().c_str()); -} - -void DlgSettingsWorkbenchesImp::removeFromEnabledWorkenches(const QString wbToRemoveName) -{ - QStringList enabled_wbs_list = getEnabledWorkbenches(); - - std::ostringstream enabledStr; - for (const auto& wbName : enabled_wbs_list) { - if (wbName != wbToRemoveName) { - if (!enabledStr.str().empty()) - enabledStr << ","; - enabledStr << wbName.toStdString(); + for (auto& wbName : unfiltered_disabled_wbs_list) { + if (workbenches.contains(wbName)) { //Some wb may have been removed + disabled_wbs_list.append(wbName); + } + else { + Base::Console().Log("Ignoring unknown %s workbench found in user preferences.\n", wbName.toStdString().c_str()); } } - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches"); - hGrp->SetASCII("Enabled", enabledStr.str().c_str()); + disabled_wbs_list.sort(); + + return disabled_wbs_list; } /** diff --git a/src/Gui/DlgSettingsWorkbenchesImp.h b/src/Gui/DlgSettingsWorkbenchesImp.h index a0ed157d20..ce91a31034 100644 --- a/src/Gui/DlgSettingsWorkbenchesImp.h +++ b/src/Gui/DlgSettingsWorkbenchesImp.h @@ -49,8 +49,7 @@ public: void loadSettings() override; static QStringList getEnabledWorkbenches(); - static void addToEnabledWorkenches(const QString wbName); - static void removeFromEnabledWorkenches(const QString wbName); + static QStringList getDisabledWorkbenches(); protected Q_SLOTS: void wbToggled(const QString& wbName, bool enabled);