From 97ffabda0e3eabfa6831e124fe0df99fff022b8c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 21 Jul 2024 17:51:46 -0400 Subject: [PATCH] add (text) file viewer class --- tools/lammps-gui/CMakeLists.txt | 2 + tools/lammps-gui/fileviewer.cpp | 100 ++++++++++++++++++++++++++++++++ tools/lammps-gui/fileviewer.h | 39 +++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 tools/lammps-gui/fileviewer.cpp create mode 100644 tools/lammps-gui/fileviewer.h diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index 74ceddacde..30e9fc824f 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -135,6 +135,8 @@ set(PROJECT_SOURCES linenumberarea.h logwindow.cpp logwindow.h + fileviewer.cpp + fileviewer.h preferences.cpp preferences.h setvariables.cpp diff --git a/tools/lammps-gui/fileviewer.cpp b/tools/lammps-gui/fileviewer.cpp new file mode 100644 index 0000000000..f93039d809 --- /dev/null +++ b/tools/lammps-gui/fileviewer.cpp @@ -0,0 +1,100 @@ +/* ---------------------------------------------------------------------- + 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 "fileviewer.h" + +#include "lammpsgui.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +FileViewer::FileViewer(const QString &_filename, QWidget *parent) : + QPlainTextEdit(parent), fileName(_filename) +{ + auto *action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this); + connect(action, &QShortcut::activated, this, &FileViewer::quit); + action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash), this); + connect(action, &QShortcut::activated, this, &FileViewer::stop_run); + + installEventFilter(this); + + // open and read file. Set editor to read-only. + QFile file(fileName); + if (file.open(QIODevice::Text | QIODevice::ReadOnly)) { + QTextStream in(&file); + QString content = in.readAll(); + file.close(); + + QFont text_font; + QSettings settings; + text_font.fromString(settings.value("textfont", text_font.toString()).toString()); + document()->setDefaultFont(text_font); + + document()->setPlainText(content); + moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor); + setReadOnly(true); + setLineWrapMode(NoWrap); + setMinimumSize(800, 500); + setWindowIcon(QIcon(":/icons/lammps-icon-128x128.png")); + setWindowTitle("LAMMPS-GUI - Viewer - " + fileName); + } +} + +void FileViewer::quit() +{ + LammpsGui *main = nullptr; + for (QWidget *widget : QApplication::topLevelWidgets()) + if (widget->objectName() == "LammpsGui") main = dynamic_cast(widget); + if (main) main->quit(); +} + +void FileViewer::stop_run() +{ + LammpsGui *main = nullptr; + for (QWidget *widget : QApplication::topLevelWidgets()) + if (widget->objectName() == "LammpsGui") main = dynamic_cast(widget); + if (main) main->stop_run(); +} + +// event filter to handle "Ambiguous shortcut override" issues +bool FileViewer::eventFilter(QObject *watched, QEvent *event) +{ + if (event->type() == QEvent::ShortcutOverride) { + auto *keyEvent = dynamic_cast(event); + if (!keyEvent) return QWidget::eventFilter(watched, event); + if (keyEvent->modifiers().testFlag(Qt::ControlModifier) && keyEvent->key() == '/') { + stop_run(); + event->accept(); + return true; + } + if (keyEvent->modifiers().testFlag(Qt::ControlModifier) && keyEvent->key() == 'W') { + close(); + event->accept(); + return true; + } + } + return QWidget::eventFilter(watched, event); +} + +// Local Variables: +// c-basic-offset: 4 +// End: diff --git a/tools/lammps-gui/fileviewer.h b/tools/lammps-gui/fileviewer.h new file mode 100644 index 0000000000..d52fa15a12 --- /dev/null +++ b/tools/lammps-gui/fileviewer.h @@ -0,0 +1,39 @@ +/* -*- 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 FILEVIEWER_H +#define FILEVIEWER_H + +#include + +class FileViewer : public QPlainTextEdit { + Q_OBJECT + +public: + FileViewer(const QString &filename, QWidget *parent = nullptr); + +private slots: + void quit(); + void stop_run(); + +protected: + bool eventFilter(QObject *watched, QEvent *event) override; + +private: + QString fileName; +}; + +#endif +// Local Variables: +// c-basic-offset: 4 +// End: