Improve File Dialog

* QFileDialog returns UNIX paths, even on Windows. This patch converts what QFileDialog returns to the native path format.

* Improve const correctness

* Avoid imposing file extension on Linux

* This patch improves things like unneeded passes by values, missing const qualifiers, ugly copies because of variable reuse and consistency in variable names.
This commit is contained in:
Gianluca Recchia 2019-08-24 17:53:11 +02:00 committed by Jonathan White
parent c12fd369d9
commit fccbb98b8e
6 changed files with 70 additions and 159 deletions

View file

@ -394,8 +394,8 @@ void DatabaseTabWidget::exportToCsv()
return; return;
} }
QString fileName = fileDialog()->getSaveFileName( const QString fileName = fileDialog()->getSaveFileName(
this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, nullptr, "csv"); this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, nullptr);
if (fileName.isEmpty()) { if (fileName.isEmpty()) {
return; return;
} }
@ -419,13 +419,8 @@ void DatabaseTabWidget::exportToHtml()
return; return;
} }
QString fileName = fileDialog()->getSaveFileName(this, const QString fileName = fileDialog()->getSaveFileName(
tr("Export database to HTML file"), this, tr("Export database to HTML file"), QString(), tr("HTML file").append(" (*.html)"), nullptr, nullptr);
QString(),
tr("HTML file").append(" (*.html)"),
nullptr,
nullptr,
"html");
if (fileName.isEmpty()) { if (fileName.isEmpty()) {
return; return;
} }

View file

