diff options
Diffstat (limited to 'plugin/src/Caelestia/Models/filesystemmodel.cpp')
| -rw-r--r-- | plugin/src/Caelestia/Models/filesystemmodel.cpp | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/plugin/src/Caelestia/Models/filesystemmodel.cpp b/plugin/src/Caelestia/Models/filesystemmodel.cpp index 0eadebb..81c07d9 100644 --- a/plugin/src/Caelestia/Models/filesystemmodel.cpp +++ b/plugin/src/Caelestia/Models/filesystemmodel.cpp @@ -266,14 +266,11 @@ void FileSystemModel::updateWatcher() { void FileSystemModel::updateEntries() { if (m_path.isEmpty()) { if (!m_entries.isEmpty()) { - auto toDelete = m_entries; - beginResetModel(); + qDeleteAll(m_entries); m_entries.clear(); endResetModel(); emit entriesChanged(); - - qDeleteAll(toDelete); } return; @@ -396,8 +393,7 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet } std::sort(removedIndices.begin(), removedIndices.end(), std::greater<int>()); - QList<FileSystemEntry*> toDelete; - + // Batch remove old entries int start = -1; int end = -1; for (int idx : std::as_const(removedIndices)) { @@ -409,8 +405,7 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet } else { beginRemoveRows(QModelIndex(), end, start); for (int i = start; i >= end; --i) { - emit removed(m_entries[i]->path()); - toDelete << m_entries.takeAt(i); + m_entries.takeAt(i)->deleteLater(); } endRemoveRows(); @@ -421,12 +416,12 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet if (start != -1) { beginRemoveRows(QModelIndex(), end, start); for (int i = start; i >= end; --i) { - emit removed(m_entries[i]->path()); - toDelete << m_entries.takeAt(i); + m_entries.takeAt(i)->deleteLater(); } endRemoveRows(); } + // Create new entries QList<FileSystemEntry*> newEntries; for (const auto& path : addedPaths) { newEntries << new FileSystemEntry(path, m_dir.relativeFilePath(path), this); @@ -435,57 +430,50 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet return compareEntries(a, b); }); + // Batch insert new entries int insertStart = -1; - int prevRow = -1; QList<FileSystemEntry*> batchItems; for (const auto& entry : std::as_const(newEntries)) { const auto it = std::lower_bound( m_entries.begin(), m_entries.end(), entry, [this](const FileSystemEntry* a, const FileSystemEntry* b) { return compareEntries(a, b); }); - int row = static_cast<int>(it - m_entries.begin()); + const auto row = static_cast<int>(it - m_entries.begin()); if (insertStart == -1) { insertStart = row; - prevRow = row; - batchItems.clear(); batchItems << entry; - } else if (row == prevRow + 1) { - prevRow = row; + } else if (row == insertStart + batchItems.size()) { batchItems << entry; } else { - beginInsertRows(QModelIndex(), insertStart, static_cast<int>(insertStart + batchItems.size() - 1)); + beginInsertRows(QModelIndex(), insertStart, insertStart + static_cast<int>(batchItems.size()) - 1); for (int i = 0; i < batchItems.size(); ++i) { m_entries.insert(insertStart + i, batchItems[i]); - emit added(batchItems[i]); } endInsertRows(); insertStart = row; - prevRow = row; batchItems.clear(); batchItems << entry; } - prevRow = static_cast<int>(m_entries.indexOf(entry)); } if (!batchItems.isEmpty()) { - beginInsertRows(QModelIndex(), insertStart, static_cast<int>(insertStart + batchItems.size() - 1)); + beginInsertRows(QModelIndex(), insertStart, insertStart + static_cast<int>(batchItems.size()) - 1); for (int i = 0; i < batchItems.size(); ++i) { m_entries.insert(insertStart + i, batchItems[i]); - emit added(batchItems[i]); } endInsertRows(); } emit entriesChanged(); - qDeleteAll(toDelete); } bool FileSystemModel::compareEntries(const FileSystemEntry* a, const FileSystemEntry* b) const { if (a->isDir() != b->isDir()) { return m_sortReverse ^ a->isDir(); } - return m_sortReverse ^ (a->relativePath().localeAwareCompare(b->relativePath()) < 0); + const auto cmp = a->relativePath().localeAwareCompare(b->relativePath()); + return m_sortReverse ? cmp > 0 : cmp < 0; } } // namespace caelestia |