void QgsClassificationWidgetWrapper::initWidget( QWidget *editor ) { mComboBox = qobject_cast<QComboBox *>( editor ); if ( mComboBox ) { const QgsCategorizedSymbolRenderer *csr = dynamic_cast<const QgsCategorizedSymbolRenderer *>( layer()->renderer() ); if ( csr ) { const QgsCategoryList categories = csr->categories(); for ( int i = 0; i < categories.size(); i++ ) { QString label = categories[i].label(); QString value = categories[i].value().toString(); if ( label.isEmpty() ) label = value; mComboBox->addItem( label, value ); } } connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) ); } }
void QgsClassificationWidget::initWidget( QWidget* editor ) { mComboBox = qobject_cast<QComboBox*>( editor ); if ( mComboBox ) { const QgsCategorizedSymbolRendererV2 *csr = dynamic_cast<const QgsCategorizedSymbolRendererV2 *>( layer()->rendererV2() ); if ( csr ) { const QgsCategoryList categories = csr->categories(); for ( int i = 0; i < categories.size(); i++ ) { QString label = categories[i].label(); QString value = categories[i].value().toString(); if ( label.isEmpty() ) label = value; mComboBox->addItem( label, value ); } } connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) ); } }
void QgsCategorizedSymbolRendererV2Widget::addCategories() { QString attrName = cboCategorizedColumn->currentText(); int idx = mLayer->fieldNameIndex( attrName ); QList<QVariant> unique_vals; mLayer->uniqueValues( idx, unique_vals ); //DlgAddCategories dlg(mStyle, createDefaultSymbol(), unique_vals, this); //if (!dlg.exec()) // return; QgsVectorColorRampV2* ramp = cboCategorizedColorRamp->currentColorRamp(); if ( ramp == NULL ) { if ( cboCategorizedColorRamp->count() == 0 ) QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) ); else QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) ); return; } QgsCategoryList cats; _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; } deleteExisting = ( res == QMessageBox::Yes ); } 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; break; } } if ( !contains ) prevCats.append( cats.at( i ) ); } cats = prevCats; } mOldClassificationAttribute = attrName; // TODO: if not all categories are desired, delete some! /* if (not dlg.readAllCats.isChecked()) { cats2 = {} for item in dlg.listCategories.selectedItems(): for k,c in cats.iteritems(): if item.text() == k.toString(): break cats2[k] = c cats = cats2 } */ // recreate renderer delete mRenderer; mRenderer = new QgsCategorizedSymbolRendererV2( attrName, cats ); mRenderer->setSourceSymbol( mCategorizedSymbol->clone() ); mRenderer->setSourceColorRamp( ramp->clone() ); populateCategories(); }