Skip to content

Commit

Permalink
[FEATURE] manual adding of categories in symbology-ng. Patch contribu…
Browse files Browse the repository at this point in the history
…ted by Lynx, thanks!

+ added missing python bindings and fixed some issues.


git-svn-id: http://svn.osgeo.org/qgis/trunk@13601 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 30, 2010
1 parent 712b108 commit 03216df
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 28 deletions.
3 changes: 3 additions & 0 deletions python/core/symbology-ng-core.sip
Expand Up @@ -181,6 +181,7 @@ public:
QgsSymbolV2* symbol(); QgsSymbolV2* symbol();
QString label() const; QString label() const;


void setValue( const QVariant &value );
void setSymbol(QgsSymbolV2* s /Transfer/); void setSymbol(QgsSymbolV2* s /Transfer/);
void setLabel(QString label); void setLabel(QString label);


Expand Down Expand Up @@ -220,9 +221,11 @@ public:
//! return index of category with specified value (-1 if not found) //! return index of category with specified value (-1 if not found)
int categoryIndexForValue(QVariant val); int categoryIndexForValue(QVariant val);


bool updateCategoryValue( int catIndex, const QVariant &value );
bool updateCategorySymbol(int catIndex, QgsSymbolV2* symbol /Transfer/); bool updateCategorySymbol(int catIndex, QgsSymbolV2* symbol /Transfer/);
bool updateCategoryLabel(int catIndex, QString label); bool updateCategoryLabel(int catIndex, QString label);


void addCategory( const QgsRendererCategoryV2 &category );
bool deleteCategory(int catIndex); bool deleteCategory(int catIndex);
void deleteAllCategories(); void deleteAllCategories();


Expand Down
27 changes: 26 additions & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Expand Up @@ -45,6 +45,11 @@ QString QgsRendererCategoryV2::label() const
return mLabel; return mLabel;
} }


void QgsRendererCategoryV2::setValue( const QVariant &value )
{
mValue = value;
}

void QgsRendererCategoryV2::setSymbol( QgsSymbolV2* s ) void QgsRendererCategoryV2::setSymbol( QgsSymbolV2* s )
{ {
if ( mSymbol == s ) if ( mSymbol == s )
Expand All @@ -53,7 +58,7 @@ void QgsRendererCategoryV2::setSymbol( QgsSymbolV2* s )
mSymbol = s; mSymbol = s;
} }


void QgsRendererCategoryV2::setLabel( QString label ) void QgsRendererCategoryV2::setLabel( const QString &label )
{ {
mLabel = label; mLabel = label;
} }
Expand Down Expand Up @@ -143,6 +148,14 @@ int QgsCategorizedSymbolRendererV2::categoryIndexForValue( QVariant val )
return -1; return -1;
} }


bool QgsCategorizedSymbolRendererV2::updateCategoryValue( int catIndex, const QVariant &value )
{
if ( catIndex < 0 || catIndex >= mCategories.size() )
return false;
mCategories[catIndex].setValue( value );
return true;
}

