From 8b7d76e79a13b603ae25de014efc0c4ede26ea52 Mon Sep 17 00:00:00 2001 From: Jaka Kranjc Date: Mon, 22 Aug 2011 12:21:49 +0200 Subject: [PATCH] symbology-ng: sort the category items when classifying them #4206 --- .../symbology-ng/qgssymbollayerv2utils.cpp | 44 +++++++++++++++++++ src/core/symbology-ng/qgssymbollayerv2utils.h | 4 ++ .../qgscategorizedsymbolrendererv2widget.cpp | 3 +- .../qgscategorizedsymbolrendererv2widget.h | 1 + 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.cpp b/src/core/symbology-ng/qgssymbollayerv2utils.cpp index 786e7a92dacd..b4d493e2937e 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.cpp +++ b/src/core/symbology-ng/qgssymbollayerv2utils.cpp @@ -765,3 +765,47 @@ void QgsSymbolLayerV2Utils::multiplyImageOpacity( QImage* image, qreal alpha ) } } } + +static bool _QVariantLessThan( const QVariant& lhs, const QVariant& rhs ) +{ + switch( lhs.type() ) + { + case QVariant::Int: + return lhs.toInt() < rhs.toInt(); + case QVariant::UInt: + return lhs.toUInt() < rhs.toUInt(); + case QVariant::LongLong: + return lhs.toLongLong() < rhs.toLongLong(); + case QVariant::ULongLong: + return lhs.toULongLong() < rhs.toULongLong(); + case QVariant::Double: + return lhs.toDouble() < rhs.toDouble(); + case QVariant::Char: + return lhs.toChar() < rhs.toChar(); + case QVariant::Date: + return lhs.toDate() < rhs.toDate(); + case QVariant::Time: + return lhs.toTime() < rhs.toTime(); + case QVariant::DateTime: + return lhs.toDateTime() < rhs.toDateTime(); + default: + return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0; + } +} + +static bool _QVariantGreaterThan( const QVariant& lhs, const QVariant& rhs ) +{ + return ! _QVariantLessThan( lhs, rhs); +} + +void QgsSymbolLayerV2Utils::sortVariantList( QList& list, Qt::SortOrder order ) +{ + if (order == Qt::AscendingOrder) + { + qSort( list.begin(), list.end(), _QVariantLessThan ); + } + else // Qt::DescendingOrder + { + qSort( list.begin(), list.end(), _QVariantGreaterThan ); + } +} diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.h b/src/core/symbology-ng/qgssymbollayerv2utils.h index b6b88705608f..01c313791087 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.h +++ b/src/core/symbology-ng/qgssymbollayerv2utils.h @@ -5,6 +5,7 @@ #include #include +#include #include "qgssymbolv2.h" class QgsSymbolV2; @@ -81,6 +82,9 @@ class CORE_EXPORT QgsSymbolLayerV2Utils /**Multiplies opacity of image pixel values with a (global) transparency value*/ static void multiplyImageOpacity( QImage* image, qreal alpha ); + + /**Sorts the passed list in requested order*/ + static void sortVariantList( QList& list, Qt::SortOrder order ); }; class QPolygonF; diff --git a/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp b/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp index f4a97926538f..940b785bf7c8 100644 --- a/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp +++ b/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp @@ -222,7 +222,8 @@ void QgsCategorizedSymbolRendererV2Widget::changeCategorySymbol() static void _createCategories( QgsCategoryList& cats, QList& values, QgsSymbolV2* symbol, QgsVectorColorRampV2* ramp ) { // sort the categories first - // TODO: sortVariantList(values); + //TODO: make the order configurable? + QgsSymbolLayerV2Utils::sortVariantList( values, Qt::DescendingOrder ); int num = values.count(); diff --git a/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h b/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h index 06e6cbfe8b6c..d663f6d3e8dc 100644 --- a/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h +++ b/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h @@ -1,6 +1,7 @@ #ifndef QGSCATEGORIZEDSYMBOLRENDERERV2WIDGET_H #define QGSCATEGORIZEDSYMBOLRENDERERV2WIDGET_H +#include "qgscategorizedsymbolrendererv2.h" #include "qgsrendererv2widget.h" #include