Skip to content

Commit

Permalink
[FEATURE] Allow creation of color ramps in color ramp combo box.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@14421 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Oct 20, 2010
1 parent 8ee79eb commit 7f32157
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 167 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
SET(QGIS_GUI_SRCS

symbology-ng/qgsbrushstylecombobox.cpp
symbology-ng/qgscolorrampcombobox.cpp
symbology-ng/qgsdashspacedialog.cpp
symbology-ng/qgspenstylecombobox.cpp
symbology-ng/qgssymbollayerv2widget.cpp
Expand Down Expand Up @@ -73,6 +74,7 @@ symbology-ng/qgssymbolv2selectordialog.h
symbology-ng/qgsvectorgradientcolorrampv2dialog.h
symbology-ng/qgsvectorrandomcolorrampv2dialog.h
symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.h
symbology-ng/qgscolorrampcombobox.h
symbology-ng/characterwidget.h
symbology-ng/qgspenstylecombobox.h
symbology-ng/qgsbrushstylecombobox.h
Expand Down
93 changes: 35 additions & 58 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ QgsRendererV2Widget* QgsCategorizedSymbolRendererV2Widget::create( QgsVectorLaye
}

QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
: QgsRendererV2Widget( layer, style )
: 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;
Expand All @@ -48,7 +48,9 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
setupUi( this );

populateColumns();
populateColorRamps();

cboCategorizedColorRamp->populate( mStyle );

QStandardItemModel* m = new QStandardItemModel( this );
QStringList labels;
labels << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
Expand Down Expand Up @@ -100,23 +102,17 @@ void QgsCategorizedSymbolRendererV2Widget::updateUiFromRenderer()
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 );
else
cboCategorizedColorRamp->insertItem( 0, icon, "[source]" );
cboCategorizedColorRamp->setCurrentIndex( 0 );
cboCategorizedColorRamp->setSourceColorRamp( mRenderer->sourceColorRamp() );
}

}
Expand All @@ -129,7 +125,7 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2Widget::renderer()
void QgsCategorizedSymbolRendererV2Widget::changeCategorizedSymbol()
{
QgsSymbolV2SelectorDialog dlg( mCategorizedSymbol, mStyle, this );
if ( !dlg.exec() )
if( !dlg.exec() )
return;

updateCategorizedSymbolIcon();
Expand All @@ -154,7 +150,7 @@ void QgsCategorizedSymbolRendererV2Widget::populateCategories()

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

for ( i = 0; i < count; i++ )
for( i = 0; i < count; i++ )
{
const QgsRendererCategoryV2 &cat = mRenderer->categories()[i];
addCategory( cat );
Expand All @@ -170,26 +166,12 @@ 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() );
}
}

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

QStringList rampNames = mStyle->colorRampNames();
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 );
delete ramp;
}
}

void QgsCategorizedSymbolRendererV2Widget::addCategory( const QgsRendererCategoryV2 &cat )
{
Expand All @@ -214,21 +196,21 @@ void QgsCategorizedSymbolRendererV2Widget::categoryColumnChanged()

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 );
QgsSymbolV2* newSymbol = mRenderer->categories()[catIdx].symbol()->clone();

QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, this );
if ( !dlg.exec() )
if( !dlg.exec() )
{
delete newSymbol;
return;
Expand All @@ -246,7 +228,7 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q

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;
Expand All @@ -269,16 +251,11 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
//if (!dlg.exec())
// return;

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

if ( ramp == NULL )
if( ramp == NULL )
{
if ( cboCategorizedColorRamp->count() == 0 )
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." ) );
Expand All @@ -288,34 +265,34 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
QgsCategoryList cats;
_createCategories( cats, unique_vals, mCategorizedSymbol, ramp );

if ( !mOldClassificationAttribute.isEmpty() &&
attrName != mOldClassificationAttribute &&
mRenderer->categories().count() > 0 )
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 )
if( res == QMessageBox::Cancel )
return;

bool deleteExisting = ( res == QMessageBox::Yes );
if ( !deleteExisting )
if( !deleteExisting )
{
QgsCategoryList prevCats = mRenderer->categories();
for ( int i = 0; i < cats.size(); ++i )
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 )
for( int j = 0; j < prevCats.size() && !contains; ++j )
{
if ( prevCats.at( j ).value() == value )
if( prevCats.at( j ).value() == value )
contains = true;
}

if ( !contains )
if( !contains )
prevCats.append( cats.at( i ) );
}
cats = prevCats;
Expand Down Expand Up @@ -351,15 +328,15 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
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, 1 )->data();
Expand All @@ -368,11 +345,11 @@ QVariant QgsCategorizedSymbolRendererV2Widget::currentCategory()
void QgsCategorizedSymbolRendererV2Widget::deleteCategory()
{
QVariant k = currentCategory();
if ( !k.isValid() )
if( !k.isValid() )
return;

int idx = mRenderer->categoryIndexForValue( k );
if ( idx < 0 )
if( idx < 0 )
return;

mRenderer->deleteCategory( idx );
Expand All @@ -390,19 +367,19 @@ void QgsCategorizedSymbolRendererV2Widget::changeCurrentValue( QStandardItem * i
{
int idx = item->row();
QString newtext = item->text();
if ( item->column() == 1 )
if( item->column() == 1 )
{
QVariant value = newtext;
// try to preserve variant type for this value
QVariant::Type t = item->data().type();
if ( t == QVariant::Int )
if( t == QVariant::Int )
value = newtext.toInt();
else if ( t == QVariant::Double )
else if( t == QVariant::Double )
value = newtext.toDouble();
mRenderer->updateCategoryValue( idx, value );
item->setData( value );
}
else if ( item->column() == 2 )
else if( item->column() == 2 )
{
mRenderer->updateCategoryLabel( idx, newtext );
}
Expand Down
90 changes: 90 additions & 0 deletions src/gui/symbology-ng/qgscolorrampcombobox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "qgscolorrampcombobox.h"

#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsstylev2.h"
#include "qgsstylev2managerdialog.h"

QSize QgsColorRampComboBox::rampIconSize( 50, 16 );

QgsColorRampComboBox::QgsColorRampComboBox( QWidget *parent ) :
QComboBox( parent ), mStyle( NULL ), mSourceColorRamp( NULL )
{
}

QgsColorRampComboBox::~QgsColorRampComboBox()
{
delete mSourceColorRamp;
}

void QgsColorRampComboBox::populate( QgsStyleV2* style )
{
if( count() != 0 )
return; // already populated!

mStyle = style;

setIconSize( rampIconSize );

QStringList rampNames = mStyle->colorRampNames();
for( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
{
QgsVectorColorRampV2* ramp = style->colorRamp( *it );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize );

addItem( icon, *it );

delete ramp;
}

addItem( tr( "New color ramp..." ) );
connect( this, SIGNAL( currentIndexChanged( int ) ), SLOT( colorRampChanged( int ) ) );
}

QgsVectorColorRampV2* QgsColorRampComboBox::currentColorRamp()
{
QString rampName = currentText();
if( rampName == "[source]" && mSourceColorRamp )
return mSourceColorRamp->clone();
else
return mStyle->colorRamp( rampName );
}

void QgsColorRampComboBox::setSourceColorRamp( QgsVectorColorRampV2* sourceRamp )
{
mSourceColorRamp = sourceRamp->clone();

QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( mSourceColorRamp, rampIconSize );
if( itemText( 0 ) == "[source]" )
setItemIcon( 0, icon );
else
insertItem( 0, icon, "[source]" );
setCurrentIndex( 0 );
}

void QgsColorRampComboBox::colorRampChanged( int index )
{
if( index != count() - 1 )
return;

// last item: "new color ramp..."
QString rampName = QgsStyleV2ManagerDialog::addColorRampStatic( this, mStyle );
if( rampName.isEmpty() )
return;

// put newly added ramp into the combo
QgsVectorColorRampV2* ramp = mStyle->colorRamp( rampName );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize );

blockSignals( true ); // avoid calling this method again!
insertItem( index, icon, rampName );
blockSignals( false );

delete ramp;

// ... and set it as active
setCurrentIndex( index );

// make sure the color ramp is stored
mStyle->save();
}
39 changes: 39 additions & 0 deletions src/gui/symbology-ng/qgscolorrampcombobox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef QGSCOLORRAMPCOMBOBOX_H
#define QGSCOLORRAMPCOMBOBOX_H

#include <QComboBox>

class QgsStyleV2;
class QgsVectorColorRampV2;

class QgsColorRampComboBox : public QComboBox
{
Q_OBJECT
public:
explicit QgsColorRampComboBox( QWidget *parent = 0 );

~QgsColorRampComboBox();

//! initialize the combo box with color ramps from the style
void populate( QgsStyleV2* style );

//! add/select color ramp which was used previously by the renderer
void setSourceColorRamp( QgsVectorColorRampV2* sourceRamp );

//! return new instance of the current color ramp or NULL if there is no active color ramp
QgsVectorColorRampV2* currentColorRamp();

static QSize rampIconSize;

signals:

public slots:

void colorRampChanged( int index );

protected:
QgsStyleV2* mStyle;
QgsVectorColorRampV2* mSourceColorRamp; // owns the copy
};

#endif // QGSCOLORRAMPCOMBOBOX_H
Loading

0 comments on commit 7f32157

Please sign in to comment.