diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-07 11:26:11 -0500 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-07 11:26:11 -0500 |
| commit | 9c2fc694051b8e500a3343322dd45cf8be1c1b7d (patch) | |
| tree | 243180a392d84bb4649d0b38909560a7b3ccb7bf | |
| parent | 41e6189be431dbd41b9d41751b01708df06a9610 (diff) | |
sort on demand
| -rw-r--r-- | src/filetreeitem.cpp | 30 | ||||
| -rw-r--r-- | src/filetreeitem.h | 15 | ||||
| -rw-r--r-- | src/filetreemodel.cpp | 11 | ||||
| -rw-r--r-- | src/filetreemodel.h | 17 |
4 files changed, 53 insertions, 20 deletions
diff --git a/src/filetreeitem.cpp b/src/filetreeitem.cpp index 3332fb7d..8602ea6d 100644 --- a/src/filetreeitem.cpp +++ b/src/filetreeitem.cpp @@ -41,9 +41,9 @@ const QString& directoryFileType() FileTreeItem::FileTreeItem( - FileTreeItem* parent, + FileTreeModel* model, FileTreeItem* parent, std::wstring dataRelativeParentPath, bool isDirectory, std::wstring file) : - m_parent(parent), m_indexGuess(NoIndexGuess), + m_model(model), m_parent(parent), m_indexGuess(NoIndexGuess), m_virtualParentPath(QString::fromStdWString(dataRelativeParentPath)), m_wsFile(file), m_wsLcFile(ToLowerCopy(file)), @@ -53,23 +53,25 @@ FileTreeItem::FileTreeItem( m_originID(-1), m_flags(NoFlags), m_loaded(false), - m_expanded(false) + m_expanded(false), + m_sortingStale(true) { } FileTreeItem::Ptr FileTreeItem::createFile( - FileTreeItem* parent, std::wstring dataRelativeParentPath, std::wstring file) + FileTreeModel* model, FileTreeItem* parent, + std::wstring dataRelativeParentPath, std::wstring file) { return std::unique_ptr<FileTreeItem>(new FileTreeItem( - parent, std::move(dataRelativeParentPath), false, std::move(file))); + model, parent, std::move(dataRelativeParentPath), false, std::move(file))); } FileTreeItem::Ptr FileTreeItem::createDirectory( - FileTreeItem* parent, + FileTreeModel* model, FileTreeItem* parent, std::wstring dataRelativeParentPath, std::wstring file) { return std::unique_ptr<FileTreeItem>(new FileTreeItem( - parent, std::move(dataRelativeParentPath), true, std::move(file))); + model, parent, std::move(dataRelativeParentPath), true, std::move(file))); } void FileTreeItem::setOrigin( @@ -174,9 +176,23 @@ public: } }; +void FileTreeItem::sort() +{ + sort(m_model->sortInfo().column, m_model->sortInfo().order); +} void FileTreeItem::sort(int column, Qt::SortOrder order) { + if (!m_expanded) { + m_sortingStale = true; + return; + } + + if (m_sortingStale) { + //log::debug("sorting is stale for {}, sorting now", debugName()); + m_sortingStale = false; + } + std::sort(m_children.begin(), m_children.end(), [&](auto&& a, auto&& b) { int r = 0; diff --git a/src/filetreeitem.h b/src/filetreeitem.h index 0d92b3f7..375a05c4 100644 --- a/src/filetreeitem.h +++ b/src/filetreeitem.h @@ -4,6 +4,8 @@ #include "directoryentry.h" #include <QFileIconProvider> +class FileTreeModel; + class FileTreeItem { class Sorter; @@ -23,11 +25,11 @@ public: Q_DECLARE_FLAGS(Flags, Flag); static Ptr createFile( - FileTreeItem* parent, + FileTreeModel* model, FileTreeItem* parent, std::wstring dataRelativeParentPath, std::wstring file); static Ptr createDirectory( - FileTreeItem* parent, + FileTreeModel* model, FileTreeItem* parent, std::wstring dataRelativeParentPath, std::wstring file); FileTreeItem(const FileTreeItem&) = delete; @@ -215,6 +217,10 @@ public: void setExpanded(bool b) { m_expanded = b; + + if (m_sortingStale) { + sort(); + } } bool isStrictlyExpanded() const @@ -272,6 +278,7 @@ private: static constexpr std::size_t NoIndexGuess = std::numeric_limits<std::size_t>::max(); + FileTreeModel* m_model; FileTreeItem* m_parent; mutable std::size_t m_indexGuess; @@ -294,14 +301,16 @@ private: bool m_loaded; bool m_expanded; + bool m_sortingStale; Children m_children; FileTreeItem( - FileTreeItem* parent, + FileTreeModel* model, FileTreeItem* parent, std::wstring dataRelativeParentPath, bool isDirectory, std::wstring file); void getFileType() const; + void sort(); }; #endif // MODORGANIZER_FILETREEITEM_INCLUDED diff --git a/src/filetreemodel.cpp b/src/filetreemodel.cpp index 169d0a02..01a10afc 100644 --- a/src/filetreemodel.cpp +++ b/src/filetreemodel.cpp @@ -138,7 +138,7 @@ void* makeInternalPointer(FileTreeItem* item) FileTreeModel::FileTreeModel(OrganizerCore& core, QObject* parent) : QAbstractItemModel(parent), m_core(core), m_enabled(true), - m_root(FileTreeItem::createDirectory(nullptr, L"", L"")), + m_root(FileTreeItem::createDirectory(this, nullptr, L"", L"")), m_flags(NoFlags), m_fullyLoaded(false) { m_root->setExpanded(true); @@ -193,6 +193,11 @@ void FileTreeModel::setEnabled(bool b) m_enabled = b; } +const FileTreeModel::SortInfo& FileTreeModel::sortInfo() const +{ + return m_sort; +} + bool FileTreeModel::showArchives() const { return (m_flags.testFlag(Archives) && m_core.getArchiveParsing()); @@ -756,7 +761,7 @@ FileTreeItem::Ptr FileTreeModel::createDirectoryItem( const DirectoryEntry& d) { auto item = FileTreeItem::createDirectory( - &parentItem, parentPath, d.getName()); + this, &parentItem, parentPath, d.getName()); if (d.isEmpty()) { // if this directory is empty, mark the item as loaded so the expand @@ -772,7 +777,7 @@ FileTreeItem::Ptr FileTreeModel::createFileItem( const FileEntry& file) { auto item = FileTreeItem::createFile( - &parentItem, parentPath, file.getName()); + this, &parentItem, parentPath, file.getName()); updateFileItem(*item, file); diff --git a/src/filetreemodel.h b/src/filetreemodel.h index 1bc7f73b..deb6d092 100644 --- a/src/filetreemodel.h +++ b/src/filetreemodel.h @@ -34,6 +34,13 @@ public: Q_DECLARE_FLAGS(Flags, Flag); + struct SortInfo + { + int column = 0; + Qt::SortOrder order = Qt::AscendingOrder; + }; + + FileTreeModel(OrganizerCore& core, QObject* parent=nullptr); void setFlags(Flags f) @@ -48,6 +55,8 @@ public: bool enabled() const; void setEnabled(bool b); + const SortInfo& sortInfo() const; + QModelIndex index(int row, int col, const QModelIndex& parent={}) const override; QModelIndex parent(const QModelIndex& index) const override; int rowCount(const QModelIndex& parent={}) const override; @@ -63,12 +72,6 @@ public: FileTreeItem* itemFromIndex(const QModelIndex& index) const; private: - struct Sort - { - int column = 0; - Qt::SortOrder order = Qt::AscendingOrder; - }; - class Range; using DirectoryIterator = std::vector<MOShared::DirectoryEntry*>::const_iterator; @@ -80,7 +83,7 @@ private: mutable IconFetcher m_iconFetcher; mutable std::vector<QModelIndex> m_iconPending; mutable QTimer m_iconPendingTimer; - Sort m_sort; + SortInfo m_sort; bool m_fullyLoaded; bool showConflictsOnly() const |
