diff --git a/src/GUI/LauncherMainWindow.cxx b/src/GUI/LauncherMainWindow.cxx index fbfc23829..24097b3d7 100644 --- a/src/GUI/LauncherMainWindow.cxx +++ b/src/GUI/LauncherMainWindow.cxx @@ -242,7 +242,7 @@ void LauncherMainWindow::initQML() qmlRegisterUncreatableType("FlightGear.Launcher", 1, 0, "Control", "Base class"); qmlRegisterUncreatableType("FlightGear.Launcher", 1, 0, "LaunchConfig", "Singleton API"); - qmlRegisterType("FlightGear.Launcher", 1, 0, "PathUrlHelper"); + qmlRegisterType("FlightGear.Launcher", 1, 0, "FileDialog"); qmlRegisterType("FlightGear.Launcher", 1, 0, "FlickableExtentQuery"); qmlRegisterType("FlightGear.Launcher", 1, 0, "AircraftInfo"); diff --git a/src/GUI/PathUrlHelper.cxx b/src/GUI/PathUrlHelper.cxx index a82270d4f..61d2dbc87 100644 --- a/src/GUI/PathUrlHelper.cxx +++ b/src/GUI/PathUrlHelper.cxx @@ -1,16 +1,118 @@ #include "PathUrlHelper.hxx" -PathUrlHelper::PathUrlHelper(QObject *parent) : QObject(parent) +#include + +FileDialogWrapper::FileDialogWrapper(QObject *parent) : QObject(parent) { } -QString PathUrlHelper::urlToLocalFilePath(QUrl url) const +QString FileDialogWrapper::urlToLocalFilePath(QUrl url) const { return url.toLocalFile(); } -QUrl PathUrlHelper::urlFromLocalFilePath(QString path) const +QUrl FileDialogWrapper::urlFromLocalFilePath(QString path) const { return QUrl::fromLocalFile(path); } + +void FileDialogWrapper::open() +{ + QUrl u; + if (m_selectFolder) { + u = QFileDialog::getExistingDirectoryUrl(nullptr, m_dialogTitle, + m_currentFolder); + + } else { + u = QFileDialog::getOpenFileUrl(nullptr, m_dialogTitle, m_currentFolder, + m_filter); + } + + if (u.isValid()) { + m_fileUrl = u; + accepted(); + } else { + rejected(); + } +} + +QUrl FileDialogWrapper::folder() const +{ + return m_currentFolder; +} + +QString FileDialogWrapper::title() const +{ + return m_dialogTitle; +} + +bool FileDialogWrapper::selectFolder() const +{ + return m_selectFolder; +} + +QUrl FileDialogWrapper::fileUrl() const +{ + return m_fileUrl; +} + +QString FileDialogWrapper::filePath() const +{ + return urlToLocalFilePath(fileUrl()); +} + +QString FileDialogWrapper::filter() const +{ + return m_filter; +} + +void FileDialogWrapper::setFolder(QUrl folder) +{ + if (m_currentFolder == folder) + return; + + m_currentFolder = folder; + emit folderChanged(m_currentFolder); +} + +void FileDialogWrapper::setTitle(QString title) +{ + if (m_dialogTitle == title) + return; + + m_dialogTitle = title; + emit titleChanged(m_dialogTitle); +} + +void FileDialogWrapper::setSelectFolder(bool selectFolder) +{ + if (m_selectFolder == selectFolder) + return; + + m_selectFolder = selectFolder; + emit selectFolderChanged(m_selectFolder); +} + +void FileDialogWrapper::setFileUrl(QUrl fileUrl) +{ + if (m_fileUrl == fileUrl) + return; + + m_fileUrl = fileUrl; + emit fileUrlChanged(m_fileUrl); +} + +void FileDialogWrapper::setFilePath(QString filePath) +{ + setFileUrl(urlFromLocalFilePath(filePath)); +} + +void FileDialogWrapper::setFilter(QString filter) +{ + if (m_filter == filter) + return; + + m_filter = filter; + emit filterChanged(m_filter); +} diff --git a/src/GUI/PathUrlHelper.hxx b/src/GUI/PathUrlHelper.hxx index 5b1f64e1e..da7c750a5 100644 --- a/src/GUI/PathUrlHelper.hxx +++ b/src/GUI/PathUrlHelper.hxx @@ -4,18 +4,72 @@ #include #include -class PathUrlHelper : public QObject +class FileDialogWrapper : public QObject { Q_OBJECT + + Q_PROPERTY(QUrl folder READ folder WRITE setFolder NOTIFY folderChanged) + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(bool selectFolder READ selectFolder WRITE setSelectFolder NOTIFY selectFolderChanged) + Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged) + Q_PROPERTY(QString filePath READ filePath WRITE setFilePath NOTIFY fileUrlChanged) + Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged) public: - explicit PathUrlHelper(QObject *parent = nullptr); + explicit FileDialogWrapper(QObject *parent = nullptr); Q_INVOKABLE QString urlToLocalFilePath(QUrl url) const; Q_INVOKABLE QUrl urlFromLocalFilePath(QString path) const; + + Q_INVOKABLE void open(); + + QUrl folder() const; + + QString title() const; + + bool selectFolder() const; + + QUrl fileUrl() const; + + QString filePath() const; + + QString filter() const; + signals: + void accepted(); + void rejected(); + + void folderChanged(QUrl folder); + + void titleChanged(QString title); + + void selectFolderChanged(bool selectFolder); + + void fileUrlChanged(QUrl fileUrl); + + void filterChanged(QString filter); + public slots: + + void setFolder(QUrl folder); + + void setTitle(QString title); + + void setSelectFolder(bool selectFolder); + + void setFileUrl(QUrl fileUrl); + + void setFilePath(QString filePath); + + void setFilter(QString filter); + +private: + QUrl m_currentFolder; + QUrl m_fileUrl; + bool m_selectFolder = false; + QString m_dialogTitle; + QString m_filter; }; #endif // PATHURLHELPER_H diff --git a/src/GUI/qml/SettingPathChooser.qml b/src/GUI/qml/SettingPathChooser.qml index 7b980b3cf..0480efd2a 100644 --- a/src/GUI/qml/SettingPathChooser.qml +++ b/src/GUI/qml/SettingPathChooser.qml @@ -1,5 +1,4 @@ import QtQuick 2.0 -import QtQuick.Dialogs 1.0 as Dialogs import FlightGear.Launcher 1.0 as FG import "." @@ -20,7 +19,7 @@ SettingControl { readonly property bool isDefault: (path.length == 0) || (path == defaultPath) - readonly property url effectiveUrl: pathHelper.urlFromLocalFilePath(effectivePath()); + readonly property url effectiveUrl: picker.urlFromLocalFilePath(effectivePath()); function effectivePath() { @@ -91,19 +90,15 @@ SettingControl { width: parent.width } - Dialogs.FileDialog { + FG.FileDialog { id: picker onAccepted: { - path = pathHelper.urlToLocalFilePath(picker.fileUrl); + path = filePath } onRejected: { } } - - FG.PathUrlHelper { - id: pathHelper - } }