From 51192e8ef5d9ab385de390b38409b73a56f55f84 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 13 Aug 2016 22:25:39 +0200 Subject: FileDialog: replace file(s)Selected() with declarative properties Follow the same convention that ColorDialog, FontDialog and FolderDialog. Change-Id: I960d4fc1ba275ab997f2a079a799d2b90796eca3 Reviewed-by: J-P Nurmi --- src/imports/platform/plugins.qmltypes | 11 +- src/imports/platform/qquickplatformfiledialog.cpp | 145 ++++++++++++++++------ src/imports/platform/qquickplatformfiledialog_p.h | 21 +++- 3 files changed, 127 insertions(+), 50 deletions(-) (limited to 'src/imports/platform') diff --git a/src/imports/platform/plugins.qmltypes b/src/imports/platform/plugins.qmltypes index d7e85c9e..dc9e1562 100644 --- a/src/imports/platform/plugins.qmltypes +++ b/src/imports/platform/plugins.qmltypes @@ -111,20 +111,15 @@ Module { } } Property { name: "fileMode"; type: "FileMode" } + Property { name: "file"; type: "QUrl" } + Property { name: "files"; type: "QList" } Property { name: "currentFile"; type: "QUrl" } + Property { name: "currentFiles"; type: "QList" } Property { name: "options"; type: "QFileDialogOptions::FileDialogOptions" } Property { name: "nameFilters"; type: "QStringList" } Property { name: "defaultSuffix"; type: "string" } Property { name: "acceptLabel"; type: "string" } Property { name: "rejectLabel"; type: "string" } - Signal { - name: "fileSelected" - Parameter { name: "file"; type: "QUrl" } - } - Signal { - name: "filesSelected" - Parameter { name: "files"; type: "QList" } - } } Component { name: "QQuickPlatformFolderDialog" diff --git a/src/imports/platform/qquickplatformfiledialog.cpp b/src/imports/platform/qquickplatformfiledialog.cpp index 1900952c..0a617a8c 100644 --- a/src/imports/platform/qquickplatformfiledialog.cpp +++ b/src/imports/platform/qquickplatformfiledialog.cpp @@ -59,9 +59,11 @@ QT_BEGIN_NAMESPACE \image qtlabsplatform-filedialog-gtk.png To show a file dialog, construct an instance of FileDialog, set the - desired properties, and call \l {Dialog::}{open()}. FileDialog emits - the \l fileSelected() and \l filesSelected() signals when the user has - selected file(s). + desired properties, and call \l {Dialog::}{open()}. The \l currentFile + or \l currentFiles properties can be used to determine the currently + selected file(s) in the dialog. The \l file and \l files properties + are updated only after the final selection has been made by accepting + the dialog. \code MenuItem { @@ -72,7 +74,11 @@ QT_BEGIN_NAMESPACE FileDialog { id: fileDialog currentFile: document.source - onFileSelected: document.source = file + } + + MyDocument { + id: document + source: fileDialog.file } \endcode @@ -95,22 +101,6 @@ QT_BEGIN_NAMESPACE \sa FolderDialog */ -/*! - \qmlsignal void Qt.labs.platform::FileDialog::fileSelected(url file) - - This signal is emitted when a \a file has been selected. - - \sa filesSelected(), currentFile -*/ - -/*! - \qmlsignal void Qt.labs.platform::FileDialog::filesSelected(list files) - - This signal is emitted when multiple \a files have been selected. - - \sa fileSelected(), currentFile -*/ - Q_DECLARE_LOGGING_CATEGORY(qtLabsPlatformDialogs) QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent) @@ -161,20 +151,75 @@ void QQuickPlatformFileDialog::setFileMode(FileMode mode) emit fileModeChanged(); } +/*! + \qmlproperty url Qt.labs.platform::FileDialog::file + + This property holds the final accepted file. + + Unlike the \l currentFile property, the \c file property is not updated + while the user is selecting files in the dialog, but only after the final + selection has been made. That is, when the user has clicked \uicontrol OK + to accept a file. Alternatively, the \l {Dialog::}{accepted()} signal + can be handled to get the final selection. + + \sa currentFile, {Dialog::}{accepted()} +*/ +QUrl QQuickPlatformFileDialog::file() const +{ + return addDefaultSuffix(m_files.value(0)); +} + +void QQuickPlatformFileDialog::setFile(const QUrl &file) +{ + setFiles(QList() << file); +} + +/*! + \qmlproperty list Qt.labs.platform::FileDialog::files + + This property holds the final accepted files. + + Unlike the \l currentFiles property, the \c files property is not updated + while the user is selecting files in the dialog, but only after the final + selection has been made. That is, when the user has clicked \uicontrol OK + to accept files. Alternatively, the \l {Dialog::}{accepted()} signal + can be handled to get the final selection. + + \sa currentFiles, {Dialog::}{accepted()} +*/ +QList QQuickPlatformFileDialog::files() const +{ + return addDefaultSuffixes(m_files); +} + +void QQuickPlatformFileDialog::setFiles(const QList &files) +{ + if (m_files == files) + return; + + bool firstChanged = m_files.value(0) != files.value(0); + m_files = files; + if (firstChanged) + emit fileChanged(); + emit filesChanged(); +} + /*! \qmlproperty url Qt.labs.platform::FileDialog::currentFile This property holds the currently selected file in the dialog. - \sa fileSelected(), filesSelected() + Unlike the \l file property, the \c currentFile property is updated + while the user is selecting files in the dialog, even before the final + selection has been made. + + \sa file, currentFiles */ QUrl QQuickPlatformFileDialog::currentFile() const { - if (m_current.isEmpty()) { - if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) - m_current = fileDialog->selectedFiles().value(0); - } - return addDefaultSuffix(m_current); + if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) + return fileDialog->selectedFiles().value(0); + return QUrl(); } void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file) @@ -183,6 +228,32 @@ void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file) fileDialog->selectFile(file); } +/*! + \qmlproperty list Qt.labs.platform::FileDialog::currentFiles + + This property holds the currently selected files in the dialog. + + Unlike the \l files property, the \c currentFiles property is updated + while the user is selecting files in the dialog, even before the final + selection has been made. + + \sa files, currentFile +*/ +QList QQuickPlatformFileDialog::currentFiles() const +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) + return fileDialog->selectedFiles(); + return QList(); +} + +void QQuickPlatformFileDialog::setCurrentFiles(const QList &files) +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) { + for (const QUrl &file : files) + fileDialog->selectFile(file); + } +} + /*! \qmlproperty flags Qt.labs.platform::FileDialog::options @@ -352,18 +423,9 @@ QPlatformDialogHelper *QQuickPlatformFileDialog::createHelper() qCDebug(qtLabsPlatformDialogs) << "FileDialog:" << dialog; if (QPlatformFileDialogHelper *fileDialog = qobject_cast(dialog)) { - connect(fileDialog, &QPlatformFileDialogHelper::fileSelected, [this](const QUrl &file) { - emit fileSelected(addDefaultSuffix(file)); - }); - connect(fileDialog, &QPlatformFileDialogHelper::filesSelected, [this](const QList &files) { - emit filesSelected(addDefaultSuffixes(files)); - }); - connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, [this](const QUrl &url) { - if (m_current == url) - return; - m_current = url; - emit currentFileChanged(); - }); + // TODO: emit currentFileChanged only when the first entry in currentFiles changes + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickPlatformFileDialog::currentFileChanged); + connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickPlatformFileDialog::currentFilesChanged); fileDialog->setOptions(m_options); } return dialog; @@ -374,6 +436,13 @@ void QQuickPlatformFileDialog::applyOptions() m_options->setWindowTitle(title()); } +void QQuickPlatformFileDialog::accept() +{ + if (QPlatformFileDialogHelper *fileDialog = qobject_cast(handle())) + setFiles(fileDialog->selectedFiles()); + QQuickPlatformDialog::accept(); +} + QUrl QQuickPlatformFileDialog::addDefaultSuffix(const QUrl &file) const { QUrl url = file; diff --git a/src/imports/platform/qquickplatformfiledialog_p.h b/src/imports/platform/qquickplatformfiledialog_p.h index c3c6a490..76acd105 100644 --- a/src/imports/platform/qquickplatformfiledialog_p.h +++ b/src/imports/platform/qquickplatformfiledialog_p.h @@ -58,7 +58,10 @@ class QQuickPlatformFileDialog : public QQuickPlatformDialog { Q_OBJECT Q_PROPERTY(FileMode fileMode READ fileMode WRITE setFileMode NOTIFY fileModeChanged FINAL) + Q_PROPERTY(QUrl file READ file WRITE setFile NOTIFY fileChanged FINAL) + Q_PROPERTY(QList files READ files WRITE setFiles NOTIFY filesChanged FINAL) Q_PROPERTY(QUrl currentFile READ currentFile WRITE setCurrentFile NOTIFY currentFileChanged FINAL) + Q_PROPERTY(QList currentFiles READ currentFiles WRITE setCurrentFiles NOTIFY currentFilesChanged FINAL) Q_PROPERTY(QFileDialogOptions::FileDialogOptions options READ options WRITE setOptions RESET resetOptions NOTIFY optionsChanged FINAL) Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters RESET resetNameFilters NOTIFY nameFiltersChanged FINAL) Q_PROPERTY(QString defaultSuffix READ defaultSuffix WRITE setDefaultSuffix RESET resetDefaultSuffix NOTIFY defaultSuffixChanged FINAL) @@ -79,9 +82,18 @@ public: FileMode fileMode() const; void setFileMode(FileMode fileMode); + QUrl file() const; + void setFile(const QUrl &file); + + QList files() const; + void setFiles(const QList &files); + QUrl currentFile() const; void setCurrentFile(const QUrl &file); + QList currentFiles() const; + void setCurrentFiles(const QList &files); + QFileDialogOptions::FileDialogOptions options() const; void setOptions(QFileDialogOptions::FileDialogOptions options); void resetOptions(); @@ -104,26 +116,27 @@ public: Q_SIGNALS: void fileModeChanged(); + void fileChanged(); + void filesChanged(); void currentFileChanged(); + void currentFilesChanged(); void optionsChanged(); void nameFiltersChanged(); void defaultSuffixChanged(); void acceptLabelChanged(); void rejectLabelChanged(); - void fileSelected(const QUrl &file); - void filesSelected(const QList &files); - protected: QPlatformDialogHelper *createHelper() override; void applyOptions() override; + void accept() override; private: QUrl addDefaultSuffix(const QUrl &file) const; QList addDefaultSuffixes(const QList &files) const; FileMode m_fileMode; - mutable QUrl m_current; + QList m_files; QSharedPointer m_options; }; -- cgit v1.2.3