implement auto-save on run and quit

This commit is contained in:
Axel Kohlmeyer
2024-08-04 11:27:03 -04:00
parent 1d1b448e18
commit d470919a44
5 changed files with 50 additions and 7 deletions

View File

@ -5,7 +5,8 @@ LAMMPS-GUI TODO list:
- figure out how widgets can be resized to fraction of available screen size. - figure out how widgets can be resized to fraction of available screen size.
- figure out stacking order of frames and whether it can be more flexible - figure out stacking order of frames and whether it can be more flexible
- figure out how to avoid corrupted cached thermo data while reading it. - figure out how to avoid corrupted cached thermo data while reading it.
- bundle LAMMPS tutorial input files - implement a timed "Auto-Save" feature that saves after some idle time. set timeout in Editor preferences.
- bundle or download? LAMMPS tutorial input files
- add a "Colors" menu to the image viewer to adjust color settings for the - add a "Colors" menu to the image viewer to adjust color settings for the
current image (unlike the defaults in the perferences). Support color by current image (unlike the defaults in the perferences). Support color by
property (e.g. scan computes or fixes with per-atom data), define colormaps etc. property (e.g. scan computes or fixes with per-atom data), define colormaps etc.

View File

@ -16,6 +16,7 @@
#include "lammpsgui.h" #include "lammpsgui.h"
#include <QApplication> #include <QApplication>
#include <QEvent>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QIcon> #include <QIcon>

View File

@ -29,6 +29,7 @@
#include <QClipboard> #include <QClipboard>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDesktopServices> #include <QDesktopServices>
#include <QEvent>
#include <QFileDialog> #include <QFileDialog>
#include <QFileInfo> #include <QFileInfo>
#include <QFont> #include <QFont>
@ -154,6 +155,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
lammps_args.push_back(mystrdup("-log")); lammps_args.push_back(mystrdup("-log"));
lammps_args.push_back(mystrdup("none")); lammps_args.push_back(mystrdup("none"));
installEventFilter(this);
setWindowIcon(QIcon(":/icons/lammps-icon-128x128.png")); setWindowIcon(QIcon(":/icons/lammps-icon-128x128.png"));
QFont all_font("Arial", -1); QFont all_font("Arial", -1);
@ -715,6 +718,7 @@ void LammpsGui::quit()
lammpsstatus->hide(); lammpsstatus->hide();
lammps.finalize(); lammps.finalize();
autoSave();
if (ui->textEdit->document()->isModified()) { if (ui->textEdit->document()->isModified()) {
QMessageBox msg; QMessageBox msg;
msg.setWindowTitle("Unsaved Changes"); msg.setWindowTitle("Unsaved Changes");
@ -1004,6 +1008,7 @@ void LammpsGui::do_run(bool use_buffer)
return; return;
} }
autoSave();
if (!use_buffer && ui->textEdit->document()->isModified()) { if (!use_buffer && ui->textEdit->document()->isModified()) {
QMessageBox msg; QMessageBox msg;
msg.setWindowTitle("Unsaved Changes"); msg.setWindowTitle("Unsaved Changes");
@ -1231,6 +1236,23 @@ void LammpsGui::setDocver()
} }
} }
void LammpsGui::autoSave()
{
// no need to auto-save, if the document has no name or is not modified.
QString fileName = current_file;
if (fileName.isEmpty()) return;
if (!ui->textEdit->document()->isModified()) return;
// check preference
bool autosave = false;
QSettings settings;
settings.beginGroup("reformat");
autosave = settings.value("autosave", false).toBool();
settings.endGroup();
if (autosave) write_file(fileName);
}
void LammpsGui::about() void LammpsGui::about()
{ {
std::string version = "This is LAMMPS-GUI version " LAMMPS_GUI_VERSION; std::string version = "This is LAMMPS-GUI version " LAMMPS_GUI_VERSION;
@ -1505,6 +1527,13 @@ void LammpsGui::start_lammps()
} }
} }
bool LammpsGui::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::Close) {
autoSave();
}
return QWidget::eventFilter(watched, event);
}
// Local Variables: // Local Variables:
// c-basic-offset: 4 // c-basic-offset: 4
// End: // End:

View File

@ -16,6 +16,7 @@
#include <QMainWindow> #include <QMainWindow>
#include <QEvent>
#include <QGridLayout> #include <QGridLayout>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
@ -70,6 +71,8 @@ protected:
void start_lammps(); void start_lammps();
void run_done(); void run_done();
void setDocver(); void setDocver();
void autoSave();
bool eventFilter(QObject *watched, QEvent *event) override;
public slots: public slots:
void quit(); void quit();

