Skip to content

Commit

Permalink
Gui: don't show non-existing and unusual directories in file dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 11, 2022
1 parent 9311e29 commit 746a56f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
54 changes: 24 additions & 30 deletions src/Gui/FileDialog.cpp
Expand Up @@ -93,6 +93,27 @@ void FileDialog::onSelectedFilter(const QString& /*filter*/)
}
}

QList<QUrl> FileDialog::fetchSidebarUrls()
{
QStringList list;
list << QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
list << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
list << QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
list << QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
list << getWorkingDirectory();
list << restoreLocation();
list << QDir::currentPath();

QList<QUrl> urls;
for (const auto& it : list) {
if (QFileInfo::exists(it)) {
urls << QUrl::fromLocalFile(it);
}
}

return urls;
}

bool FileDialog::hasSuffix(const QString& ext) const
{
QRegExp rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext));
Expand Down Expand Up @@ -182,18 +203,9 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption,
// before showing the file dialog.
QString file;
if (DialogOptions::dontUseNativeFileDialog()) {
QList<QUrl> urls;
QList<QUrl> urls = fetchSidebarUrls();

options |= QFileDialog::DontUseNativeDialog;
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
urls << QUrl::fromLocalFile(restoreLocation());
urls << QUrl::fromLocalFile(QDir::currentPath());

FileDialog dlg(parent);
dlg.setOptions(options);
Expand Down Expand Up @@ -264,18 +276,9 @@ QString FileDialog::getOpenFileName(QWidget * parent, const QString & caption, c

QString file;
if (DialogOptions::dontUseNativeFileDialog()) {
QList<QUrl> urls;
QList<QUrl> urls = fetchSidebarUrls();

options |= QFileDialog::DontUseNativeDialog;
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
urls << QUrl::fromLocalFile(restoreLocation());
urls << QUrl::fromLocalFile(QDir::currentPath());

FileDialog dlg(parent);
dlg.setOptions(options);
Expand Down Expand Up @@ -326,18 +329,9 @@ QStringList FileDialog::getOpenFileNames (QWidget * parent, const QString & capt

QStringList files;
if (DialogOptions::dontUseNativeFileDialog()) {
QList<QUrl> urls;
QList<QUrl> urls = fetchSidebarUrls();

options |= QFileDialog::DontUseNativeDialog;
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
urls << QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
urls << QUrl::fromLocalFile(restoreLocation());
urls << QUrl::fromLocalFile(QDir::currentPath());

FileDialog dlg(parent);
dlg.setOptions(options);
Expand Down
1 change: 1 addition & 0 deletions src/Gui/FileDialog.h
Expand Up @@ -86,6 +86,7 @@ private Q_SLOTS:

private:
bool hasSuffix(const QString&) const;
static QList<QUrl> fetchSidebarUrls();
static QString workingDirectory;
};

Expand Down

0 comments on commit 746a56f

Please sign in to comment.