Skip to content

Commit 76a2a8b

Browse files
author
mhugent
committed
Change map layer display name if name is changed in the legend
git-svn-id: http://svn.osgeo.org/qgis/trunk@5859 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d9f2c14 commit 76a2a8b

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/gui/qgsmaplayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ QString const & QgsMapLayer::getLayerID() const
112112
}
113113

114114
/** Write property of QString layerName. */
115-
void QgsMapLayer::setLayerName(const QString & _newVal)
115+
void QgsMapLayer::setLayerName(const QString & _newVal, bool updateLegend)
116116
{
117117
QgsDebugMsg("QgsMapLayer::setLayerName: new name is '" + _newVal);
118118
layerName = capitaliseLayerName(_newVal);
119119
// And update the legend if one exists
120-
if (mLegend)
120+
if (mLegend && updateLegend)
121121
mLegend->setName(mLegendLayerFile, layerName);
122122
}
123123

src/gui/qgsmaplayer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ class QgsMapLayer : public QObject
7373

7474
/*! Set the display name of the layer
7575
# @param name New name for the layer
76+
# @param updateLegend false if legend should not be updated
77+
(to avoid infinite recursion when this function is called from the legend itself)
7678
*/
77-
void setLayerName(const QString & name);
79+
void setLayerName(const QString & name, bool updateLegend=true);
7880

7981
/*! Get the display name of the layer
8082
* @return the layer name

src/legend/qgslegend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,18 @@ void QgsLegend::handleItemChange(QTreeWidgetItem* item, int row)
14781478
return;
14791479
}
14801480

1481+
//close the editor (for QgsLegendLayer and QgsLegendGroup)
14811482
closePersistentEditor(item, row);
1483+
//if the text of a QgsLegendLayer has changed, change the display names of all its maplayers
1484+
QgsLegendLayer* theLegendLayer = dynamic_cast<QgsLegendLayer*>(item); //item is a legend layer
1485+
if(theLegendLayer)
1486+
{
1487+
std::list<QgsMapLayer*> theMapLayers = theLegendLayer->mapLayers();
1488+
for(std::list<QgsMapLayer*>::iterator it = theMapLayers.begin(); it != theMapLayers.end(); ++it)
1489+
{
1490+
(*it)->setLayerName(theLegendLayer->text(0), false);
1491+
}
1492+
}
14821493

14831494
std::map<QTreeWidgetItem*, Qt::CheckState>::iterator it = mStateOfCheckBoxes.find(item);
14841495
if(it != mStateOfCheckBoxes.end())

0 commit comments

Comments
 (0)