ItemModel: Extract Method isVariantLessThan()
As noted in comments, QSortFilterProxyModel and QStandardItemModel duplicate code to compare QVariant; extract this into a separate method they can share. Since there is only one common suitable header for both files, the method was placed in qabstractitemmodel.cpp Change-Id: I82bb4a2d6084059b8a70a8d556c16f1a29f4f686 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
parent
98526119c6
commit
2185b2f054
@ -47,6 +47,7 @@
|
||||
#include <qvector.h>
|
||||
#include <qstack.h>
|
||||
#include <qbitarray.h>
|
||||
#include <qdatetime.h>
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
@ -547,6 +548,43 @@ const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames()
|
||||
return *qDefaultRoleNames();
|
||||
}
|
||||
|
||||
bool QAbstractItemModelPrivate::isVariantLessThan(const QVariant &left, const QVariant &right,
|
||||
Qt::CaseSensitivity cs, bool isLocaleAware)
|
||||
{
|
||||
if (left.userType() == QVariant::Invalid)
|
||||
return false;
|
||||
if (right.userType() == QVariant::Invalid)
|
||||
return true;
|
||||
switch (left.userType()) {
|
||||
case QVariant::Int:
|
||||
return left.toInt() < right.toInt();
|
||||
case QVariant::UInt:
|
||||
return left.toUInt() < right.toUInt();
|
||||
case QVariant::LongLong:
|
||||
return left.toLongLong() < right.toLongLong();
|
||||
case QVariant::ULongLong:
|
||||
return left.toULongLong() < right.toULongLong();
|
||||
case QMetaType::Float:
|
||||
return left.toFloat() < right.toFloat();
|
||||
case QVariant::Double:
|
||||
return left.toDouble() < right.toDouble();
|
||||
case QVariant::Char:
|
||||
return left.toChar() < right.toChar();
|
||||
case QVariant::Date:
|
||||
return left.toDate() < right.toDate();
|
||||
case QVariant::Time:
|
||||
return left.toTime() < right.toTime();
|
||||
case QVariant::DateTime:
|
||||
return left.toDateTime() < right.toDateTime();
|
||||
case QVariant::String:
|
||||
default:
|
||||
if (isLocaleAware)
|
||||
return left.toString().localeAwareCompare(right.toString()) < 0;
|
||||
else
|
||||
return left.toString().compare(right.toString(), cs) < 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static uint typeOfVariant(const QVariant &value)
|
||||
{
|
||||
|
@ -150,6 +150,8 @@ public:
|
||||
|
||||
QHash<int,QByteArray> roleNames;
|
||||
static const QHash<int,QByteArray> &defaultRoleNames();
|
||||
static bool isVariantLessThan(const QVariant &left, const QVariant &right,
|
||||
Qt::CaseSensitivity cs = Qt::CaseSensitive, bool isLocaleAware = false);
|
||||
};
|
||||
Q_DECLARE_TYPEINFO(QAbstractItemModelPrivate::Change, Q_MOVABLE_TYPE);
|
||||
|
||||
|
@ -2652,40 +2652,7 @@ bool QSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QMode
|
||||
Q_D(const QSortFilterProxyModel);
|
||||
QVariant l = (source_left.model() ? source_left.model()->data(source_left, d->sort_role) : QVariant());
|
||||
QVariant r = (source_right.model() ? source_right.model()->data(source_right, d->sort_role) : QVariant());
|
||||
// Duplicated in QStandardItem::operator<()
|
||||
if (l.userType() == QVariant::Invalid)
|
||||
return false;
|
||||
if (r.userType() == QVariant::Invalid)
|
||||
return true;
|
||||
switch (l.userType()) {
|
||||
case QVariant::Int:
|
||||
return l.toInt() < r.toInt();
|
||||
case QVariant::UInt:
|
||||
return l.toUInt() < r.toUInt();
|
||||
case QVariant::LongLong:
|
||||
return l.toLongLong() < r.toLongLong();
|
||||
case QVariant::ULongLong:
|
||||
return l.toULongLong() < r.toULongLong();
|
||||
case QMetaType::Float:
|
||||
return l.toFloat() < r.toFloat();
|
||||
case QVariant::Double:
|
||||
return l.toDouble() < r.toDouble();
|
||||
case QVariant::Char:
|
||||
return l.toChar() < r.toChar();
|
||||
case QVariant::Date:
|
||||
return l.toDate() < r.toDate();
|
||||
case QVariant::Time:
|
||||
return l.toTime() < r.toTime();
|
||||
case QVariant::DateTime:
|
||||
return l.toDateTime() < r.toDateTime();
|
||||
case QVariant::String:
|
||||
default:
|
||||
if (d->sort_localeaware)
|
||||
return l.toString().localeAwareCompare(r.toString()) < 0;
|
||||
else
|
||||
return l.toString().compare(r.toString(), d->sort_casesensitivity) < 0;
|
||||
}
|
||||
return false;
|
||||
return QAbstractItemModelPrivate::isVariantLessThan(l, r, d->sort_casesensitivity, d->sort_localeaware);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -1865,36 +1865,7 @@ bool QStandardItem::operator<(const QStandardItem &other) const
|
||||
{
|
||||
const int role = model() ? model()->sortRole() : Qt::DisplayRole;
|
||||
const QVariant l = data(role), r = other.data(role);
|
||||
// this code is copied from QSortFilterProxyModel::lessThan()
|
||||
if (l.userType() == QVariant::Invalid)
|
||||
return false;
|
||||
if (r.userType() == QVariant::Invalid)
|
||||
return true;
|
||||
switch (l.userType()) {
|
||||
case QVariant::Int:
|
||||
return l.toInt() < r.toInt();
|
||||
case QVariant::UInt:
|
||||
return l.toUInt() < r.toUInt();
|
||||
case QVariant::LongLong:
|
||||
return l.toLongLong() < r.toLongLong();
|
||||
case QVariant::ULongLong:
|
||||
return l.toULongLong() < r.toULongLong();
|
||||
case QMetaType::Float:
|
||||
return l.toFloat() < r.toFloat();
|
||||
case QVariant::Double:
|
||||
return l.toDouble() < r.toDouble();
|
||||
case QVariant::Char:
|
||||
return l.toChar() < r.toChar();
|
||||
case QVariant::Date:
|
||||
return l.toDate() < r.toDate();
|
||||
case QVariant::Time:
|
||||
return l.toTime() < r.toTime();
|
||||
case QVariant::DateTime:
|
||||
return l.toDateTime() < r.toDateTime();
|
||||
case QVariant::String:
|
||||
default:
|
||||
return l.toString().compare(r.toString()) < 0;
|
||||
}
|
||||
return QAbstractItemModelPrivate::isVariantLessThan(l, r);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
Loading…
Reference in New Issue
Block a user