| 1 | // QtFileDialog.cxx - Qt5 implementation of FGFileDialog |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | // SPDX-FileCopyrightText: Copyright (C) 2015 Rebecca Palmer <[email protected]> |
| 4 | |
| 5 | #include "QtFileDialog.hxx" |
| 6 | #include "QtLauncher.hxx" |
| 7 | #include <simgear/debug/logstream.hxx> |
| 8 | |
| 9 | // Qt |
| 10 | #include <QFileDialog> |
| 11 | #include <QDir> |
| 12 | #include <QString> |
| 13 | #include <QStringList> |
| 14 | |
| 15 | QtFileDialog::QtFileDialog(FGFileDialog::Usage use) : |
| 16 | FGFileDialog(use) |
| 17 | { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | QtFileDialog::~QtFileDialog() {} |
| 22 | |
| 23 | void QtFileDialog::exec() |
| 24 | { |
| 25 | // concatenate filter patterns, as Qt uses a single string |
| 26 | std::string filter=""; |
| 27 | for( string_list::const_iterator it = _filterPatterns.begin(); it != _filterPatterns.end();++it ) { |
| 28 | if(!filter.empty()){ |
| 29 | filter=filter+" "; |
| 30 | } |
| 31 | filter=filter+*it; |
| 32 | } |
| 33 | QFileDialog dlg(0,QString::fromStdString(_title),QString::fromStdString(_initialPath.utf8Str()),QString::fromStdString(filter)); |
| 34 | if (_usage==USE_SAVE_FILE) { |
| 35 | dlg.setAcceptMode(QFileDialog::AcceptSave); |
| 36 | } |
| 37 | if (_usage==USE_CHOOSE_DIR) { |
| 38 | dlg.setFileMode(QFileDialog::Directory); |
| 39 | } |
| 40 | if (_usage==USE_OPEN_FILE) { |
| 41 | dlg.setFileMode(QFileDialog::ExistingFile); |
| 42 | } |
| 43 | dlg.setLabelText(QFileDialog::Accept,QString::fromStdString(_buttonText)); |
| 44 | dlg.selectFile(QString::fromStdString(_placeholder)); |
| 45 | if(_showHidden){ |
| 46 | dlg.setFilter(dlg.filter() | QDir::Hidden); |
| 47 | } |
| 48 | |
| 49 | if(dlg.exec()){ |
| 50 | QStringList result = dlg.selectedFiles(); |
| 51 | if (!result.isEmpty()) { |
| 52 | const auto sgp = SGPath::fromUtf8(result[0].toStdString()); |
| 53 | handleSelectedPath(sgp); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void QtFileDialog::close(){} |
| 59 |