Skip to content
Permalink
Browse files

Categorized vector GUI DnD and sorting, fixes #2816

  • Loading branch information
blazek committed Nov 11, 2012
1 parent ae55e65 commit c4b74c9266d48bb864d4c424f87adc8fa940d9bf
@@ -16,10 +16,10 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
void changeCategorizedSymbol();
void categoryColumnChanged();
void categoriesDoubleClicked( const QModelIndex & idx );
void addCategory();
void addCategories();
void deleteCategory();
void deleteCategories();
void deleteAllCategories();
void changeCurrentValue( QStandardItem * item );

void rotationFieldChanged( QString fldName );
void sizeScaleFieldChanged( QString fldName );
@@ -39,19 +39,11 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
//! populate column combo
void populateColumns();

void addCategory( const QgsRendererCategoryV2& cat );

//! return row index for the currently selected category (-1 if on no selection)
int currentCategoryRow();

//! return key for the currently selected category
QVariant currentCategory();

void changeCategorySymbol();

QList<QgsSymbolV2*> selectedSymbols();
void refreshSymbolView();

protected slots:
void addCategory();
};
@@ -20,6 +20,9 @@
#include "qgsversion.h"
#endif
#include <QCoreApplication>
#include <QDate>
#include <QTime>
#include <QDateTime>
#include "qgsconfig.h"
#include "qgslogger.h"

@@ -140,3 +143,36 @@ void QgsFree( void *ptr )
{
free( ptr );
}

bool qgsVariantLessThan( 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;
}
}

bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
{
return ! qgsVariantLessThan( lhs, rhs );
}

@@ -21,6 +21,7 @@
#include <QEvent>
#include <QString>
#include <QMetaType>
#include <QVariant>
#include <stdlib.h>
#include <cfloat>
#include <cmath>
@@ -194,6 +195,10 @@ inline bool doubleNearSig( double a, double b, int significantDigits = 10 )
qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) ) ;
}

bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs );

bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs );

