diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 4a9db3e19b..ceb206b69d 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -450,7 +450,10 @@ PythonConsole::PythonConsole(QWidget *parent) // use the console highlighter pythonSyntax = new PythonConsoleHighlighter(this); - pythonSyntax->setDocument(this->document()); + setSyntaxHighlighter(pythonSyntax); + + setVisibleLineNumbers(false); + setEnabledHighlightCurrentLine(false); // create the window for call tips d->callTipsList = new CallTipsList(this); @@ -511,7 +514,6 @@ PythonConsole::~PythonConsole() saveHistory(); Base::PyGILStateLocker lock; d->hGrpSettings->Detach(this); - delete pythonSyntax; Py_XDECREF(d->_stdoutPy); Py_XDECREF(d->_stderrPy); Py_XDECREF(d->_stdinPy); diff --git a/src/Gui/TextEdit.cpp b/src/Gui/TextEdit.cpp index faa508295d..90dd2acc6c 100644 --- a/src/Gui/TextEdit.cpp +++ b/src/Gui/TextEdit.cpp @@ -205,6 +205,8 @@ void TextEdit::createListBox() namespace Gui { struct TextEditorP { + bool highlightLine = true; + bool visibleMarker = true; QMap colormap; // Color map TextEditorP() { @@ -257,11 +259,6 @@ TextEditor::TextEditor(QWidget* parent) highlightCurrentLine(); } -void TextEditor::keyPressEvent(QKeyEvent *e) -{ - TextEdit::keyPressEvent( e ); -} - /** Destroys the object and frees any allocated resources */ TextEditor::~TextEditor() { @@ -270,6 +267,27 @@ TextEditor::~TextEditor() delete d; } +void TextEditor::setVisibleLineNumbers(bool value) +{ + lineNumberArea->setVisible(value); + d->visibleMarker = value; +} + +bool TextEditor::isVisibleLineNumbers() const +{ + return d->visibleMarker; +} + +void TextEditor::setEnabledHighlightCurrentLine(bool value) +{ + d->highlightLine = value; +} + +bool TextEditor::isEnabledHighlightCurrentLine() const +{ + return d->highlightLine; +} + int TextEditor::lineNumberAreaWidth() { return QtTools::horizontalAdvance(fontMetrics(), QLatin1String("0000")) + 10; @@ -277,33 +295,42 @@ int TextEditor::lineNumberAreaWidth() void TextEditor::updateLineNumberAreaWidth(int /* newBlockCount */) { - setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); + int left = isVisibleLineNumbers() ? lineNumberAreaWidth() : 0; + setViewportMargins(left, 0, 0, 0); } void TextEditor::updateLineNumberArea(const QRect &rect, int dy) { - if (dy) - lineNumberArea->scroll(0, dy); - else - lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); + if (isVisibleLineNumbers()) { + if (dy) { + lineNumberArea->scroll(0, dy); + } + else { + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); + } - if (rect.contains(viewport()->rect())) - updateLineNumberAreaWidth(0); + if (rect.contains(viewport()->rect())) { + updateLineNumberAreaWidth(0); + } + } } void TextEditor::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); - QRect cr = contentsRect(); - lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); + if (isVisibleLineNumbers()) { + QRect cr = contentsRect(); + int width = lineNumberAreaWidth(); + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), width, cr.height())); + } } void TextEditor::highlightCurrentLine() { QList extraSelections; - if (!isReadOnly()) { + if (!isReadOnly() && isEnabledHighlightCurrentLine()) { QTextEdit::ExtraSelection selection; QColor lineColor = d->colormap[QLatin1String("Current line highlight")]; unsigned int col = App::Color::asPackedRGB(lineColor); @@ -332,6 +359,9 @@ void TextEditor::drawMarker(int line, int x, int y, QPainter* p) void TextEditor::lineNumberAreaPaintEvent(QPaintEvent *event) { + if (!isVisibleLineNumbers()) { + return; + } QPainter painter(lineNumberArea); //painter.fillRect(event->rect(), Qt::lightGray); @@ -404,31 +434,29 @@ void TextEditor::OnChange(Base::Subject &rCaller,const char* sReaso // Enables/Disables Line number in the Macro Editor from Edit->Preferences->Editor menu. if (strcmp(sReason, "EnableLineNumber") == 0) { + int width = 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 (hPrefGrp->GetBool("EnableLineNumber", true)) { + width = lineNumberAreaWidth(); + } + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), width, cr.height())); } if (strcmp(sReason, "EnableBlockCursor") == 0 || strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) { bool block = hPrefGrp->GetBool("EnableBlockCursor", false); - if (block) + if (block) { setCursorWidth(QFontMetrics(font()).averageCharWidth()); - else + } + else { setCursorWidth(1); + } } } -void TextEditor::paintEvent (QPaintEvent * e) -{ - TextEdit::paintEvent( e ); -} - // ------------------------------------------------------------------------------ + PythonTextEditor::PythonTextEditor(QWidget *parent) : TextEditor(parent) { diff --git a/src/Gui/TextEdit.h b/src/Gui/TextEdit.h index ec17c852ed..87e0bce5cf 100644 --- a/src/Gui/TextEdit.h +++ b/src/Gui/TextEdit.h @@ -93,8 +93,13 @@ public: void OnChange(Base::Subject &rCaller,const char* rcReason) override; + /** Draw a beam in the line where the cursor is. */ void lineNumberAreaPaintEvent(QPaintEvent* ); int lineNumberAreaWidth(); + void setVisibleLineNumbers(bool value); + bool isVisibleLineNumbers() const; + void setEnabledHighlightCurrentLine(bool value); + bool isEnabledHighlightCurrentLine() const; private Q_SLOTS: void updateLineNumberAreaWidth(int newBlockCount); @@ -102,9 +107,6 @@ private Q_SLOTS: void highlightCurrentLine(); protected: - void keyPressEvent (QKeyEvent * e) override; - /** Draw a beam in the line where the cursor is. */ - void paintEvent (QPaintEvent * e) override; void resizeEvent(QResizeEvent* e) override; QWidget* getMarker() const { return lineNumberArea; } @@ -128,9 +130,9 @@ class GuiExport PythonTextEditor : public TextEditor public: explicit PythonTextEditor(QWidget *parent = nullptr); ~PythonTextEditor() override; + protected: void keyPressEvent(QKeyEvent *) override; - };