diff --git a/src/kitemviews/kfileitemmodel.h b/src/kitemviews/kfileitemmodel.h --- a/src/kitemviews/kfileitemmodel.h +++ b/src/kitemviews/kfileitemmodel.h @@ -189,14 +189,36 @@ bool requiresIndexer; }; + struct ItemData + { + KFileItem item; + QHash values; + ItemData* parent; + }; + /** * @return Provides static information for all available roles that * are supported by KFileItemModel. Some roles can only be * determined if Baloo is enabled and/or the Baloo * indexing is enabled. */ static QList rolesInformation(); + template + void execute(Func&& executor) + { + std::for_each(m_itemData.cbegin(), m_itemData.cend(), std::forward(executor)); + } + + template + void executeOnIndexes(const Indexes& indexes, Func&& executor) + { + std::for_each(indexes.begin(), indexes.end(), [&](int index) + { + std::forward(executor)(m_itemData.at(index)); + }); + } + signals: /** * Is emitted if the loading of a directory has been started. It is @@ -294,13 +316,6 @@ RolesCount }; - struct ItemData - { - KFileItem item; - QHash values; - ItemData* parent; - }; - enum RemoveItemsBehavior { KeepItemData, DeleteItemData diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -705,8 +705,15 @@ * It is recommend using this method instead of asking the * directory lister or the model directly, as it takes * filtering and hierarchical previews into account. + * + * If selectionBased is true, do the calculation based on + * the selection. If it is true while there is no selection + * then it behaves the same as if false were given. + * + * If selectionBased is false then it will always work on + * all items. */ - void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const; + void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize, bool selectionBased = false) const; void slotTwoClicksRenamingTimerTimeout(); diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -521,28 +521,19 @@ int fileCount = 0; KIO::filesize_t totalFileSize = 0; - if (m_container->controller()->selectionManager()->hasSelection()) { - // Give a summary of the status of the selected files - const KFileItemList list = selectedItems(); - foreach (const KFileItem& item, list) { - if (item.isDir()) { - ++folderCount; - } else { - ++fileCount; - totalFileSize += item.size(); - } - } + calculateItemCount(fileCount, folderCount, totalFileSize, true); + if (m_container->controller()->selectionManager()->hasSelection()) { if (folderCount + fileCount == 1) { // If only one item is selected, show info about it - return list.first().getStatusBarInfo(); + int currentItem = m_container->controller()->selectionManager()->currentItem(); + return m_model->fileItem(currentItem).getStatusBarInfo(); } else { // At least 2 items are selected foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount); } } else { - calculateItemCount(fileCount, folderCount, totalFileSize); foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount); filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount); } @@ -1423,17 +1414,30 @@ void DolphinView::calculateItemCount(int& fileCount, int& folderCount, - KIO::filesize_t& totalFileSize) const -{ - const int itemCount = m_model->count(); - for (int i = 0; i < itemCount; ++i) { - const KFileItem item = m_model->fileItem(i); - if (item.isDir()) { - ++folderCount; - } else { - ++fileCount; - totalFileSize += item.size(); - } + KIO::filesize_t& totalFileSize, + bool selectionBased) const +{ + if (selectionBased && m_container->controller()->selectionManager()->hasSelection()) { + const auto selectedItems = m_container->controller()->selectionManager()->selectedItems(); + m_model->executeOnIndexes(selectedItems, [&](const KFileItemModel::ItemData* data) + { + if (data->item.isDir()) { + ++folderCount; + } else { + ++fileCount; + totalFileSize += data->item.size(); + } + }); + } else { + m_model->execute([&](const KFileItemModel::ItemData* data) + { + if (data->item.isDir()) { + ++folderCount; + } else { + ++fileCount; + totalFileSize += data->item.size(); + } + }); } }