| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/dialogs/filedialog.h" |
| 4 | |
| 5 | #include "miscellaneous/settings.h" |
| 6 | |
| 7 | #include <QFileInfo> |
| 8 | |
| 9 | QString FileDialog::existingDirectory(QWidget* parent, |
| 10 | const QString& caption, |
| 11 | const QString& dir, |
| 12 | const QString& id, |
| 13 | QFileDialog::Options options) { |
| 14 | QString initial_dir_file = id.isEmpty() ? dir : storedFolder(id, dir); |
| 15 | QFileInfo initial_dir_file_info(initial_dir_file); |
| 16 | QString fldr = QFileDialog::getExistingDirectory(parent, |
| 17 | caption.isEmpty() ? QObject::tr("Select existing folder") : caption, |
| 18 | initial_dir_file_info.absolutePath(), |
| 19 | options); |
| 20 | |
| 21 | if (!fldr.isEmpty() && !id.isEmpty()) { |
| 22 | qApp->settings()->setValue(GROUP(FileDialogPaths), id, fldr); |
| 23 | } |
| 24 | |
| 25 | return fldr; |
| 26 | } |
| 27 | |
| 28 | QString FileDialog::storedFolder(const QString& id, const QString& dir) { |
| 29 | return qApp->settings()->value(GROUP(FileDialogPaths), id, dir).toString(); |
| 30 | } |
| 31 | |
| 32 | QString FileDialog::saveFileName(QWidget* parent, |
| 33 | const QString& caption, |
| 34 | const QString& dir, |
| 35 | const QString& filter, |
| 36 | QString* selected_filter, |
| 37 | const QString& id, |
| 38 | QFileDialog::Options options) { |
| 39 | QString initial_dir_file = id.isEmpty() ? dir : storedFolder(id, dir); |
| 40 | QString file = QFileDialog::getSaveFileName(parent, |
| 41 | caption.isEmpty() ? QObject::tr("Save file") : caption, |
| 42 | initial_dir_file, |
| 43 | filter, |
| 44 | selected_filter, |
| 45 | options); |
| 46 | |
| 47 | if (!file.isEmpty() && !id.isEmpty()) { |
| 48 | qApp->settings()->setValue(GROUP(FileDialogPaths), id, QFileInfo(file).absolutePath()); |
| 49 | } |
| 50 | |
| 51 | return file; |
| 52 | } |
| 53 | |
| 54 | QString FileDialog::openFileName(QWidget* parent, |
| 55 | const QString& caption, |
| 56 | const QString& dir, |
| 57 | const QString& filter, |
| 58 | QString* selected_filter, |
| 59 | const QString& id, |
| 60 | QFileDialog::Options options) { |
| 61 | QString initial_dir_file = id.isEmpty() ? dir : storedFolder(id, dir); |
| 62 | QString file = QFileDialog::getOpenFileName(parent, |
| 63 | caption.isEmpty() ? QObject::tr("Select existing file") : caption, |
| 64 | initial_dir_file, |
| 65 | filter, |
| 66 | selected_filter, |
| 67 | options); |
| 68 | |
| 69 | if (!file.isEmpty() && !id.isEmpty()) { |
| 70 | qApp->settings()->setValue(GROUP(FileDialogPaths), id, QFileInfo(file).absolutePath()); |
| 71 | } |
| 72 | |
| 73 | return file; |
| 74 | } |