From 58e612c85fb8320fd99a28d573372220cbfe309a Mon Sep 17 00:00:00 2001 From: Henning Gruendl Date: Thu, 15 Oct 2020 17:36:18 +0200 Subject: [PATCH] QmlDesigner: Sort items in FileResourceModel * Sort items in FileResourceModel * Add FileSystemWatcher on directory Task-number: QDS-2919 Change-Id: I0ba50f03d4f901a48709ed0cc0e7f05d3037aeec Reviewed-by: Mahmoud Badri --- .../propertyeditor/fileresourcesmodel.cpp | 33 ++++++++++++++----- .../propertyeditor/fileresourcesmodel.h | 6 ++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp index a42db492d9e..71e2c996277 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.cpp @@ -29,15 +29,21 @@ #include +#include + #include #include #include static QString s_lastBrowserPath; -FileResourcesModel::FileResourcesModel(QObject *parent) : - QObject(parent), m_filter(QLatin1String("(*.*)")), m_lock(false) +FileResourcesModel::FileResourcesModel(QObject *parent) + : QObject(parent) + , m_filter(QLatin1String("(*.*)")) + , m_fileSystemWatcher(new Utils::FileSystemWatcher(this)) { + connect(m_fileSystemWatcher, &Utils::FileSystemWatcher::directoryChanged, + this, &FileResourcesModel::refreshModel); } void FileResourcesModel::setModelNodeBackend(const QVariant &modelNodeBackend) @@ -163,7 +169,6 @@ QVariant FileResourcesModel::modelNodeBackend() const bool filterMetaIcons(const QString &fileName) { - QFileInfo info(fileName); if (info.dir().path().split('/').contains("designer")) { @@ -189,12 +194,20 @@ bool filterMetaIcons(const QString &fileName) void FileResourcesModel::setupModel() { - m_lock = true; + m_dirPath = QFileInfo(m_path.toLocalFile()).dir(); + + refreshModel(); + + m_fileSystemWatcher->removeDirectories(m_fileSystemWatcher->directories()); + m_fileSystemWatcher->addDirectory(m_dirPath.absolutePath(), + Utils::FileSystemWatcher::WatchAllChanges); +} + +void FileResourcesModel::refreshModel() +{ m_fullPathModel.clear(); m_fileNameModel.clear(); - m_dirPath = QFileInfo(m_path.toLocalFile()).dir(); - QStringList filterList = m_filter.split(QLatin1Char(' ')); QDirIterator it(m_dirPath.absolutePath(), filterList, QDir::Files, QDirIterator::Subdirectories); @@ -203,11 +216,15 @@ void FileResourcesModel::setupModel() if (filterMetaIcons(absolutePath)) { QString filePath = m_dirPath.relativeFilePath(absolutePath); m_fullPathModel.append(filePath); - m_fileNameModel.append(filePath.mid(filePath.lastIndexOf('/') + 1)); } } - m_lock = false; + Utils::sort(m_fullPathModel, [](const QString &s1, const QString &s2) { + return s1.mid(s1.lastIndexOf('/') + 1).toLower() < s2.mid(s2.lastIndexOf('/') + 1).toLower(); + }); + + for (const QString &fullPath : qAsConst(m_fullPathModel)) + m_fileNameModel.append(fullPath.mid(fullPath.lastIndexOf('/') + 1)); emit fullPathModelChanged(); emit fileNameModelChanged(); diff --git a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h index e0a19643479..226421f441f 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h +++ b/src/plugins/qmldesigner/components/propertyeditor/fileresourcesmodel.h @@ -27,6 +27,8 @@ #include +#include + #include #include #include @@ -60,6 +62,7 @@ public: QStringList fullPathModel() const; QStringList fileNameModel() const; void setupModel(); + void refreshModel(); Q_INVOKABLE void openFileDialog(); @@ -79,12 +82,11 @@ private: QUrl m_path; QDir m_dirPath; QString m_filter; - bool m_lock; QString m_currentPath; QString m_lastModelPath; QStringList m_fullPathModel; QStringList m_fileNameModel; - + Utils::FileSystemWatcher *m_fileSystemWatcher; }; QML_DECLARE_TYPE(FileResourcesModel)