Skip to content

Commit 4c7f51e

Browse files
committedMay 8, 2013
composer legend: store user defined labels separately, fixes partialy #6960
1 parent 1877eb5 commit 4c7f51e

9 files changed

+218
-41
lines changed
 

‎images/images.qrc

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<file>themes/default/mActionAddArrow.png</file>
122122
<file>themes/default/mActionAddBasicShape.png</file>
123123
<file>themes/default/mActionAddGPSLayer.png</file>
124+
<file>themes/default/mActionAddGroup.png</file>
124125
<file>themes/default/mActionAddHtml.png</file>
125126
<file>themes/default/mActionAddImage.png</file>
126127
<file>themes/default/mActionAddLayer.png</file>
662 Bytes
Loading

‎src/app/composer/qgscomposerlegendwidget.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
716716
return;
717717
}
718718

719-
QStandardItem* currentItem = itemModel->itemFromIndex( currentIndex );
719+
QgsComposerLegendItem* currentItem = dynamic_cast<QgsComposerLegendItem *>( itemModel->itemFromIndex( currentIndex ) );
720720
if ( !currentItem )
721721
{
722722
return;
@@ -725,7 +725,8 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
725725
QgsComposerLegendItemDialog itemDialog( currentItem );
726726
if ( itemDialog.exec() == QDialog::Accepted )
727727
{
728-
currentItem->setText( itemDialog.itemText() );
728+
currentItem->setUserText( itemDialog.itemText() );
729+
mLegend->model()->updateItemText( currentItem );
729730
}
730731

731732
mLegend->beginCommand( tr( "Legend item edited" ) );
@@ -822,7 +823,7 @@ void QgsComposerLegendWidget::on_mUpdateAllPushButton_clicked()
822823
updateLegend();
823824
}
824825

825-
void QgsComposerLegendWidget::on_mAddGroupButton_clicked()
826+
void QgsComposerLegendWidget::on_mAddGroupToolButton_clicked()
826827
{
827828
if ( mLegend && mLegend->model() )
828829
{

‎src/app/composer/qgscomposerlegendwidget.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
8282
void on_mCountToolButton_clicked( bool checked );
8383
void on_mUpdatePushButton_clicked();
8484
void on_mUpdateAllPushButton_clicked();
85-
void on_mAddGroupButton_clicked();
85+
void on_mAddGroupToolButton_clicked();
8686

8787
void selectedChanged( const QModelIndex & current, const QModelIndex & previous );
8888

‎src/core/composer/qgscomposerlegenditem.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) c
105105
vectorClassElem.appendChild( symbolsElem );
106106
}
107107
vectorClassElem.setAttribute( "text", text() );
108+
vectorClassElem.setAttribute( "userText", userText() );
108109
elem.appendChild( vectorClassElem );
109110
}
110111

@@ -116,6 +117,7 @@ void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServer
116117
}
117118

118119
setText( itemElem.attribute( "text", "" ) );
120+
setUserText( itemElem.attribute( "userText", "" ) );
119121
QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
120122
if ( !symbolsElem.isNull() )
121123
{
@@ -174,6 +176,7 @@ void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc
174176
QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
175177
rasterClassElem.setAttribute( "layerId", mLayerID );
176178
rasterClassElem.setAttribute( "text", text() );
179+
rasterClassElem.setAttribute( "userText", userText() );
177180
rasterClassElem.setAttribute( "color", mColor.name() );
178181
elem.appendChild( rasterClassElem );
179182
}
@@ -185,6 +188,7 @@ void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem, bool xSe
185188
return;
186189
}
187190
setText( itemElem.attribute( "text", "" ) );
191+
setUserText( itemElem.attribute( "userText", "" ) );
188192
setLayerID( itemElem.attribute( "layerId", "" ) );
189193
setColor( QColor( itemElem.attribute( "color" ) ) );
190194

