Skip to content

Commit

Permalink
Update composer legend entry when layer symbology changes. Funded by …
Browse files Browse the repository at this point in the history
…Sourcepole QGIS Enterprise.
  • Loading branch information
manisandro committed Jun 19, 2014
1 parent ee93d65 commit 8ed8d15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/core/composer/qgslegendmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@ void QgsLegendModel::removeLayer( const QString& layerId )

if ( layerId == lItem->layerID() )
{
if ( QgsMapLayerRegistry::instance() )
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
if ( layer )
{
disconnect( layer, SIGNAL( rendererChanged() ), this, SLOT( updateLayer() ) );
}
}
removeRow( i ); //todo: also remove the subitems and their symbols...
emit layersChanged();
return;
Expand Down Expand Up @@ -554,9 +562,30 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator
default:
break;
}

if ( mAutoUpdate )
{
connect( theMapLayer, SIGNAL( rendererChanged() ), this, SLOT( updateLayer() ) );
}

emit layersChanged();
}

void QgsLegendModel::updateLayer()
{
QString layerId = qobject_cast<QgsMapLayer*>( QObject::sender() )->id();

for ( int i = 0, n = rowCount(); i < n ; ++i )
{
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
if ( lItem && layerId == lItem->layerID() )
{
updateLayer( lItem );
emit layersChanged();
return;
}
}
}

bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const
{
Expand Down Expand Up @@ -595,6 +624,9 @@ bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocu
}

clear();
//disable autoupdates here in order to have a setAutoUpdate(true)
//below connect the rendererChanged signals to the layers
setAutoUpdate( false );

QDomNodeList topLevelItemList = legendModelElem.childNodes();
QDomElement currentElem;
Expand Down Expand Up @@ -834,6 +866,19 @@ void QgsLegendModel::setAutoUpdate( bool autoUpdate )
{
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );

for ( int i = 0, n = rowCount(); i < n ; ++i )
{
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
if ( lItem )
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
if ( layer )
{
connect( layer, SIGNAL( rendererChanged() ), this, SLOT( updateLayer() ) );
}
}
}
}
}
else
Expand All @@ -842,6 +887,19 @@ void QgsLegendModel::setAutoUpdate( bool autoUpdate )
{
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );

for ( int i = 0, n = rowCount(); i < n ; ++i )
{
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
if ( lItem )
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
if ( layer )
{
disconnect( layer, SIGNAL( rendererChanged() ), this, SLOT( updateLayer() ) );
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/core/composer/qgslegendmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
void removeLayer( const QString& layerId );
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "" );

private slots:
void updateLayer();

signals:
void layersChanged();

Expand Down

0 comments on commit 8ed8d15

Please sign in to comment.