From 2185b2f054ddae0fc67b8799b3561c57729f1cd7 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Tue, 19 Apr 2016 18:12:27 +0300 Subject: [PATCH] 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 Reviewed-by: Oswald Buddenhagen --- src/corelib/itemmodels/qabstractitemmodel.cpp | 38 +++++++++++++++++++ src/corelib/itemmodels/qabstractitemmodel_p.h | 2 + .../itemmodels/qsortfilterproxymodel.cpp | 35 +---------------- src/gui/itemmodels/qstandarditemmodel.cpp | 31 +-------------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index d645205155..54ea45782d 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include @@ -547,6 +548,43 @@ const QHash &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) { diff --git a/src/corelib/itemmodels/qabstractitemmodel_p.h b/src/corelib/itemmodels/qabstractitemmodel_p.h index 9ff40cd60e..7086ae730a 100644 --- a/src/corelib/itemmodels/qabstractitemmodel_p.h +++ b/src/corelib/itemmodels/qabstractitemmodel_p.h @@ -150,6 +150,8 @@ public: QHash roleNames; static const QHash &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); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 98202b71ae..2b684b855b 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -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); } /*! diff --git a/src/gui/itemmodels/qstandarditemmodel.cpp b/src/gui/itemmodels/qstandarditemmodel.cpp index 973e5c4333..3279880601 100644 --- a/src/gui/itemmodels/qstandarditemmodel.cpp +++ b/src/gui/itemmodels/qstandarditemmodel.cpp @@ -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); } /*!