bool QgsCategorizedSymbolRendererV2::updateCategorySymbol( int catIndex, QgsSymbolV2* symbol ) bool QgsCategorizedSymbolRendererV2::updateCategorySymbol( int catIndex, QgsSymbolV2* symbol )
{ {
if ( catIndex < 0 || catIndex >= mCategories.size() ) if ( catIndex < 0 || catIndex >= mCategories.size() )
Expand All @@ -159,6 +172,18 @@ bool QgsCategorizedSymbolRendererV2::updateCategoryLabel( int catIndex, QString
return true; return true;
} }


void QgsCategorizedSymbolRendererV2::addCategory( const QgsRendererCategoryV2 &cat )
{
if ( cat.symbol() == NULL )
{
QgsDebugMsg( "invalid symbol in a category! ignoring..." );
}
else
{
mCategories.append( cat );
}
}

bool QgsCategorizedSymbolRendererV2::deleteCategory( int catIndex ) bool QgsCategorizedSymbolRendererV2::deleteCategory( int catIndex )
{ {
if ( catIndex < 0 || catIndex >= mCategories.size() ) if ( catIndex < 0 || catIndex >= mCategories.size() )
Expand Down
5 changes: 4 additions & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.h
Expand Up @@ -24,8 +24,9 @@ class CORE_EXPORT QgsRendererCategoryV2
QgsSymbolV2* symbol() const; QgsSymbolV2* symbol() const;
QString label() const; QString label() const;


void setValue( const QVariant &value );
void setSymbol( QgsSymbolV2* s ); void setSymbol( QgsSymbolV2* s );
void setLabel( QString label ); void setLabel( const QString &label );


// debugging // debugging
QString dump(); QString dump();
Expand Down Expand Up @@ -65,9 +66,11 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! return index of category with specified value (-1 if not found) //! return index of category with specified value (-1 if not found)
int categoryIndexForValue( QVariant val ); int categoryIndexForValue( QVariant val );


bool updateCategoryValue( int catIndex, const QVariant &value );
bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol ); bool updateCategorySymbol( int catIndex, QgsSymbolV2* symbol );
bool updateCategoryLabel( int catIndex, QString label ); bool updateCategoryLabel( int catIndex, QString label );


void addCategory( const QgsRendererCategoryV2 &category );
bool deleteCategory( int catIndex ); bool deleteCategory( int catIndex );
void deleteAllCategories(); void deleteAllCategories();


Expand Down
114 changes: 90 additions & 24 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Expand Up @@ -40,14 +40,17 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
mRenderer = static_cast<QgsCategorizedSymbolRendererV2*>( renderer ); mRenderer = static_cast<QgsCategorizedSymbolRendererV2*>( renderer );
} }


QString attrName = mRenderer->classAttribute();
mOldClassificationAttribute = attrName;

// setup user interface // setup user interface
setupUi( this ); setupUi( this );


populateColumns(); populateColumns();
populateColorRamps(); populateColorRamps();
QStandardItemModel* m = new QStandardItemModel( this ); QStandardItemModel* m = new QStandardItemModel( this );
QStringList labels; QStringList labels;
labels << "Value" << "Label"; labels << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
m->setHorizontalHeaderLabels( labels ); m->setHorizontalHeaderLabels( labels );
viewCategories->setModel( m ); viewCategories->setModel( m );


Expand All @@ -61,6 +64,7 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
connect( btnAddCategories, SIGNAL( clicked() ), this, SLOT( addCategories() ) ); connect( btnAddCategories, SIGNAL( clicked() ), this, SLOT( addCategories() ) );
connect( btnDeleteCategory, SIGNAL( clicked() ), this, SLOT( deleteCategory() ) ); connect( btnDeleteCategory, SIGNAL( clicked() ), this, SLOT( deleteCategory() ) );
connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) ); connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) );
connect( btnAddCategory, SIGNAL( clicked() ), this, SLOT( addCategory() ) );
connect( m, SIGNAL( itemChanged( QStandardItem * ) ), this, SLOT( changeCurrentValue( QStandardItem * ) ) ); connect( m, SIGNAL( itemChanged( QStandardItem * ) ), this, SLOT( changeCurrentValue( QStandardItem * ) ) );


// update GUI from renderer // update GUI from renderer
Expand All @@ -81,6 +85,7 @@ void QgsCategorizedSymbolRendererV2Widget::updateUiFromRenderer()
// set column // set column
disconnect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) ); disconnect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) );
QString attrName = mRenderer->classAttribute(); QString attrName = mRenderer->classAttribute();
mOldClassificationAttribute = attrName;
int idx = cboCategorizedColumn->findText( attrName, Qt::MatchExactly ); int idx = cboCategorizedColumn->findText( attrName, Qt::MatchExactly );
cboCategorizedColumn->setCurrentIndex( idx >= 0 ? idx : 0 ); cboCategorizedColumn->setCurrentIndex( idx >= 0 ? idx : 0 );
connect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) ); connect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) );
Expand Down Expand Up @@ -127,36 +132,28 @@ void QgsCategorizedSymbolRendererV2Widget::updateCategorizedSymbolIcon()
btnChangeCategorizedSymbol->setIcon( icon ); btnChangeCategorizedSymbol->setIcon( icon );
} }



