diff --git a/tools/lammps-gui/TODO.md b/tools/lammps-gui/TODO.md index 23728c60d9..8b08708ffc 100644 --- a/tools/lammps-gui/TODO.md +++ b/tools/lammps-gui/TODO.md @@ -8,11 +8,8 @@ LAMMPS-GUI TODO list: handle "&" continuation and multiline strings with """ like C style comments in Qt docs example - add CTRL-q hotkey to log windows so you can exit the entire application (add do you really want to? dialog to this) - add "syntax check" with enabled "-skiprun" flag -- add multi-tab settings dialog where certain properties can be set through customizing the LAMMPS command line - + select Font -- add list of 5(?) most recently opened/saved files to file dialog (and also write to settings state on exit) (note: must store full path!) -- need to handle "label" and "jump" commands from within -- switch processing of input to line based commands +- need to handle "label" and "jump" commands from within ? +- switch processing of input to line based commands or? - switch input file editor to read-only while loop is running # Long term ideas diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 237a318cc0..f69d164454 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -55,6 +55,7 @@ #endif static const QString blank(" "); +static constexpr int MAXRECENT = 5; // duplicate string static char *mystrdup(const std::string &text) @@ -72,19 +73,21 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : // enforce using the plain ASCII C locale within the GUI. QLocale::setDefault(QLocale("C")); + // register QList + qRegisterMetaTypeStreamOperators>("QList"); + ui->setupUi(this); this->setCentralWidget(ui->textEdit); highlighter = new Highlighter(ui->textEdit->document()); capturer = new StdCapture; current_file.clear(); current_dir = QDir(".").absolutePath(); - recent_files.clear(); QCoreApplication::setOrganizationName("The LAMMPS Developers"); QCoreApplication::setOrganizationDomain("lammps.org"); QCoreApplication::setApplicationName("LAMMPS GUI"); - // restorge and initialize settings + // restore and initialize settings QSettings settings; // switch configured accelerator back to "none" if needed. @@ -160,6 +163,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : ui->textEdit->document()->setDefaultFont(text_font); ui->textEdit->setMinimumSize(600, 400); + update_recents(); + connect(ui->actionNew, &QAction::triggered, this, &LammpsGui::new_document); connect(ui->actionOpen, &QAction::triggered, this, &LammpsGui::open); connect(ui->actionSave, &QAction::triggered, this, &LammpsGui::save); @@ -180,6 +185,11 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : connect(ui->actionDefaults, &QAction::triggered, this, &LammpsGui::defaults); connect(ui->actionView_Log_Window, &QAction::triggered, this, &LammpsGui::view_log); connect(ui->actionView_Graph_Window, &QAction::triggered, this, &LammpsGui::view_chart); + connect(ui->action_1, &QAction::triggered, this, &LammpsGui::open_recent); + connect(ui->action_2, &QAction::triggered, this, &LammpsGui::open_recent); + connect(ui->action_3, &QAction::triggered, this, &LammpsGui::open_recent); + connect(ui->action_4, &QAction::triggered, this, &LammpsGui::open_recent); + connect(ui->action_5, &QAction::triggered, this, &LammpsGui::open_recent); connect(ui->textEdit->document(), &QTextDocument::modificationChanged, this, &LammpsGui::modified); @@ -260,6 +270,66 @@ void LammpsGui::open() open_file(fileName); } +void LammpsGui::open_recent() +{ + QAction *act = qobject_cast(sender()); + if (act) open_file(act->data().toString()); +} + +void LammpsGui::update_recents(const QString &filename) +{ + QSettings settings; + recent = settings.value("recent").value>(); + + for (int i = 0; i < recent.size(); ++i) { + QFileInfo fi(recent[i]); + if (!fi.isReadable()) { + recent.removeAt(i); + i = 0; + } + } + + if (!filename.isEmpty() && !recent.contains(filename)) recent.prepend(filename); + if (recent.size() > 5) recent.removeLast(); + settings.setValue("recent", QVariant::fromValue(recent)); + + ui->action_1->setVisible(false); + if ((recent.size() > 0) && !recent[0].isEmpty()) { + QFileInfo fi(recent[0]); + ui->action_1->setText(QString("1. ") + fi.fileName()); + ui->action_1->setData(recent[0]); + ui->action_1->setVisible(true); + } + ui->action_2->setVisible(false); + if ((recent.size() > 1) && !recent[1].isEmpty()) { + QFileInfo fi(recent[1]); + ui->action_2->setText(QString("2. ") + fi.fileName()); + ui->action_2->setData(recent[1]); + ui->action_2->setVisible(true); + } + ui->action_3->setVisible(false); + if ((recent.size() > 2) && !recent[2].isEmpty()) { + QFileInfo fi(recent[2]); + ui->action_3->setText(QString("3. ") + fi.fileName()); + ui->action_3->setData(recent[2]); + ui->action_3->setVisible(true); + } + ui->action_4->setVisible(false); + if ((recent.size() > 3) && !recent[3].isEmpty()) { + QFileInfo fi(recent[3]); + ui->action_4->setText(QString("4. ") + fi.fileName()); + ui->action_4->setData(recent[0]); + ui->action_4->setVisible(true); + } + ui->action_5->setVisible(false); + if ((recent.size() > 4) && !recent[4].isEmpty()) { + QFileInfo fi(recent[4]); + ui->action_5->setText(QString("5. ") + fi.fileName()); + ui->action_5->setData(recent[0]); + ui->action_5->setVisible(true); + } +} + // open file and switch CWD to path of file void LammpsGui::open_file(const QString &fileName) { @@ -291,6 +361,8 @@ void LammpsGui::open_file(const QString &fileName) current_dir = path.absolutePath(); QFile file(path.absoluteFilePath()); + update_recents(path.absoluteFilePath()); + QDir::setCurrent(current_dir); if (!file.open(QIODevice::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "Warning", @@ -310,16 +382,19 @@ void LammpsGui::open_file(const QString &fileName) void LammpsGui::write_file(const QString &fileName) { - QFile file(fileName); - QFileInfo path(file); + QFileInfo path(fileName); current_file = path.fileName(); current_dir = path.absolutePath(); + QFile file(path.absoluteFilePath()); if (!file.open(QIODevice::WriteOnly | QFile::Text)) { QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString()); return; } setWindowTitle(QString("LAMMPS-GUI - " + current_file)); + + update_recents(path.absoluteFilePath()); + QTextStream out(&file); QString text = ui->textEdit->toPlainText(); out << text; diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index 45f840fab3..d539b51529 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -56,12 +56,14 @@ public: protected: void open_file(const QString &filename); void write_file(const QString &filename); + void update_recents(const QString &filename = ""); void start_lammps(); void run_done(); private slots: void new_document(); void open(); + void open_recent(); void save(); void save_as(); void quit(); @@ -101,10 +103,11 @@ private: QString current_file; QString current_dir; + QList recent; + LammpsWrapper lammps; std::string plugin_path; bool is_running; - QList recent_files; std::vector lammps_args; }; #endif // LAMMPSGUI_H diff --git a/tools/lammps-gui/lammpsgui.ui b/tools/lammps-gui/lammpsgui.ui index cab83abcf6..3031b31d23 100644 --- a/tools/lammps-gui/lammpsgui.ui +++ b/tools/lammps-gui/lammpsgui.ui @@ -27,7 +27,7 @@ 0 0 600 - 23 + 24 @@ -35,8 +35,15 @@ &File + + + + + + + @@ -356,7 +363,8 @@ - + + .. &Log Window @@ -364,12 +372,38 @@ - + + .. &Chart Window + + + &1. + + + + + &2. + + + + + &3. + + + + + &4. + + + + + &5. + + diff --git a/tools/lammps-gui/lammpsrunner.h b/tools/lammps-gui/lammpsrunner.h index 3816010868..72ad52986e 100644 --- a/tools/lammps-gui/lammpsrunner.h +++ b/tools/lammps-gui/lammpsrunner.h @@ -32,7 +32,7 @@ public: emit resultReady(); } - // transfer info to worker thread + // transfer info to worker thread and reset LAMMPS instance void setup_run(LammpsWrapper *_lammps, const char *_input) { lammps = _lammps;