highlight error and warning messages in output window

This commit is contained in:
Axel Kohlmeyer
2024-12-26 17:19:12 -05:00
parent 5947e3e82c
commit 48178ccc50
6 changed files with 96 additions and 2 deletions

View File

@ -130,6 +130,8 @@ set(PROJECT_SOURCES
codeeditor.h
findandreplace.cpp
findandreplace.h
flagwarnings.cpp
flagwarnings.h
helpers.cpp
highlighter.cpp
highlighter.h

View File

@ -3,7 +3,6 @@ LAMMPS-GUI TODO list:
# Short term goals (v1.x)
- add wizards for all tutorials. try to reduce redundant code in wizards.
- add a highlighting to the log window that highlights warnings
- add a preferences option to override light/dark theme setting and add choice for theme
- implement a timed "Auto-Save" feature that saves after some idle time. set timeout in Editor preferences.
- add a "Filter data" checkbox to the "Charts" window to select whether data should be dropped.

View File

@ -0,0 +1,44 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "flagwarnings.h"
#include "helpers.h"
#include <QColor>
#include <QFont>
// workaround for Qt-5.12
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
namespace QColorConstants {
const QColor Red = QColor::fromRgb(0xff, 0x00, 0x00);
} // namespace QColorConstants
#endif
FlagWarnings::FlagWarnings(QTextDocument *parent) :
QSyntaxHighlighter(parent), isWarning(QStringLiteral("^(ERROR|WARNING).*$"))
{
formatWarning.setForeground(QColorConstants::Red);
formatWarning.setFontWeight(QFont::Bold);
}
void FlagWarnings::highlightBlock(const QString &text)
{
// nothing to do for empty lines
if (text.isEmpty()) return;
auto match = isWarning.match(text);
if (match.hasMatch()) {
setFormat(match.capturedStart(0), match.capturedLength(0), formatWarning);
}
}

View File

@ -0,0 +1,37 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef FLAGWARNINGS_H
#define FLAGWARNINGS_H
#include <QRegularExpression>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
class FlagWarnings : public QSyntaxHighlighter {
Q_OBJECT
public:
FlagWarnings(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text) override;
private:
QRegularExpression isWarning;
QTextCharFormat formatWarning;
};
#endif
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -13,6 +13,7 @@
#include "logwindow.h"
#include "flagwarnings.h"
#include "lammpsgui.h"
#include <QAction>
@ -33,11 +34,13 @@ const QString LogWindow::yaml_regex =
QStringLiteral("^(keywords:.*$|data:$|---$|\\.\\.\\.$| - \\[.*\\]$)");
LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
QPlainTextEdit(parent), filename(_filename)
QPlainTextEdit(parent), filename(_filename), warnings(nullptr)
{
QSettings settings;
resize(settings.value("logx", 500).toInt(), settings.value("logy", 320).toInt());
warnings = new FlagWarnings(document());
auto *action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_S), this);
connect(action, &QShortcut::activated, this, &LogWindow::save_as);
action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Y), this);
@ -50,6 +53,11 @@ LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
installEventFilter(this);
}
LogWindow::~LogWindow()
{
delete warnings;
}
void LogWindow::closeEvent(QCloseEvent *event)
{
QSettings settings;

View File

@ -16,11 +16,14 @@
#include <QPlainTextEdit>
class FlagWarnings;
class LogWindow : public QPlainTextEdit {
Q_OBJECT
public:
LogWindow(const QString &filename, QWidget *parent = nullptr);
~LogWindow() override;
private slots:
void extract_yaml();
@ -37,6 +40,7 @@ protected:
private:
QString filename;
static const QString yaml_regex;
FlagWarnings *warnings;
};
#endif