Skip to content
Permalink
Browse files

Handle legend node in parent gracefully with embedded widgets

  • Loading branch information
wonder-sk committed Jun 2, 2016
1 parent 75e5da3 commit 291dbb59a12fd1565c4c0c28d1c57b0cc97217dd
Showing with 27 additions and 9 deletions.
  1. +26 −9 src/core/layertree/qgslayertreemodel.cpp
  2. +1 −0 src/core/layertree/qgslayertreemodel.h
@@ -1196,7 +1196,7 @@ void QgsLayerTreeModel::addLegendToLayer( QgsLayerTreeLayer* nodeL )

QList<QgsLayerTreeModelLegendNode*> filteredLstNew = filterLegendNodes( lstNew );

bool isEmbedded = filteredLstNew.count() == 1 && filteredLstNew[0]->isEmbeddedInParent();
bool hasOnlyEmbedded = filteredLstNew.count() == 1 && filteredLstNew[0]->isEmbeddedInParent();

Q_FOREACH ( QgsLayerTreeModelLegendNode* n, lstNew )
{
@@ -1215,11 +1215,11 @@ void QgsLayerTreeModel::addLegendToLayer( QgsLayerTreeLayer* nodeL )

int count = data.tree ? data.tree->children[nullptr].count() : filteredLstNew.count();

if ( ! isEmbedded ) beginInsertRows( node2index( nodeL ), 0, count - 1 );
if ( ! hasOnlyEmbedded ) beginInsertRows( node2index( nodeL ), 0, count - 1 );

mLegend[nodeL] = data;

if ( ! isEmbedded ) endInsertRows();
if ( ! hasOnlyEmbedded ) endInsertRows();

if ( hasStyleOverride )
ml->styleManager()->restoreOverrideStyle();
@@ -1316,17 +1316,19 @@ int QgsLayerTreeModel::legendNodeRowCount( QgsLayerTreeModelLegendNode* node ) c

int QgsLayerTreeModel::legendRootRowCount( QgsLayerTreeLayer* nL ) const
{
if ( legendEmbeddedInParent( nL ) )
return 0;

if ( !mLegend.contains( nL ) )
return 0;

const LayerLegendData& data = mLegend[nL];
if ( data.tree )
return data.tree->children[nullptr].count();

return data.activeNodes.count();
int count = data.activeNodes.count();

if ( legendEmbeddedInParent( nL ) )
count--; // one item less -- it is embedded in parent

return count;
}


@@ -1390,14 +1392,29 @@ Qt::ItemFlags QgsLayerTreeModel::legendNodeFlags( QgsLayerTreeModelLegendNode* n

bool QgsLayerTreeModel::legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const
{
return legendNodeEmbeddedInParent( nodeLayer );
}

QgsLayerTreeModelLegendNode* QgsLayerTreeModel::legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const
{
// legend node embedded in parent does not have to be the first one...
// there could be extra legend nodes generated for embedded widgets
const LayerLegendData& data = mLegend[nodeLayer];
return data.activeNodes.count() == 1 && data.activeNodes[0]->isEmbeddedInParent();
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, data.activeNodes )
{
if ( legendNode->isEmbeddedInParent() )
return legendNode;
}
return nullptr;
}


QIcon QgsLayerTreeModel::legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const
{
return QIcon( qvariant_cast<QPixmap>( mLegend[nodeLayer].activeNodes[0]->data( Qt::DecorationRole ) ) );
QgsLayerTreeModelLegendNode* legendNode = legendNodeEmbeddedInParent( nodeLayer );
if ( !legendNode )
return QIcon();
return QIcon( qvariant_cast<QPixmap>( legendNode->data( Qt::DecorationRole ) ) );
}


@@ -277,6 +277,7 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
QVariant legendNodeData( QgsLayerTreeModelLegendNode* node, int role ) const;
Qt::ItemFlags legendNodeFlags( QgsLayerTreeModelLegendNode* node ) const;
bool legendEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
QgsLayerTreeModelLegendNode* legendNodeEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
QIcon legendIconEmbeddedInParent( QgsLayerTreeLayer* nodeLayer ) const;
void legendCleanup();
void legendInvalidateMapBasedData();

0 comments on commit 291dbb5

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