diff options
author | Thierry Bastian <thierry.bastian@nokia.com> | 2009-08-17 10:51:37 (GMT) |
---|---|---|
committer | Thierry Bastian <thierry.bastian@nokia.com> | 2009-08-17 10:52:07 (GMT) |
commit | dc2771eb57702a0daf5164b0f9ad569c9fe0e5ba (patch) | |
tree | 7de557caf7a4d7bfb52aaf68572f81c0cdba01da | |
parent | 213e2c937b667dba7e4996b0857ae5791c6d5fc8 (diff) | |
download | Qt-dc2771eb57702a0daf5164b0f9ad569c9fe0e5ba.zip Qt-dc2771eb57702a0daf5164b0f9ad569c9fe0e5ba.tar.gz Qt-dc2771eb57702a0daf5164b0f9ad569c9fe0e5ba.tar.bz2 |
Better handling of qreal with QVariant
-rw-r--r-- | src/corelib/kernel/qabstractitemmodel.cpp | 39 | ||||
-rw-r--r-- | src/corelib/kernel/qabstractitemmodel_p.h | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 2 | ||||
-rw-r--r-- | src/gui/itemviews/qlistwidget.cpp | 4 | ||||
-rw-r--r-- | src/gui/itemviews/qstyleditemdelegate.cpp | 2 | ||||
-rw-r--r-- | src/gui/itemviews/qtablewidget.cpp | 4 | ||||
-rw-r--r-- | src/gui/itemviews/qtreewidget.cpp | 4 | ||||
-rw-r--r-- | src/gui/text/qcssparser.cpp | 2 |
8 files changed, 33 insertions, 26 deletions
diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp index cfc961c..61b19a2 100644 --- a/src/corelib/kernel/qabstractitemmodel.cpp +++ b/src/corelib/kernel/qabstractitemmodel.cpp @@ -488,35 +488,48 @@ const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames() return *qDefaultRoleNames(); } -/*! - \internal - return true if \a value contains a numerical type - This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort. - We cannot rely on QVariant::canConvert because this would take strings as double - and then not sort strings correctly -*/ -bool QAbstractItemModelPrivate::canConvertToDouble(const QVariant &value) +static uint typeOfVariant(const QVariant &value) { + //return 0 for integer, 1 for floating point and 2 for other switch (value.userType()) { case QVariant::Bool: case QVariant::Int: case QVariant::UInt: case QVariant::LongLong: case QVariant::ULongLong: - case QVariant::Double: case QVariant::Char: - case QMetaType::Float: case QMetaType::Short: case QMetaType::UShort: case QMetaType::UChar: case QMetaType::ULong: case QMetaType::Long: - return true; + return 0; + case QVariant::Double: + case QMetaType::Float: + return 1; default: - return false; + return 2; + } +} + +/*! + \internal + return true if \a value contains a numerical type + + This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort. +*/ +bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2) +{ + switch(qMax(typeOfVariant(v1), typeOfVariant(v2))) + { + case 0: //integer type + return v1.toLongLong() < v2.toLongLong(); + case 1: //floating point + return v1.toReal() < v2.toReal(); + default: + return v1.toString() < v2.toString(); } - return false; } void QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexData *data) diff --git a/src/corelib/kernel/qabstractitemmodel_p.h b/src/corelib/kernel/qabstractitemmodel_p.h index 76c2d70..e81e627 100644 --- a/src/corelib/kernel/qabstractitemmodel_p.h +++ b/src/corelib/kernel/qabstractitemmodel_p.h @@ -89,7 +89,7 @@ public: void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last); void columnsRemoved(const QModelIndex &parent, int first, int last); static QAbstractItemModel *staticEmptyModel(); - static bool canConvertToDouble(const QVariant &value); + static bool variantLessThan(const QVariant &v1, const QVariant &v2); inline QModelIndex createIndex(int row, int column, void *data = 0) const { return q_func()->createIndex(row, column, data); diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 3c430eb..66c4176 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2776,7 +2776,7 @@ bool QVariant::cmp(const QVariant &v) const if (d.type != v2.d.type) { if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) { if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type)) - return qFuzzyCompare(toDouble(), v.toDouble()); + return qFuzzyCompare(toReal(), v.toReal()); else return toLongLong() == v.toLongLong(); } diff --git a/src/gui/itemviews/qlistwidget.cpp b/src/gui/itemviews/qlistwidget.cpp index 2792bbd..a1f8288 100644 --- a/src/gui/itemviews/qlistwidget.cpp +++ b/src/gui/itemviews/qlistwidget.cpp @@ -708,9 +708,7 @@ QVariant QListWidgetItem::data(int role) const bool QListWidgetItem::operator<(const QListWidgetItem &other) const { const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole); - if (QAbstractItemModelPrivate::canConvertToDouble(v1) && QAbstractItemModelPrivate::canConvertToDouble(v2)) - return v1.toDouble() < v2.toDouble(); - return v1.toString() < v2.toString(); + return QAbstractItemModelPrivate::variantLessThan(v1, v2); } #ifndef QT_NO_DATASTREAM diff --git a/src/gui/itemviews/qstyleditemdelegate.cpp b/src/gui/itemviews/qstyleditemdelegate.cpp index f7c7d12..a8ea218 100644 --- a/src/gui/itemviews/qstyleditemdelegate.cpp +++ b/src/gui/itemviews/qstyleditemdelegate.cpp @@ -270,7 +270,7 @@ QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale& l switch (value.userType()) { case QMetaType::Float: case QVariant::Double: - text = locale.toString(value.toDouble()); + text = locale.toString(value.toReal()); break; case QVariant::Int: case QVariant::LongLong: diff --git a/src/gui/itemviews/qtablewidget.cpp b/src/gui/itemviews/qtablewidget.cpp index fea81e5..de8ebde 100644 --- a/src/gui/itemviews/qtablewidget.cpp +++ b/src/gui/itemviews/qtablewidget.cpp @@ -1392,9 +1392,7 @@ QVariant QTableWidgetItem::data(int role) const bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const { const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole); - if (QAbstractItemModelPrivate::canConvertToDouble(v1) && QAbstractItemModelPrivate::canConvertToDouble(v2)) - return v1.toDouble() < v2.toDouble(); - return v1.toString() < v2.toString(); + return QAbstractItemModelPrivate::variantLessThan(v1, v2); } #ifndef QT_NO_DATASTREAM diff --git a/src/gui/itemviews/qtreewidget.cpp b/src/gui/itemviews/qtreewidget.cpp index 1ab69df..f48e393 100644 --- a/src/gui/itemviews/qtreewidget.cpp +++ b/src/gui/itemviews/qtreewidget.cpp @@ -1790,9 +1790,7 @@ bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const int column = view ? view->sortColumn() : 0; const QVariant v1 = data(column, Qt::DisplayRole); const QVariant v2 = other.data(column, Qt::DisplayRole); - if (QAbstractItemModelPrivate::canConvertToDouble(v1) && QAbstractItemModelPrivate::canConvertToDouble(v2)) - return v1.toDouble() < v2.toDouble(); - return v1.toString() < v2.toString(); + return QAbstractItemModelPrivate::variantLessThan(v1, v2); } #ifndef QT_NO_DATASTREAM diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 181ec7e..f11104f 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -793,7 +793,7 @@ static BrushData parseBrushValue(const Value &v, const QPalette &pal) if (attr.compare(QLatin1String("spread"), Qt::CaseInsensitive) == 0) { spread = spreads.indexOf(value.variant.toString()); } else { - vars[attr] = value.variant.toString().toDouble(); + vars[attr] = value.variant.toReal(); } } parser.skipSpace(); |