Use a template function instead of a macro
This commit is contained in:
parent
c366f722fb
commit
b628c2a26e
@ -17,7 +17,7 @@ void MultiSortFilterProxy::AddSortSpec(int role, Qt::SortOrder order) {
|
||||
bool MultiSortFilterProxy::lessThan(const QModelIndex& left,
|
||||
const QModelIndex& right) const {
|
||||
foreach (const SortSpec& spec, sorting_) {
|
||||
const int ret = compare(left.data(spec.first), right.data(spec.first));
|
||||
const int ret = Compare(left.data(spec.first), right.data(spec.first));
|
||||
|
||||
if (ret < 0) {
|
||||
return spec.second == Qt::AscendingOrder;
|
||||
@ -29,28 +29,31 @@ bool MultiSortFilterProxy::lessThan(const QModelIndex& left,
|
||||
return left.row() < right.row();
|
||||
}
|
||||
|
||||
int MultiSortFilterProxy::compare(const QVariant& left, const QVariant& right) const {
|
||||
template <typename T>
|
||||
static inline int DoCompare(T left, T right) {
|
||||
if (left < right)
|
||||
return -1;
|
||||
if (left > right)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MultiSortFilterProxy::Compare(const QVariant& left, const QVariant& right) const {
|
||||
// Copied from the QSortFilterProxyModel::lessThan implementation, but returns
|
||||
// -1, 0 or 1 instead of true or false.
|
||||
|
||||
#define docompare(left, right) \
|
||||
if (left < right) return -1; \
|
||||
if (left > right) return 1; \
|
||||
return 0;
|
||||
|
||||
switch (left.userType()) {
|
||||
case QVariant::Invalid:
|
||||
return (right.type() != QVariant::Invalid) ? -1 : 0;
|
||||
case QVariant::Int: docompare(left.toInt(), right.toInt());
|
||||
case QVariant::UInt: docompare(left.toUInt(), right.toUInt());
|
||||
case QVariant::LongLong: docompare(left.toLongLong(), right.toLongLong());
|
||||
case QVariant::ULongLong: docompare(left.toULongLong(), right.toULongLong());
|
||||
case QMetaType::Float: docompare(left.toFloat(), right.toFloat());
|
||||
case QVariant::Double: docompare(left.toDouble(), right.toDouble());
|
||||
case QVariant::Char: docompare(left.toChar(), right.toChar());
|
||||
case QVariant::Date: docompare(left.toDate(), right.toDate());
|
||||
case QVariant::Time: docompare(left.toTime(), right.toTime());
|
||||
case QVariant::DateTime: docompare(left.toDateTime(), right.toDateTime());
|
||||
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());
|
||||
case QVariant::String:
|
||||
default:
|
||||
if (isSortLocaleAware())
|
||||
@ -59,7 +62,5 @@ int MultiSortFilterProxy::compare(const QVariant& left, const QVariant& right) c
|
||||
return left.toString().compare(right.toString(), sortCaseSensitivity());
|
||||
}
|
||||
|
||||
#undef docompare
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ protected:
|
||||
bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
|
||||
|
||||
private:
|
||||
int compare(const QVariant& left, const QVariant& right) const;
|
||||
int Compare(const QVariant& left, const QVariant& right) const;
|
||||
|
||||
typedef QPair<int, Qt::SortOrder> SortSpec;
|
||||
QList<SortSpec> sorting_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user