Skip to content
Permalink
Browse files

fix crash when removing category

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12146 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Nov 16, 2009
1 parent 080f12d commit b3385bbc0d3c7f17430acc9cccb1f7f856e753a1
Showing with 79 additions and 85 deletions.
  1. +79 −85 src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
@@ -16,50 +16,50 @@
#include <QStandardItem>


QgsRendererV2Widget* QgsCategorizedSymbolRendererV2Widget::create(QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer)
QgsRendererV2Widget* QgsCategorizedSymbolRendererV2Widget::create( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
{
return new QgsCategorizedSymbolRendererV2Widget(layer, style, renderer);
return new QgsCategorizedSymbolRendererV2Widget( layer, style, renderer );
}

QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget(QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer)
: QgsRendererV2Widget(layer, style)
QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
: QgsRendererV2Widget( layer, style )
{

// try to recognize the previous renderer
// (null renderer means "no previous renderer")
if (!renderer || renderer->type() != "categorizedSymbol")
if ( !renderer || renderer->type() != "categorizedSymbol" )
{
// we're not going to use it - so let's delete the renderer
delete renderer;

mRenderer = new QgsCategorizedSymbolRendererV2("", QgsCategoryList());
mRenderer = new QgsCategorizedSymbolRendererV2( "", QgsCategoryList() );
}
else
{
mRenderer = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
mRenderer = static_cast<QgsCategorizedSymbolRendererV2*>( renderer );
}

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

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

mCategorizedSymbol = QgsSymbolV2::defaultSymbol(mLayer->geometryType());
mCategorizedSymbol = QgsSymbolV2::defaultSymbol( mLayer->geometryType() );

connect(cboCategorizedColumn, SIGNAL(currentIndexChanged(int)), this, SLOT(categoryColumnChanged()));
connect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) );

connect(viewCategories, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(categoriesDoubleClicked(const QModelIndex &)));
connect( viewCategories, SIGNAL( doubleClicked( const QModelIndex & ) ), this, SLOT( categoriesDoubleClicked( const QModelIndex & ) ) );

connect(btnChangeCategorizedSymbol, SIGNAL(clicked()), this, SLOT(changeCategorizedSymbol()));
connect(btnAddCategories, SIGNAL(clicked()), this, SLOT(addCategories()));
connect(btnDeleteCategory, SIGNAL(clicked()), this, SLOT(deleteCategory()));
connect(btnDeleteAllCategories, SIGNAL(clicked()), this, SLOT(deleteAllCategories()));
connect( btnChangeCategorizedSymbol, SIGNAL( clicked() ), this, SLOT( changeCategorizedSymbol() ) );
connect( btnAddCategories, SIGNAL( clicked() ), this, SLOT( addCategories() ) );
connect( btnDeleteCategory, SIGNAL( clicked() ), this, SLOT( deleteCategory() ) );
connect( btnDeleteAllCategories, SIGNAL( clicked() ), this, SLOT( deleteAllCategories() ) );

// update GUI from renderer
updateUiFromRenderer();
@@ -77,30 +77,30 @@ void QgsCategorizedSymbolRendererV2Widget::updateUiFromRenderer()
populateCategories();

// set column
disconnect(cboCategorizedColumn, SIGNAL(currentIndexChanged(int)), this, SLOT(categoryColumnChanged()));
disconnect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) );
QString attrName = mRenderer->classAttribute();
int idx = cboCategorizedColumn->findText(attrName, Qt::MatchExactly);
cboCategorizedColumn->setCurrentIndex(idx >= 0 ? idx : 0);
connect(cboCategorizedColumn, SIGNAL(currentIndexChanged(int)), this, SLOT(categoryColumnChanged()));
int idx = cboCategorizedColumn->findText( attrName, Qt::MatchExactly );
cboCategorizedColumn->setCurrentIndex( idx >= 0 ? idx : 0 );
connect( cboCategorizedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( categoryColumnChanged() ) );

// set source symbol
if (mRenderer->sourceSymbol())
if ( mRenderer->sourceSymbol() )
{
delete mCategorizedSymbol;
mCategorizedSymbol = mRenderer->sourceSymbol()->clone();
updateCategorizedSymbolIcon();
}

