From 800c2b9ec0fcdb1f038b8713b4db59abcb4e84b2 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Sat, 30 Jul 2016 11:21:40 +1200 Subject: [PATCH] Refresh file dialog when directory content changes --- interface/resources/qml/dialogs/FileDialog.qml | 11 +++++++++++ libraries/ui/src/FileDialogHelper.cpp | 12 ++++++++++++ libraries/ui/src/FileDialogHelper.h | 12 +++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/interface/resources/qml/dialogs/FileDialog.qml b/interface/resources/qml/dialogs/FileDialog.qml index fa5be18cd3..6a37886cb3 100644 --- a/interface/resources/qml/dialogs/FileDialog.qml +++ b/interface/resources/qml/dialogs/FileDialog.qml @@ -87,6 +87,15 @@ ModalWindow { currentSelection.text = d.capitalizeDrive(helper.urlToPath(initialFolder)); } + helper.contentsChanged.connect(function() { + if (folderListModel) { + // Make folderListModel refresh. + var save = folderListModel.folder; + folderListModel.folder = ""; + folderListModel.folder = save; + } + }); + fileTableView.forceActiveFocus(); } @@ -343,12 +352,14 @@ ModalWindow { onFolderChanged: { if (folder === rootFolder) { model = driveListModel; + helper.monitorDirectory(""); update(); } else { var needsUpdate = model === driveListModel && folder === folderListModel.folder; model = folderListModel; folderListModel.folder = folder; + helper.monitorDirectory(helper.urlToPath(folder)); if (needsUpdate) { update(); diff --git a/libraries/ui/src/FileDialogHelper.cpp b/libraries/ui/src/FileDialogHelper.cpp index 3a12e054df..9d791ec562 100644 --- a/libraries/ui/src/FileDialogHelper.cpp +++ b/libraries/ui/src/FileDialogHelper.cpp @@ -115,3 +115,15 @@ QList FileDialogHelper::urlToList(const QUrl& url) { return results; } +void FileDialogHelper::monitorDirectory(const QString& path) { + if (!_fsWatcherPath.isEmpty()) { + _fsWatcher.removePath(_fsWatcherPath); + _fsWatcherPath = ""; + } + + if (!path.isEmpty()) { + _fsWatcher.addPath(path); + _fsWatcherPath = path; + connect(&_fsWatcher, &QFileSystemWatcher::directoryChanged, this, &FileDialogHelper::contentsChanged); + } +} diff --git a/libraries/ui/src/FileDialogHelper.h b/libraries/ui/src/FileDialogHelper.h index 6058f8f7bb..6c352ecdfc 100644 --- a/libraries/ui/src/FileDialogHelper.h +++ b/libraries/ui/src/FileDialogHelper.h @@ -9,11 +9,12 @@ #ifndef hifi_ui_FileDialogHelper_h #define hifi_ui_FileDialogHelper_h +#include #include #include -#include #include #include +#include class FileDialogHelper : public QObject { @@ -61,6 +62,15 @@ public: Q_INVOKABLE QList urlToList(const QUrl& url); Q_INVOKABLE void openDirectory(const QString& path); + + Q_INVOKABLE void monitorDirectory(const QString& path); + +signals: + void contentsChanged(); + +private: + QFileSystemWatcher _fsWatcher; + QString _fsWatcherPath; };