/** Allocates size bytes and returns a pointer to the allocated memory.
Works like C malloc() but prints debug message by QgsLogger if allocation fails.
@param size size in bytes
@@ -12,6 +12,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include <algorithm>

#include "qgscategorizedsymbolrendererv2.h"

@@ -27,21 +28,40 @@
#include <QDomElement>
#include <QSettings> // for legend

QgsRendererCategoryV2::QgsRendererCategoryV2()
: mValue(), mSymbol( 0 ), mLabel()
{
}

QgsRendererCategoryV2::QgsRendererCategoryV2( QVariant value, QgsSymbolV2* symbol, QString label )
: mValue( value ), mSymbol( symbol ), mLabel( label )
{
}

QgsRendererCategoryV2::QgsRendererCategoryV2( const QgsRendererCategoryV2& cat )
: mValue( cat.mValue ), mLabel( cat.mLabel )
: mValue( cat.mValue ), mSymbol( 0 ), mLabel( cat.mLabel )
{
mSymbol = cat.mSymbol->clone();
if ( cat.mSymbol )
{
mSymbol = cat.mSymbol->clone();
}
}


QgsRendererCategoryV2::~QgsRendererCategoryV2()
{
delete mSymbol;
if ( mSymbol ) delete mSymbol;
}

QgsRendererCategoryV2& QgsRendererCategoryV2::operator=( const QgsRendererCategoryV2 & cat )
{
mValue = cat.mValue;
mLabel = cat.mLabel;
mSymbol = 0;
if ( cat.mSymbol )
{
mSymbol = cat.mSymbol->clone();
}
return *this;
}

QVariant QgsRendererCategoryV2::value() const
@@ -293,6 +313,55 @@ void QgsCategorizedSymbolRendererV2::deleteAllCategories()
mCategories.clear();
}

void QgsCategorizedSymbolRendererV2::moveCategory( int from, int to )
{
if ( from < 0 || from >= mCategories.size() || to < 0 || to >= mCategories.size() ) return;
mCategories.move( from, to );
}

bool valueLessThan( const QgsRendererCategoryV2 &c1, const QgsRendererCategoryV2 &c2 )
{
return qgsVariantLessThan( c1.value(), c2.value() );
}
bool valueGreaterThan( const QgsRendererCategoryV2 &c1, const QgsRendererCategoryV2 &c2 )
{
return qgsVariantGreaterThan( c1.value(), c2.value() );
}

void QgsCategorizedSymbolRendererV2::sortByValue( Qt::SortOrder order )
{
if ( order == Qt::AscendingOrder )
{
qSort( mCategories.begin(), mCategories.end(), valueLessThan );
}
else
{
qSort( mCategories.begin(), mCategories.end(), valueGreaterThan );
}
}

bool labelLessThan( const QgsRendererCategoryV2 &c1, const QgsRendererCategoryV2 &c2 )
{
return QString::localeAwareCompare( c1.label(), c2.label() ) < 0;
}

bool labelGreaterThan( const QgsRendererCategoryV2 &c1, const QgsRendererCategoryV2 &c2 )
{
return !labelLessThan( c1, c2 );
}

void QgsCategorizedSymbolRendererV2::sortByLabel( Qt::SortOrder order )
{
if ( order == Qt::AscendingOrder )
{
qSort( mCategories.begin(), mCategories.end(), labelLessThan );
}
else
{
qSort( mCategories.begin(), mCategories.end(), labelGreaterThan );
}
}

void QgsCategorizedSymbolRendererV2::startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer )
{
// make sure that the hash table is up to date
@@ -27,15 +27,19 @@ class QgsVectorLayer;
class CORE_EXPORT QgsRendererCategoryV2
{
public:
QgsRendererCategoryV2( );

//! takes ownership of symbol
QgsRendererCategoryV2( QVariant value, QgsSymbolV2* symbol, QString label );

//! copy constructor
QgsRendererCategoryV2( const QgsRendererCategoryV2& cat );


~QgsRendererCategoryV2();

QgsRendererCategoryV2& operator=( const QgsRendererCategoryV2& cat );

QVariant value() const;
QgsSymbolV2* symbol() const;
QString label() const;
@@ -100,6 +104,12 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
bool deleteCategory( int catIndex );
void deleteAllCategories();

//! Moves the category at index position from to index position to.
void moveCategory( int from, int to );

void sortByValue( Qt::SortOrder order = Qt::AscendingOrder );
void sortByLabel( Qt::SortOrder order = Qt::AscendingOrder );

QString classAttribute() const { return mAttrName; }
void setClassAttribute( QString attr ) { mAttrName = attr; }

@@ -2528,6 +2528,7 @@ void QgsSymbolLayerV2Utils::multiplyImageOpacity( QImage* image, qreal alpha )
}
}

#if 0
static bool _QVariantLessThan( const QVariant& lhs, const QVariant& rhs )
{
switch ( lhs.type() )
@@ -2559,16 +2560,19 @@ static bool _QVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
{
return ! _QVariantLessThan( lhs, rhs );
}
#endif

void QgsSymbolLayerV2Utils::sortVariantList( QList<QVariant>& list, Qt::SortOrder order )
{
if ( order == Qt::AscendingOrder )
{
qSort( list.begin(), list.end(), _QVariantLessThan );
//qSort( list.begin(), list.end(), _QVariantLessThan );
qSort( list.begin(), list.end(), qgsVariantLessThan );
}
else // Qt::DescendingOrder
{
qSort( list.begin(), list.end(), _QVariantGreaterThan );
//qSort( list.begin(), list.end(), _QVariantGreaterThan );
qSort( list.begin(), list.end(), qgsVariantGreaterThan );
}
}

0 comments on commit c4b74c9

Please sign in to comment.
You can’t perform that action at this time.