@ -1656,13 +1656,8 @@ bool DatabaseWidget::saveAs()
oldFilePath = QDir::toNativeSeparators(config()->get("LastDir", QDir::homePath()).toString() + "/" oldFilePath = QDir::toNativeSeparators(config()->get("LastDir", QDir::homePath()).toString() + "/"
+ tr("Passwords").append(".kdbx")); + tr("Passwords").append(".kdbx"));
} }
QString newFilePath = fileDialog()->getSaveFileName(this, const QString newFilePath = fileDialog()->getSaveFileName(
tr("Save database as"), this, tr("Save database as"), oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)"), nullptr, nullptr);
oldFilePath,
tr("KeePass 2 Database").append(" (*.kdbx)"),
nullptr,
nullptr,
"kdbx");
if (!newFilePath.isEmpty()) { if (!newFilePath.isEmpty()) {
// Ensure we don't recurse back into this function // Ensure we don't recurse back into this function

View file

@ -19,31 +19,32 @@
#include "core/Config.h" #include "core/Config.h"
#include <QDir>
FileDialog* FileDialog::m_instance(nullptr); FileDialog* FileDialog::m_instance(nullptr);
QString FileDialog::getOpenFileName(QWidget* parent, QString FileDialog::getOpenFileName(QWidget* parent,
const QString& caption, const QString& caption,
QString dir, const QString& dir,
const QString& filter, const QString& filter,
QString* selectedFilter, QString* selectedFilter,
QFileDialog::Options options) const QFileDialog::Options options)
{ {
if (!m_nextFileName.isEmpty()) { if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName; const QString result = m_nextFileName;
m_nextFileName.clear(); m_nextFileName.clear();
return result; return result;
} else { } else {
if (dir.isEmpty()) { const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
dir = config()->get("LastDir").toString(); const auto result = QDir::toNativeSeparators(
} QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options));
QString result = QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options);
#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog // on Mac OS X the focus is lost after closing the native dialog
if (parent) { if (parent) {
parent->activateWindow(); parent->activateWindow();
} }
#endif
saveLastDir(result); saveLastDir(result);
return result; return result;
} }
@ -51,27 +52,28 @@ QString FileDialog::getOpenFileName(QWidget* parent,
QStringList FileDialog::getOpenFileNames(QWidget* parent, QStringList FileDialog::getOpenFileNames(QWidget* parent,
const QString& caption, const QString& caption,
QString dir, const QString& dir,
const QString& filter, const QString& filter,
QString* selectedFilter, QString* selectedFilter,
QFileDialog::Options options) const QFileDialog::Options options)
{ {
if (!m_nextFileNames.isEmpty()) { if (!m_nextFileNames.isEmpty()) {
QStringList results = m_nextFileNames; const QStringList results = m_nextFileNames;
m_nextFileNames.clear(); m_nextFileNames.clear();
return results; return results;
} else { } else {
if (dir.isEmpty()) { const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
dir = config()->get("LastDir").toString(); auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options);
}
QStringList results = QFileDialog::getOpenFileNames(parent, caption, dir, filter, selectedFilter, options); for (auto& path : results)
path = QDir::toNativeSeparators(path);
#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog // on Mac OS X the focus is lost after closing the native dialog
if (parent) { if (parent) {
parent->activateWindow(); parent->activateWindow();
} }
#endif
if (!results.isEmpty()) { if (!results.isEmpty()) {
saveLastDir(results[0]); saveLastDir(results[0]);
} }
@ -81,57 +83,26 @@ QStringList FileDialog::getOpenFileNames(QWidget* parent,
QString FileDialog::getFileName(QWidget* parent, QString FileDialog::getFileName(QWidget* parent,
const QString& caption, const QString& caption,
QString dir, const QString& dir,
const QString& filter, const QString& filter,
QString* selectedFilter, QString* selectedFilter,
QFileDialog::Options options, const QFileDialog::Options options)
const QString& defaultExtension,
const QString& defaultName)
{ {
if (!m_nextFileName.isEmpty()) { if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName; const QString result = m_nextFileName;
m_nextFileName.clear(); m_nextFileName.clear();
return result; return result;
} else { } else {
if (dir.isEmpty()) { const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
dir = config()->get("LastDir").toString(); const auto result = QDir::toNativeSeparators(
} QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));
QString result;
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
Q_UNUSED(defaultName);
Q_UNUSED(defaultExtension);
// the native dialogs on these platforms already append the file extension
result = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
#else
QFileDialog dialog(parent, caption, dir, filter);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (selectedFilter) {
dialog.selectNameFilter(*selectedFilter);
}
if (!defaultName.isEmpty()) {
dialog.selectFile(defaultName);
}
dialog.setOptions(options);
if (!defaultExtension.isEmpty()) {
dialog.setDefaultSuffix(defaultExtension);
}
dialog.setLabelText(QFileDialog::Accept, QFileDialog::tr("Select"));
QStringList results;
if (dialog.exec()) {
results = dialog.selectedFiles();
if (!results.isEmpty()) {
result = results[0];
}
}
#endif
#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog // on Mac OS X the focus is lost after closing the native dialog
if (parent) { if (parent) {
parent->activateWindow(); parent->activateWindow();
} }
#endif
saveLastDir(result); saveLastDir(result);
return result; return result;
} }
@ -139,81 +110,53 @@ QString FileDialog::getFileName(QWidget* parent,
QString FileDialog::getSaveFileName(QWidget* parent, QString FileDialog::getSaveFileName(QWidget* parent,
const QString& caption, const QString& caption,
QString dir, const QString& dir,
const QString& filter, const QString& filter,
QString* selectedFilter, QString* selectedFilter,
QFileDialog::Options options, const QFileDialog::Options options)
const QString& defaultExtension,
const QString& defaultName)
{ {
if (!m_nextFileName.isEmpty()) { if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName; const QString result = m_nextFileName;
m_nextFileName.clear(); m_nextFileName.clear();
return result; return result;
} else { } else {
if (dir.isEmpty()) { const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
dir = config()->get("LastDir").toString(); const auto result = QDir::toNativeSeparators(
} QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));
QString result;
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
Q_UNUSED(defaultName);
Q_UNUSED(defaultExtension);
// the native dialogs on these platforms already append the file extension
result = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
#else
QFileDialog dialog(parent, caption, dir, filter);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
if (selectedFilter) {
dialog.selectNameFilter(*selectedFilter);
}
if (!defaultName.isEmpty()) {
dialog.selectFile(defaultName);
}
dialog.setOptions(options);
dialog.setDefaultSuffix(defaultExtension);
QStringList results;
if (dialog.exec()) {
results = dialog.selectedFiles();
if (!results.isEmpty()) {
result = results[0];
}
}
#endif
#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog // on Mac OS X the focus is lost after closing the native dialog
if (parent) { if (parent) {
parent->activateWindow(); parent->activateWindow();
} }
#endif
saveLastDir(result); saveLastDir(result);
return result; return result;
} }
} }
QString QString FileDialog::getExistingDirectory(QWidget* parent,
FileDialog::getExistingDirectory(QWidget* parent, const QString& caption, QString dir, QFileDialog::Options options) const QString& caption,
const QString& dir,
const QFileDialog::Options options)
{ {
if (!m_nextDirName.isEmpty()) { if (!m_nextDirName.isEmpty()) {
QString result = m_nextDirName; const QString result = m_nextDirName;
m_nextDirName.clear(); m_nextDirName.clear();
return result; return result;
} else { } else {
if (dir.isEmpty()) { const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
dir = config()->get("LastDir").toString(); const auto result =
} QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options));
dir = QFileDialog::getExistingDirectory(parent, caption, dir, options);
#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog // on Mac OS X the focus is lost after closing the native dialog
if (parent) { if (parent) {
parent->activateWindow(); parent->activateWindow();
} }
#endif
saveLastDir(dir); saveLastDir(result);
return dir; return result;
} }
} }

View file

@ -25,40 +25,36 @@ class FileDialog
public: public:
QString getOpenFileName(QWidget* parent = nullptr, QString getOpenFileName(QWidget* parent = nullptr,
const QString& caption = QString(), const QString& caption = QString(),
QString dir = QString(), const QString& dir = QString(),
const QString& filter = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QString* selectedFilter = nullptr,
QFileDialog::Options options = 0); const QFileDialog::Options options = {});
QStringList getOpenFileNames(QWidget* parent = nullptr, QStringList getOpenFileNames(QWidget* parent = nullptr,
const QString& caption = QString(), const QString& caption = QString(),
QString dir = QString(), const QString& dir = QString(),
const QString& filter = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QString* selectedFilter = nullptr,
QFileDialog::Options options = 0); const QFileDialog::Options options = {});
QString getFileName(QWidget* parent = nullptr, QString getFileName(QWidget* parent = nullptr,
const QString& caption = QString(), const QString& caption = QString(),
QString dir = QString(), const QString& dir = QString(),
const QString& filter = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QString* selectedFilter = nullptr,
QFileDialog::Options options = 0, const QFileDialog::Options options = {});
const QString& defaultExtension = QString(),
const QString& defaultName = QString());
QString getSaveFileName(QWidget* parent = nullptr, QString getSaveFileName(QWidget* parent = nullptr,
const QString& caption = QString(), const QString& caption = QString(),
QString dir = QString(), const QString& dir = QString(),
const QString& filter = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QString* selectedFilter = nullptr,
QFileDialog::Options options = 0, const QFileDialog::Options options = {});
const QString& defaultExtension = QString(),
const QString& defaultName = QString());
QString getExistingDirectory(QWidget* parent = nullptr, QString getExistingDirectory(QWidget* parent = nullptr,
const QString& caption = QString(), const QString& caption = QString(),
QString dir = QString(), const QString& dir = QString(),
QFileDialog::Options options = QFileDialog::ShowDirsOnly); const QFileDialog::Options options = QFileDialog::ShowDirsOnly);
void setNextForgetDialog(); void setNextForgetDialog();
/** /**

View file

@ -187,7 +187,7 @@ void SettingsWidgetKeeShare::exportCertificate()
const auto filters = QString("%1 (*." + filetype + ");;%2 (*)").arg(tr("KeeShare key file"), tr("All files")); const auto filters = QString("%1 (*." + filetype + ");;%2 (*)").arg(tr("KeeShare key file"), tr("All files"));
QString filename = QString("%1.%2").arg(m_own.certificate.signer).arg(filetype); QString filename = QString("%1.%2").arg(m_own.certificate.signer).arg(filetype);
filename = fileDialog()->getSaveFileName( filename = fileDialog()->getSaveFileName(
this, tr("Select path"), defaultDirPath, filters, nullptr, QFileDialog::Options(0), filetype, filename); this, tr("Select path"), defaultDirPath, filters, nullptr, QFileDialog::Options(0));
if (filename.isEmpty()) { if (filename.isEmpty()) {
return; return;
} }

View file

@ -284,35 +284,17 @@ void EditGroupWidgetKeeShare::launchPathSelectionDialog()
} }
switch (reference.type) { switch (reference.type) {
case KeeShareSettings::ImportFrom: case KeeShareSettings::ImportFrom:
filename = fileDialog()->getFileName(this, filename = fileDialog()->getFileName(
tr("Select import source"), this, tr("Select import source"), defaultDirPath, filters, nullptr, QFileDialog::DontConfirmOverwrite);
defaultDirPath,
filters,
nullptr,
QFileDialog::DontConfirmOverwrite,
defaultFiletype,
filename);
break; break;
case KeeShareSettings::ExportTo: case KeeShareSettings::ExportTo:
filename = fileDialog()->getFileName(this, filename = fileDialog()->getFileName(
tr("Select export target"), this, tr("Select export target"), defaultDirPath, filters, nullptr, QFileDialog::Option(0));
defaultDirPath,
filters,
nullptr,
QFileDialog::Option(0),
defaultFiletype,
filename);
break; break;
case KeeShareSettings::SynchronizeWith: case KeeShareSettings::SynchronizeWith:
case KeeShareSettings::Inactive: case KeeShareSettings::Inactive:
filename = fileDialog()->getFileName(this, filename = fileDialog()->getFileName(
tr("Select import/export file"), this, tr("Select import/export file"), defaultDirPath, filters, nullptr, QFileDialog::Option(0));
defaultDirPath,
filters,
nullptr,
QFileDialog::Option(0),
defaultFiletype,
filename);
break; break;
} }