X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/31dd81bb0c7cd77e65c095ec4b49ad95935946c6..576a97d8eb9601f4ad74def6528cc90fffe97b1c:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index ad996a59e..28376c5d0 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -1,27 +1,15 @@ -/***************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * Copyright (C) 2013 by Frank Reininghaus * - * Copyright (C) 2013 by Emmanuel Pescosta * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - *****************************************************************************/ +/* + * SPDX-FileCopyrightText: 2011 Peter Penz + * SPDX-FileCopyrightText: 2013 Frank Reininghaus + * SPDX-FileCopyrightText: 2013 Emmanuel Pescosta + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "kfileitemmodel.h" #include "dolphin_generalsettings.h" +#include "dolphin_detailsmodesettings.h" #include "dolphindebug.h" #include "private/kfileitemmodeldirlister.h" #include "private/kfileitemmodelsortalgorithm.h" @@ -33,6 +21,9 @@ #include #include #include +#include + +Q_GLOBAL_STATIC_WITH_ARGS(QMutex, s_collatorMutex, (QMutex::Recursive)) // #define KFILEITEMMODEL_DEBUG @@ -243,7 +234,7 @@ QMimeData* KFileItemModel::createMimeData(const KItemSet& indexes) const // The following code has been taken from KDirModel::mimeData() // (kdelibs/kio/kio/kdirmodel.cpp) - // Copyright (C) 2006 David Faure + // SPDX-FileCopyrightText: 2006 David Faure QList urls; QList mostLocalUrls; const ItemData* lastAddedItem = nullptr; @@ -1187,12 +1178,20 @@ void KFileItemModel::insertItems(QList& newItems) m_groups.clear(); prepareItemsForSorting(newItems); - if (m_sortRole == NameRole && m_naturalSorting) { - // Natural sorting of items can be very slow. However, it becomes much - // faster if the input sequence is already mostly sorted. Therefore, we - // first sort 'newItems' according to the QStrings returned by - // KFileItem::text() using QString::operator<(), which is quite fast. - parallelMergeSort(newItems.begin(), newItems.end(), nameLessThan, QThread::idealThreadCount()); + // Natural sorting of items can be very slow. However, it becomes much faster + // if the input sequence is already mostly sorted. Therefore, we first sort + // 'newItems' according to the QStrings using QString::operator<(), which is quite fast. + if (m_naturalSorting) { + if (m_sortRole == NameRole) { + parallelMergeSort(newItems.begin(), newItems.end(), nameLessThan, QThread::idealThreadCount()); + } else if (isRoleValueNatural(m_sortRole)) { + auto lambdaLessThan = [&] (const KFileItemModel::ItemData* a, const KFileItemModel::ItemData* b) + { + const QByteArray role = roleForType(m_sortRole); + return a->values.value(role).toString() < b->values.value(role).toString(); + }; + parallelMergeSort(newItems.begin(), newItems.end(), lambdaLessThan, QThread::idealThreadCount()); + } } sort(newItems.begin(), newItems.end()); @@ -1726,8 +1725,8 @@ void KFileItemModel::sort(const QList::iterator &begi return lessThan(a, b, m_collator); }; - if (m_sortRole == NameRole) { - // Sorting by name can be expensive, in particular if natural sorting is + if (m_sortRole == NameRole || isRoleValueNatural(m_sortRole)) { + // Sorting by string can be expensive, in particular if natural sorting is // enabled. Use all CPU cores to speed up the sorting process. static const int numberOfThreads = QThread::idealThreadCount(); parallelMergeSort(begin, end, lambdaLessThan, numberOfThreads); @@ -1756,8 +1755,15 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const // See "if (m_sortFoldersFirst || m_sortRole == SizeRole)" in KFileItemModel::lessThan(): Q_ASSERT(itemB.isDir()); - const QVariant valueA = a->values.value("size"); - const QVariant valueB = b->values.value("size"); + QVariant valueA, valueB; + if (DetailsModeSettings::directorySizeCount()) { + valueA = a->values.value("count"); + valueB = b->values.value("count"); + } else { + // use dir size then + valueA = a->values.value("size"); + valueB = b->values.value("size"); + } if (valueA.isNull() && valueB.isNull()) { result = 0; } else if (valueA.isNull()) { @@ -1765,7 +1771,11 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const } else if (valueB.isNull()) { result = +1; } else { - result = valueA.toInt() - valueB.toInt(); + if (valueA < valueB) { + return -1; + } else { + return +1; + } } } else { // See "if (m_sortFoldersFirst || m_sortRole == SizeRole)" in KFileItemModel::lessThan(): @@ -1835,6 +1845,8 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const result = -1; } else if (roleValueA.isEmpty() && !roleValueB.isEmpty()) { result = +1; + } else if (isRoleValueNatural(m_sortRole)) { + result = stringCompare(roleValueA, roleValueB, collator); } else { result = QString::compare(roleValueA, roleValueB); } @@ -1868,6 +1880,8 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const int KFileItemModel::stringCompare(const QString& a, const QString& b, const QCollator& collator) const { + QMutexLocker collatorLock(s_collatorMutex()); + if (m_naturalSorting) { return collator.compare(a, b); }