make handling of plugin path and recent files more consistent

This commit is contained in:
Axel Kohlmeyer
2024-08-27 14:27:39 -04:00
parent 83aab0f425
commit 4060de6a9c
3 changed files with 27 additions and 22 deletions

View File

@ -69,7 +69,7 @@
static const QString blank(" "); static const QString blank(" ");
static constexpr int BUFLEN = 256; static constexpr int BUFLEN = 256;
LammpsGui::LammpsGui(QWidget *parent, const char *filename) : LammpsGui::LammpsGui(QWidget *parent, const QString &filename) :
QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr), QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr),
status(nullptr), logwindow(nullptr), imagewindow(nullptr), chartwindow(nullptr), status(nullptr), logwindow(nullptr), imagewindow(nullptr), chartwindow(nullptr),
slideshow(nullptr), logupdater(nullptr), dirstatus(nullptr), progress(nullptr), slideshow(nullptr), logupdater(nullptr), dirstatus(nullptr), progress(nullptr),
@ -98,21 +98,21 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
plugin_path.clear(); plugin_path.clear();
std::string deffile = settings.value("plugin_path", "liblammps.so").toString().toStdString(); QString deffile = settings.value("plugin_path", "liblammps.so").toString();
for (const char *libfile : {deffile.c_str(), "./liblammps.so", "liblammps.dylib", for (const char *libfile : {deffile.toStdString().c_str(), "./liblammps.so", "liblammps.dylib",
"./liblammps.dylib", "liblammps.dll"}) { "./liblammps.dylib", "liblammps.dll"}) {
if (lammps.load_lib(libfile)) { if (lammps.load_lib(libfile)) {
auto canonical = QFileInfo(libfile).canonicalFilePath(); plugin_path = QFileInfo(libfile).canonicalFilePath();
plugin_path = canonical.toStdString(); settings.setValue("plugin_path", plugin_path);
settings.setValue("plugin_path", canonical);
break; break;
} }
} }
if (plugin_path.empty()) { if (plugin_path.isEmpty()) {
// none of the plugin paths could load, remove key // none of the plugin paths could load, remove key
settings.remove("plugin_path"); settings.remove("plugin_path");
QMessageBox::critical(this, "Error", "Cannot open LAMMPS shared library file"); QMessageBox::critical(this, "Error", "Cannot open LAMMPS shared library file.\n"
"Use -p command line flag to specify a path to the library.");
exit(1); exit(1);
} }
#endif #endif
@ -281,7 +281,7 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
dirstatus->show(); dirstatus->show();
ui->statusbar->addWidget(progress); ui->statusbar->addWidget(progress);
if (filename) { if (filename.size() > 0) {
open_file(filename); open_file(filename);
} else { } else {
setWindowTitle("LAMMPS-GUI - Editor - *unknown*"); setWindowTitle("LAMMPS-GUI - Editor - *unknown*");
@ -505,7 +505,8 @@ void LammpsGui::start_exe()
void LammpsGui::update_recents(const QString &filename) void LammpsGui::update_recents(const QString &filename)
{ {
QSettings settings; QSettings settings;
recent = settings.value("recent").value<QList<QString>>(); if (settings.contains("recent"))
recent = settings.value("recent").value<QList<QString>>();
for (int i = 0; i < recent.size(); ++i) { for (int i = 0; i < recent.size(); ++i) {
QFileInfo fi(recent[i]); QFileInfo fi(recent[i]);
@ -517,7 +518,8 @@ void LammpsGui::update_recents(const QString &filename)
if (!filename.isEmpty() && !recent.contains(filename)) recent.prepend(filename); if (!filename.isEmpty() && !recent.contains(filename)) recent.prepend(filename);
if (recent.size() > 5) recent.removeLast(); if (recent.size() > 5) recent.removeLast();
settings.setValue("recent", QVariant::fromValue(recent)); if (recent.size() > 0) settings.setValue("recent", QVariant::fromValue(recent));
else settings.remove("recent");
ui->action_1->setVisible(false); ui->action_1->setVisible(false);
if ((recent.size() > 0) && !recent[0].isEmpty()) { if ((recent.size() > 0) && !recent[0].isEmpty()) {
@ -1438,9 +1440,9 @@ void LammpsGui::about()
version += " using dark theme\n"; version += " using dark theme\n";
if (lammps.has_plugin()) { if (lammps.has_plugin()) {
version += "LAMMPS library loaded as plugin"; version += "LAMMPS library loaded as plugin";
if (!plugin_path.empty()) { if (!plugin_path.isEmpty()) {
version += " from file "; version += " from file ";
version += plugin_path; version += plugin_path.toStdString();
} }
} else { } else {
version += "LAMMPS library linked to executable"; version += "LAMMPS library linked to executable";

View File

@ -68,7 +68,7 @@ class LammpsGui : public QMainWindow {
friend class Tutorial2Wizard; friend class Tutorial2Wizard;
public: public:
LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr); LammpsGui(QWidget *parent = nullptr, const QString &filename = QString());
~LammpsGui() override; ~LammpsGui() override;
protected: protected:
@ -172,7 +172,7 @@ private:
LammpsWrapper lammps; LammpsWrapper lammps;
LammpsRunner *runner; LammpsRunner *runner;
QString docver; QString docver;
std::string plugin_path; QString plugin_path;
bool is_running; bool is_running;
int run_counter; int run_counter;
std::vector<char *> lammps_args; std::vector<char *> lammps_args;

View File

@ -53,19 +53,22 @@ int main(int argc, char *argv[])
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("file", "The LAMMPS input file to open (optional)."); parser.addPositionalArgument("file", "The LAMMPS input file to open (optional).");
parser.process(app); // this removes known arguments parser.process(app);
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
if (parser.isSet(plugindir)) { if (parser.isSet(plugindir)) {
QString pluginpath = parser.value(plugindir); QStringList pluginpath = parser.values(plugindir);
QSettings settings; if (pluginpath.length() > 0) {
settings.setValue("plugin_path", pluginpath); QSettings settings;
settings.sync(); settings.setValue("plugin_path", QString(pluginpath.at(0)));
settings.sync();
}
} }
#endif #endif
const char *infile = nullptr; QString infile;
if (argc > 1) infile = argv[1]; QStringList args = parser.positionalArguments();
if (args.size() > 0) infile = args[0];
LammpsGui w(nullptr, infile); LammpsGui w(nullptr, infile);
w.show(); w.show();
return app.exec(); return app.exec();