Skip to content
Permalink
Browse files

Identify renderer's categories/rules by key instead of index

  • Loading branch information
wonder-sk committed Aug 6, 2014
1 parent 54946a4 commit cf955822a6a8ae5d210cb25bcacaacfb129022c2
@@ -141,11 +141,11 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2

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

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

protected:
void rebuildHash();
@@ -167,11 +167,11 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

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

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



@@ -133,11 +133,11 @@ class QgsFeatureRendererV2

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

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

//! return a list of item text / symbol
//! @note: this method was added in version 1.5
@@ -376,13 +376,13 @@ QSizeF QgsComposerSymbolV2Item::drawSymbol( const QgsLegendSettings& settings, I
qMax( height + 2 * heightOffset, settings.symbolSize().height() ) );
}

QgsComposerSymbolV2Item* QgsComposerSymbolV2Item::findItemByRuleIndex( QgsComposerLayerItem* parentLayerItem, int ruleIndex )
QgsComposerSymbolV2Item* QgsComposerSymbolV2Item::findItemByRuleKey( QgsComposerLayerItem* parentLayerItem, QString ruleKey )
{
for ( int i = 0; i < parentLayerItem->rowCount(); ++i )
{
if ( QgsComposerSymbolV2Item* sItem = dynamic_cast<QgsComposerSymbolV2Item*>( parentLayerItem->child( 0 ) ) )
{
if ( sItem->ruleIndex() == ruleIndex )
if ( sItem->ruleKey() == ruleKey )
return sItem;
}
}
@@ -163,11 +163,11 @@ class CORE_EXPORT QgsComposerSymbolV2Item : public QgsComposerBaseSymbolItem
it is possible that it differs from mSymbolHeight */
QSizeF drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const;


int ruleIndex() const { return mItem.index; }
//! @note added in 2.6
QString ruleKey() const { return mItem.key; }

//! @note added in 2.6
static QgsComposerSymbolV2Item* findItemByRuleIndex( QgsComposerLayerItem* parentLayerItem, int ruleIndex );
static QgsComposerSymbolV2Item* findItemByRuleKey( QgsComposerLayerItem* parentLayerItem, QString ruleKey );

private:

@@ -68,11 +68,11 @@ bool QgsLayerTreeModelLegendNode::setData( const QVariant& value, int role )
// -------------------------------------------------------------------------


QgsSymbolV2LegendNode::QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, int rendererRef )
QgsSymbolV2LegendNode::QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, const QString& ruleKey )
: QgsLayerTreeModelLegendNode( nodeLayer )
, mSymbol( symbol )
, mSymbol( symbol ? symbol->clone() : 0 )
, mLabel( label )
, mRendererRef( rendererRef )
, mRuleKey( ruleKey )
{
if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toBool() && symbol )
{
@@ -82,6 +82,11 @@ QgsSymbolV2LegendNode::QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsS
}
}

QgsSymbolV2LegendNode::~QgsSymbolV2LegendNode()
{
delete mSymbol;
}

Qt::ItemFlags QgsSymbolV2LegendNode::flags() const
{
if ( mParent && mParent->childrenCheckable() )
@@ -116,7 +121,7 @@ QVariant QgsSymbolV2LegendNode::data( int role ) const
if ( !vlayer || !vlayer->rendererV2() )
return QVariant();

return vlayer->rendererV2()->legendSymbolItemChecked( mRendererRef ) ? Qt::Checked : Qt::Unchecked;
return vlayer->rendererV2()->legendSymbolItemChecked( mRuleKey ) ? Qt::Checked : Qt::Unchecked;
}

return QVariant();
@@ -134,7 +139,7 @@ bool QgsSymbolV2LegendNode::setData( const QVariant& value, int role )
if ( !vlayer || !vlayer->rendererV2() )
return false;

vlayer->rendererV2()->checkLegendSymbolItem( mRendererRef, value == Qt::Checked );
vlayer->rendererV2()->checkLegendSymbolItem( mRuleKey, value == Qt::Checked );

