Skip to content

Commit

Permalink
Handle legend node in parent gracefully with embedded widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jun 2, 2016
1 parent 75e5da3 commit 291dbb5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/core/layertree/qgslayertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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 ) ) );
}


Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 291dbb5

Please sign in to comment.