From e81ece3f8f4f930182f2b415e387691b7fa8c985 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 9 Jul 2019 10:01:01 +0200 Subject: [PATCH] QFileSystemModel: Improve class structure Use member initialization in private classes and repack members to minimize padding. Use delegating constructors and default constructors/destructors. Task-number: QTBUG-76493 Change-Id: Iaea8880811782ee5846c128590b83c23e6fae445 Reviewed-by: Volker Hilsheimer --- src/widgets/dialogs/qfileinfogatherer.cpp | 10 +---- src/widgets/dialogs/qfileinfogatherer_p.h | 8 ++-- src/widgets/dialogs/qfilesystemmodel.cpp | 13 +++--- src/widgets/dialogs/qfilesystemmodel_p.h | 55 +++++++++-------------- 4 files changed, 32 insertions(+), 54 deletions(-) diff --git a/src/widgets/dialogs/qfileinfogatherer.cpp b/src/widgets/dialogs/qfileinfogatherer.cpp index f39ae2b53e5..9ab75e0b0af 100644 --- a/src/widgets/dialogs/qfileinfogatherer.cpp +++ b/src/widgets/dialogs/qfileinfogatherer.cpp @@ -79,14 +79,8 @@ static QString translateDriveName(const QFileInfo &drive) Creates thread */ QFileInfoGatherer::QFileInfoGatherer(QObject *parent) - : QThread(parent), abort(false), -#if QT_CONFIG(filesystemwatcher) - watcher(0), -#endif -#ifdef Q_OS_WIN - m_resolveSymlinks(true), -#endif - m_iconProvider(&defaultProvider) + : QThread(parent) + , m_iconProvider(&defaultProvider) { #if QT_CONFIG(filesystemwatcher) watcher = new QFileSystemWatcher(this); diff --git a/src/widgets/dialogs/qfileinfogatherer_p.h b/src/widgets/dialogs/qfileinfogatherer_p.h index 795f60249f5..829c620c1ec 100644 --- a/src/widgets/dialogs/qfileinfogatherer_p.h +++ b/src/widgets/dialogs/qfileinfogatherer_p.h @@ -210,13 +210,13 @@ private: QAtomicInt abort; #if QT_CONFIG(filesystemwatcher) - QFileSystemWatcher *watcher; -#endif -#ifdef Q_OS_WIN - bool m_resolveSymlinks; // not accessed by run() + QFileSystemWatcher *watcher = nullptr; #endif QFileIconProvider *m_iconProvider; // not accessed by run() QFileIconProvider defaultProvider; +#ifdef Q_OS_WIN + bool m_resolveSymlinks = true; // not accessed by run() +#endif }; QT_END_NAMESPACE diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index b521f50f407..820fc6e2201 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -227,11 +227,9 @@ bool QFileSystemModel::remove(const QModelIndex &aindex) /*! Constructs a file system model with the given \a parent. */ -QFileSystemModel::QFileSystemModel(QObject *parent) - : QAbstractItemModel(*new QFileSystemModelPrivate, parent) +QFileSystemModel::QFileSystemModel(QObject *parent) : + QFileSystemModel(*new QFileSystemModelPrivate, parent) { - Q_D(QFileSystemModel); - d->init(); } /*! @@ -247,9 +245,7 @@ QFileSystemModel::QFileSystemModel(QFileSystemModelPrivate &dd, QObject *parent) /*! Destroys this file system model. */ -QFileSystemModel::~QFileSystemModel() -{ -} +QFileSystemModel::~QFileSystemModel() = default; /*! \reimp @@ -1945,6 +1941,9 @@ QStringList QFileSystemModelPrivate::unwatchPathsAt(const QModelIndex &index) void QFileSystemModelPrivate::init() { Q_Q(QFileSystemModel); + + delayedSortTimer.setSingleShot(true); + qRegisterMetaType > >(); #if QT_CONFIG(filesystemwatcher) q->connect(&fileInfoGatherer, SIGNAL(newListOfFiles(QString,QStringList)), diff --git a/src/widgets/dialogs/qfilesystemmodel_p.h b/src/widgets/dialogs/qfilesystemmodel_p.h index d8f9f2b076f..844e417e2d3 100644 --- a/src/widgets/dialogs/qfilesystemmodel_p.h +++ b/src/widgets/dialogs/qfilesystemmodel_p.h @@ -99,13 +99,13 @@ public: class QFileSystemNode { public: + Q_DISABLE_COPY_MOVE(QFileSystemNode) + explicit QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = nullptr) - : fileName(filename), populatedChildren(false), isVisible(false), dirtyChildrenIndex(-1), parent(p), info(nullptr) {} + : fileName(filename), parent(p) {} ~QFileSystemNode() { qDeleteAll(children); delete info; - info = nullptr; - parent = nullptr; } QString fileName; @@ -204,31 +204,16 @@ public: } } - bool populatedChildren; - bool isVisible; QHash children; QList visibleChildren; - int dirtyChildrenIndex; + QExtendedInformation *info = nullptr; QFileSystemNode *parent; - - - QExtendedInformation *info; - + int dirtyChildrenIndex = -1; + bool populatedChildren = false; + bool isVisible = false; }; - QFileSystemModelPrivate() : - forceSort(true), - sortColumn(0), - sortOrder(Qt::AscendingOrder), - readOnly(true), - setRootPath(false), - filters(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs), - nameFilterDisables(true), // false on windows, true on mac and unix - disableRecursiveSort(false) - { - delayedSortTimer.setSingleShot(true); - } - + QFileSystemModelPrivate() = default; void init(); /* \internal @@ -303,18 +288,7 @@ public: QFileInfoGatherer fileInfoGatherer; #endif // filesystemwatcher QTimer delayedSortTimer; - bool forceSort; - int sortColumn; - Qt::SortOrder sortOrder; - bool readOnly; - bool setRootPath; - QDir::Filters filters; QHash bypassFilters; - bool nameFilterDisables; - //This flag is an optimization for the QFileDialog - //It enable a sort which is not recursive, it means - //we sort only what we see. - bool disableRecursiveSort; #if QT_CONFIG(regularexpression) QStringList nameFilters; #endif @@ -322,7 +296,6 @@ public: QFileSystemNode root; - QBasicTimer fetchingTimer; struct Fetching { QString dir; QString file; @@ -330,6 +303,18 @@ public: }; QVector toFetch; + QBasicTimer fetchingTimer; + + QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs; + int sortColumn = 0; + Qt::SortOrder sortOrder = Qt::AscendingOrder; + bool forceSort = true; + bool readOnly = true; + bool setRootPath = false; + bool nameFilterDisables = true; // false on windows, true on mac and unix + // This flag is an optimization for QFileDialog. It enables a sort which is + // not recursive, meaning we sort only what we see. + bool disableRecursiveSort = false; }; Q_DECLARE_TYPEINFO(QFileSystemModelPrivate::Fetching, Q_MOVABLE_TYPE);