From d388ef762d91766b09564ce1b52a18da3e2df3f3 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 6 Oct 2023 06:42:40 -0400 Subject: [PATCH] Implement more conventional behavior when opening non-existing file. Instead of only printing a warning we now create a *named* and empty buffer and change to the directory of the filename, so that the file will be created in the desired location as soon as it is saved. --- tools/lammps-gui/lammpsgui.cpp | 15 ++++++++------- tools/lammps-gui/main.cpp | 5 ----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 01933c46eb..c9d8b9bef0 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -588,22 +588,23 @@ void LammpsGui::open_file(const QString &fileName) if (!file.open(QIODevice::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "Warning", "Cannot open file " + path.absoluteFilePath() + ": " + - file.errorString()); - return; + file.errorString() + ".\nWill create new file on saving editor buffer."); + ui->textEdit->document()->setPlainText(QString()); + } else { + QTextStream in(&file); + QString text = in.readAll(); + ui->textEdit->document()->setPlainText(text); + ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor); + file.close(); } setWindowTitle(QString("LAMMPS-GUI - " + current_file)); run_counter = 0; - QTextStream in(&file); - QString text = in.readAll(); - ui->textEdit->document()->setPlainText(text); - ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor); ui->textEdit->document()->setModified(false); ui->textEdit->setGroupList(); ui->textEdit->setVarNameList(); ui->textEdit->setComputeIDList(); ui->textEdit->setFixIDList(); ui->textEdit->setFileList(); - file.close(); dirstatus->setText(QString(" Directory: ") + current_dir); status->setText("Ready."); diff --git a/tools/lammps-gui/main.cpp b/tools/lammps-gui/main.cpp index 310ccb291f..cf09fbb892 100644 --- a/tools/lammps-gui/main.cpp +++ b/tools/lammps-gui/main.cpp @@ -32,11 +32,6 @@ int main(int argc, char *argv[]) printf("Usage: %s [-h|-help|]\n", argv[0]); return 1; } - if (!QFileInfo::exists(infile)) { - printf("Input file %s does not exist\n", infile); - printf("Usage: %s [-h|-help|]\n", argv[0]); - return 2; - } } LammpsGui w(nullptr, infile);