Skip to content

Commit

Permalink
[FEATURE][API] categorized render: support disabling rendering of som…
Browse files Browse the repository at this point in the history
…e categories

Funded-By: norBIT
  • Loading branch information
jef-n committed Jul 4, 2014
1 parent 6af7dbd commit 4867b7f
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 21 deletions.
7 changes: 6 additions & 1 deletion python/core/layertree/qgslayertreelayer.sip
Expand Up @@ -37,6 +37,11 @@ class QgsLayerTreeLayer : QgsLayerTreeNode
Qt::CheckState isVisible() const;
void setVisible( Qt::CheckState visible );

//! @note added in 2.5
bool childrenCheckable() const;
//! @note added in 2.5
void setChildrenCheckable( bool checkable );

static QgsLayerTreeLayer* readXML( QDomElement& element ) /Factory/;
virtual void writeXML( QDomElement& parentElement );

Expand All @@ -50,5 +55,5 @@ class QgsLayerTreeLayer : QgsLayerTreeNode

private:
QgsLayerTreeLayer( const QgsLayerTreeLayer& other );

};
25 changes: 24 additions & 1 deletion python/core/symbology-ng/qgscategorizedsymbolrendererv2.sip
Expand Up @@ -6,7 +6,7 @@ class QgsRendererCategoryV2
public:

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

//! copy constructor
QgsRendererCategoryV2( const QgsRendererCategoryV2& cat );
Expand All @@ -21,6 +21,10 @@ class QgsRendererCategoryV2
void setSymbol( QgsSymbolV2* s /Transfer/ );
void setLabel( const QString &label );

// @note added in 2.5
bool renderState() const;
void setRenderState( bool render );

// debugging
QString dump() const;

Expand Down Expand Up @@ -68,10 +72,17 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
//! return index of category with specified value (-1 if not found)
int categoryIndexForValue( QVariant val );

//! return index of category with specified label (-1 if not found or not unique)
//! @note added in 2.5
int categoryIndexForLabel( QString val );

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

//! @note added in 2.5
bool updateCategoryRenderState( int catIndex, bool render );

void addCategory( const QgsRendererCategoryV2 &category );
bool deleteCategory( int catIndex );
void deleteAllCategories();
Expand Down Expand Up @@ -124,6 +135,18 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const;

//! items of symbology items in legend should be checkable
// @note added in 2.5
virtual bool legendSymbolItemsCheckable() const;

//! item in symbology was checked
// @note added in 2.5
virtual bool legendSymbolItemChecked( QString key );

//! item in symbology was checked
// @note added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

protected:
void rebuildHash();

Expand Down
12 changes: 12 additions & 0 deletions python/core/symbology-ng/qgsrendererv2.sip
Expand Up @@ -127,6 +127,18 @@ class QgsFeatureRendererV2
//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );

//! items of symbology items in legend should be checkable
//! @node added in 2.5
virtual bool legendSymbolItemsCheckable() const;

//! items of symbology items in legend is checked
//! @node added in 2.5
virtual bool legendSymbolItemChecked( QString key );

//! item in symbology was checked
//! @node added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note not available in python bindings
Expand Down
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreelayer.cpp
Expand Up @@ -25,6 +25,7 @@ QgsLayerTreeLayer::QgsLayerTreeLayer( QgsMapLayer *layer )
, mLayerId( layer->id() )
, mLayer( layer )
, mVisible( Qt::Checked )
, mChildrenCheckable( false )
{
Q_ASSERT( QgsMapLayerRegistry::instance()->mapLayer( mLayerId ) == layer );
}
Expand All @@ -35,6 +36,7 @@ QgsLayerTreeLayer::QgsLayerTreeLayer( QString layerId, QString name )
, mLayerName( name )
, mLayer( 0 )
, mVisible( Qt::Checked )
, mChildrenCheckable( false )
{
attachToLayer();
}
Expand All @@ -45,6 +47,7 @@ QgsLayerTreeLayer::QgsLayerTreeLayer( const QgsLayerTreeLayer& other )
, mLayerName( other.mLayerName )
, mLayer( 0 )
, mVisible( other.mVisible )
, mChildrenCheckable( other.mChildrenCheckable )
{
attachToLayer();
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/layertree/qgslayertreelayer.h
Expand Up @@ -57,6 +57,11 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
Qt::CheckState isVisible() const { return mVisible; }
void setVisible( Qt::CheckState visible );

//! @note added in 2.5
bool childrenCheckable() const { return mChildrenCheckable; }
//! @note added in 2.5
void setChildrenCheckable( bool checkable ) { mChildrenCheckable = checkable; }

static QgsLayerTreeLayer* readXML( QDomElement& element );
virtual void writeXML( QDomElement& parentElement );

Expand All @@ -78,6 +83,7 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
QString mLayerName; // only used if layer does not exist
QgsMapLayer* mLayer; // not owned! may be null
Qt::CheckState mVisible;
bool mChildrenCheckable;
};