// set source color ramp
if (mRenderer->sourceColorRamp())
if ( mRenderer->sourceColorRamp() )
{
QSize rampIconSize(50,16);
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon(mRenderer->sourceColorRamp(), rampIconSize);
if (cboCategorizedColorRamp->itemText(0) == "[source]")
cboCategorizedColorRamp->setItemIcon(0, icon);
QSize rampIconSize( 50, 16 );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( mRenderer->sourceColorRamp(), rampIconSize );
if ( cboCategorizedColorRamp->itemText( 0 ) == "[source]" )
cboCategorizedColorRamp->setItemIcon( 0, icon );
else
cboCategorizedColorRamp->insertItem(0, icon, "[source]");
cboCategorizedColorRamp->setCurrentIndex(0);
cboCategorizedColorRamp->insertItem( 0, icon, "[source]" );
cboCategorizedColorRamp->setCurrentIndex( 0 );
}

}
@@ -112,47 +112,47 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2Widget::renderer()

void QgsCategorizedSymbolRendererV2Widget::changeCategorizedSymbol()
{
QgsSymbolV2SelectorDialog dlg(mCategorizedSymbol, mStyle, this);
if (!dlg.exec())
QgsSymbolV2SelectorDialog dlg( mCategorizedSymbol, mStyle, this );
if ( !dlg.exec() )
return;

updateCategorizedSymbolIcon();
}

void QgsCategorizedSymbolRendererV2Widget::updateCategorizedSymbolIcon()
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon(mCategorizedSymbol, btnChangeCategorizedSymbol->iconSize());
btnChangeCategorizedSymbol->setIcon(icon);
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mCategorizedSymbol, btnChangeCategorizedSymbol->iconSize() );
btnChangeCategorizedSymbol->setIcon( icon );
}


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

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

QSize iconSize(16,16);
QSize iconSize( 16, 16 );

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

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

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

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);
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());
list << item << new QStandardItem( cat.label() );
m->appendRow( list );
}
}
@@ -162,23 +162,23 @@ void QgsCategorizedSymbolRendererV2Widget::populateColumns()
cboCategorizedColumn->clear();
const QgsFieldMap& flds = mLayer->pendingFields();
QgsFieldMap::ConstIterator it = flds.begin();
for ( ; it != flds.end(); ++it)
for ( ; it != flds.end(); ++it )
{
cboCategorizedColumn->addItem(it->name());
cboCategorizedColumn->addItem( it->name() );
}
}

void QgsCategorizedSymbolRendererV2Widget::populateColorRamps()
{
QSize rampIconSize(50,16);
cboCategorizedColorRamp->setIconSize(rampIconSize);
QSize rampIconSize( 50, 16 );
cboCategorizedColorRamp->setIconSize( rampIconSize );

QStringList rampNames = mStyle->colorRampNames();
for (QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it)
for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
{
QgsVectorColorRampV2* ramp = mStyle->colorRamp(*it);
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon(ramp, rampIconSize);
cboCategorizedColorRamp->addItem(icon, *it);
QgsVectorColorRampV2* ramp = mStyle->colorRamp( *it );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize );
cboCategorizedColorRamp->addItem( icon, *it );
delete ramp;
}
}
@@ -189,75 +189,75 @@ void QgsCategorizedSymbolRendererV2Widget::categoryColumnChanged()
deleteAllCategories();
}

void QgsCategorizedSymbolRendererV2Widget::categoriesDoubleClicked(const QModelIndex & idx)
void QgsCategorizedSymbolRendererV2Widget::categoriesDoubleClicked( const QModelIndex & idx )
{
if (idx.isValid() && idx.column() == 0)
if ( idx.isValid() && idx.column() == 0 )
changeCategorySymbol();
}

void QgsCategorizedSymbolRendererV2Widget::changeCategorySymbol()
{
QVariant k = currentCategory();
if (!k.isValid())
if ( !k.isValid() )
return;

int catIdx = mRenderer->categoryIndexForValue(k);
int catIdx = mRenderer->categoryIndexForValue( k );
QgsSymbolV2* newSymbol = mRenderer->categories()[catIdx].symbol()->clone();

QgsSymbolV2SelectorDialog dlg(newSymbol, mStyle, this);
if (!dlg.exec())
QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, this );
if ( !dlg.exec() )
{
delete newSymbol;
return;
}

