Gui: Hide line marker and do not highlight current line in Python console

This commit is contained in:
wmayer
2024-12-25 18:31:03 +01:00
committed by Yorik van Havre
parent 35700db40e
commit 0dba0a3b31
3 changed files with 65 additions and 33 deletions

View File

@@ -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);

View File

@@ -205,6 +205,8 @@ void TextEdit::createListBox()
namespace Gui {
struct TextEditorP
{
bool highlightLine = true;
bool visibleMarker = true;
QMap<QString, QColor> 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<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
if (!isReadOnly() && isEnabledHighlightCurrentLine()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = d->colormap[QLatin1String("Current line highlight")];
unsigned int col = App::Color::asPackedRGB<QColor>(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<const char*> &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)
{

View File

@@ -93,8 +93,13 @@ public:
void OnChange(Base::Subject<const char*> &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;
};