Skip to content

Commit

Permalink
[FEATURE] display rule-based renderer legend in a pseudo-tree (in lay…
Browse files Browse the repository at this point in the history
…er tree model/view)
  • Loading branch information
wonder-sk committed Sep 12, 2014
1 parent 95f3550 commit 1a7dfa1
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 12 deletions.
5 changes: 4 additions & 1 deletion python/core/symbology-ng/qgslegendsymbolitemv2.sip
Expand Up @@ -15,7 +15,7 @@ class QgsLegendSymbolItemV2
public:
QgsLegendSymbolItemV2();
//! Construct item. Does not take ownership of symbol (makes internal clone)
QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable = false, int scaleMinDenom = -1, int scaleMaxDenom = -1 );
QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable = false, int scaleMinDenom = -1, int scaleMaxDenom = -1, int level = 0 );
~QgsLegendSymbolItemV2();
QgsLegendSymbolItemV2( const QgsLegendSymbolItemV2& other );
//QgsLegendSymbolItemV2& operator=( const QgsLegendSymbolItemV2& other );
Expand All @@ -41,6 +41,9 @@ class QgsLegendSymbolItemV2
//! Value <= 0 means the range is unbounded on this side
int scaleMaxDenom() const;

//! Identation level that tells how deep the item is in a hierarchy of items. For flat lists level is 0
int level() const;

};


Expand Down
2 changes: 2 additions & 0 deletions python/core/symbology-ng/qgsrulebasedrendererv2.sip
Expand Up @@ -63,6 +63,8 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
QgsSymbolV2List symbols();
//! @note not available in python bindings
// QgsLegendSymbolList legendSymbolItems();
//! @note added in 2.6
QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
bool isFilterOK( QgsFeature& f ) const;
bool isScaleOK( double scale ) const;

Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -35,6 +35,7 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
, mRootNode( rootNode )
, mFlags( ShowLegend | AllowLegendChangeState )
, mAutoCollapseLegendNodesCount( -1 )
, mLegendFilterByScale( 0 )
{
connectToRootNode();

Expand Down
29 changes: 26 additions & 3 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -155,9 +155,32 @@ QVariant QgsSymbolV2LegendNode::data( int role ) const
else if ( role == Qt::DecorationRole )
{
QSize iconSize( 16, 16 ); // TODO: configurable
if ( mIcon.isNull() && mItem.symbol() )
mIcon = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), iconSize );
return mIcon;
const int indentSize = 20;
if ( mPixmap.isNull() )
{
QPixmap pix;
if ( mItem.symbol() )
pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), iconSize );
else
{
pix = QPixmap( iconSize );
pix.fill( Qt::transparent );
}

if ( mItem.level() == 0 )
mPixmap = pix;
else
{
// ident the symbol icon to make it look like a tree structure
QPixmap pix2( pix.width() + mItem.level() * indentSize, pix.height() );
pix2.fill( Qt::transparent );
QPainter p( &pix2 );
p.drawPixmap( mItem.level() * indentSize, 0, pix );
p.end();
mPixmap = pix2;
}
}
return mPixmap;
}
else if ( role == Qt::CheckStateRole )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -150,7 +150,7 @@ class CORE_EXPORT QgsSymbolV2LegendNode : public QgsLayerTreeModelLegendNode

private:
QgsLegendSymbolItemV2 mItem;
mutable QIcon mIcon; // cached symbol preview
mutable QPixmap mPixmap; // cached symbol preview
QString mLabel;
};

Expand Down
5 changes: 4 additions & 1 deletion src/core/symbology-ng/qgslegendsymbolitemv2.cpp
Expand Up @@ -23,17 +23,19 @@ QgsLegendSymbolItemV2::QgsLegendSymbolItemV2()
, mOriginalSymbolPointer( 0 )
, mScaleMinDenom( -1 )
, mScaleMaxDenom( -1 )
, mLevel( 0 )
{
}

QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable, int scaleMinDenom, int scaleMaxDenom )
QgsLegendSymbolItemV2::QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable, int scaleMinDenom, int scaleMaxDenom, int level )
: mSymbol( symbol ? symbol->clone() : 0 )
, mLabel( label )
, mKey( ruleKey )
, mCheckable( checkable )
, mOriginalSymbolPointer( symbol )
, mScaleMinDenom( scaleMinDenom )
, mScaleMaxDenom( scaleMaxDenom )
, mLevel( level )
{
}

Expand Down Expand Up @@ -61,6 +63,7 @@ QgsLegendSymbolItemV2& QgsLegendSymbolItemV2::operator=( const QgsLegendSymbolIt
mOriginalSymbolPointer = other.mOriginalSymbolPointer;
mScaleMinDenom = other.mScaleMinDenom;
mScaleMaxDenom = other.mScaleMaxDenom;
mLevel = other.mLevel;

return *this;
}
Expand Down
8 changes: 7 additions & 1 deletion src/core/symbology-ng/qgslegendsymbolitemv2.h
Expand Up @@ -32,7 +32,7 @@ class CORE_EXPORT QgsLegendSymbolItemV2
public:
QgsLegendSymbolItemV2();
//! Construct item. Does not take ownership of symbol (makes internal clone)
QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable = false, int scaleMinDenom = -1, int scaleMaxDenom = -1 );
QgsLegendSymbolItemV2( QgsSymbolV2* symbol, const QString& label, const QString& ruleKey, bool checkable = false, int scaleMinDenom = -1, int scaleMaxDenom = -1, int level = 0 );
~QgsLegendSymbolItemV2();
QgsLegendSymbolItemV2( const QgsLegendSymbolItemV2& other );
QgsLegendSymbolItemV2& operator=( const QgsLegendSymbolItemV2& other );
Expand All @@ -58,6 +58,9 @@ class CORE_EXPORT QgsLegendSymbolItemV2
//! Value <= 0 means the range is unbounded on this side
int scaleMaxDenom() const { return mScaleMaxDenom; }

//! Identation level that tells how deep the item is in a hierarchy of items. For flat lists level is 0
int level() const { return mLevel; }

protected:
//! Set symbol of the item. Takes ownership of symbol.
void setSymbol( QgsSymbolV2* s );
Expand All @@ -78,6 +81,9 @@ class CORE_EXPORT QgsLegendSymbolItemV2

int mScaleMinDenom;
int mScaleMaxDenom;

//! Identation level that tells how deep the item is in a hierarchy of items. For flat lists level is 0
int mLevel;
};


Expand Down
8 changes: 4 additions & 4 deletions src/core/symbology-ng/qgsrulebasedrendererv2.cpp
Expand Up @@ -211,18 +211,18 @@ QgsLegendSymbolList QgsRuleBasedRendererV2::Rule::legendSymbolItems( double scal
return lst;
}

QgsLegendSymbolListV2 QgsRuleBasedRendererV2::Rule::legendSymbolItemsV2() const
QgsLegendSymbolListV2 QgsRuleBasedRendererV2::Rule::legendSymbolItemsV2( int currentLevel ) const
{
QgsLegendSymbolListV2 lst;
if ( mSymbol )
if ( currentLevel != -1 ) // root rule should not be shown
{
lst << QgsLegendSymbolItemV2( mSymbol, mLabel, mRuleKey, true, mScaleMinDenom, mScaleMaxDenom );
lst << QgsLegendSymbolItemV2( mSymbol, mLabel, mRuleKey, true, mScaleMinDenom, mScaleMaxDenom, currentLevel );
}

for ( RuleList::const_iterator it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
{
Rule* rule = *it;
lst << rule->legendSymbolItemsV2();
lst << rule->legendSymbolItemsV2( currentLevel + 1 );
}
return lst;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsrulebasedrendererv2.h
Expand Up @@ -95,7 +95,7 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
//! @note not available in python bindings
QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
//! @note added in 2.6
QgsLegendSymbolListV2 legendSymbolItemsV2() const;
QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
bool isFilterOK( QgsFeature& f ) const;
bool isScaleOK( double scale ) const;

Expand Down

0 comments on commit 1a7dfa1

Please sign in to comment.