void QgsCategorizedSymbolRendererV2Widget::populateCategories() void QgsCategorizedSymbolRendererV2Widget::populateCategories()
{ {
QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewCategories->model() ); QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewCategories->model() );
m->clear(); m->clear();


QStringList labels; QStringList labels;
labels << "Value" << "Label"; labels << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
m->setHorizontalHeaderLabels( labels ); m->setHorizontalHeaderLabels( labels );


QSize iconSize( 16, 16 );

int i, count = mRenderer->categories().count(); int i, count = mRenderer->categories().count();


// TODO: sort?? utils.sortVariantList(keys); // TODO: sort?? utils.sortVariantList(keys);


for ( i = 0; i < count; i++ ) for ( i = 0; i < count; i++ )
{ {
const QgsRendererCategoryV2& cat = mRenderer->categories()[i]; const QgsRendererCategoryV2 &cat = mRenderer->categories()[i];
QVariant k = cat.value(); addCategory( cat );

QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( cat.symbol(), iconSize );
QStandardItem* item = new QStandardItem( icon, k.toString() );
item->setData( k ); // set attribute value as user role
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );

QList<QStandardItem *> list;
list << item << new QStandardItem( cat.label() );
m->appendRow( list );
} }

viewCategories->resizeColumnToContents( 0 );
viewCategories->resizeColumnToContents( 1 );
viewCategories->resizeColumnToContents( 2 );
} }


void QgsCategorizedSymbolRendererV2Widget::populateColumns() void QgsCategorizedSymbolRendererV2Widget::populateColumns()
Expand Down Expand Up @@ -185,6 +182,21 @@ void QgsCategorizedSymbolRendererV2Widget::populateColorRamps()
} }
} }


void QgsCategorizedSymbolRendererV2Widget::addCategory( const QgsRendererCategoryV2 &cat )
{
QSize iconSize( 16, 16 );

QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( cat.symbol(), iconSize );
QStandardItem *symbolItem = new QStandardItem( icon, "" );
symbolItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );

QStandardItem *valueItem = new QStandardItem( cat.value().toString() );
valueItem->setData( cat.value() ); // set attribute value as user role

QList<QStandardItem *> list;
list << symbolItem << valueItem << new QStandardItem( cat.label() );
qobject_cast<QStandardItemModel *>( viewCategories->model() )->appendRow( list );
}


void QgsCategorizedSymbolRendererV2Widget::categoryColumnChanged() void QgsCategorizedSymbolRendererV2Widget::categoryColumnChanged()
{ {
Expand Down Expand Up @@ -218,9 +230,6 @@ void QgsCategorizedSymbolRendererV2Widget::changeCategorySymbol()
populateCategories(); populateCategories();
} }





