Skip to content

Commit fc29941

Browse files
author
mhugent
committed
update composer legend on layer level
git-svn-id: http://svn.osgeo.org/qgis/trunk@9190 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 874b29b commit fc29941

7 files changed

+120
-13
lines changed

src/app/composer/qgscomposerlegendwidget.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,42 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
340340
mLegend->update();
341341
}
342342
}
343+
344+
void QgsComposerLegendWidget::on_mUpdatePushButton_clicked()
345+
{
346+
//get current item
347+
QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>( mItemTreeView->model() );
348+
if ( !itemModel )
349+
{
350+
return;
351+
}
352+
353+
//get current item
354+
QModelIndex currentIndex = mItemTreeView->currentIndex();
355+
if ( !currentIndex.isValid() )
356+
{
357+
return;
358+
}
359+
360+
QStandardItem* currentItem = itemModel->itemFromIndex( currentIndex );
361+
if ( !currentItem )
362+
{
363+
return;
364+
}
365+
366+
QModelIndex parentIndex = currentIndex.parent();
367+
if ( !parentIndex.isValid() ) // a layer item
368+
{
369+
QString mapLayerId = currentItem->data().toString();
370+
mLegend->model()->updateLayer(mapLayerId);
371+
mLegend->update();
372+
}
373+
}
374+
375+
void QgsComposerLegendWidget::on_mUpdateAllPushButton_clicked()
376+
{
377+
if(mLegend)
378+
{
379+
mLegend->updateLegend();
380+
}
381+
}

src/app/composer/qgscomposerlegendwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
5252
void on_mMoveUpPushButton_clicked();
5353
void on_mRemovePushButton_clicked();
5454
void on_mEditPushButton_clicked();
55+
void on_mUpdatePushButton_clicked();
56+
void on_mUpdateAllPushButton_clicked();
5557

5658
private:
5759
QgsComposerLegendWidget();

src/core/composer/qgscomposerlegend.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ QFont QgsComposerLegend::itemFont() const
438438
return mItemFont;
439439
}
440440

441+
void QgsComposerLegend::updateLegend()
442+
{
443+
mLegendModel.setLayerSet(layerIdList());
444+
adjustBoxSize();
445+
update();
446+
}
447+
441448
bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc )
442449
{
443450
if ( elem.isNull() )

src/core/composer/qgscomposerlegend.h

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class CORE_EXPORT QgsComposerLegend: public QObject, public QgsComposerItem
7878
double symbolHeight() const {return mSymbolHeight;}
7979
void setSymbolHeight( double h ) {mSymbolHeight = h;}
8080

81+
/**Updates the model and all legend entries*/
82+
void updateLegend();
83+
8184
/** stores state in Dom node
8285
* @param elem is Dom element corresponding to 'Composer' tag
8386
* @param temp write template file

src/core/composer/qgslegendmodel.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ void QgsLegendModel::removeAllSymbols()
230230
mSymbols.clear();
231231
}
232232

233+
#if 0
233234
void QgsLegendModel::updateLayerEntries( const QStringList& newLayerIds )
234235
{
235236
if ( !invisibleRootItem() )
@@ -276,6 +277,57 @@ void QgsLegendModel::updateLayerEntries( const QStringList& newLayerIds )
276277

277278
mLayerIds = newLayerIds;
278279
}
280+
#endif //0
281+
282+
void QgsLegendModel::updateLayer(const QString& layerId)
283+
{
284+
//find layer item
285+
QStandardItem* layerItem = 0;
286+
QStandardItem* currentLayerItem = 0;
287+
288+
int numRootItems = rowCount();
289+
for ( int i = 0; i < numRootItems ; ++i )
290+
{
291+
currentLayerItem = item( i );
292+
if ( !currentLayerItem )
293+
{
294+
continue;
295+
}
296+
297+
QString currentId = currentLayerItem->data().toString();
298+
if ( currentId == layerId )
299+
{
300+
layerItem = currentLayerItem;
301+
break;
302+
}
303+
}
304+
305+
if(layerItem)
306+
{
307+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer(layerId);
308+
if(mapLayer)
309+
{
310+
//delete all the entries under layer item
311+
for(int i = rowCount() - 1; i >= 0; --i)
312+
{
313+
layerItem->removeRow(i);
314+
}
315+
316+
//and add the new ones...
317+
switch(mapLayer->type())
318+
{
319+
case QgsMapLayer::VECTOR:
320+
addVectorLayerItems(layerItem, mapLayer);
321+
break;
322+
case QgsMapLayer::RASTER:
323+
addRasterLayerItem(layerItem, mapLayer);
324+
break;
325+
default:
326+
break;
327+
}
328+
}
329+
}
330+
}
279331

280332
void QgsLegendModel::removeLayer( const QString& layerId )
281333
{

src/core/composer/qgslegendmodel.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
4141

4242
void setLayerSet( const QStringList& layerIds );
4343
/**Apply added or removed layers to the model*/
44-
void updateLayerEntries( const QStringList& newLayerIds );
44+
//void updateLayerEntries( const QStringList& newLayerIds );
45+
46+
/**Updates the symbology of a single layer*/
47+
void updateLayer(const QString& layerId);
4548

4649
bool writeXML( QDomElement& composerLegendElem, QDomDocument& doc );
4750
bool readXML( const QDomElement& legendModelElem, const QDomDocument& doc );

src/ui/qgscomposerlegendwidgetbase.ui

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<rect>
66
<x>0</x>
77
<y>0</y>
8-
<width>496</width>
9-
<height>626</height>
8+
<width>560</width>
9+
<height>537</height>
1010
</rect>
1111
</property>
1212
<property name="sizePolicy" >
@@ -183,19 +183,20 @@
183183
</widget>
184184
</item>
185185
<item row="0" column="4" >
186-
<spacer>
187-
<property name="orientation" >
188-
<enum>Qt::Horizontal</enum>
186+
<widget class="QPushButton" name="mUpdatePushButton" >
187+
<property name="text" >
188+
<string>update</string>
189189
</property>
190-
<property name="sizeHint" >
191-
<size>
192-
<width>121</width>
193-
<height>33</height>
194-
</size>
190+
</widget>
191+
</item>
192+
<item row="0" column="5" >
193+
<widget class="QPushButton" name="mUpdateAllPushButton" >
194+
<property name="text" >
195+
<string>update all</string>
195196
</property>
196-
</spacer>
197+
</widget>
197198
</item>
198-
<item row="1" column="0" colspan="5" >
199+
<item row="1" column="0" colspan="6" >
199200
<widget class="QTreeView" name="mItemTreeView" >
200201
<property name="headerHidden" stdset="0" >
201202
<bool>true</bool>

0 commit comments

Comments
 (0)