Skip to content

Commit

Permalink
Auto-select a format for the user (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghutchis authored Sep 15, 2023
1 parent f3a462e commit e694a22
Showing 1 changed file with 25 additions and 2 deletions.
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

0 comments on commit e694a22

Please sign in to comment.