diff --git a/src/Gui/Action.cpp b/src/Gui/Action.cpp index fdaa2d9460..e0d422b2b1 100644 --- a/src/Gui/Action.cpp +++ b/src/Gui/Action.cpp @@ -55,6 +55,7 @@ #include "Macro.h" #include "MainWindow.h" #include "PythonEditor.h" +#include "UserSettings.h" #include "WhatsThis.h" #include "Widgets.h" #include "Workbench.h" @@ -725,8 +726,7 @@ void WorkbenchGroup::addTo(QWidget *widget) auto* box = new WorkbenchComboBox(this, widget); setupBox(box); - std::string pos = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow")->GetASCII("WSPosition", "WSToolbar"); - bool left = (pos == "WSLeftCorner"); + bool left = WorkbenchSwitcher::isLeftCorner(WorkbenchSwitcher::getValue()); qobject_cast(widget)->setCornerWidget(box, left ? Qt::TopLeftCorner : Qt::TopRightCorner); } else if (widget->inherits("QMenu")) { diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index b442d9c3cd..c2f0eabf0a 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -1127,6 +1127,7 @@ SET(FreeCADGui_CPP_SRCS WaitCursor.cpp ManualAlignment.cpp TransactionObject.cpp + UserSettings.cpp ) SET(FreeCADGui_SRCS Application.h @@ -1161,6 +1162,7 @@ SET(FreeCADGui_SRCS WaitCursor.h ManualAlignment.h TransactionObject.h + UserSettings.h ${FreeCADGui_SDK_MOC_HDRS} ) diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index 090ff653d0..4b8e847221 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -41,6 +41,7 @@ #include "DlgRevertToBackupConfigImp.h" #include "MainWindow.h" #include "PreferencePackManager.h" +#include "UserSettings.h" #include "Language/Translator.h" using namespace Gui::Dialog; @@ -104,8 +105,6 @@ DlgGeneralImp::DlgGeneralImp( QWidget* parent ) else ui->RevertToSavedConfig->setEnabled(true); connect(ui->RevertToSavedConfig, &QPushButton::clicked, this, &DlgGeneralImp::revertToSavedConfig); - - wsPositions << "WSToolbar" << "WSLeftCorner" << "WSRightCorner"; } /** @@ -502,10 +501,7 @@ void DlgGeneralImp::saveWorkbenchSelector() { //save workbench selector position auto index = ui->WorkbenchSelectorPosition->currentIndex(); - auto hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); - if (index >= 0 && index < wsPositions.size()) { - hGrp->SetASCII("WSPosition", wsPositions[index].c_str()); - } + WorkbenchSwitcher::setIndex(index); } void DlgGeneralImp::loadWorkbenchSelector() @@ -515,11 +511,7 @@ void DlgGeneralImp::loadWorkbenchSelector() ui->WorkbenchSelectorPosition->addItem(tr("Toolbar")); ui->WorkbenchSelectorPosition->addItem(tr("Left corner")); ui->WorkbenchSelectorPosition->addItem(tr("Right corner")); - - auto hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); - std::string pos = hGrp->GetASCII("WSPosition", "WSToolbar"); - int index = std::max(0, wsPositions.indexOf(pos)); - ui->WorkbenchSelectorPosition->setCurrentIndex(index); + ui->WorkbenchSelectorPosition->setCurrentIndex(WorkbenchSwitcher::getIndex()); } #include "moc_DlgGeneralImp.cpp" diff --git a/src/Gui/DlgGeneralImp.h b/src/Gui/DlgGeneralImp.h index dfa0f6c955..c92f9c0fc8 100644 --- a/src/Gui/DlgGeneralImp.h +++ b/src/Gui/DlgGeneralImp.h @@ -27,7 +27,6 @@ #include "PropertyPage.h" #include #include -#include class QTabWidget; @@ -74,7 +73,6 @@ private: void loadWorkbenchSelector(); private: - QVector wsPositions; int localeIndex; std::unique_ptr ui; std::unique_ptr newPreferencePackDialog; diff --git a/src/Gui/MenuManager.cpp b/src/Gui/MenuManager.cpp index 72c2f0939c..37cc3f2421 100644 --- a/src/Gui/MenuManager.cpp +++ b/src/Gui/MenuManager.cpp @@ -31,6 +31,7 @@ #include "Application.h" #include "Command.h" #include "MainWindow.h" +#include "UserSettings.h" using namespace Gui; @@ -370,13 +371,13 @@ void MenuManager::setupMenuBarCornerWidgets() const { /*Note: currently only workbench selector uses corner widget.*/ QMenuBar* menuBar = getMainWindow()->menuBar(); - std::string pos = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow")->GetASCII("WSPosition", "WSToolbar"); + std::string pos = WorkbenchSwitcher::getValue(); bool showLeftWidget = false; bool showRightWidget = false; //Right corner widget - if (pos == "WSRightCorner") { + if (WorkbenchSwitcher::isRightCorner(pos)) { //add workbench selector to menubar right corner widget. if (!menuBar->cornerWidget(Qt::TopRightCorner)) { Application::Instance->commandManager().addTo("Std_Workbench", menuBar); @@ -384,7 +385,7 @@ void MenuManager::setupMenuBarCornerWidgets() const showRightWidget = true; } //Left corner widget - else if (pos == "WSLeftCorner") { + else if (WorkbenchSwitcher::isLeftCorner(pos)) { //add workbench selector to menubar left corner widget. if (!menuBar->cornerWidget(Qt::TopLeftCorner)) { Application::Instance->commandManager().addTo("Std_Workbench", menuBar); diff --git a/src/Gui/UserSettings.cpp b/src/Gui/UserSettings.cpp new file mode 100644 index 0000000000..21736c9f42 --- /dev/null +++ b/src/Gui/UserSettings.cpp @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#include "PreCompiled.h" +#ifndef _PreComp_ +#endif + + +#include "UserSettings.h" +#include + + +using namespace Gui; + +namespace { + +ParameterGrp::handle getWSParameter() +{ + return App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); +} + +} + +std::string WorkbenchSwitcher::getValue() +{ + return getWSParameter()->GetASCII("WSPosition", "WSToolbar"); +} + +bool WorkbenchSwitcher::isLeftCorner(const std::string& value) +{ + return (value == "WSLeftCorner"); +} + +bool WorkbenchSwitcher::isRightCorner(const std::string& value) +{ + return (value == "WSRightCorner"); +} + +bool WorkbenchSwitcher::isToolbar(const std::string& value) +{ + return (value == "WSToolbar"); +} + +QVector WorkbenchSwitcher::values() +{ + QVector wsPositions; + wsPositions << "WSToolbar" << "WSLeftCorner" << "WSRightCorner"; + return wsPositions; +} + +int WorkbenchSwitcher::getIndex() +{ + auto hGrp = getWSParameter(); + std::string pos = hGrp->GetASCII("WSPosition", "WSToolbar"); + auto wsPositions = values(); + int index = std::max(0, wsPositions.indexOf(pos)); + return index; +} + +void WorkbenchSwitcher::setIndex(int index) +{ + auto wsPositions = values(); + auto hGrp = getWSParameter(); + if (index >= 0 && index < wsPositions.size()) { + hGrp->SetASCII("WSPosition", wsPositions[index].c_str()); + } +} diff --git a/src/Gui/UserSettings.h b/src/Gui/UserSettings.h new file mode 100644 index 0000000000..c116f17e27 --- /dev/null +++ b/src/Gui/UserSettings.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (c) 2022 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef GUI_USERSETTINGS_H +#define GUI_USERSETTINGS_H + +#include +#include +#include + +namespace Gui { + +class GuiExport WorkbenchSwitcher +{ +public: + static bool isLeftCorner(const std::string&); + static bool isRightCorner(const std::string&); + static bool isToolbar(const std::string&); + static std::string getValue(); + static int getIndex(); + static void setIndex(int); + +private: + static QVector values(); +}; + +} // namespace Gui + +#endif // GUI_USERSETTINGS_H diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index 87585deac9..8ac87c8670 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -40,6 +40,7 @@ #include "Selection.h" #include "ToolBarManager.h" #include "ToolBoxManager.h" +#include "UserSettings.h" #include "Window.h" #include @@ -751,9 +752,7 @@ ToolBarItem* StdWorkbench::setupToolBars() const << "Std_Refresh" << "Separator" << "Std_WhatsThis"; // Workbench switcher - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); - std::string defaultPos = "WSToolbar"; - if (hGrp->GetASCII("WSPosition", defaultPos.c_str()) == defaultPos) { + if (WorkbenchSwitcher::isToolbar(WorkbenchSwitcher::getValue())) { auto wb = new ToolBarItem(root); wb->setCommand("Workbench"); *wb << "Std_Workbench";