mRenderer->updateCategorySymbol(catIdx, newSymbol);
mRenderer->updateCategorySymbol( catIdx, newSymbol );

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
// TODO: sortVariantList(values);

int num = values.count();

for (int i = 0; i < num; i++)
for ( int i = 0; i < num; i++ )
{
QVariant value = values[i];
double x = i / (double) num;
double x = i / ( double ) num;
QgsSymbolV2* newSymbol = symbol->clone();
newSymbol->setColor( ramp->color(x) );
newSymbol->setColor( ramp->color( x ) );

cats.append( QgsRendererCategoryV2(value, newSymbol, value.toString()) );
cats.append( QgsRendererCategoryV2( value, newSymbol, value.toString() ) );
}

}

void QgsCategorizedSymbolRendererV2Widget::addCategories()
{
QString attrName = cboCategorizedColumn->currentText();
int idx = QgsFeatureRendererV2::fieldNameIndex(mLayer->pendingFields(), attrName);
int idx = QgsFeatureRendererV2::fieldNameIndex( mLayer->pendingFields(), attrName );
QList<QVariant> unique_vals;
mLayer->dataProvider()->uniqueValues(idx, unique_vals);
mLayer->dataProvider()->uniqueValues( idx, unique_vals );

//DlgAddCategories dlg(mStyle, createDefaultSymbol(), unique_vals, this);
//if (!dlg.exec())
// return;

QgsVectorColorRampV2* ramp = NULL;
QString rampName = cboCategorizedColorRamp->currentText();
if (rampName == "[source]")
if ( rampName == "[source]" )
ramp = mRenderer->sourceColorRamp()->clone();
else
ramp = mStyle->colorRamp( rampName );

QgsCategoryList cats;
_createCategories(cats, unique_vals, mCategorizedSymbol, ramp );
_createCategories( cats, unique_vals, mCategorizedSymbol, ramp );

// TODO: if not all categories are desired, delete some!
/*
@@ -275,47 +275,41 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()

// recreate renderer
delete mRenderer;
mRenderer = new QgsCategorizedSymbolRendererV2(attrName, cats);
mRenderer->setSourceSymbol(mCategorizedSymbol->clone());
mRenderer->setSourceColorRamp(ramp->clone());
mRenderer = new QgsCategorizedSymbolRendererV2( attrName, cats );
mRenderer->setSourceSymbol( mCategorizedSymbol->clone() );
mRenderer->setSourceColorRamp( ramp->clone() );

populateCategories();
}

int QgsCategorizedSymbolRendererV2Widget::currentCategoryRow()
{
QModelIndex idx = viewCategories->selectionModel()->currentIndex();
if (!idx.isValid())
if ( !idx.isValid() )
return -1;
return idx.row();
}

QVariant QgsCategorizedSymbolRendererV2Widget::currentCategory()
{
int row = currentCategoryRow();
if (row == -1)
if ( row == -1 )
return QVariant();
QStandardItemModel* m = qobject_cast<QStandardItemModel*>(viewCategories->model());
return m->item(row)->data();
QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewCategories->model() );
return m->item( row )->data();
}

void QgsCategorizedSymbolRendererV2Widget::deleteCategory()
{
QVariant k = currentCategory();
if (!k.isValid())
if ( !k.isValid() )
return;

const QgsCategoryList& cats = mRenderer->categories();
QgsCategoryList::ConstIterator it = cats.begin();
int idx = mRenderer->categoryIndexForValue( k );
if ( idx < 0 )
return;

for ( ; it != cats.end(); ++it)
{
if (k.toString() == it->value().toString())
{
int idx = mRenderer->categoryIndexForValue(k);
mRenderer->deleteCategory(idx);
}
}
mRenderer->deleteCategory( idx );

populateCategories();
}

0 comments on commit b3385bb

Please sign in to comment.