Expand Down
76 changes: 66 additions & 10 deletions src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Expand Up @@ -32,17 +32,19 @@ QgsRendererCategoryV2::QgsRendererCategoryV2()
{
}

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

QgsRendererCategoryV2::QgsRendererCategoryV2( const QgsRendererCategoryV2& cat )
: mValue( cat.mValue )
, mSymbol( cat.mSymbol.data() ? cat.mSymbol->clone() : NULL )
, mLabel( cat.mLabel )
, mRender( cat.mRender )
{
}

Expand Down Expand Up @@ -75,6 +77,11 @@ QString QgsRendererCategoryV2::label() const
return mLabel;
}

bool QgsRendererCategoryV2::renderState() const
{
return mRender;
}

void QgsRendererCategoryV2::setValue( const QVariant &value )
{
mValue = value;
Expand All @@ -90,9 +97,14 @@ void QgsRendererCategoryV2::setLabel( const QString &label )
mLabel = label;
}

void QgsRendererCategoryV2::setRenderState( bool render )
{
mRender = render;
}

QString QgsRendererCategoryV2::dump() const
{
return QString( "%1::%2::%3\n" ).arg( mValue.toString() ).arg( mLabel ).arg( mSymbol->dump() );
return QString( "%1::%2::%3:%4\n" ).arg( mValue.toString() ).arg( mLabel ).arg( mSymbol->dump() ).arg( mRender );
}

void QgsRendererCategoryV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
Expand Down Expand Up @@ -157,7 +169,7 @@ void QgsCategorizedSymbolRendererV2::rebuildHash()
for ( int i = 0; i < mCategories.count(); ++i )
{
QgsRendererCategoryV2& cat = mCategories[i];
mSymbolHash.insert( cat.value().toString(), cat.symbol() );
mSymbolHash.insert( cat.value().toString(), cat.renderState() ? cat.symbol() : 0 );
}
}

Expand Down Expand Up @@ -197,7 +209,7 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature( QgsFeature& featu

// find the right symbol for the category
QgsSymbolV2* symbol = symbolForValue( value );
if ( symbol == NULL )
if ( !symbol )
{
// if no symbol found use default one
return symbolForValue( QVariant( "" ) );
Expand Down Expand Up @@ -240,6 +252,22 @@ int QgsCategorizedSymbolRendererV2::categoryIndexForValue( QVariant val )
return -1;
}

int QgsCategorizedSymbolRendererV2::categoryIndexForLabel( QString val )
{
int idx = -1;
for ( int i = 0; i < mCategories.count(); i++ )
{
if ( mCategories[i].label() == val )
{
if ( idx != -1 )
return -1;
else
idx = i;
}
}
return idx;
}

bool QgsCategorizedSymbolRendererV2::updateCategoryValue( int catIndex, const QVariant &value )
{
if ( catIndex < 0 || catIndex >= mCategories.size() )
Expand All @@ -264,16 +292,23 @@ bool QgsCategorizedSymbolRendererV2::updateCategoryLabel( int catIndex, QString
return true;
}

bool QgsCategorizedSymbolRendererV2::updateCategoryRenderState( int catIndex, bool render )
{
if ( catIndex < 0 || catIndex >= mCategories.size() )
return false;
mCategories[catIndex].setRenderState( render );
return true;
}

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

mCategories.append( cat );
}

bool QgsCategorizedSymbolRendererV2::deleteCategory( int catIndex )
Expand Down Expand Up @@ -486,10 +521,11 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create( QDomElement& eleme
QVariant value = QVariant( catElem.attribute( "value" ) );
QString symbolName = catElem.attribute( "symbol" );
QString label = catElem.attribute( "label" );
bool render = catElem.attribute( "render" ) != "false";
if ( symbolMap.contains( symbolName ) )
{
QgsSymbolV2* symbol = symbolMap.take( symbolName );
cats.append( QgsRendererCategoryV2( value, symbol, label ) );
cats.append( QgsRendererCategoryV2( value, symbol, label, render ) );
}
}
catElem = catElem.nextSiblingElement();
Expand Down Expand Up @@ -561,6 +597,7 @@ QDomElement QgsCategorizedSymbolRendererV2::save( QDomDocument& doc )
catElem.setAttribute( "value", cat.value().toString() );
catElem.setAttribute( "symbol", symbolName );
catElem.setAttribute( "label", cat.label() );
catElem.setAttribute( "render", cat.renderState() ? "true" : "false" );
catsElem.appendChild( catElem );
i++;
}
Expand Down Expand Up @@ -707,3 +744,22 @@ void QgsCategorizedSymbolRendererV2::setScaleMethod( QgsSymbolV2::ScaleMethod sc
setScaleMethodToSymbol( catIt->symbol(), scaleMethod );
}
}

