From 7c4d3c6fb958d61e8d74fac982907f48831c7f7e Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 20 Jan 2020 18:28:50 -0500 Subject: refactored removeDisappearingFiles() to just check for directories in the main loop --- src/filetreemodel.cpp | 71 +++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 39 deletions(-) (limited to 'src/filetreemodel.cpp') diff --git a/src/filetreemodel.cpp b/src/filetreemodel.cpp index f5877dbb..00ab13d6 100644 --- a/src/filetreemodel.cpp +++ b/src/filetreemodel.cpp @@ -435,60 +435,49 @@ void FileTreeModel::removeDisappearingFiles( auto& children = parentItem.children(); auto itor = children.begin(); + firstFileRow = -1; + // keeps track of the contiguous directories that need to be removed to // avoid calling beginRemoveRows(), etc. for each item int removeStart = -1; - firstFileRow = 0; + int row = 0; - // directories are always first, so find the first file item + // for each item in this tree item while (itor != children.end()) { const auto& item = *itor; if (!item->isDirectory()) { - break; - } - - ++firstFileRow; - ++itor; - } - - if (itor == children.end()) { - // no file items - return; - } - - int row = firstFileRow; - - // for each item in this tree item - while (itor != children.end()) { - const auto& item = *itor; + if (firstFileRow == -1) { + firstFileRow = row; + } - auto f = parentEntry.findFile(item->key()); + auto f = parentEntry.findFile(item->key()); - if (f) { - trace([&]{ log::debug("file {} still there", item->filename()); }); + if (f) { + trace([&]{ log::debug("file {} still there", item->filename()); }); - // file is still there - seen.emplace(f->getIndex()); + // file is still there + seen.emplace(f->getIndex()); - // if there were files before this row that need to be removed, - // do it now - if (removeStart != -1) { - removeRange(parentItem, removeStart, row - 1); + // if there were files before this row that need to be removed, + // do it now + if (removeStart != -1) { + removeRange(parentItem, removeStart, row - 1); - // adjust current row to account for those that were just removed - row -= (row - removeStart); - itor = children.begin() + row; + // adjust current row to account for those that were just removed + row -= (row - removeStart); + itor = children.begin() + row; - removeStart = -1; - } - } else { - // file is gone from the parent entry - trace([&]{ log::debug("file {} is gone", item->filename()); }); + removeStart = -1; + } + } else { + // file is gone from the parent entry + trace([&]{ log::debug("file {} is gone", item->filename()); }); - if (removeStart == -1) { - // start a new contiguous sequence - removeStart = row; + if (removeStart == -1) { + // start a new contiguous sequence + removeStart = row; + } } } @@ -496,6 +485,10 @@ void FileTreeModel::removeDisappearingFiles( ++itor; } + if (firstFileRow == -1) { + firstFileRow = static_cast(children.size()); + } + // remove the last file range, if any if (removeStart != -1) { removeRange(parentItem, removeStart, row -1 ); -- cgit v1.3.1