add support for shortcuts CTRL-/ (stop run) and CTRL-Q (quit) to most windows

This commit is contained in:
Axel Kohlmeyer
2023-10-03 21:13:06 -04:00
parent 6ff7050d2d
commit a3c843f590
10 changed files with 104 additions and 17 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(lammps-gui VERSION 1.5.7 LANGUAGES CXX)
project(lammps-gui VERSION 1.5.8 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)

View File

@ -13,6 +13,8 @@
#include "chartviewer.h"
#include "lammpsgui.h"
#include <QHBoxLayout>
#include <QLineSeries>
#include <QSettings>
@ -40,8 +42,14 @@ ChartWindow::ChartWindow(const QString &_filename, QWidget *parent) :
exportDatAct = file->addAction("Export data to &Gnuplot...", this, &ChartWindow::exportDat);
exportDatAct->setIcon(QIcon(":/icons/application-plot.png"));
file->addSeparator();
stopAct = file->addAction("Stop &Run", this, &ChartWindow::stop_run);
stopAct->setIcon(QIcon(":/icons/process-stop.png"));
stopAct->setShortcut(QKeySequence(Qt::Key_Slash, Qt::CTRL));
closeAct = file->addAction("&Close", this, &QWidget::close);
closeAct->setIcon(QIcon(":/icons/window-close.png"));
quitAct = file->addAction("&Quit", this, &ChartWindow::quit);
quitAct->setIcon(QIcon(":/icons/application-exit.png"));
quitAct->setShortcut(QKeySequence::fromString("Ctrl+Q"));
auto *layout = new QVBoxLayout;
layout->addLayout(top);
setLayout(layout);
@ -92,6 +100,22 @@ void ChartWindow::add_data(int step, double data, int index)
if (c->get_index() == index) c->add_data(step, data);
}
void ChartWindow::quit()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->quit();
}
void ChartWindow::stop_run()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->stop_run();
}
void ChartWindow::saveAs()
{
if (charts.empty()) return;

View File

@ -42,6 +42,9 @@ public:
void add_data(int step, double data, int index);
private slots:
void quit();
void stop_run();
void saveAs();
void exportDat();
void exportCsv();
@ -55,10 +58,8 @@ private:
QMenuBar *menu;
QMenu *file;
QComboBox *columns;
QAction *saveAsAct;
QAction *exportCsvAct;
QAction *exportDatAct;
QAction *closeAct;
QAction *saveAsAct, *exportCsvAct, *exportDatAct;
QAction *closeAct, *stopAct, *quitAct;
QString filename;
QList<ChartViewer *> charts;

View File

@ -12,9 +12,12 @@
------------------------------------------------------------------------- */
#include "imageviewer.h"
#include "lammpsgui.h"
#include "lammpswrapper.h"
#include <QAction>
#include <QApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileDialog>
@ -510,15 +513,11 @@ void ImageViewer::createImage()
QImageReader reader(dumpfile.fileName());
reader.setAutoTransform(true);
const QImage newImage = reader.read();
if (newImage.isNull()) {
QMessageBox::warning(
this, QGuiApplication::applicationDisplayName(),
QString("Cannot load %1: %2").arg(dumpfile.fileName(), reader.errorString()));
return;
}
dumpfile.remove();
// read of new image failed. Don't try to scale and load it.
if (newImage.isNull()) return;
// scale back to achieve antialiasing
image = newImage.scaled(xsize, ysize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
imageLabel->setPixmap(QPixmap::fromImage(image));
@ -536,6 +535,14 @@ void ImageViewer::saveAs()
void ImageViewer::copy() {}
void ImageViewer::quit()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->quit();
}
void ImageViewer::saveFile(const QString &fileName)
{
if (!fileName.isEmpty()) image.save(fileName);
@ -557,6 +564,9 @@ void ImageViewer::createActions()
QAction *exitAct = fileMenu->addAction("&Close", this, &QWidget::close);
exitAct->setIcon(QIcon(":/icons/window-close.png"));
exitAct->setShortcut(QKeySequence::fromString("Ctrl+W"));
QAction *quitAct = fileMenu->addAction("&Quit", this, &ImageViewer::quit);
quitAct->setIcon(QIcon(":/icons/application-exit.png"));
quitAct->setShortcut(QKeySequence::fromString("Ctrl+Q"));
}
void ImageViewer::updateActions()

View File

@ -40,6 +40,7 @@ public:
private slots:
void saveAs();
void copy();
void quit();
void edit_size();
void reset_view();

View File

@ -66,6 +66,10 @@ protected:
void start_lammps();
void run_done();
public slots:
void quit();
void stop_run();
private slots:
void new_document();
void open();
@ -73,7 +77,6 @@ private slots:
void start_exe();
void save();
void save_as();
void quit();
void copy();
void cut();
void paste();
@ -82,7 +85,6 @@ private slots:
void run_buffer() { do_run(true); }
void run_file() { do_run(false); }
void stop_run();
void edit_variables();
void render_image();
void view_slides();

View File

@ -13,7 +13,10 @@
#include "logwindow.h"
#include "lammpsgui.h"
#include <QAction>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QFileDialog>
@ -32,8 +35,12 @@ LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
QSettings settings;
resize(settings.value("logx", 500).toInt(), settings.value("logy", 320).toInt());
auto action = new QShortcut(QKeySequence("Ctrl+S"), this);
auto action = new QShortcut(QKeySequence::fromString("Ctrl+S"), this);
connect(action, &QShortcut::activated, this, &LogWindow::save_as);
action = new QShortcut(QKeySequence::fromString("Ctrl+Q"), this);
connect(action, &QShortcut::activated, this, &LogWindow::quit);
action = new QShortcut(QKeySequence(Qt::Key_Slash, Qt::CTRL), this);
connect(action, &QShortcut::activated, this, &LogWindow::stop_run);
}
void LogWindow::closeEvent(QCloseEvent *event)
@ -46,6 +53,22 @@ void LogWindow::closeEvent(QCloseEvent *event)
QPlainTextEdit::closeEvent(event);
}
void LogWindow::quit()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->quit();
}
void LogWindow::stop_run()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->stop_run();
}
void LogWindow::save_as()
{
QString defaultname = filename + ".log";

View File

@ -23,7 +23,9 @@ public:
LogWindow(const QString &filename, QWidget *parent = nullptr);
private slots:
void quit();
void save_as();
void stop_run();
protected:
void closeEvent(QCloseEvent *event) override;

View File

@ -14,7 +14,9 @@
#include "slideshow.h"
#include "helpers.h"
#include "lammpsgui.h"
#include <QApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileDialog>
@ -48,8 +50,12 @@ SlideShow::SlideShow(const QString &fileName, QWidget *parent) :
imageName->setAlignment(Qt::AlignCenter);
imageName->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
auto *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::close);
auto *shortcut = new QShortcut(QKeySequence::fromString("Ctrl+W"), this);
QObject::connect(shortcut, &QShortcut::activated, this, &QWidget::close);
shortcut = new QShortcut(QKeySequence::fromString("Ctrl+/"), this);
QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::stop_run);
shortcut = new QShortcut(QKeySequence::fromString("Ctrl+Q"), this);
QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::quit);
buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
@ -190,6 +196,22 @@ void SlideShow::loadImage(int idx)
} while (idx >= 0);
}
void SlideShow::quit()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->quit();
}
void SlideShow::stop_run()
{
LammpsGui *main;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
main->stop_run();
}
void SlideShow::movie()
{
QString fileName = QFileDialog::getSaveFileName(this, "Export to Movie File", ".",

View File

@ -32,6 +32,8 @@ public:
void clear();
private slots:
void quit();
void stop_run();
void movie();
void first();
void last();