add support for add a highlight for the currently active input line to the line number display

This commit is contained in:
Axel Kohlmeyer
2023-08-24 21:25:57 -04:00
parent 3a7efdfb8c
commit 8a7c1f3d0d
3 changed files with 30 additions and 7 deletions

View File

@ -28,7 +28,7 @@
#include <QTextBlock>
#include <QUrl>
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);
}

View File

@ -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<QString, QString> cmd_map;
QMap<QString, QString> fix_map;

View File

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