summaryrefslogtreecommitdiff
path: root/src/filetreemodel.cpp
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2020-07-19 16:48:31 -0400
committerisanae <14251494+isanae@users.noreply.github.com>2020-07-19 16:48:31 -0400
commit6c4e237d4b43db5c3f9dc01f8c0d3313f3bd2605 (patch)
tree7613427fe55d29abe21a11810ff355f84bdb31e7 /src/filetreemodel.cpp
parentd32250597abf3139268ec3480af9a1fadcf0d18e (diff)
fixed crash because items were sorted while being expanded
when expanding all or updating the tree, only sort once at the end cache file types
Diffstat (limited to 'src/filetreemodel.cpp')
-rw-r--r--src/filetreemodel.cpp59
1 files changed, 45 insertions, 14 deletions
diff --git a/src/filetreemodel.cpp b/src/filetreemodel.cpp
index 11901c43..8790f981 100644
--- a/src/filetreemodel.cpp
+++ b/src/filetreemodel.cpp
@@ -196,13 +196,13 @@ void* makeInternalPointer(FileTreeItem* item)
FileTreeModel::FileTreeModel(OrganizerCore& core, QObject* parent) :
QAbstractItemModel(parent), m_core(core), m_enabled(true),
m_root(FileTreeItem::createDirectory(this, nullptr, L"", L"")),
- m_flags(NoFlags), m_fullyLoaded(false)
+ m_flags(NoFlags), m_fullyLoaded(false), m_sortingEnabled(true)
{
m_root->setExpanded(true);
+ m_sortTimer.setSingleShot(true);
connect(&m_removeTimer, &QTimer::timeout, [&]{ removeItems(); });
connect(&m_sortTimer, &QTimer::timeout, [&]{ sortItems(); });
-
connect(&m_iconPendingTimer, &QTimer::timeout, [&]{ updatePendingIcons(); });
}
@@ -212,6 +212,7 @@ void FileTreeModel::refresh()
m_fullyLoaded = false;
update(*m_root, *m_core.directoryStructure(), L"", false);
+ sortItem(*m_root, false);
}
void FileTreeModel::clear()
@@ -253,6 +254,17 @@ void FileTreeModel::setEnabled(bool b)
m_enabled = b;
}
+void FileTreeModel::aboutToExpandAll()
+{
+ m_sortingEnabled = false;
+}
+
+void FileTreeModel::expandedAll()
+{
+ m_sortingEnabled = true;
+ sortItem(*m_root, false);
+}
+
const FileTreeModel::SortInfo& FileTreeModel::sortInfo() const
{
return m_sort;
@@ -360,6 +372,10 @@ void FileTreeModel::doFetchMore(const QModelIndex& parent, bool forFetch)
const auto parentPath = item->dataRelativeParentPath();
update(*item, *parentEntry, parentPath.toStdWString(), forFetch);
+
+ if (!forFetch) {
+ sortItem(*item, false);
+ }
}
QVariant FileTreeModel::data(const QModelIndex& index, int role) const
@@ -485,7 +501,7 @@ void FileTreeModel::sort(int column, Qt::SortOrder order)
m_sort.column = column;
m_sort.order = order;
- sortItem(*m_root, false);
+ sortItem(*m_root, true);
}
FileTreeItem* FileTreeModel::itemFromIndex(const QModelIndex& index) const
@@ -558,11 +574,15 @@ void FileTreeModel::update(
}
if (added) {
+ parentItem.makeSortingStale();
+
// see comment at the top of this file
- if (forFetching)
- queueSortItem(&parentItem);
- else
- sortItem(parentItem, true);
+ if (forFetching) {
+ // don't pass a specific item, this will start a timer and re-sort the
+ // whole tree, which is faster than potentially queuing every single
+ // node if the whole tree is expanded
+ queueSortItem(nullptr);
+ }
}
}
@@ -873,21 +893,32 @@ void FileTreeModel::removeItems()
void FileTreeModel::queueSortItem(FileTreeItem* item)
{
- m_sortItems.push_back(item);
+ if (!m_sortingEnabled) {
+ return;
+ }
+
+ if (item) {
+ m_sortItems.push_back(item);
+ }
+
m_sortTimer.start(1);
}
void FileTreeModel::sortItems()
{
// see comment at the top of this file
- trace(log::debug("sort item timer: sorting {} items", m_sortItems.size()));
- auto copy = std::move(m_sortItems);
- m_sortItems.clear();
- m_sortTimer.stop();
+ if (m_sortItems.empty()) {
+ sortItem(*m_root, false);
+ } else {
+ log::debug("sort item timer: sorting {} items", m_sortItems.size());
- for (auto&& f : copy) {
- sortItem(*f, true);
+ auto items = std::move(m_sortItems);
+ m_sortItems.clear();
+
+ for (auto* item : items) {
+ sortItem(*item, false);
+ }
}
}