improve highlighting settings to also show errors with different color

This commit is contained in:
Axel Kohlmeyer
2023-08-25 21:19:05 -04:00
parent 401133acec
commit 5b5210130c
6 changed files with 38 additions and 13 deletions

View File

@ -28,7 +28,7 @@
#include <QTextBlock>
#include <QUrl>
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent), highlight(-1)
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent), highlight(NO_HIGHLIGHT)
{
help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent);
connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help);
@ -94,11 +94,9 @@ void CodeEditor::setFont(const QFont &newfont)
document()->setDefaultFont(newfont);
}
void CodeEditor::setHighlight(int block)
void CodeEditor::setCursor(int block)
{
highlight = block;
// also move cursor to current position
// move cursor to given position
auto cursor = textCursor();
int moves = block - cursor.blockNumber();
if (moves < 0)
@ -106,6 +104,17 @@ void CodeEditor::setHighlight(int block)
else
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, moves);
setTextCursor(cursor);
}
void CodeEditor::setHighlight(int block, bool error)
{
if (error)
highlight = -block;
else
highlight = block;
// also reset the cursor
setCursor(block);
// update graphics
repaint();
@ -173,12 +182,17 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1) + " ";
if ((highlight < 0) || (blockNumber != highlight)) {
if ((highlight == NO_HIGHLIGHT) || (blockNumber != std::abs(highlight))) {
painter.setPen(Qt::black);
} else {
number = QString(">") + QString::number(blockNumber + 1) + "<";
painter.fillRect(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::darkRed);
if (highlight < 0)
painter.fillRect(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::darkRed);
else
painter.fillRect(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::darkGreen);
painter.setPen(Qt::white);
}
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),

View File

@ -29,7 +29,10 @@ public:
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
void setFont(const QFont &newfont);
void setHighlight(int block);
void setCursor(int block);
void setHighlight(int block, bool error);
static constexpr int NO_HIGHLIGHT = 1 << 30;
protected:
void resizeEvent(QResizeEvent *event) override;

View File

@ -504,6 +504,7 @@ void LammpsGui::open_file(const QString &fileName)
break;
}
}
ui->textEdit->setHighlight(CodeEditor::NO_HIGHLIGHT, false);
QFileInfo path(fileName);
current_file = path.fileName();
@ -527,6 +528,7 @@ void LammpsGui::open_file(const QString &fileName)
ui->textEdit->document()->setModified(false);
file.close();
dirstatus->setText(QString(" Directory: ") + current_dir);
status->setText("Ready.");
update_variables();
}
@ -665,7 +667,7 @@ void LammpsGui::logupdate()
void *ptr = lammps.last_thermo("line", 0);
if (ptr) {
nline = *((int *)ptr);
ui->textEdit->setHighlight(nline);
ui->textEdit->setHighlight(nline, false);
}
}
@ -755,7 +757,7 @@ void LammpsGui::run_done()
delete logupdater;
logupdater = nullptr;
progress->setValue(1000);
ui->textEdit->setHighlight(-1);
ui->textEdit->setHighlight(CodeEditor::NO_HIGHLIGHT, false);
capturer->EndCapture();
auto log = capturer->GetCapture();
@ -800,13 +802,19 @@ void LammpsGui::run_done()
success = false;
}
int nline = CodeEditor::NO_HIGHLIGHT;
void *ptr = lammps.last_thermo("line", 0);
if (ptr) nline = *((int *)ptr);
if (success) {
status->setText("Ready.");
} else {
status->setText("Failed.");
ui->textEdit->setHighlight(nline, true);
QMessageBox::critical(this, "LAMMPS-GUI Error",
QString("Error running LAMMPS:\n\n") + errorbuf);
}
ui->textEdit->setCursor(nline);
progress->hide();
dirstatus->show();
}

View File

@ -42,7 +42,7 @@ public:
{
lammps = _lammps;
input = _input;
file = _file;
file = _file;
lammps->command("clear");
}

View File

@ -397,7 +397,7 @@ SnapshotTab::SnapshotTab(QSettings *_settings, QWidget *parent) :
auto *ssao = new QLabel("HQ Image mode:");
auto *bbox = new QLabel("Show Box:");
auto *axes = new QLabel("Show Axes:");
auto *vdw = new QLabel("VDW Style:");
auto *vdw = new QLabel("VDW Style:");
auto *cback = new QLabel("Background Color:");
auto *cbox = new QLabel("Box Color:");
settings->beginGroup("snapshot");

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB