Skip to content
Permalink
Browse files

[layer tree] Fix corrupted tree when filter (by extent) is on with le…

…gend widget(s) enabled
  • Loading branch information
nirvn committed Nov 11, 2020
1 parent 34e457f commit 55eaaa7484cedda65b6428557a5966e9a7fbb813
@@ -38,7 +38,19 @@ and customized look.
enum LegendNodeRoles
{
RuleKeyRole,
ParentRuleKeyRole
ParentRuleKeyRole,
NodeTypeRole,
};

enum NodeTypes
{
SimpleLegend,
SymbolLegend,
RasterSymbolLegend,
ImageLegend,
WmsLegend,
DataDefinedSizeLegend,
EmbeddedWidget,
};

QgsLayerTreeLayer *layerNode() const;
@@ -1230,22 +1230,38 @@ QList<QgsLayerTreeModelLegendNode *> QgsLayerTreeModel::filterLegendNodes( const
{
for ( QgsLayerTreeModelLegendNode *node : qgis::as_const( nodes ) )
{
QString ruleKey = node->data( QgsSymbolLegendNode::RuleKeyRole ).toString();
bool checked = mLegendFilterUsesExtent || node->data( Qt::CheckStateRole ).toInt() == Qt::Checked;
if ( checked )
switch ( node->data( QgsSymbolLegendNode::NodeTypeRole ).value<QgsLayerTreeModelLegendNode::NodeTypes>() )
{
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( node->layerNode()->layer() ) )
case QgsLayerTreeModelLegendNode::EmbeddedWidget:
filtered << node;
break;

case QgsLayerTreeModelLegendNode::SimpleLegend:
case QgsLayerTreeModelLegendNode::SymbolLegend:
case QgsLayerTreeModelLegendNode::RasterSymbolLegend:
case QgsLayerTreeModelLegendNode::ImageLegend:
case QgsLayerTreeModelLegendNode::WmsLegend:
case QgsLayerTreeModelLegendNode::DataDefinedSizeLegend:
{
if ( mLegendFilterHitTest->legendKeyVisible( ruleKey, vl ) )
const QString ruleKey = node->data( QgsSymbolLegendNode::RuleKeyRole ).toString();
bool checked = mLegendFilterUsesExtent || node->data( Qt::CheckStateRole ).toInt() == Qt::Checked;
if ( checked )
{
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( node->layerNode()->layer() ) )
{
if ( mLegendFilterHitTest->legendKeyVisible( ruleKey, vl ) )
filtered << node;
}
else
{
filtered << node;
}
}
else // unknown node type or unchecked
filtered << node;
}
else
{
filtered << node;
break;
}
}
else // unknown node type or unchecked
filtered << node;
}
}
}
@@ -500,6 +500,8 @@ class EmbeddedWidgetLegendNode : public QgsLayerTreeModelLegendNode
{
if ( role == RuleKeyRole )
return mRuleKey;
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
return QgsLayerTreeModelLegendNode::EmbeddedWidget;
return QVariant();
}

@@ -511,6 +511,10 @@ QVariant QgsSymbolLegendNode::data( int role ) const
{
return mItem.parentRuleKey();
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::SymbolLegend;
}

return QVariant();
}
@@ -851,6 +855,8 @@ QVariant QgsSimpleLegendNode::data( int role ) const
return mIcon;
else if ( role == RuleKeyRole && !mKey.isEmpty() )
return mKey;
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
return QgsLayerTreeModelLegendNode::SimpleLegend;
else
return QVariant();
}
@@ -874,6 +880,10 @@ QVariant QgsImageLegendNode::data( int role ) const
{
return mImage.size();
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::ImageLegend;
}
return QVariant();
}

@@ -931,9 +941,14 @@ QVariant QgsRasterSymbolLegendNode::data( int role ) const
return QIcon( pix );
}
else if ( role == Qt::DisplayRole || role == Qt::EditRole )
{
return mUserLabel.isEmpty() ? mLabel : mUserLabel;
else
return QVariant();
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::RasterSymbolLegend;
}
return QVariant();
}


@@ -1085,6 +1100,10 @@ QVariant QgsWmsLegendNode::data( int role ) const
{
return getLegendGraphic().size();
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::WmsLegend;
}
return QVariant();
}

@@ -1221,6 +1240,10 @@ QVariant QgsDataDefinedSizeLegendNode::data( int role ) const
cacheImage();
return mImage.size();
}
else if ( role == QgsLayerTreeModelLegendNode::NodeTypeRole )
{
return QgsLayerTreeModelLegendNode::DataDefinedSizeLegend;
}
return QVariant();
}

@@ -60,10 +60,24 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
#endif
public:

//! Legend node data roles
enum LegendNodeRoles
{
RuleKeyRole = Qt::UserRole, //!< Rule key of the node (QString)
ParentRuleKeyRole //!< Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2.8
RuleKeyRole = Qt::UserRole, //!< Rule key of the node (QString)
ParentRuleKeyRole, //!< Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2.8
NodeTypeRole, //!< Type of node. Added in 3.16
};

//! Types of legend nodes
enum NodeTypes
{
SimpleLegend, //!< Simple label with icon legend node type
SymbolLegend, //!< Vector symbol legend node type
RasterSymbolLegend, //!< Raster symbol legend node type
ImageLegend, //!< Raster image legend node type
WmsLegend, //!< WMS legend node type
DataDefinedSizeLegend, //!< Marker symbol legend node type
EmbeddedWidget, //!< Embedded widget placeholder node type
};

//! Returns pointer to the parent layer node
@@ -277,6 +291,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
QSizeF mUserSize;
bool mColumnBreakBeforeNode = false;
};
Q_DECLARE_METATYPE( QgsLayerTreeModelLegendNode::NodeTypes )

#include "qgslegendsymbolitem.h"
#include "qgstextformat.h"

0 comments on commit 55eaaa7

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