add (text) file viewer class
This commit is contained in:
@ -135,6 +135,8 @@ set(PROJECT_SOURCES
|
||||
linenumberarea.h
|
||||
logwindow.cpp
|
||||
logwindow.h
|
||||
fileviewer.cpp
|
||||
fileviewer.h
|
||||
preferences.cpp
|
||||
preferences.h
|
||||
setvariables.cpp
|
||||
|
||||
100
tools/lammps-gui/fileviewer.cpp
Normal file
100
tools/lammps-gui/fileviewer.cpp
Normal file
@ -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 <QAction>
|
||||
#include <QApplication>
|
||||
#include <QByteArray>
|
||||
#include <QFile>
|
||||
#include <QIcon>
|
||||
#include <QKeySequence>
|
||||
#include <QSettings>
|
||||
#include <QShortcut>
|
||||
#include <QString>
|
||||
#include <QTextCursor>
|
||||
#include <QTextStream>
|
||||
|
||||
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<LammpsGui *>(widget);
|
||||
if (main) main->quit();
|
||||
}
|
||||
|
||||
void FileViewer::stop_run()
|
||||
{
|
||||
LammpsGui *main = nullptr;
|
||||
for (QWidget *widget : QApplication::topLevelWidgets())
|
||||
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(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<QKeyEvent *>(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:
|
||||
39
tools/lammps-gui/fileviewer.h
Normal file
39
tools/lammps-gui/fileviewer.h
Normal file
@ -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 <QPlainTextEdit>
|
||||
|
||||
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:
|
||||
Reference in New Issue
Block a user