2014-11-01 19:26:05 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011-2012, David Sansome <me@davidsansome.com>
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-11-01 19:26:05 +01:00
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-07-26 13:17:28 +02:00
|
|
|
#include "multisortfilterproxy.h"
|
|
|
|
|
|
|
|
#include <QDate>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTime>
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
2011-07-26 13:17:28 +02:00
|
|
|
MultiSortFilterProxy::MultiSortFilterProxy(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QSortFilterProxyModel(parent) {}
|
2011-07-26 13:17:28 +02:00
|
|
|
|
|
|
|
void MultiSortFilterProxy::AddSortSpec(int role, Qt::SortOrder order) {
|
|
|
|
sorting_ << SortSpec(role, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiSortFilterProxy::lessThan(const QModelIndex& left,
|
|
|
|
const QModelIndex& right) const {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const SortSpec& spec : sorting_) {
|
2011-07-26 14:22:23 +02:00
|
|
|
const int ret = Compare(left.data(spec.first), right.data(spec.first));
|
2011-07-26 13:17:28 +02:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return spec.second == Qt::AscendingOrder;
|
|
|
|
} else if (ret > 0) {
|
|
|
|
return spec.second != Qt::AscendingOrder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return left.row() < right.row();
|
|
|
|
}
|
|
|
|
|
2011-07-26 14:22:23 +02:00
|
|
|
template <typename T>
|
|
|
|
static inline int DoCompare(T left, T right) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (left < right) return -1;
|
|
|
|
if (left > right) return 1;
|
2011-07-26 14:22:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int MultiSortFilterProxy::Compare(const QVariant& left,
|
|
|
|
const QVariant& right) const {
|
2011-07-26 13:17:28 +02:00
|
|
|
// Copied from the QSortFilterProxyModel::lessThan implementation, but returns
|
|
|
|
// -1, 0 or 1 instead of true or false.
|
|
|
|
switch (left.userType()) {
|
|
|
|
case QVariant::Invalid:
|
|
|
|
return (right.type() != QVariant::Invalid) ? -1 : 0;
|
2014-02-07 16:34:20 +01:00
|
|
|
case QVariant::Int:
|
|
|
|
return DoCompare(left.toInt(), right.toInt());
|
|
|
|
case QVariant::UInt:
|
|
|
|
return DoCompare(left.toUInt(), right.toUInt());
|
|
|
|
case QVariant::LongLong:
|
|
|
|
return DoCompare(left.toLongLong(), right.toLongLong());
|
|
|
|
case QVariant::ULongLong:
|
|
|
|
return DoCompare(left.toULongLong(), right.toULongLong());
|
|
|
|
case QMetaType::Float:
|
|
|
|
return DoCompare(left.toFloat(), right.toFloat());
|
|
|
|
case QVariant::Double:
|
|
|
|
return DoCompare(left.toDouble(), right.toDouble());
|
|
|
|
case QVariant::Char:
|
|
|
|
return DoCompare(left.toChar(), right.toChar());
|
|
|
|
case QVariant::Date:
|
|
|
|
return DoCompare(left.toDate(), right.toDate());
|
|
|
|
case QVariant::Time:
|
|
|
|
return DoCompare(left.toTime(), right.toTime());
|
|
|
|
case QVariant::DateTime:
|
|
|
|
return DoCompare(left.toDateTime(), right.toDateTime());
|
2011-07-26 13:17:28 +02:00
|
|
|
case QVariant::String:
|
|
|
|
default:
|
|
|
|
if (isSortLocaleAware())
|
|
|
|
return left.toString().localeAwareCompare(right.toString());
|
|
|
|
else
|
|
|
|
return left.toString().compare(right.toString(), sortCaseSensitivity());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|