forked from qt-creator/qt-creator
QmlDesigner: Sort items in FileResourceModel
* Sort items in FileResourceModel * Add FileSystemWatcher on directory Task-number: QDS-2919 Change-Id: I0ba50f03d4f901a48709ed0cc0e7f05d3037aeec Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
committed by
Henning Gründl
parent
5a97fa53dc
commit
58e612c85f
@@ -29,15 +29,21 @@
|
||||
|
||||
#include <model.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QDirIterator>
|
||||
#include <qmlmodelnodeproxy.h>
|
||||
|
||||
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();
|
||||
|
@@ -27,6 +27,8 @@
|
||||
|
||||
#include <qmlitemnode.h>
|
||||
|
||||
#include <utils/filesystemwatcher.h>
|
||||
|
||||
#include <QDir>
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
@@ -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)
|
||||
|
Reference in New Issue
Block a user