bool QgsCategorizedSymbolRendererV2::legendSymbolItemsCheckable() const
{
return true;
}

bool QgsCategorizedSymbolRendererV2::legendSymbolItemChecked( QString key )
{
int idx = categoryIndexForLabel( key );
return idx < 0 ? true : mCategories[ idx ].renderState();
}

void QgsCategorizedSymbolRendererV2::checkLegendSymbolItem( QString key, bool state )
{
int idx = categoryIndexForLabel( key );
if ( idx < 0 )
return;
updateCategoryRenderState( idx, state );
}
26 changes: 25 additions & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.h
Expand Up @@ -32,7 +32,7 @@ class CORE_EXPORT QgsRendererCategoryV2
QgsRendererCategoryV2( );

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

//! copy constructor
QgsRendererCategoryV2( const QgsRendererCategoryV2& cat );
Expand All @@ -47,6 +47,10 @@ class CORE_EXPORT QgsRendererCategoryV2
void setSymbol( QgsSymbolV2* s );
void setLabel( const QString &label );

// @note added in 2.5
bool renderState() const;
void setRenderState( bool render );

// debugging
QString dump() const;

Expand All @@ -56,6 +60,7 @@ class CORE_EXPORT QgsRendererCategoryV2
QVariant mValue;
QScopedPointer<QgsSymbolV2> mSymbol;
QString mLabel;
bool mRender;

void swap( QgsRendererCategoryV2 & other );
};
Expand Down Expand Up @@ -97,10 +102,17 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! return index of category with specified value (-1 if not found)
int categoryIndexForValue( QVariant val );

//! return index of category with specified label (-1 if not found or not unique)
//! @note added in 2.5
int categoryIndexForLabel( QString val );

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

//! @note added in 2.5
bool updateCategoryRenderState( int catIndex, bool render );

void addCategory( const QgsRendererCategoryV2 &category );
bool deleteCategory( int catIndex );
void deleteAllCategories();
Expand Down Expand Up @@ -153,6 +165,18 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }

//! items of symbology items in legend should be checkable
// @note added in 2.5
virtual bool legendSymbolItemsCheckable() const;

//! item in symbology was checked
// @note added in 2.5
virtual bool legendSymbolItemChecked( QString key );

//! item in symbology was checked
// @note added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

protected:
QString mAttrName;
QgsCategoryList mCategories;
Expand Down
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsrendererv2.cpp
Expand Up @@ -513,6 +513,22 @@ QgsLegendSymbologyList QgsFeatureRendererV2::legendSymbologyItems( QSize iconSiz
return QgsLegendSymbologyList();
}

bool QgsFeatureRendererV2::legendSymbolItemsCheckable() const
{
return false;
}

bool QgsFeatureRendererV2::legendSymbolItemChecked( QString item )
{
return false;
}

void QgsFeatureRendererV2::checkLegendSymbolItem( QString item, bool state )
{
Q_UNUSED( item );
Q_UNUSED( state );
}

QgsLegendSymbolList QgsFeatureRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
Expand Down

0 comments on commit 4867b7f

Please sign in to comment.