diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index 5ac23daac7..057d686b12 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -130,6 +130,8 @@ set(PROJECT_SOURCES codeeditor.h findandreplace.cpp findandreplace.h + flagwarnings.cpp + flagwarnings.h helpers.cpp highlighter.cpp highlighter.h diff --git a/tools/lammps-gui/TODO.md b/tools/lammps-gui/TODO.md index 3bb8c8d3dd..c9c8d26568 100644 --- a/tools/lammps-gui/TODO.md +++ b/tools/lammps-gui/TODO.md @@ -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. diff --git a/tools/lammps-gui/flagwarnings.cpp b/tools/lammps-gui/flagwarnings.cpp new file mode 100644 index 0000000000..8c0a4c4cde --- /dev/null +++ b/tools/lammps-gui/flagwarnings.cpp @@ -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 +#include + +// 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); + } +} diff --git a/tools/lammps-gui/flagwarnings.h b/tools/lammps-gui/flagwarnings.h new file mode 100644 index 0000000000..b34f1eba6a --- /dev/null +++ b/tools/lammps-gui/flagwarnings.h @@ -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 +#include +#include + +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: diff --git a/tools/lammps-gui/logwindow.cpp b/tools/lammps-gui/logwindow.cpp index 4527bcb0dd..85ed1d8744 100644 --- a/tools/lammps-gui/logwindow.cpp +++ b/tools/lammps-gui/logwindow.cpp @@ -13,6 +13,7 @@ #include "logwindow.h" +#include "flagwarnings.h" #include "lammpsgui.h" #include @@ -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; diff --git a/tools/lammps-gui/logwindow.h b/tools/lammps-gui/logwindow.h index b6cf78c48f..84240b6e70 100644 --- a/tools/lammps-gui/logwindow.h +++ b/tools/lammps-gui/logwindow.h @@ -16,11 +16,14 @@ #include +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