if ( mParent->isVisible() )
vlayer->clearCacheImage();
@@ -185,10 +190,9 @@ QList<QgsLayerTreeModelLegendNode*> QgsDefaultVectorLayerLegend::createLayerTree

nodeLayer->setChildrenCheckable( r->legendSymbolItemsCheckable() );

int k = 0;
foreach ( const QgsLegendSymbolItemV2& i, r->legendSymbolItemsV2() )
{
nodes.append( new QgsSymbolV2LegendNode( nodeLayer, i.symbol, i.label, k++ ) );
nodes.append( new QgsSymbolV2LegendNode( nodeLayer, i.symbol, i.label, i.key ) );
}
return nodes;
}
@@ -213,12 +217,12 @@ void QgsDefaultVectorLayerLegend::createLegendModelItems( QgsComposerLayerItem*
}

// Remember old user texts
QHash<int, QString> oldUserTexts;
QHash<QString, QString> oldUserTexts;
for ( int i = 0; i < layerItem->rowCount(); ++i )
{
QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( i, 0 ) );
if ( oldSymbolItem && !oldSymbolItem->userText().isEmpty() )
oldUserTexts.insert( oldSymbolItem->ruleIndex(), oldSymbolItem->userText() );
oldUserTexts.insert( oldSymbolItem->ruleKey(), oldSymbolItem->userText() );
}

int row = 0;
@@ -229,9 +233,9 @@ void QgsDefaultVectorLayerLegend::createLegendModelItems( QgsComposerLayerItem*
}

// Restore previously used user texts
for ( QHash<int, QString>::const_iterator it = oldUserTexts.begin(); it != oldUserTexts.end(); ++it )
for ( QHash<QString, QString>::const_iterator it = oldUserTexts.begin(); it != oldUserTexts.end(); ++it )
{
QgsComposerSymbolV2Item* item = QgsComposerSymbolV2Item::findItemByRuleIndex( layerItem, it.key() );
QgsComposerSymbolV2Item* item = QgsComposerSymbolV2Item::findItemByRuleKey( layerItem, it.key() );
if ( item )
item->setUserText( it.value() );
}
@@ -71,7 +71,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
{
public:
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, int rendererRef = -1 );
QgsSymbolV2LegendNode( QgsLayerTreeLayer* nodeLayer, QgsSymbolV2* symbol, const QString& label, const QString& ruleKey = QString() );
~QgsSymbolV2LegendNode();

virtual Qt::ItemFlags flags() const;
virtual QVariant data( int role ) const;
@@ -81,7 +82,7 @@ class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode
QgsSymbolV2* mSymbol;
mutable QIcon mIcon; // cached symbol preview
QString mLabel;
int mRendererRef;
QString mRuleKey;
};


@@ -754,17 +754,22 @@ bool QgsCategorizedSymbolRendererV2::legendSymbolItemsCheckable() const
return true;
}

bool QgsCategorizedSymbolRendererV2::legendSymbolItemChecked( int index )
bool QgsCategorizedSymbolRendererV2::legendSymbolItemChecked( QString key )
{
if ( index >= 0 && index < mCategories.size() )
bool ok;
int index = key.toInt( &ok );
if ( ok && index >= 0 && index < mCategories.size() )
return mCategories[ index ].renderState();
else
return true;
}

void QgsCategorizedSymbolRendererV2::checkLegendSymbolItem( int index, bool state )
void QgsCategorizedSymbolRendererV2::checkLegendSymbolItem( QString key, bool state )
{
updateCategoryRenderState( index, state );
bool ok;
int index = key.toInt( &ok );
if ( ok )
updateCategoryRenderState( index, state );
}

QgsMarkerSymbolV2 QgsCategorizedSymbolRendererV2::sSkipRender;
@@ -171,11 +171,11 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2

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

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