View File

@ -196,6 +196,8 @@ void Preferences::accept()
if (box) settings->setValue("return", box->isChecked()); if (box) settings->setValue("return", box->isChecked());
box = tabWidget->findChild<QCheckBox *>("autoval"); box = tabWidget->findChild<QCheckBox *>("autoval");
if (box) settings->setValue("automatic", box->isChecked()); if (box) settings->setValue("automatic", box->isChecked());
box = tabWidget->findChild<QCheckBox *>("savval");
if (box) settings->setValue("autosave", box->isChecked());
settings->endGroup(); settings->endGroup();
QDialog::accept(); QDialog::accept();
@ -536,29 +538,34 @@ EditorTab::EditorTab(QSettings *_settings, QWidget *parent) : QWidget(parent), s
auto *namelbl = new QLabel("Name width:"); auto *namelbl = new QLabel("Name width:");
auto *retlbl = new QLabel("Reformat with 'Enter':"); auto *retlbl = new QLabel("Reformat with 'Enter':");
auto *autolbl = new QLabel("Automatic completion:"); auto *autolbl = new QLabel("Automatic completion:");
auto *savlbl = new QLabel("Auto-save on 'Run' and 'Quit':");
auto *cmdval = new QSpinBox; auto *cmdval = new QSpinBox;
auto *typeval = new QSpinBox; auto *typeval = new QSpinBox;
auto *idval = new QSpinBox; auto *idval = new QSpinBox;
auto *nameval = new QSpinBox; auto *nameval = new QSpinBox;
auto *retval = new QCheckBox; auto *retval = new QCheckBox;
auto *autoval = new QCheckBox; auto *autoval = new QCheckBox;
auto *savval = new QCheckBox;
cmdval->setObjectName("cmdval");
cmdval->setRange(1, 32); cmdval->setRange(1, 32);
cmdval->setValue(settings->value("command", "16").toInt()); cmdval->setValue(settings->value("command", "16").toInt());
cmdval->setObjectName("cmdval"); typeval->setObjectName("typeval");
typeval->setRange(1, 32); typeval->setRange(1, 32);
typeval->setValue(settings->value("type", "4").toInt()); typeval->setValue(settings->value("type", "4").toInt());
typeval->setObjectName("typeval"); idval->setObjectName("idval");
idval->setRange(1, 32); idval->setRange(1, 32);
idval->setValue(settings->value("id", "8").toInt()); idval->setValue(settings->value("id", "8").toInt());
idval->setObjectName("idval"); nameval->setObjectName("nameval");
nameval->setRange(1, 32); nameval->setRange(1, 32);
nameval->setValue(settings->value("name", "8").toInt()); nameval->setValue(settings->value("name", "8").toInt());
nameval->setObjectName("nameval");
retval->setCheckState(settings->value("return", false).toBool() ? Qt::Checked : Qt::Unchecked);
retval->setObjectName("retval"); retval->setObjectName("retval");
retval->setCheckState(settings->value("return", false).toBool() ? Qt::Checked : Qt::Unchecked);
autoval->setObjectName("autoval");
autoval->setCheckState(settings->value("automatic", true).toBool() ? Qt::Checked autoval->setCheckState(settings->value("automatic", true).toBool() ? Qt::Checked
: Qt::Unchecked); : Qt::Unchecked);
autoval->setObjectName("autoval"); savval->setObjectName("savval");
savval->setCheckState(settings->value("autosave", false).toBool() ? Qt::Checked
: Qt::Unchecked);
settings->endGroup(); settings->endGroup();
int i = 0; int i = 0;
@ -575,6 +582,8 @@ EditorTab::EditorTab(QSettings *_settings, QWidget *parent) : QWidget(parent), s
grid->addWidget(retval, i++, 1, Qt::AlignVCenter); grid->addWidget(retval, i++, 1, Qt::AlignVCenter);
grid->addWidget(autolbl, i, 0, Qt::AlignTop); grid->addWidget(autolbl, i, 0, Qt::AlignTop);
grid->addWidget(autoval, i++, 1, Qt::AlignVCenter); grid->addWidget(autoval, i++, 1, Qt::AlignVCenter);
grid->addWidget(savlbl, i, 0, Qt::AlignTop);
grid->addWidget(savval, i++, 1, Qt::AlignVCenter);
grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 0); grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 0);
grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 1); grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 1);