-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #621 from GriffinRichards/file-dialogs
Correctly restore window focus for file dialogs
- Loading branch information
Showing
16 changed files
with
161 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#ifndef FILEDIALOG_H | ||
#define FILEDIALOG_H | ||
|
||
#include <QFileDialog> | ||
|
||
/* | ||
Static QFileDialog functions will (unless otherwise specified) use native file dialogs. | ||
In general this is good (we want our file dialogs to be visually seamless) but unfortunately | ||
the native file dialogs ignore the parent widget, so in some cases they'll return focus to | ||
the main window rather than the window that opened the file dialog. | ||
To make working around this a little easier we use this class, which will use the native | ||
file dialog and manually return focus to the parent widget. | ||
It will also save the directory of the previous file selected in a file dialog, and if | ||
no 'dir' argument is specified it will open new dialogs at that directory. | ||
*/ | ||
|
||
class FileDialog : public QFileDialog | ||
{ | ||
public: | ||
FileDialog(QWidget *parent, Qt::WindowFlags flags) : QFileDialog(parent, flags) {}; | ||
FileDialog(QWidget *parent = nullptr, | ||
const QString &caption = QString(), | ||
const QString &directory = QString(), | ||
const QString &filter = QString()) : QFileDialog(parent, caption, directory, filter) {}; | ||
|
||
static void setDirectory(const QString &dir) { FileDialog::prevDirectory = dir; } | ||
static QString getDirectory() { return FileDialog::prevDirectory; } | ||
|
||
static QString getOpenFileName(QWidget *parent = nullptr, | ||
const QString &caption = QString(), | ||
const QString &dir = QString(), | ||
const QString &filter = QString(), | ||
QString *selectedFilter = nullptr, | ||
QFileDialog::Options options = Options()); | ||
|
||
static QStringList getOpenFileNames(QWidget *parent = nullptr, | ||
const QString &caption = QString(), | ||
const QString &dir = QString(), | ||
const QString &filter = QString(), | ||
QString *selectedFilter = nullptr, | ||
QFileDialog::Options options = Options()); | ||
|
||
static QString getExistingDirectory(QWidget *parent = nullptr, | ||
const QString &caption = QString(), | ||
const QString &dir = QString(), | ||
QFileDialog::Options options = ShowDirsOnly); | ||
|
||
static QString getSaveFileName(QWidget *parent = nullptr, | ||
const QString &caption = QString(), | ||
const QString &dir = QString(), | ||
const QString &filter = QString(), | ||
QString *selectedFilter = nullptr, | ||
QFileDialog::Options options = Options()); | ||
|
||
private: | ||
static QString prevDirectory; | ||
static QString getDirectoryFromInput(const QString &dir); | ||
static void setDirectoryFromFile(const QString &fileName); | ||
static void restoreFocus(QWidget *parent); | ||
}; | ||
|
||
#endif // FILEDIALOG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "filedialog.h" | ||
|
||
QString FileDialog::prevDirectory; | ||
|
||
QString FileDialog::getDirectoryFromInput(const QString &dir) { | ||
if (dir.isEmpty()) | ||
return FileDialog::prevDirectory; | ||
return dir; | ||
} | ||
|
||
void FileDialog::setDirectoryFromFile(const QString &fileName) { | ||
if (!fileName.isEmpty()) | ||
FileDialog::prevDirectory = QFileInfo(fileName).absolutePath(); | ||
} | ||
|
||
void FileDialog::restoreFocus(QWidget *parent) { | ||
if (parent) { | ||
parent->raise(); | ||
parent->activateWindow(); | ||
} | ||
} | ||
|
||
QString FileDialog::getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) { | ||
const QString fileName = QFileDialog::getOpenFileName(parent, caption, getDirectoryFromInput(dir), filter, selectedFilter, options); | ||
setDirectoryFromFile(fileName); | ||
restoreFocus(parent); | ||
return fileName; | ||
} | ||
|
||
QStringList FileDialog::getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) { | ||
const QStringList fileNames = QFileDialog::getOpenFileNames(parent, caption, getDirectoryFromInput(dir), filter, selectedFilter, options); | ||
if (!fileNames.isEmpty()) | ||
setDirectoryFromFile(fileNames.last()); | ||
restoreFocus(parent); | ||
return fileNames; | ||
} | ||
|
||
QString FileDialog::getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) { | ||
const QString fileName = QFileDialog::getSaveFileName(parent, caption, getDirectoryFromInput(dir), filter, selectedFilter, options); | ||
setDirectoryFromFile(fileName); | ||
restoreFocus(parent); | ||
return fileName; | ||
} | ||
|
||
QString FileDialog::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options) { | ||
const QString existingDir = QFileDialog::getExistingDirectory(parent, caption, getDirectoryFromInput(dir), options); | ||
if (!existingDir.isEmpty()) | ||
setDirectory(existingDir); | ||
restoreFocus(parent); | ||
return existingDir; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.