From 8a7c1f3d0de8009e71454bac445c410b9ef6837d Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 24 Aug 2023 21:25:57 -0400 Subject: [PATCH] add support for add a highlight for the currently active input line to the line number display --- tools/lammps-gui/codeeditor.cpp | 27 ++++++++++++++++++++------- tools/lammps-gui/codeeditor.h | 2 ++ tools/lammps-gui/lammpsgui.cpp | 8 ++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/tools/lammps-gui/codeeditor.cpp b/tools/lammps-gui/codeeditor.cpp index a1e120517d..71d9cfd370 100644 --- a/tools/lammps-gui/codeeditor.cpp +++ b/tools/lammps-gui/codeeditor.cpp @@ -28,7 +28,7 @@ #include #include -CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) +CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent), highlight(-1) { help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent); connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help); @@ -83,7 +83,7 @@ int CodeEditor::lineNumberAreaWidth() ++digits; } - int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits; + int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * (digits + 2); return space; } @@ -94,6 +94,12 @@ void CodeEditor::setFont(const QFont &newfont) document()->setDefaultFont(newfont); } +void CodeEditor::setHighlight(int block) +{ + highlight = block; + repaint(); +} + void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); @@ -167,15 +173,22 @@ void CodeEditor::highlightCurrentLine() void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) { QPainter painter(lineNumberArea); - painter.fillRect(event->rect(), Qt::lightGray); QTextBlock block = firstVisibleBlock(); int blockNumber = block.blockNumber(); - int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top()); - int bottom = top + qRound(blockBoundingRect(block).height()); + + int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top()); + int bottom = top + qRound(blockBoundingRect(block).height()); while (block.isValid() && top <= event->rect().bottom()) { if (block.isVisible() && bottom >= event->rect().top()) { - QString number = QString::number(blockNumber + 1); - painter.setPen(Qt::black); + QString number = QString::number(blockNumber + 1) + " "; + if ((highlight < 0) || (blockNumber != highlight)) { + painter.setPen(Qt::black); + } else { + number = QString(">") + QString::number(blockNumber + 1) + "<"; + painter.fillRect(0, top, lineNumberArea->width(), fontMetrics().height(), + Qt::darkRed); + painter.setPen(Qt::white); + } painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number); } diff --git a/tools/lammps-gui/codeeditor.h b/tools/lammps-gui/codeeditor.h index a6c49ed9db..2ecf8b8f24 100644 --- a/tools/lammps-gui/codeeditor.h +++ b/tools/lammps-gui/codeeditor.h @@ -29,6 +29,7 @@ public: void lineNumberAreaPaintEvent(QPaintEvent *event); int lineNumberAreaWidth(); void setFont(const QFont &newfont); + void setHighlight(int block); protected: void resizeEvent(QResizeEvent *event) override; @@ -48,6 +49,7 @@ private slots: private: QWidget *lineNumberArea; QShortcut *help_action; + int highlight; QMap cmd_map; QMap fix_map; diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 5f1f1e1dda..2fe612817e 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -659,6 +659,13 @@ void LammpsGui::logupdate() t_remain = lammps.get_thermo("cpuremain"); t_total = t_elapsed + t_remain + 1.0e-10; completed = t_elapsed / t_total * 1000.0; + + int nline = -1; + void *ptr = lammps.last_thermo("line", 0); + if (ptr) { + nline = *((int *)ptr); + ui->textEdit->setHighlight(nline); + } } progress->setValue(completed); @@ -747,6 +754,7 @@ void LammpsGui::run_done() delete logupdater; logupdater = nullptr; progress->setValue(1000); + ui->textEdit->setHighlight(-1); capturer->EndCapture(); auto log = capturer->GetCapture();