-/*****************************************************************************
- * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
- * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
- * Copyright (C) 2013 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
- * *
- * 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 <peter.penz19@gmail.com>
+ * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
+ * SPDX-FileCopyrightText: 2013 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
+ *
+ * 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"
#include <QMimeData>
#include <QTimer>
#include <QWidget>
+#include <QMutex>
+
+Q_GLOBAL_STATIC_WITH_ARGS(QMutex, s_collatorMutex, (QMutex::Recursive))
// #define KFILEITEMMODEL_DEBUG
// The following code has been taken from KDirModel::mimeData()
// (kdelibs/kio/kio/kdirmodel.cpp)
- // Copyright (C) 2006 David Faure <faure@kde.org>
+ // SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
QList<QUrl> urls;
QList<QUrl> mostLocalUrls;
const ItemData* lastAddedItem = nullptr;
urls << item.url();
bool isLocal;
- mostLocalUrls << item.mostLocalUrl(isLocal);
+ mostLocalUrls << item.mostLocalUrl(&isLocal);
}
}
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());
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);
// 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()) {
} 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():
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);
}
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);
}