Skip to content
Permalink
Browse files

[FEATURE] display rule-based renderer legend in a pseudo-tree (in lay…

…er tree model/view)
  • Loading branch information
wonder-sk committed Sep 12, 2014
1 parent 95f3550 commit 1a7dfa1a6be9113352ba16d035e9802a11c3800d
@@ -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 );
@@ -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;

};


@@ -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;

@@ -35,6 +35,7 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
, mRootNode( rootNode )
, mFlags( ShowLegend | AllowLegendChangeState )
, mAutoCollapseLegendNodesCount( -1 )
, mLegendFilterByScale( 0 )
{
connectToRootNode();

@@ -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 )
{
@@ -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;
};

@@ -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 )
{
}

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

return *this;
}
@@ -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 );
@@ -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 );
@@ -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;
};


@@ -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;
}
@@ -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;

0 comments on commit 1a7dfa1

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