protected:
QString mAttrName;
@@ -1233,17 +1233,22 @@ bool QgsGraduatedSymbolRendererV2::legendSymbolItemsCheckable() const
return true;
}

bool QgsGraduatedSymbolRendererV2::legendSymbolItemChecked( int index )
bool QgsGraduatedSymbolRendererV2::legendSymbolItemChecked( QString key )
{
if ( index >= 0 && index < mRanges.size() )
bool ok;
int index = key.toInt( &ok );
if ( ok && index >= 0 && index < mRanges.size() )
return mRanges[ index ].renderState();
else
return true;
}

void QgsGraduatedSymbolRendererV2::checkLegendSymbolItem( int index, bool state )
void QgsGraduatedSymbolRendererV2::checkLegendSymbolItem( QString key, bool state )
{
updateRangeRenderState( index, state );
bool ok;
int index = key.toInt( &ok );
if ( ok )
updateRangeRenderState( index, state );
}


@@ -192,11 +192,11 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2

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

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

protected:
QString mAttrName;
@@ -4,24 +4,22 @@

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2()
: symbol( 0 )
, index( -1 )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
{
}

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* s, const QString& lbl, int idx )
QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* s, const QString& lbl, const QString& k )
: symbol( s )
, label( lbl )
, index( idx )
, key( k )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
{
}

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( const QgsLegendSymbolItemV2& other )
: symbol( 0 )
, index( -1 )
, scaleDenomMin( -1 )
, scaleDenomMax( -1 )
{
@@ -41,7 +39,7 @@ QgsLegendSymbolItemV2& QgsLegendSymbolItemV2::operator=( const QgsLegendSymbolIt
delete symbol;
symbol = other.symbol ? other.symbol->clone() : 0;
label = other.label;
index = other.index;
key = other.key;
scaleDenomMin = other.scaleDenomMin;
scaleDenomMax = other.scaleDenomMax;

@@ -10,14 +10,14 @@ class QgsLegendSymbolItemV2
public:
QgsLegendSymbolItemV2();
//! construct item, takes ownership of symbol
QgsLegendSymbolItemV2( QgsSymbolV2* s, const QString& lbl, int idx );
QgsLegendSymbolItemV2( QgsSymbolV2* s, const QString& lbl, const QString& k );
~QgsLegendSymbolItemV2();
QgsLegendSymbolItemV2( const QgsLegendSymbolItemV2& other );
QgsLegendSymbolItemV2& operator=( const QgsLegendSymbolItemV2& other );

QgsSymbolV2* symbol; //!< owned by the struct
QString label;
int index; //!< identifier of the symbol item (within renderer)
QString key; //!< identifier of the symbol item (within renderer)

// additional data that may be used for filtering

@@ -513,15 +513,15 @@ bool QgsFeatureRendererV2::legendSymbolItemsCheckable() const
return false;
}

bool QgsFeatureRendererV2::legendSymbolItemChecked( int index )
bool QgsFeatureRendererV2::legendSymbolItemChecked( QString key )
{
Q_UNUSED( index );
Q_UNUSED( key );
return false;
}

void QgsFeatureRendererV2::checkLegendSymbolItem( int index, bool state )
void QgsFeatureRendererV2::checkLegendSymbolItem( QString key, bool state )
{
Q_UNUSED( index );
Q_UNUSED( key );
Q_UNUSED( state );
}

@@ -542,7 +542,7 @@ QgsLegendSymbolListV2 QgsFeatureRendererV2::legendSymbolItemsV2() const
QgsLegendSymbolItemV2 item;
item.symbol = it->second ? it->second->clone() : 0;
item.label = it->first;
item.index = i;
item.key = QString::number( i );
lst2 << item;
}
return lst2;
@@ -159,11 +159,11 @@ class CORE_EXPORT QgsFeatureRendererV2

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

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

//! return a list of item text / symbol
//! @note: this method was added in version 1.5

0 comments on commit cf95582

Please sign in to comment.
You can’t perform that action at this time.