From 7b6180a2d6b5def11b95485a3323a37f92b57ba9 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 13 Aug 2016 21:31:19 +0200 Subject: FolderDialog: replace folderSelected() with a declarative property Follow the same convention that ColorDialog and FontDialog. Change-Id: I49834daf908aadf145949c0b749c6c066f63fd83 Reviewed-by: J-P Nurmi --- src/imports/platform/plugins.qmltypes | 9 +-- .../platform/qquickplatformfolderdialog.cpp | 84 +++++++++++++--------- .../platform/qquickplatformfolderdialog_p.h | 13 ++-- 3 files changed, 60 insertions(+), 46 deletions(-) (limited to 'src/imports/platform') diff --git a/src/imports/platform/plugins.qmltypes b/src/imports/platform/plugins.qmltypes index d751e45f..d7e85c9e 100644 --- a/src/imports/platform/plugins.qmltypes +++ b/src/imports/platform/plugins.qmltypes @@ -132,18 +132,11 @@ Module { prototype: "QQuickPlatformDialog" exports: ["Qt.labs.platform/FolderDialog 1.0"] exportMetaObjectRevisions: [0] + Property { name: "folder"; type: "QUrl" } Property { name: "currentFolder"; type: "QUrl" } Property { name: "options"; type: "QFileDialogOptions::FileDialogOptions" } Property { name: "acceptLabel"; type: "string" } Property { name: "rejectLabel"; type: "string" } - Signal { - name: "folderSelected" - Parameter { name: "folder"; type: "QUrl" } - } - Signal { - name: "foldersSelected" - Parameter { name: "folders"; type: "QList" } - } } Component { name: "QQuickPlatformFontDialog" diff --git a/src/imports/platform/qquickplatformfolderdialog.cpp b/src/imports/platform/qquickplatformfolderdialog.cpp index 04c39f6f..7aa0a7e9 100644 --- a/src/imports/platform/qquickplatformfolderdialog.cpp +++ b/src/imports/platform/qquickplatformfolderdialog.cpp @@ -59,9 +59,10 @@ QT_BEGIN_NAMESPACE \image qtlabsplatform-folderdialog-gtk.png To show a folder dialog, construct an instance of FolderDialog, set the - desired properties, and call \l {Dialog::}{open()}. FolderDialog emits - the \l folderSelected() and \l foldersSelected() signals when the user - has selected folder(s). + desired properties, and call \l {Dialog::}{open()}. The \l currentFolder + property can be used to determine the currently selected folder in the + dialog. The \l folder property is updated only after the final selection + has been made by accepting the dialog. \code MenuItem { @@ -72,7 +73,11 @@ QT_BEGIN_NAMESPACE FolderDialog { id: folderDialog currentFolder: viewer.folder - onFolderSelected: viewer.folder = folder + } + + MyViewer { + id: viewer + folder: folderDialog.folder } \endcode @@ -95,29 +100,41 @@ QT_BEGIN_NAMESPACE \sa FileDialog */ -/*! - \qmlsignal void Qt.labs.platform::FolderDialog::folderSelected(url folder) - - This signal is emitted when a \a folder has been selected. +Q_DECLARE_LOGGING_CATEGORY(qtLabsPlatformDialogs) - \sa foldersSelected(), currentFolder -*/ +QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent) + : QQuickPlatformDialog(parent), m_options(QFileDialogOptions::create()) +{ + m_options->setFileMode(QFileDialogOptions::Directory); + m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); +} /*! - \qmlsignal void Qt.labs.platform::FolderDialog::foldersSelected(list folders) + \qmlproperty url Qt.labs.platform::FolderDialog::folder - This signal is emitted when multiple \a folders have been selected. + This property holds the final accepted folder. - \sa folderSelected(), currentFolder -*/ + Unlike the \l currentFolder property, the \c folder property is not updated + while the user is selecting folders in the dialog, but only after the final + selection has been made. That is, when the user has clicked \uicontrol OK + to accept a folder. Alternatively, the \l {Dialog::}{accepted()} signal + can be handled to get the final selection. -Q_DECLARE_LOGGING_CATEGORY(qtLabsPlatformDialogs) + \sa currentFolder, {Dialog::}{accepted()} +*/ +QUrl QQuickPlatformFolderDialog::folder() const +{ + return m_folder; +} -QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent) - : QQuickPlatformDialog(parent), m_options(QFileDialogOptions::create()) +void QQuickPlatformFolderDialog::setFolder(const QUrl &folder) { - m_options->setFileMode(QFileDialogOptions::Directory); - m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); + if (m_folder == folder) + return; + + m_folder = folder; + setCurrentFolder(folder); + emit folderChanged(); } /*! @@ -125,15 +142,17 @@ QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent) This property holds the currently selected folder in the dialog. - \sa folderSelected(), foldersSelected() + Unlike the \l folder property, the \c currentFolder property is updated + while the user is selecting folders in the dialog, even before the final + selection has been made. + + \sa folder */ QUrl QQuickPlatformFolderDialog::currentFolder() const { - if (m_current.isEmpty()) { - if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) - m_current = fileDialog->directory(); - } - return m_current; + if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) + return fileDialog->directory(); + return QUrl(); } void QQuickPlatformFolderDialog::setCurrentFolder(const QUrl &folder) @@ -249,14 +268,7 @@ QPlatformDialogHelper *QQuickPlatformFolderDialog::createHelper() qCDebug(qtLabsPlatformDialogs) << "FolderDialog:" << dialog; if (QPlatformFileDialogHelper *fileDialog = qobject_cast(dialog)) { - connect(fileDialog, &QPlatformFileDialogHelper::fileSelected, this, &QQuickPlatformFolderDialog::folderSelected); - connect(fileDialog, &QPlatformFileDialogHelper::filesSelected, this, &QQuickPlatformFolderDialog::foldersSelected); - connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, [this](const QUrl &url) { - if (m_current == url) - return; - m_current = url; - emit currentFolderChanged(); - }); + connect(fileDialog, &QPlatformFileDialogHelper::directoryEntered, this, &QQuickPlatformFolderDialog::currentFolderChanged); fileDialog->setOptions(m_options); } return dialog; @@ -267,4 +279,10 @@ void QQuickPlatformFolderDialog::applyOptions() m_options->setWindowTitle(title()); } +void QQuickPlatformFolderDialog::accept() +{ + setFolder(currentFolder()); + QQuickPlatformDialog::accept(); +} + QT_END_NAMESPACE diff --git a/src/imports/platform/qquickplatformfolderdialog_p.h b/src/imports/platform/qquickplatformfolderdialog_p.h index 39dc025f..2c0fdf51 100644 --- a/src/imports/platform/qquickplatformfolderdialog_p.h +++ b/src/imports/platform/qquickplatformfolderdialog_p.h @@ -57,6 +57,7 @@ QT_BEGIN_NAMESPACE class QQuickPlatformFolderDialog : public QQuickPlatformDialog { Q_OBJECT + Q_PROPERTY(QUrl folder READ folder WRITE setFolder NOTIFY folderChanged FINAL) Q_PROPERTY(QUrl currentFolder READ currentFolder WRITE setCurrentFolder NOTIFY currentFolderChanged FINAL) Q_PROPERTY(QFileDialogOptions::FileDialogOptions options READ options WRITE setOptions RESET resetOptions NOTIFY optionsChanged FINAL) Q_PROPERTY(QString acceptLabel READ acceptLabel WRITE setAcceptLabel RESET resetAcceptLabel NOTIFY acceptLabelChanged FINAL) @@ -66,8 +67,11 @@ class QQuickPlatformFolderDialog : public QQuickPlatformDialog public: explicit QQuickPlatformFolderDialog(QObject *parent = nullptr); + QUrl folder() const; + void setFolder(const QUrl &folder); + QUrl currentFolder() const; - void setCurrentFolder(const QUrl &url); + void setCurrentFolder(const QUrl &folder); QFileDialogOptions::FileDialogOptions options() const; void setOptions(QFileDialogOptions::FileDialogOptions options); @@ -82,20 +86,19 @@ public: void resetRejectLabel(); Q_SIGNALS: + void folderChanged(); void currentFolderChanged(); void optionsChanged(); void acceptLabelChanged(); void rejectLabelChanged(); - void folderSelected(const QUrl &folder); - void foldersSelected(const QList &folders); - protected: QPlatformDialogHelper *createHelper() override; void applyOptions() override; + void accept() override; private: - mutable QUrl m_current; + QUrl m_folder; QSharedPointer m_options; }; -- cgit v1.2.3