static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, QgsSymbolV2* symbol, QgsVectorColorRampV2* ramp ) static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, QgsSymbolV2* symbol, QgsVectorColorRampV2* ramp )
{ {
// sort the categories first // sort the categories first
Expand Down Expand Up @@ -270,9 +279,47 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
QgsCategoryList cats; QgsCategoryList cats;
_createCategories( cats, unique_vals, mCategorizedSymbol, ramp ); _createCategories( cats, unique_vals, mCategorizedSymbol, ramp );


bool deleteExisting = false;
if ( !mOldClassificationAttribute.isEmpty() &&
attrName != mOldClassificationAttribute &&
mRenderer->categories().count() > 0 )
{
int res = QMessageBox::question( this,
tr( "Confirm Delete" ),
tr( "The classification field was changed from '%1' to '%2'.\n"
"Should the existing classes be deleted before classification?" )
.arg( mOldClassificationAttribute ).arg( attrName ),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel );
if ( res == QMessageBox::Cancel )
return;
if ( res == QMessageBox::Yes )
deleteExisting = true;
}

mOldClassificationAttribute = attrName;

if ( !deleteExisting )
{
QgsCategoryList prevCats = mRenderer->categories();
for ( int i = 0; i < cats.size(); ++i )
{
bool contains = false;
QVariant value = cats.at( i ).value();
for ( int j = 0; j < prevCats.size() && !contains; ++j )
{
if ( prevCats.at( j ).value() == value )
contains = true;
}

if ( !contains )
prevCats.append( cats.at( i ) );
}
cats = prevCats;
}

// TODO: if not all categories are desired, delete some! // TODO: if not all categories are desired, delete some!
/* /*
if (not dlg.radAllCats.isChecked()) if (not dlg.readAllCats.isChecked())
{ {
cats2 = {} cats2 = {}
for item in dlg.listCategories.selectedItems(): for item in dlg.listCategories.selectedItems():
Expand Down Expand Up @@ -307,7 +354,7 @@ QVariant QgsCategorizedSymbolRendererV2Widget::currentCategory()
if ( row == -1 ) if ( row == -1 )
return QVariant(); return QVariant();
QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewCategories->model() ); QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewCategories->model() );
return m->item( row )->data(); return m->item( row, 1 )->data();
} }


void QgsCategorizedSymbolRendererV2Widget::deleteCategory() void QgsCategorizedSymbolRendererV2Widget::deleteCategory()
Expand All @@ -333,10 +380,29 @@ void QgsCategorizedSymbolRendererV2Widget::deleteAllCategories()


void QgsCategorizedSymbolRendererV2Widget::changeCurrentValue( QStandardItem * item ) void QgsCategorizedSymbolRendererV2Widget::changeCurrentValue( QStandardItem * item )
{ {
int idx = item->row();
QString newtext = item->text();
if ( item->column() == 1 ) if ( item->column() == 1 )
{ {
QString label = item->text(); QVariant value = newtext;
int idx = item->row(); // try to preserve variant type for this value
mRenderer->updateCategoryLabel( idx, label ); QVariant::Type t = item->data().type();
if ( t == QVariant::Int )
value = newtext.toInt();
else if ( t == QVariant::Double )
value = newtext.toDouble();
mRenderer->updateCategoryValue( idx, value );
}
else if ( item->column() == 2 )
{
mRenderer->updateCategoryLabel( idx, newtext );
} }
} }

void QgsCategorizedSymbolRendererV2Widget::addCategory()
{
QgsSymbolV2 *symbol = QgsSymbolV2::defaultSymbol( mLayer->geometryType() );
QgsRendererCategoryV2 cat( QString(), symbol, QString() );
addCategory( cat );
mRenderer->addCategory( cat );
}
9 changes: 9 additions & 0 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.h
Expand Up @@ -5,6 +5,7 @@
#include <QStandardItem> #include <QStandardItem>


class QgsCategorizedSymbolRendererV2; class QgsCategorizedSymbolRendererV2;
class QgsRendererCategoryV2;


#include "ui_qgscategorizedsymbolrendererv2widget.h" #include "ui_qgscategorizedsymbolrendererv2widget.h"


Expand All @@ -30,6 +31,9 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
void deleteAllCategories(); void deleteAllCategories();
void changeCurrentValue( QStandardItem * item ); void changeCurrentValue( QStandardItem * item );


protected slots:
void addCategory();

protected: protected:


void updateUiFromRenderer(); void updateUiFromRenderer();
Expand All @@ -44,6 +48,8 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg


void populateColorRamps(); void populateColorRamps();


void addCategory( const QgsRendererCategoryV2& cat );

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


Expand All @@ -56,6 +62,9 @@ class GUI_EXPORT QgsCategorizedSymbolRendererV2Widget : public QgsRendererV2Widg
QgsCategorizedSymbolRendererV2* mRenderer; QgsCategorizedSymbolRendererV2* mRenderer;


QgsSymbolV2* mCategorizedSymbol; QgsSymbolV2* mCategorizedSymbol;

private:
QString mOldClassificationAttribute;
}; };




Expand Down
11 changes: 9 additions & 2 deletions src/ui/qgscategorizedsymbolrendererv2widget.ui
Expand Up @@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>464</width> <width>599</width>
<height>316</height> <height>298</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -114,6 +114,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="btnAddCategory">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="btnDeleteCategory"> <widget class="QPushButton" name="btnDeleteCategory">
<property name="text"> <property name="text">
Expand Down

0 comments on commit 03216df

Please sign in to comment.