@@ -225,6 +229,7 @@ void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) cons
225229
QDomElement layerItemElem = doc.createElement( "LayerItem" );
226230
layerItemElem.setAttribute( "layerId", mLayerID );
227231
layerItemElem.setAttribute( "text", text() );
232+
layerItemElem.setAttribute( "userText", userText() );
228233
layerItemElem.setAttribute( "showFeatureCount", showFeatureCount() );
229234
layerItemElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
230235
writeXMLChildren( layerItemElem, doc );
@@ -238,6 +243,7 @@ void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAva
238243
return;
239244
}
240245
setText( itemElem.attribute( "text", "" ) );
246+
setUserText( itemElem.attribute( "userText", "" ) );
241247
setLayerID( itemElem.attribute( "layerId", "" ) );
242248
setShowFeatureCount( itemElem.attribute( "showFeatureCount", "" ) == "1" ? true : false );
243249
setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "subgroup" ) ) );
@@ -327,7 +333,9 @@ QStandardItem* QgsComposerGroupItem::clone() const
327333
void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
328334
{
329335
QDomElement layerGroupElem = doc.createElement( "GroupItem" );
336+
// text is always user text, but for forward compatibility for now write both
330337
layerGroupElem.setAttribute( "text", text() );
338+
layerGroupElem.setAttribute( "userText", userText() );
331339
layerGroupElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
332340
writeXMLChildren( layerGroupElem, doc );
333341
elem.appendChild( layerGroupElem );
@@ -339,7 +347,14 @@ void QgsComposerGroupItem::readXML( const QDomElement& itemElem, bool xServerAva
339347
{
340348
return;
341349
}
342-
setText( itemElem.attribute( "text", "" ) );
350+
// text is always user text but for backward compatibility we read also text
351+
QString userText = itemElem.attribute( "userText", "" );
352+
if ( userText.isEmpty() )
353+
{
354+
userText = itemElem.attribute( "text", "" );
355+
}
356+
setText( userText );
357+
setUserText( userText );
343358

344359
setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "group" ) ) );
345360

‎src/core/composer/qgscomposerlegenditem.h

+9
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ class CORE_EXPORT QgsComposerLegendItem: public QStandardItem
5353
QgsComposerLegendStyle::Style style() const { return mStyle; }
5454
void setStyle( QgsComposerLegendStyle::Style style ) { mStyle = style; }
5555

56+
// Get text defined by user
57+
virtual QString userText() const { return mUserText; }
58+
// Set text defined by user
59+
virtual void setUserText( const QString & text ) { mUserText = text; }
60+
61+
5662
protected:
5763
void writeXMLChildren( QDomElement& elem, QDomDocument& doc ) const;
5864

5965
QgsComposerLegendStyle::Style mStyle;
66+
67+
// User defined text
68+
QString mUserText;
6069
};
6170

6271

‎src/core/composer/qgslegendmodel.cpp

+155-26
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ QStandardItem* QgsLegendModel::addGroup( QString text, int position )
121121
text = tr( "Group" );
122122

123123
QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text );
124+
groupItem->setUserText( text );
124125

125126
if ( position == -1 )
126127
{
@@ -159,19 +160,18 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa
159160

160161
QgsLegendSymbolList lst = renderer->legendSymbolItems();
161162
QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
163+
int row = 0;
162164
for ( ; symbolIt != lst.constEnd(); ++symbolIt )
163165
{
164-
QString label = symbolIt->first;
165-
if ( lItem->showFeatureCount() )
166+
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" );
167+
168+
// Get userText from old item if exists
169+
QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( row, 0 ) );
170+
if ( oldSymbolItem )
166171
{
167-
// Add counts to multi symbols layers only or labeled single symbols,
168-
// so that single symbol layers are still drawn on single line
169-
if ( lst.size() > 1 || !label.isEmpty() )
170-
{
171-
label += QString( " [%1]" ).arg( vlayer->featureCount( symbolIt->second ) );
172-
}
172+
currentSymbolItem->setUserText( oldSymbolItem->userText() );
173173
}
174-
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( label );
174+
175175
currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
176176
if ( symbolIt->second )
177177
{
@@ -181,14 +181,23 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa
181181
}
182182
currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
183183
}
184-
layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
184+
layerItem->setChild( row, 0, currentSymbolItem );
185+
186+
// updateSymbolV2ItemText needs layer set
187+
updateSymbolV2ItemText( currentSymbolItem );
188+
189+
row++;
190+
}
191+
192+
// Delete following old items (if current number of items decreased)
193+
for ( int i = layerItem->rowCount() - 1; i >= row; --i )
194+
{
195+
layerItem->removeRow( i );
185196
}
186197

187198
return 0;
188199
}
189200

