From fce7e11636f9c5bbdebeacfce71bc15c172c98aa Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 21 Feb 2022 19:26:44 +0100 Subject: [PATCH] Gui: Attach PythonConsole to parameter group and handle changes in its OnChange() method This avoids code duplications and makes sure the group name is used only once. The parameters are saved to the group 'PythonConsole' instead of 'General'. --- src/Gui/DlgGeneralImp.cpp | 14 ------ src/Gui/DlgSettingsPythonConsole.ui | 6 +-- src/Gui/MainWindow.cpp | 9 ---- src/Gui/PythonConsole.cpp | 67 +++++++++++------------------ 4 files changed, 28 insertions(+), 68 deletions(-) diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index 98d1a6d086..88a3c9f024 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -38,7 +38,6 @@ #include "DockWindowManager.h" #include "MainWindow.h" #include "PrefWidgets.h" -#include "PythonConsole.h" #include "Language/Translator.h" #include "Gui/PreferencePackManager.h" #include "DlgPreferencesImp.h" @@ -140,19 +139,6 @@ void DlgGeneralImp::saveSettings() ui->EnableCursorBlinking->onSave(); ui->SplashScreen->onSave(); - QWidget* pc = DockWindowManager::instance()->getDockWindow("Python console"); - PythonConsole *pcPython = qobject_cast(pc); - if (pcPython) { - bool pythonWordWrap = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General")->GetBool("PythonWordWrap", true); - - if (pythonWordWrap) { - pcPython->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - } else { - pcPython->setWordWrapMode(QTextOption::NoWrap); - } - } - setRecentFileSize(); ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General"); QString lang = QLocale::languageToString(QLocale().language()); diff --git a/src/Gui/DlgSettingsPythonConsole.ui b/src/Gui/DlgSettingsPythonConsole.ui index 979f7e4fdd..e72f80f3cf 100644 --- a/src/Gui/DlgSettingsPythonConsole.ui +++ b/src/Gui/DlgSettingsPythonConsole.ui @@ -36,7 +36,7 @@ horizontal space in Python console PythonWordWrap - General + PythonConsole @@ -55,7 +55,7 @@ horizontal space in Python console PythonBlockCursor - General + PythonConsole @@ -74,7 +74,7 @@ horizontal space in Python console SavePythonHistory - General + PythonConsole diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index e7e817575d..6343df10f1 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -425,15 +425,6 @@ MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags f) // Python console if (hiddenDockWindows.find("Std_PythonView") == std::string::npos) { PythonConsole* pcPython = new PythonConsole(this); - ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); - - if (hGrp->GetBool("PythonWordWrap", true)) { - pcPython->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - } else { - pcPython->setWordWrapMode(QTextOption::NoWrap); - } - pcPython->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python")); pcPython->setObjectName (QString::fromLatin1(QT_TRANSLATE_NOOP("QDockWidget","Python console"))); diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 265f1dbf57..6aeb50a11f 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -91,6 +91,7 @@ struct PythonConsoleP QStringList statements; bool interactive; QMap colormap; // Color map + ParameterGrp::handle hGrpSettings; PythonConsoleP() { type = Normal; @@ -438,9 +439,13 @@ PythonConsole::PythonConsole(QWidget *parent) // set colors and font from settings ParameterGrp::handle hPrefGrp = getWindowParameter(); - hPrefGrp->Attach( this ); + hPrefGrp->Attach(this); hPrefGrp->NotifyAll(); + d->hGrpSettings = WindowParameter::getDefaultParameter()->GetGroup("PythonConsole"); + d->hGrpSettings->Attach(this); + d->hGrpSettings->Notify("PythonWordWrap"); + // disable undo/redo stuff setUndoRedoEnabled( false ); setAcceptDrops( true ); @@ -468,7 +473,8 @@ PythonConsole::~PythonConsole() { saveHistory(); Base::PyGILStateLocker lock; - getWindowParameter()->Detach( this ); + d->hGrpSettings->Detach(this); + getWindowParameter()->Detach(this); delete pythonSyntax; Py_XDECREF(d->_stdoutPy); Py_XDECREF(d->_stderrPy); @@ -478,15 +484,12 @@ PythonConsole::~PythonConsole() } /** Set new font and colors according to the parameters. */ -void PythonConsole::OnChange( Base::Subject &rCaller,const char* sReason ) +void PythonConsole::OnChange(Base::Subject &rCaller, const char* sReason ) { - Q_UNUSED(rCaller); - ParameterGrp::handle hPrefGrp = getWindowParameter(); - ParameterGrp::handle hPrefGen = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); + const auto & rGrp = static_cast(rCaller); if (strcmp(sReason, "PythonWordWrap") == 0) { - bool pythonWordWrap = hPrefGen->GetBool("PythonWordWrap", true); + bool pythonWordWrap = rGrp.GetBool("PythonWordWrap", true); if (pythonWordWrap) setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); else @@ -494,8 +497,8 @@ void PythonConsole::OnChange( Base::Subject &rCaller,const char* sR } if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) { - int fontSize = hPrefGrp->GetInt("FontSize", 10); - QString fontFamily = QString::fromLatin1(hPrefGrp->GetASCII("Font", "Courier").c_str()); + int fontSize = rGrp.GetInt("FontSize", 10); + QString fontFamily = QString::fromLatin1(rGrp.GetASCII("Font", "Courier").c_str()); QFont font(fontFamily, fontSize); setFont(font); @@ -513,17 +516,15 @@ void PythonConsole::OnChange( Base::Subject &rCaller,const char* sR QColor color = it.value(); unsigned int col = (color.red() << 24) | (color.green() << 16) | (color.blue() << 8); unsigned long value = static_cast(col); - value = hPrefGrp->GetUnsigned(sReason, value); + value = rGrp.GetUnsigned(sReason, value); col = static_cast(value); color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff); pythonSyntax->setColor(QString::fromLatin1(sReason), color); } } - if (strcmp(sReason, "PythonBlockCursor") == 0 || - strcmp(sReason, "FontSize") == 0 || - strcmp(sReason, "Font") == 0) { - bool block = hPrefGen->GetBool("PythonBlockCursor", false); + if (strcmp(sReason, "PythonBlockCursor") == 0) { + bool block = rGrp.GetBool("PythonBlockCursor", false); if (block) setCursorWidth(QFontMetrics(font()).averageCharWidth()); else @@ -1258,9 +1259,6 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e ) QAction *a; bool mayPasteHere = cursorBeyond( this->textCursor(), this->inputBegin() ); - ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); - a = menu.addAction(tr("&Copy"), this, SLOT(copy()), QKeySequence(QString::fromLatin1("CTRL+C"))); a->setEnabled(textCursor().hasSelection()); @@ -1276,7 +1274,7 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e ) QAction* saveh = menu.addAction(tr("Save history")); saveh->setToolTip(tr("Saves Python history across %1 sessions").arg(qApp->applicationName())); saveh->setCheckable(true); - saveh->setChecked(hGrp->GetBool("SavePythonHistory", false)); + saveh->setChecked(d->hGrpSettings->GetBool("SavePythonHistory", false)); menu.addSeparator(); @@ -1297,25 +1295,13 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e ) QAction* wrap = menu.addAction(tr("Word wrap")); wrap->setCheckable(true); - if (hGrp->GetBool("PythonWordWrap", true)) { - wrap->setChecked(true); - this->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - } else { - wrap->setChecked(false); - this->setWordWrapMode(QTextOption::NoWrap); - } - + wrap->setChecked(d->hGrpSettings->GetBool("PythonWordWrap", true)); QAction* exec = menu.exec(e->globalPos()); if (exec == wrap) { - if (wrap->isChecked()) { - this->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - hGrp->SetBool("PythonWordWrap", true); - } else { - this->setWordWrapMode(QTextOption::NoWrap); - hGrp->SetBool("PythonWordWrap", false); - } - } else if (exec == saveh) { - hGrp->SetBool("SavePythonHistory", saveh->isChecked()); + d->hGrpSettings->SetBool("PythonWordWrap", wrap->isChecked()); + } + else if (exec == saveh) { + d->hGrpSettings->SetBool("SavePythonHistory", saveh->isChecked()); } } @@ -1405,9 +1391,8 @@ void PythonConsole::loadHistory() const // only load contents if history is empty, to not overwrite anything if (!d->history.isEmpty()) return; - ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); - if (!hGrp->GetBool("SavePythonHistory", false)) + + if (!d->hGrpSettings->GetBool("SavePythonHistory", false)) return; QFile f(d->historyFile); if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -1430,9 +1415,7 @@ void PythonConsole::saveHistory() const { if (d->history.isEmpty()) return; - ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). - GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); - if (!hGrp->GetBool("SavePythonHistory", false)) + if (!d->hGrpSettings->GetBool("SavePythonHistory", false)) return; QFile f(d->historyFile); if (f.open(QIODevice::WriteOnly)) {