Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-select a format for the user #1348

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions avogadro/qtgui/fileformatdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <avogadro/core/molecule.h>
#include <avogadro/io/fileformatmanager.h>

#include <QtWidgets/QApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>

Expand Down Expand Up @@ -222,7 +223,7 @@ QString FileFormatDialog::generateFilterString(
for (auto ff : ffs) {
QString name(QString::fromStdString(ff->name()));
std::vector<std::string> exts = ff->fileExtensions();
for (auto & eit : exts) {
for (auto& eit : exts) {
QString ext(QString::fromStdString(eit));
if (!formatMap.values(name).contains(ext)) {
formatMap.insertMulti(name, ext);
Expand Down Expand Up @@ -309,12 +310,34 @@ const Io::FileFormat* FileFormatDialog::selectFileFormat(
if (preferred.size() == 1)
return ffs[idents.indexOf(preferred.first())];

// We will only show the dialog if the user is pressing
// the shift, control or meta keys.
if (!(QApplication::keyboardModifiers() &
(Qt::ShiftModifier | Qt::ControlModifier | Qt::MetaModifier))) {
// use a script format if set (i.e., these override internal)
// otherwise use internal over Open Babel
for (int i = 0; i < static_cast<int>(ffs.size()); ++i) {
if (idents[i].startsWith("User")) {
return ffs[i];
} else if (idents[i].startsWith("Avogadro")) {
return ffs[i];
} else if (idents[i].startsWith("OpenBabel")) {
return ffs[i];
}
}
// if we get here, we should show the user the dialog
// .. but it should never happen
}

// See if they used one before:
QString lastIdent = settingsKey.isNull()
? QString()
: QSettings().value(settingsKey).toString();

int lastIdentIndex = idents.indexOf(lastIdent);

// we're going to show the dialog - if there wasn't a choice
// the default should be the first one
if (lastIdentIndex < 0)
lastIdentIndex = 0;

Expand All @@ -334,4 +357,4 @@ const Io::FileFormat* FileFormatDialog::selectFileFormat(
return ffs[index];
}

} // namespace Avogadro
} // namespace Avogadro::QtGui