190-
191-
192201
int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer )
193202
{
194203
if ( !layerItem || !rlayer )
@@ -204,9 +213,18 @@ int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer*
204213

205214
QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
206215
QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
216+
int row = 0;
207217
for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
208218
{
209219
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );
220+
221+
QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) );
222+
if ( oldSymbolItem )
223+
{
224+
currentSymbolItem->setUserText( oldSymbolItem->userText() );
225+
currentSymbolItem->setText( currentSymbolItem->userText() );
226+
}
227+
210228
if ( mHasTopLevelWindow )
211229
{
212230
QPixmap itemPixmap( 20, 20 );
@@ -217,11 +235,76 @@ int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer*
217235
currentSymbolItem->setColor( itemIt->second );
218236
int currentRowCount = layerItem->rowCount();
219237
layerItem->setChild( currentRowCount, 0, currentSymbolItem );
238+
row++;
239+
}
240+
241+
// Delete following old items (if current number of items decreased)
242+
for ( int i = layerItem->rowCount() - 1; i >= row; --i )
243+
{
244+
layerItem->removeRow( i );
220245
}
221246

222247
return 0;
223248
}
224249

250+
void QgsLegendModel::updateSymbolV2ItemText( QStandardItem* symbolItem )
251+
{
252+
QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( symbolItem );
253+
if ( !sv2Item ) return;
254+
255+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( sv2Item->parent() );
256+
if ( !lItem ) return;
257+
258+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
259+
if ( !mapLayer ) return;
260+
261+
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
262+
if ( !vLayer ) return;
263+
264+
QgsFeatureRendererV2* renderer = vLayer->rendererV2();
265+
if ( !renderer ) return;
266+
267+
if ( lItem->showFeatureCount() ) vLayer->countSymbolFeatures();
268+
269+
QgsLegendSymbolList symbolList = renderer->legendSymbolItems();
270+
271+
QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() );
272+
273+
QString label = sv2Item->userText().isEmpty() ? symbol.first : sv2Item->userText();
274+
275+
if ( lItem->showFeatureCount() )
276+
{
277+
// Add counts to multi symbols layers only or labeled single symbols,
278+
// so that single symbol layers are still drawn on single line
279+
if ( symbolList.size() > 1 || !label.isEmpty() )
280+
{
281+
label += QString( " [%1]" ).arg( vLayer->featureCount( symbol.second ) );
282+
}
283+
}
284+
symbolItem->setText( label );
285+
}
286+
287+
void QgsLegendModel::updateRasterSymbolItemText( QStandardItem* symbolItem )
288+
{
289+
QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( symbolItem );
290+
if ( !rItem ) return;
291+
292+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( rItem->parent() );
293+
if ( !lItem ) return;
294+
295+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
296+
if ( !mapLayer ) return;
297+
298+
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
299+
if ( !rLayer ) return;
300+
301+
QPair< QString, QColor> symbol = rLayer->legendSymbologyItems().value( symbolItem->row() );
302+
303+
QString label = rItem->userText().isEmpty() ? symbol.first : rItem->userText();
304+
305+
symbolItem->setText( label );
306+
}
307+
225308
void QgsLegendModel::updateItem( QStandardItem* item )
226309
{
227310
if ( !item )
@@ -243,6 +326,45 @@ void QgsLegendModel::updateItem( QStandardItem* item )
243326
}
244327
}
245328

