Gui: Remove code duplication in PythonConsole class

This commit is contained in:
wmayer
2024-12-25 19:19:23 +01:00
committed by Yorik van Havre
parent 0dba0a3b31
commit d979235a48
4 changed files with 25 additions and 55 deletions

View File

@@ -95,27 +95,12 @@ struct PythonConsoleP
QString output, error, info, historyFile;
QStringList statements;
bool interactive;
QMap<QString, QColor> colormap; // Color map
ParameterGrp::handle hGrpSettings;
PythonConsoleP()
{
type = Normal;
interactive = false;
historyFile = QString::fromUtf8((App::Application::getUserAppDataDir() + "PythonHistory.log").c_str());
colormap[QLatin1String("Text")] = qApp->palette().windowText().color();
colormap[QLatin1String("Bookmark")] = Qt::cyan;
colormap[QLatin1String("Breakpoint")] = Qt::red;
colormap[QLatin1String("Keyword")] = Qt::blue;
colormap[QLatin1String("Comment")] = QColor(0, 170, 0);
colormap[QLatin1String("Block comment")] = QColor(160, 160, 164);
colormap[QLatin1String("Number")] = Qt::blue;
colormap[QLatin1String("String")] = Qt::red;
colormap[QLatin1String("Character")] = Qt::red;
colormap[QLatin1String("Class name")] = QColor(255, 170, 0);
colormap[QLatin1String("Define name")] = QColor(255, 170, 0);
colormap[QLatin1String("Operator")] = QColor(160, 160, 164);
colormap[QLatin1String("Python output")] = QColor(170, 170, 127);
colormap[QLatin1String("Python error")] = Qt::red;
}
};
@@ -522,7 +507,7 @@ PythonConsole::~PythonConsole()
}
/** Set new font and colors according to the parameters. */
void PythonConsole::OnChange(Base::Subject<const char*> &rCaller, const char* sReason )
void PythonConsole::OnChange(Base::Subject<const char*> &rCaller, const char* sReason)
{
const auto & rGrp = static_cast<ParameterGrp &>(rCaller);
@@ -536,33 +521,6 @@ void PythonConsole::OnChange(Base::Subject<const char*> &rCaller, const char* sR
}
}
if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) {
int fontSize = rGrp.GetInt("FontSize", 10);
QString fontFamily = QString::fromLatin1(rGrp.GetASCII("Font", "Courier").c_str());
QFont font(fontFamily, fontSize);
setFont(font);
QFontMetrics metric(font);
int width = QtTools::horizontalAdvance(metric, QLatin1String("0000"));
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
setTabStopWidth(width);
#else
setTabStopDistance(width);
#endif
}
else {
QMap<QString, QColor>::Iterator it = d->colormap.find(QString::fromLatin1(sReason));
if (it != d->colormap.end()) {
QColor color = it.value();
unsigned int col = App::Color::asPackedRGB<QColor>(color);
auto value = static_cast<unsigned long>(col);
value = rGrp.GetUnsigned(sReason, value);
col = static_cast<unsigned int>(value);
color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff);
pythonSyntax->setColor(QString::fromLatin1(sReason), color);
}
}
if (strcmp(sReason, "PythonBlockCursor") == 0) {
bool block = rGrp.GetBool("PythonBlockCursor", false);
if (block) {
@@ -572,6 +530,10 @@ void PythonConsole::OnChange(Base::Subject<const char*> &rCaller, const char* sR
setCursorWidth(1);
}
}
if (strcmp(sReason, "EnableLineNumber") != 0) {
TextEditor::OnChange(rCaller, sReason);
}
}
/**