diff --git a/src/Gui/PreferencePages/DlgSettingsEditor.ui b/src/Gui/PreferencePages/DlgSettingsEditor.ui index 5cecc7f9a8..a512778b76 100644 --- a/src/Gui/PreferencePages/DlgSettingsEditor.ui +++ b/src/Gui/PreferencePages/DlgSettingsEditor.ui @@ -205,7 +205,7 @@ Keep tabs - true + false Tabs @@ -226,6 +226,9 @@ Spaces + + true + Editor diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 0722240e80..4a9db3e19b 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -435,7 +435,7 @@ void InteractiveInterpreter::clearBuffer() * Constructs a PythonConsole which is a child of 'parent'. */ PythonConsole::PythonConsole(QWidget *parent) - : TextEdit(parent), WindowParameter( "Editor" ), _sourceDrain(nullptr) + : PythonTextEditor(parent), _sourceDrain(nullptr) { d = new PythonConsoleP(); d->interactive = false; @@ -467,7 +467,6 @@ PythonConsole::PythonConsole(QWidget *parent) // set colors and font from settings ParameterGrp::handle hPrefGrp = getWindowParameter(); - hPrefGrp->Attach(this); hPrefGrp->NotifyAll(); d->hGrpSettings = WindowParameter::getDefaultParameter()->GetGroup("PythonConsole"); @@ -512,7 +511,6 @@ PythonConsole::~PythonConsole() saveHistory(); Base::PyGILStateLocker lock; d->hGrpSettings->Detach(this); - getWindowParameter()->Detach(this); delete pythonSyntax; Py_XDECREF(d->_stdoutPy); Py_XDECREF(d->_stderrPy); @@ -613,13 +611,13 @@ void PythonConsole::keyPressEvent(QKeyEvent * e) if (e->text().isEmpty() || e->matches(QKeySequence::Copy) || e->matches(QKeySequence::SelectAll)) { - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); } else if (!e->text().isEmpty() && (e->modifiers() == Qt::NoModifier || e->modifiers() == Qt::ShiftModifier)) { this->moveCursor(QTextCursor::End); - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); } break; } @@ -671,11 +669,11 @@ void PythonConsole::keyPressEvent(QKeyEvent * e) if (e->text() == QLatin1String(".")) { // analyse context and show available call tips int contextLength = cursor.position() - inputLineBegin.position(); - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); d->callTipsList->showTips( inputStrg.left( contextLength ) ); } else { - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); } } break; @@ -707,25 +705,25 @@ void PythonConsole::keyPressEvent(QKeyEvent * e) case Qt::Key_Left: { if (cursor > inputLineBegin) - { TextEdit::keyPressEvent(e); } + { PythonTextEditor::keyPressEvent(e); } restartHistory = false; } break; case Qt::Key_Right: { - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); restartHistory = false; } break; case Qt::Key_Backspace: { if (cursorBeyond( cursor, inputLineBegin, +1 )) - { TextEdit::keyPressEvent(e); } + { PythonTextEditor::keyPressEvent(e); } } break; default: { - TextEdit::keyPressEvent(e); + PythonTextEditor::keyPressEvent(e); } break; } // This can't be done in CallTipsList::eventFilter() because we must first perform diff --git a/src/Gui/PythonConsole.h b/src/Gui/PythonConsole.h index 1375334cd5..633d22a297 100644 --- a/src/Gui/PythonConsole.h +++ b/src/Gui/PythonConsole.h @@ -97,7 +97,7 @@ private: * @author Werner Mayer */ class PythonConsoleHighlighter; -class GuiExport PythonConsole : public TextEdit, public WindowParameter +class GuiExport PythonConsole : public PythonTextEditor { Q_OBJECT diff --git a/src/Gui/PythonEditor.cpp b/src/Gui/PythonEditor.cpp index 7a46064a21..4ec20d6dad 100644 --- a/src/Gui/PythonEditor.cpp +++ b/src/Gui/PythonEditor.cpp @@ -65,7 +65,7 @@ struct PythonEditorP * syntax highlighting for the Python language. */ PythonEditor::PythonEditor(QWidget* parent) - : TextEditor(parent) + : PythonTextEditor(parent) { d = new PythonEditorP(); this->setSyntaxHighlighter(new PythonSyntaxHighlighter(this)); @@ -205,7 +205,7 @@ void PythonEditor::keyPressEvent(QKeyEvent* e) setTextCursor(cursor); return; //skip default handler } - TextEditor::keyPressEvent(e); //wasn't enter key, so let base class handle it + PythonTextEditor::keyPressEvent(e); //wasn't enter key, so let base class handle it } void PythonEditor::onComment() diff --git a/src/Gui/PythonEditor.h b/src/Gui/PythonEditor.h index b2b826ba1d..8e442c9d46 100644 --- a/src/Gui/PythonEditor.h +++ b/src/Gui/PythonEditor.h @@ -36,7 +36,7 @@ class PythonSyntaxHighlighterP; * Python text editor with syntax highlighting. * \author Werner Mayer */ -class GuiExport PythonEditor : public TextEditor +class GuiExport PythonEditor : public PythonTextEditor { Q_OBJECT diff --git a/src/Gui/TextEdit.cpp b/src/Gui/TextEdit.cpp index 79acc46729..faa508295d 100644 --- a/src/Gui/TextEdit.cpp +++ b/src/Gui/TextEdit.cpp @@ -257,6 +257,11 @@ TextEditor::TextEditor(QWidget* parent) highlightCurrentLine(); } +void TextEditor::keyPressEvent(QKeyEvent *e) +{ + TextEdit::keyPressEvent( e ); +} + /** Destroys the object and frees any allocated resources */ TextEditor::~TextEditor() { @@ -359,12 +364,85 @@ void TextEditor::setSyntaxHighlighter(SyntaxHighlighter* sh) this->highlighter = sh; } -void TextEditor::keyPressEvent (QKeyEvent * e) +/** Sets the font, font size and tab size of the editor. */ +void TextEditor::OnChange(Base::Subject &rCaller,const char* sReason) +{ + Q_UNUSED(rCaller); + ParameterGrp::handle hPrefGrp = getWindowParameter(); + if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) { +#ifdef FC_OS_LINUX + int fontSize = hPrefGrp->GetInt("FontSize", 15); +#else + int fontSize = hPrefGrp->GetInt("FontSize", 10); +#endif + QString fontFamily = QString::fromLatin1(hPrefGrp->GetASCII( "Font", "Courier" ).c_str()); + + QFont font(fontFamily, fontSize); + setFont(font); + lineNumberArea->setFont(font); + } + else { + QMap::Iterator it = d->colormap.find(QString::fromLatin1(sReason)); + if (it != d->colormap.end()) { + QColor color = it.value(); + unsigned int col = App::Color::asPackedRGB(color); + auto value = static_cast(col); + value = hPrefGrp->GetUnsigned(sReason, value); + col = static_cast(value); + color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff); + if (this->highlighter) + this->highlighter->setColor(QLatin1String(sReason), color); + } + } + + if (strcmp(sReason, "TabSize") == 0 || strcmp(sReason, "FontSize") == 0) { + int tabWidth = hPrefGrp->GetInt("TabSize", 4); + QFontMetrics metric(font()); + int fontSize = QtTools::horizontalAdvance(metric, QLatin1Char('0')); + setTabStopDistance(tabWidth * fontSize); + } + + // Enables/Disables Line number in the Macro Editor from Edit->Preferences->Editor menu. + if (strcmp(sReason, "EnableLineNumber") == 0) { + QRect cr = contentsRect(); + bool show = hPrefGrp->GetBool("EnableLineNumber", true); + if(show) + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); + else + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), 0, cr.height())); + } + + if (strcmp(sReason, "EnableBlockCursor") == 0 || + strcmp(sReason, "FontSize") == 0 || + strcmp(sReason, "Font") == 0) { + bool block = hPrefGrp->GetBool("EnableBlockCursor", false); + if (block) + setCursorWidth(QFontMetrics(font()).averageCharWidth()); + else + setCursorWidth(1); + } +} + +void TextEditor::paintEvent (QPaintEvent * e) +{ + TextEdit::paintEvent( e ); +} + +// ------------------------------------------------------------------------------ +PythonTextEditor::PythonTextEditor(QWidget *parent) + : TextEditor(parent) +{ + +} + +PythonTextEditor::~PythonTextEditor() = default; + +void PythonTextEditor::keyPressEvent (QKeyEvent * e) { if ( e->key() == Qt::Key_Tab ) { ParameterGrp::handle hPrefGrp = getWindowParameter(); + bool space = hPrefGrp->GetBool("Spaces", true); int indent = hPrefGrp->GetInt( "IndentSize", 4 ); - bool space = hPrefGrp->GetBool( "Spaces", false ); QString ch = space ? QString(indent, QLatin1Char(' ')) : QString::fromLatin1("\t"); @@ -389,7 +467,7 @@ void TextEditor::keyPressEvent (QKeyEvent * e) break; // end of selection reached cursor.setPosition(block.position()); cursor.insertText(ch); - selEnd += ch.length(); + selEnd += ch.length(); } } @@ -442,78 +520,10 @@ void TextEditor::keyPressEvent (QKeyEvent * e) return; } - TextEdit::keyPressEvent( e ); + TextEditor::keyPressEvent( e ); } -/** Sets the font, font size and tab size of the editor. */ -void TextEditor::OnChange(Base::Subject &rCaller,const char* sReason) -{ - Q_UNUSED(rCaller); - ParameterGrp::handle hPrefGrp = getWindowParameter(); - if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) { -#ifdef FC_OS_LINUX - int fontSize = hPrefGrp->GetInt("FontSize", 15); -#else - int fontSize = hPrefGrp->GetInt("FontSize", 10); -#endif - QString fontFamily = QString::fromLatin1(hPrefGrp->GetASCII( "Font", "Courier" ).c_str()); - QFont font(fontFamily, fontSize); - setFont(font); - lineNumberArea->setFont(font); - } - else { - QMap::Iterator it = d->colormap.find(QString::fromLatin1(sReason)); - if (it != d->colormap.end()) { - QColor color = it.value(); - unsigned int col = App::Color::asPackedRGB(color); - auto value = static_cast(col); - value = hPrefGrp->GetUnsigned(sReason, value); - col = static_cast(value); - color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff); - if (this->highlighter) - this->highlighter->setColor(QLatin1String(sReason), color); - } - } - - if (strcmp(sReason, "TabSize") == 0 || strcmp(sReason, "FontSize") == 0) { - int tabWidth = hPrefGrp->GetInt("TabSize", 4); - QFontMetrics metric(font()); - int fontSize = QtTools::horizontalAdvance(metric, QLatin1Char('0')); -#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) - setTabStopWidth(tabWidth * fontSize); -#else - setTabStopDistance(tabWidth * fontSize); -#endif - } - - // Enables/Disables Line number in the Macro Editor from Edit->Preferences->Editor menu. - if (strcmp(sReason, "EnableLineNumber") == 0) { - QRect cr = contentsRect(); - bool show = hPrefGrp->GetBool("EnableLineNumber", true); - if(show) - lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); - else - lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), 0, cr.height())); - } - - if (strcmp(sReason, "EnableBlockCursor") == 0 || - strcmp(sReason, "FontSize") == 0 || - strcmp(sReason, "Font") == 0) { - bool block = hPrefGrp->GetBool("EnableBlockCursor", false); - if (block) - setCursorWidth(QFontMetrics(font()).averageCharWidth()); - else - setCursorWidth(1); - } -} - -void TextEditor::paintEvent (QPaintEvent * e) -{ - TextEdit::paintEvent( e ); -} - -// ------------------------------------------------------------------------------ LineMarker::LineMarker(TextEditor* editor) : QWidget(editor), textEditor(editor) diff --git a/src/Gui/TextEdit.h b/src/Gui/TextEdit.h index c1ceb49a10..ec17c852ed 100644 --- a/src/Gui/TextEdit.h +++ b/src/Gui/TextEdit.h @@ -118,6 +118,22 @@ private: friend class SyntaxHighlighter; }; +/** subclass of TextEditor that serves as base class for the + * python editor and the python console where we handle + * the tab key conversion to spaces, depending on user settings + */ +class GuiExport PythonTextEditor : public TextEditor +{ + Q_OBJECT +public: + explicit PythonTextEditor(QWidget *parent = nullptr); + ~PythonTextEditor() override; +protected: + void keyPressEvent(QKeyEvent *) override; + +}; + + class LineMarker : public QWidget { Q_OBJECT