329+
void QgsLegendModel::updateItemText( QStandardItem* item )
330+
{
331+
if ( !item )
332+
{
333+
return;
334+
}
335+
336+
//only layer items are supported for update
337+
QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
338+
if ( ! cItem )
339+
{
340+
return;
341+
}
342+
343+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( cItem );
344+
if ( lItem )
345+
{
346+
updateLayerItemText( lItem );
347+
return;
348+
}
349+
350+
QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( cItem );
351+
if ( sv2Item )
352+
{
353+
updateSymbolV2ItemText( sv2Item );
354+
return;
355+
}
356+
357+
QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( cItem );
358+
if ( rItem )
359+
{
360+
updateRasterSymbolItemText( rItem );
361+
return;
362+
}
363+
364+
// group
365+
cItem->setText( cItem->userText() );
366+
}
367+
246368
void QgsLegendModel::updateLayer( QStandardItem* layerItem )
247369
{
248370
QgsDebugMsg( "Entered." );
@@ -252,22 +374,9 @@ void QgsLegendModel::updateLayer( QStandardItem* layerItem )
252374
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
253375
if ( mapLayer )
254376
{
255-
//delete all the entries under layer item
256-
int currentRowCount = lItem->rowCount();
257-
for ( int i = currentRowCount - 1; i >= 0; --i )
258-
{
259-
lItem->removeRow( i );
260-
}
261-
262377
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
263378

264-
//set layer name as item text
265-
QString label = mapLayer->name();
266-
if ( vLayer && lItem->showFeatureCount() )
267-
{
268-
label += QString( " [%1]" ).arg( vLayer->featureCount() );
269-
}
270-
layerItem->setText( label );
379+
updateLayerItemText( lItem );
271380

272381
if ( vLayer )
273382
{
@@ -283,6 +392,26 @@ void QgsLegendModel::updateLayer( QStandardItem* layerItem )
283392
}
284393
}
285394

395+
void QgsLegendModel::updateLayerItemText( QStandardItem* layerItem )
396+
{
397+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
398+
if ( !lItem ) return;
399+
400+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
401+
if ( !mapLayer ) return;
402+
403+
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
404+
if ( !vLayer ) return;
405+
406+
QString label = lItem->userText().isEmpty() ? mapLayer->name() : lItem->userText();
407+
408+
if ( vLayer && lItem->showFeatureCount() )
409+
{
410+
label += QString( " [%1]" ).arg( vLayer->featureCount() );
411+
}
412+
lItem->setText( label );
413+
}
414+
286415
void QgsLegendModel::removeLayer( const QString& layerId )
287416
{
288417
int numRootItems = rowCount();

‎src/core/composer/qgslegendmodel.h

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
7272
void updateRasterClassificationItem( QStandardItem* classificationItem )
7373
{ Q_UNUSED( classificationItem ); }
7474

75+
/** Update single item text using item userText and other properties like showFeatureCount */
76+
void updateItemText( QStandardItem* item );
77+
78+
7579
bool writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const;
7680
bool readXML( const QDomElement& legendModelElem, const QDomDocument& doc );
7781

@@ -106,6 +110,11 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
106110
@return 0 in case of success*/
107111
int addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer );
108112

113+
void updateLayerItemText( QStandardItem* layerItem );
114+
void updateSymbolV2ItemText( QStandardItem* symbolItem );
115+
void updateRasterSymbolItemText( QStandardItem* symbolItem );
116+
117+
109118
protected:
110119
QStringList mLayerIds;
111120
/**True if this application has toplevel windows (normally true). If this is false, this means that the application

‎src/ui/qgscomposerlegendwidgetbase.ui

+23-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<property name="geometry">
5555
<rect>
5656
<x>0</x>
57-
<y>-118</y>
57+
<y>0</y>
5858
<width>369</width>
5959
<height>1088</height>
6060
</rect>
@@ -141,17 +141,13 @@
141141
<layout class="QVBoxLayout" name="verticalLayout_2">
142142
<item>
143143
<layout class="QHBoxLayout" name="horizontalLayout">
144-
<item>
145-
<widget class="QToolButton" name="mAddGroupButton">
146-
<property name="text">
147-
<string>Add group</string>
148-
</property>
149-
</widget>
150-
</item>
151144
<item>
152145
<widget class="QToolButton" name="mUpdatePushButton">
146+
<property name="toolTip">
147+
<string>Update legend. User defined labels are preserved.</string>
148+
</property>
153149
<property name="text">
154-
<string>Update</string>
150+
<string>Update item</string>
155151
</property>
156152
<property name="iconSize">
157153
<size>
@@ -163,8 +159,11 @@
163159
</item>
164160
<item>
165161
<widget class="QToolButton" name="mUpdateAllPushButton">
162+
<property name="toolTip">
163+
<string>Update whole legend. Layers are added/removed according to main application legend. User defined labels will be deleted.</string>
164+
</property>
166165
<property name="text">
167-
<string>All</string>
166+
<string>Update all</string>
168167
</property>
169168
</widget>
170169
</item>
@@ -247,6 +246,20 @@
247246
</property>
248247
</widget>
249248
</item>
249+
<item>
250+
<widget class="QToolButton" name="mAddGroupToolButton">
251+
<property name="toolTip">
252+
<string>Add group</string>
253+
</property>
254+
<property name="text">
255+
<string>...</string>
256+
</property>
257+
<property name="icon">
258+
<iconset resource="../../images/images.qrc">
259+
<normaloff>:/images/themes/default/mActionAddGroup.png</normaloff>:/images/themes/default/mActionAddGroup.png</iconset>
260+
</property>
261+
</widget>
262+
</item>
250263
<item>
251264
<widget class="QToolButton" name="mAddToolButton">
252265
<property name="text">

0 commit comments

Comments
 (0)
Please sign in to comment.