Skip to content

Commit 594be2d

Browse files
author
mhugent
committedJan 31, 2006

15 files changed

+246
-159
lines changed
 

‎src/core/qgsrenderer.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,16 @@ QgsRenderer::QgsRenderer()
1313

1414
}
1515

16-
void QgsRenderer::refreshLegend(QTreeWidgetItem* legendparent) const
16+
void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const
1717
{
18-
if(legendparent)
18+
if(symbologyList)
1919
{
20-
21-
//first remove the existing childs
22-
//legendparent->takeChildren();
23-
dynamic_cast<QgsLegendItem*>(legendparent)->removeAllChildren();
24-
2520
//add the new items
2621
QString lw, uv, label;
2722
const std::list<QgsSymbol*> sym = symbols();
2823

2924
for(std::list<QgsSymbol*>::const_iterator it=sym.begin(); it!=sym.end(); ++it)
3025
{
31-
QgsLegendVectorSymbologyItem* item = new QgsLegendVectorSymbologyItem(legendparent, "");
32-
item->addSymbol(*it);
33-
3426
QPixmap pix;
3527
if((*it)->type() == QGis::Point)
3628
{
@@ -45,8 +37,8 @@ void QgsRenderer::refreshLegend(QTreeWidgetItem* legendparent) const
4537
pix = (*it)->getPolygonSymbolAsPixmap();
4638
}
4739

48-
QIcon theIcon(pix);
49-
item->setIcon(0, theIcon);
40+
QIcon* theIcon = new QIcon(pix);
41+
5042
QString values;
5143
lw = (*it)->lowerValue();
5244
if(!lw.isEmpty())
@@ -65,7 +57,7 @@ void QgsRenderer::refreshLegend(QTreeWidgetItem* legendparent) const
6557
values += " ";
6658
values += label;
6759
}
68-
item->setText(0, values);
60+
symbologyList->push_back(std::make_pair(values, theIcon));
6961
}
7062
}
7163
}

‎src/core/qgsrenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class QgsRenderer
7575
virtual const std::list<QgsSymbol*> symbols() const=0;
7676
/**Deletes the child items of the legendparent and add new ones according to the
7777
QgsSymbols contained in this renderer*/
78-
virtual void refreshLegend(QTreeWidgetItem* legendparent) const;
78+
virtual void refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const;
7979
/**Returns a copy of the renderer (a deep copy on the heap)*/
8080
virtual QgsRenderer* clone() const=0;
8181
/**Color to draw selected features - static so we can change it in proj props and automatically

‎src/gui/qgsmapcanvas.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,10 +2340,9 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
23402340
#ifdef QGISDEBUG
23412341
std::cout << "QgsMapCanvas::mouseReleaseEvent: Completed vlayer->insertVertexBefore." << std::endl;
23422342
#endif
2343+
refresh();
23432344
}
2344-
}
2345-
2346-
// TODO: Redraw?
2345+
}
23472346
break;
23482347
}
23492348

@@ -2396,8 +2395,7 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
23962395
point.x(), point.y(),
23972396
mCanvasProperties->snappedAtFeatureId,
23982397
mCanvasProperties->snappedAtVertex);
2399-
render();
2400-
update();
2398+
refresh();
24012399

24022400
#ifdef QGISDEBUG
24032401
std::cout << "QgsMapCanvas::mouseReleaseEvent: Completed vlayer->moveVertexAt." << std::endl;
@@ -2458,10 +2456,9 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
24582456
#ifdef QGISDEBUG
24592457
std::cout << "QgsMapCanvas::mouseReleaseEvent: Completed vlayer->deleteVertexAt." << std::endl;
24602458
#endif
2459+
refresh();
24612460
}
2462-
}
2463-
// TODO: Redraw?
2464-
2461+
}
24652462
break;
24662463
}
24672464

‎src/gui/qgsmaplayer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "qgssymbol.h"
3838
#include "qgsmaplayer.h"
3939
#include "qgslegendlayerfile.h"
40-
#include "qgslegendsymbologygroup.h"
4140

4241

4342

@@ -52,7 +51,7 @@ QgsMapLayer::QgsMapLayer(int type,
5251
mShowInOverviewAction(0),
5352
mShowInOverview(false),
5453
mCoordinateTransform(0),
55-
mLegendSymbologyGroupParent(0),
54+
mLegend(0),
5655
mLegendLayerFile(0),
5756
ID(""),
5857
layerType(type),

‎src/gui/qgsmaplayer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class QAction;
3434
class QgisApp;
3535
class QgsMapToPixel;
3636
class QgsFeature;
37+
class QgsLegend;
3738
class QgsLegendLayerFile;
38-
class QgsLegendSymbologyGroup;
3939
class QDomNode;
4040
class QDomDocument;
4141
class QKeyEvent;
@@ -268,8 +268,8 @@ class QgsMapLayer : public QObject
268268
/**Returns the path to an icon which characterises the type of layer*/
269269
virtual QString layerTypeIconPath() = 0;
270270

271-
void setLegendSymbologyGroupParent(QgsLegendSymbologyGroup* item) {mLegendSymbologyGroupParent = item;}
272-
const QgsLegendSymbologyGroup* legendSymbologyGroupParent() {return mLegendSymbologyGroupParent;}
271+
void setLegend(QgsLegend* legend) {mLegend = legend;}
272+
const QgsLegend* legend() {return mLegend;}
273273

274274
/**Refresh the symbology part of the legend. Specific implementations have to be provided by subclasses*/
275275
virtual void refreshLegend() = 0;
@@ -438,9 +438,8 @@ public slots:
438438
//! A QgsCoordinateTransform is used for on the fly reprojection of map layers
439439
QgsCoordinateTransform * mCoordinateTransform;
440440

441-
/**Pointer to the symbology group item of the legend. This pointer is used if refreshLegend()
442-
is called by a subclass*/
443-
QgsLegendSymbologyGroup* mLegendSymbologyGroupParent;
441+
/**Pointer to the legend layer item of the legend*/
442+
QgsLegend* mLegend;
444443

445444
/**Pointer to the legend layer file of the legend. It is used to modify the pixmap with overview
446445
glasses, editing or pyramid symbols*/

‎src/gui/qgsvectorlayer.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@
6464
#include "qgsattributetable.h"
6565
#include "qgsfeature.h"
6666
#include "qgsfield.h"
67-
#include "qgslegenditem.h"
68-
#include "qgslegendvectorsymbologyitem.h"
69-
#include "qgslegendsymbologygroup.h"
67+
#include "qgslegend.h"
7068
#include "qgsvectorlayerproperties.h"
7169
#include "qgsrenderer.h"
7270
#include "qgssinglesymrenderer.h"
@@ -2865,9 +2863,27 @@ QString QgsVectorLayer::layerTypeIconPath()
28652863

28662864
void QgsVectorLayer::refreshLegend()
28672865
{
2868-
if(mLegendSymbologyGroupParent && m_renderer)
2866+
if(mLegend && m_renderer)
2867+
{
2868+
std::list< std::pair<QString, QIcon*> > itemList;
2869+
m_renderer->refreshLegend(&itemList);
2870+
if(m_renderer->needsAttributes()) //create an item for each classification field (only one for most renderers)
2871+
{
2872+
std::list<int> classfieldlist = m_renderer->classificationAttributes();
2873+
for(std::list<int>::iterator it = classfieldlist.begin(); it!=classfieldlist.end(); ++it)
2874+
{
2875+
const QgsField theField = (dataProvider->fields())[*it];
2876+
QString classfieldname = theField.name();
2877+
itemList.push_front(std::make_pair(classfieldname, (QIcon*)0));
2878+
}
2879+
}
2880+
mLegend->changeSymbologySettings(getLayerID(), &itemList);
2881+
}
2882+
2883+
#if 0
2884+
if(mLegendLayer && m_renderer)
28692885
{
2870-
m_renderer->refreshLegend(mLegendSymbologyGroupParent);
2886+
m_renderer->refreshLegend(mLegendLayer);
28712887
}
28722888

28732889
//create an item for each classification field (currently only one for all renderers)
@@ -2882,15 +2898,16 @@ void QgsVectorLayer::refreshLegend()
28822898
QString classfieldname = theField.name();
28832899
QgsLegendSymbologyItem* item = new QgsLegendSymbologyItem();
28842900
item->setText(0, classfieldname);
2885-
mLegendSymbologyGroupParent->insertChild(0, item);
2901+
static_cast<QTreeWidgetItem*>(mLegendLayer)->insertChild(0, item);
28862902
}
28872903
}
28882904
}
2889-
if(mLegendSymbologyGroupParent)
2905+
if(mLegendLayer)
28902906
{
28912907
//copy the symbology changes for the other layers in the same symbology group
2892-
mLegendSymbologyGroupParent->updateLayerSymbologySettings(this);
2908+
mLegendLayer->updateLayerSymbologySettings(this);
28932909
}
2910+
#endif
28942911
}
28952912

28962913
bool QgsVectorLayer::copySymbologySettings(const QgsMapLayer& other)

‎src/legend/qgslegend.cpp

Lines changed: 108 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "qgslegendgroup.h"
2525
#include "qgslegendlayer.h"
2626
#include "qgslegendpropertygroup.h"
27-
#include "qgslegendsymbologygroup.h"
27+
#include "qgslegendsymbologyitem.h"
2828
#include "qgslegendlayerfile.h"
2929
#include "qgslegendlayerfilegroup.h"
3030
#include "qgsmapcanvas.h"
@@ -52,7 +52,7 @@ const int AUTOSCROLL_MARGIN = 16;
5252
set mItemBeingMoved pointer to 0 to prevent SuSE 9.0 crash
5353
*/
5454
QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
55-
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0)
55+
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0), mShowLegendLayerFiles(false)
5656
{
5757
connect( this, SIGNAL(selectionChanged(QTreeWidgetItem *)),
5858
this, SLOT(updateLegendItem(QTreeWidgetItem *)) );
@@ -270,10 +270,8 @@ void QgsLegend::mouseReleaseEvent(QMouseEvent * e)
270270
if(originType == QgsLegendItem::LEGEND_LAYER_FILE && destType == QgsLegendItem::LEGEND_LAYER_FILE_GROUP)
271271
{
272272
QgsMapLayer* origLayer = ((QgsLegendLayerFile*)(origin))->layer();
273-
274-
if(origLayer->legendSymbologyGroupParent() != dest->nextSibling())
273+
if(1) //todo: find a test to avoid that symbology settings are copied if an item is moved within the same legend layer
275274
{
276-
origLayer->setLegendSymbologyGroupParent((QgsLegendSymbologyGroup*)(dest->nextSibling()));
277275
if(dest->childCount() > 1)
278276
{
279277
//find the first layer in the legend layer group != origLayer and copy its settings
@@ -296,11 +294,11 @@ void QgsLegend::mouseReleaseEvent(QMouseEvent * e)
296294
{
297295
QgsMapLayer* origLayer = ((QgsLegendLayerFile*)(origin))->layer();
298296
QgsMapLayer* destLayer = ((QgsLegendLayerFile*)(dest))->layer();
297+
299298
if(dest == origin)//origin item has been moved in mouseMoveEvent such that it is under the mouse cursor now
300299
{
301-
if(origLayer->legendSymbologyGroupParent() != origin->parent())
302-
{
303-
origLayer->setLegendSymbologyGroupParent((QgsLegendSymbologyGroup*)(dynamic_cast<QgsLegendItem*>(origin->parent())->nextSibling()));
300+
if(1) //todo: find a test to avoid that symbology settings are copied if an item is moved within the same legend layer
301+
{
304302
if(origin->parent()->childCount() > 1)
305303
{
306304
//find the first layer in the legend layer group != origLayer and copy its settings
@@ -324,7 +322,7 @@ void QgsLegend::mouseReleaseEvent(QMouseEvent * e)
324322
QgsMapLayer* origLayer = ((QgsLegendLayerFile*)(origin))->layer();
325323
QgsMapLayer* destLayer = ((QgsLegendLayerFile*)(dest))->layer();
326324
origLayer->copySymbologySettings(*destLayer);
327-
origLayer->setLegendSymbologyGroupParent((QgsLegendSymbologyGroup*)(dynamic_cast<QgsLegendItem*>(dest->parent())->nextSibling()));
325+
origLayer->setLegend((QgsLegend*)(dynamic_cast<QgsLegendItem*>(dest->parent())->nextSibling()));
328326
}
329327
}
330328
emit zOrderChanged(this);
@@ -414,7 +412,11 @@ void QgsLegend::handleRightClickEvent(QTreeWidgetItem* item, const QPoint& posit
414412
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/folder_new.png"))), tr("&Add group"), this, SLOT(addGroup()));
415413
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionExpandTree.png"))), tr("&Expand all"), this, SLOT(expandAll()));
416414
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionCollapseTree.png"))), tr("&Collapse all"), this, SLOT(collapseAll()));
417-
415+
QAction* showFileGroupsAction = theMenu.addAction(tr("Show file groups"), this, SLOT(showLegendLayerFileGroups(bool)));
416+
showFileGroupsAction->setCheckable(true);
417+
showFileGroupsAction->blockSignals(true);
418+
showFileGroupsAction->setChecked(mShowLegendLayerFiles);
419+
showFileGroupsAction->blockSignals(false);
418420
theMenu.exec(position);
419421
}
420422

@@ -456,18 +458,20 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
456458
}
457459
blockSignals(false);
458460

459-
QgsLegendSymbologyGroup * lsgroup = new QgsLegendSymbologyGroup(llayer,QString("Symbology"));
460-
layer->setLegendSymbologyGroupParent(lsgroup);
461-
QgsLegendPropertyGroup * lpgroup = new QgsLegendPropertyGroup(llayer,QString("Properties"));
461+
layer->setLegend(this);
462+
//QgsLegendPropertyGroup * lpgroup = new QgsLegendPropertyGroup(llayer,QString("Properties"));
462463
layer->setLegendLayerFile(llfile);
463464
layer->initContextMenu(mApp);
464465

465466
insertTopLevelItem(0, llayer);
466467

467-
setExpanded(indexFromItem(llayer), false);
468-
setExpanded(indexFromItem(lpgroup), true);
469-
setExpanded(indexFromItem(lsgroup), true);
470-
setExpanded(indexFromItem(llfgroup), true);
468+
setExpanded(indexFromItem(llayer), true);
469+
setExpanded(indexFromItem(llfgroup), false);
470+
//todo: only if qsetting for 'legend layer file visible' is not set
471+
if(!mShowLegendLayerFiles)
472+
{
473+
setItemHidden(llfgroup, true);
474+
}
471475
}
472476

473477
QgsMapLayer* QgsLegend::currentLayer()
@@ -650,6 +654,7 @@ void QgsLegend::collapseAll()
650654

651655
bool QgsLegend::writeXML( QDomNode & layer_node, QDomDocument & document )
652656
{
657+
#if 0
653658
QDomElement legendnode = document.createElement("legend");
654659
layer_node.appendChild(legendnode);
655660

@@ -780,11 +785,13 @@ bool QgsLegend::writeXML( QDomNode & layer_node, QDomDocument & document )
780785
}
781786
currentItem = nextItem(currentItem);
782787
}
788+
#endif
783789
return true;
784790
}
785791

786792
bool QgsLegend::readXML(QDomNode& legendnode)
787793
{
794+
#if 0
788795
QDomElement childelem;
789796
QDomNode child;
790797
QgsLegendGroup* lastGroup = 0; //pointer to the last inserted group
@@ -910,6 +917,7 @@ bool QgsLegend::readXML(QDomNode& legendnode)
910917
}
911918
while(!(child.isNull()));
912919
}
920+
#endif
913921
return true;
914922
}
915923

@@ -1193,6 +1201,30 @@ void QgsLegend::resetToInitialPosition(QTreeWidgetItem* li)
11931201
}
11941202
}
11951203

1204+
QgsLegendLayer* QgsLegend::findLegendLayer(const QString& layerKey)
1205+
{
1206+
QgsLegendLayer* theLegendLayer = 0;
1207+
std::list<QgsMapLayer*> theMapLayers;
1208+
QTreeWidgetItem* theItem = firstItem();
1209+
do
1210+
{
1211+
theLegendLayer = dynamic_cast<QgsLegendLayer*>(theItem);
1212+
if(theLegendLayer) //item is a legend layer
1213+
{
1214+
theMapLayers = theLegendLayer->mapLayers();
1215+
for(std::list<QgsMapLayer*>::iterator it = theMapLayers.begin(); it != theMapLayers.end(); ++it)
1216+
{
1217+
if((*it)->getLayerID() == layerKey)
1218+
{
1219+
return theLegendLayer;
1220+
}
1221+
}
1222+
}
1223+
}
1224+
while(theItem = nextItem(theItem));
1225+
return 0;
1226+
}
1227+
11961228
bool QgsLegend::yCoordAboveCenter(QgsLegendItem* it, int ycoord)
11971229
{
11981230
QRect rect = visualItemRect(it);
@@ -1363,6 +1395,49 @@ std::deque<QString> QgsLegend::layerIDs()
13631395
return layers;
13641396
}
13651397

1398+
void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems)
1399+
{
1400+
QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(key);
1401+
if(!theMapLayer)
1402+
{
1403+
return;
1404+
}
1405+
QgsLegendLayer* theLegendLayer = findLegendLayer(key);
1406+
QgsLegendSymbologyItem* theSymbologyItem = 0;
1407+
if(!theLegendLayer)
1408+
{
1409+
return;
1410+
}
1411+
1412+
//remove the symbology items under the legend layer
1413+
for(int i = theLegendLayer->childCount(); i >= 0; --i)
1414+
{
1415+
theSymbologyItem = dynamic_cast<QgsLegendSymbologyItem*>((theLegendLayer)->child(i));
1416+
if(theSymbologyItem)
1417+
{
1418+
theLegendLayer->takeChild(i);
1419+
}
1420+
}
1421+
1422+
//add the new symbology items
1423+
if(newSymbologyItems)
1424+
{
1425+
int childposition = 0; //position to insert the items
1426+
for(std::list< std::pair<QString, QIcon*> >::const_iterator it= newSymbologyItems->begin(); it != newSymbologyItems->end(); ++it)
1427+
{
1428+
QgsLegendSymbologyItem* theItem = new QgsLegendSymbologyItem();
1429+
theItem->setText(0, it->first);
1430+
theItem->setIcon(0, *(it->second));
1431+
theLegendLayer->insertChild(childposition, theItem);
1432+
delete (it->second);//free the memory for the QIcon*
1433+
++childposition;
1434+
}
1435+
}
1436+
1437+
//copy the legend settings for the other layer files in the same legend layer
1438+
theLegendLayer->updateLayerSymbologySettings(theMapLayer);
1439+
}
1440+
13661441
void QgsLegend::handleItemChange(QTreeWidgetItem* item, int row)
13671442
{
13681443
closePersistentEditor(item, row);
@@ -1462,3 +1537,19 @@ void QgsLegend::makeToTopLevelItem()
14621537
addTopLevelItem(theItem);
14631538
}
14641539
}
1540+
1541+
void QgsLegend::showLegendLayerFileGroups(bool show)
1542+
{
1543+
QgsLegendLayerFileGroup* theFileGroup = 0;
1544+
QTreeWidgetItem* theItem = firstItem();
1545+
do
1546+
{
1547+
theFileGroup = dynamic_cast<QgsLegendLayerFileGroup*>(theItem);
1548+
if(theFileGroup)
1549+
{
1550+
setItemHidden(theFileGroup, !show);
1551+
}
1552+
}
1553+
while(theItem = nextItem(theItem));
1554+
mShowLegendLayerFiles = show;
1555+
}

‎src/legend/qgslegend.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QTreeWidget>
2626

2727
class QgisApp;
28+
class QgsLegendLayer;
2829
class QgsLegendItem;
2930
class QgsMapLayer;
3031
class QgsMapCanvas;
@@ -132,6 +133,10 @@ class QgsLegend : public QTreeWidget
132133
/**Returns the ids of the layers contained in this legend. The order is bottom->top*/
133134
std::deque<QString> layerIDs();
134135

136+
/**Removes the symbology items of a layer and adds new ones. If other files are in the same legend layer, the new symbology settings are copied.
137+
Note: the QIcon* are deleted and therefore need to be allocated by calling functions using operator new*/
138+
void changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems);
139+
135140
public slots:
136141

137142
/*!Adds a new layer group with the maplayer to the canvas*/
@@ -209,6 +214,9 @@ this item may be moved back to the original position with resetToInitialPosition
209214
/**Moves an item back to the position where storeInitialPosition has been called*/
210215
void resetToInitialPosition(QTreeWidgetItem* li);
211216

217+
/**Returns the legend layer to which a map layer gelongs*/
218+
QgsLegendLayer* findLegendLayer(const QString& layerKey);
219+
212220
private slots:
213221

214222
/**Calls 'handleRightClickEvent' on the item*/
@@ -231,7 +239,8 @@ this item may be moved back to the original position with resetToInitialPosition
231239
void openEditor();
232240
/**Removes the current item and inserts it as a toplevel item at the end of the legend*/
233241
void makeToTopLevelItem();
234-
242+
/**Show/ Hide the legend layer file groups*/
243+
void showLegendLayerFileGroups(bool show);
235244
private:
236245

237246
/**Pointer to QGisApp, needed for signal/slot reasons*/
@@ -278,6 +287,8 @@ this item may be moved back to the original position with resetToInitialPosition
278287
HIERARCHY_POSITION_TYPE mRestoreInformation;
279288
QTreeWidgetItem* mRestoreItem;
280289

290+
bool mShowLegendLayerFiles;
291+
281292
/*!
282293
* A fuction sed to determin how far down in the list an item is (starting with one for the first Item.
283294
*If the item is not in the legend, -1 is returned

‎src/legend/qgslegendlayer.cpp

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,30 @@
1919
***************************************************************************/
2020
#include "qgslegendlayer.h"
2121
#include "qgslegendlayerfile.h"
22+
#include "qgslegendlayerfilegroup.h"
2223
#include "qgsmaplayer.h"
2324
#include <iostream>
2425
#include <QCoreApplication>
2526
#include <QIcon>
2627

2728
QgsLegendLayer::QgsLegendLayer(QTreeWidgetItem* parent,QString name)
28-
: QObject(), QgsLegendItem(parent, name)
29+
: QgsLegendItem(parent, name)
2930
{
3031
mType=LEGEND_LAYER;
3132
setFlags(Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
3233
setCheckState (0, Qt::Checked);
3334
setText(0, name);
3435
}
3536

36-
QgsLegendLayer::QgsLegendLayer(QTreeWidget* parent, QString name): QObject(), QgsLegendItem(parent, name)
37+
QgsLegendLayer::QgsLegendLayer(QTreeWidget* parent, QString name): QgsLegendItem(parent, name)
3738
{
3839
mType=LEGEND_LAYER;
3940
setFlags(Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
4041
setCheckState (0, Qt::Checked);
4142
setText(0, name);
4243
}
4344

44-
QgsLegendLayer::QgsLegendLayer(QString name): QObject(), QgsLegendItem()
45+
QgsLegendLayer::QgsLegendLayer(QString name): QgsLegendItem()
4546
{
4647
mType=LEGEND_LAYER;
4748
setFlags(Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
@@ -103,12 +104,23 @@ QgsLegendItem::DRAG_ACTION QgsLegendLayer::accept(const QgsLegendItem* li) const
103104

104105
QgsMapLayer* QgsLegendLayer::firstMapLayer()
105106
{
106-
QTreeWidgetItem* llfgroup = QTreeWidgetItem::child(0); //the legend layer file group
107-
if(!llfgroup)
107+
//first find the legend layer file group
108+
QgsLegendLayerFileGroup* llfg = 0;
109+
for(int i = 0; i < childCount(); ++i)
110+
{
111+
llfg = dynamic_cast<QgsLegendLayerFileGroup*>(child(i));
112+
if(llfg)
113+
{
114+
break;
115+
}
116+
}
117+
118+
if(!llfg)
108119
{
109120
return 0;
110121
}
111-
QTreeWidgetItem* llf = llfgroup->child(0);
122+
123+
QTreeWidgetItem* llf = llfg->child(0);
112124
if(!llf)
113125
{
114126
return 0;
@@ -133,54 +145,51 @@ std::list<QgsMapLayer*> QgsLegendLayer::mapLayers()
133145
list.push_back((*it)->layer());
134146
}
135147
return list;
148+
}
136149

137-
#if 0
138-
QTreeWidgetItem* llfgroup = QTreeWidgetItem::child(0); //the legend layer file group
139-
if(!llfgroup)
140-
{
141-
return list;
142-
}
143-
QgsLegendItem* llf = dynamic_cast<QgsLegendItem*>(llfgroup->child(0));
144-
if(!llf)
150+
std::list<QgsLegendLayerFile*> QgsLegendLayer::legendLayerFiles()
151+
{
152+
std::list<QgsLegendLayerFile*> list;
153+
154+
//find the layer file group
155+
QgsLegendLayerFileGroup* theLayerGroup = 0;
156+
for(int i = 0; i < childCount(); ++i)
145157
{
146-
return list;
158+
theLayerGroup = dynamic_cast<QgsLegendLayerFileGroup*>(child(i));
159+
if(theLayerGroup)
160+
{
161+
break;
162+
}
147163
}
148-
QgsLegendLayerFile* legendlayerfile = 0;
149-
do
164+
165+
//add all the legend layer files in the group
166+
if(theLayerGroup)
150167
{
151-
legendlayerfile = dynamic_cast<QgsLegendLayerFile*>(llf);
152-
if(legendlayerfile)
168+
QgsLegendLayerFile* theFile = 0;
169+
for(int i = 0; i < theLayerGroup->childCount(); ++i)
153170
{
154-
list.push_back(legendlayerfile->layer());
171+
theFile = dynamic_cast<QgsLegendLayerFile*>(theLayerGroup->child(i));
172+
if(theFile)
173+
{
174+
list.push_back(theFile);
175+
}
155176
}
156177
}
157-
while(llf = llf->nextSibling());
158-
return list;
159-
#endif
178+
return list;
160179
}
161180

162-
std::list<QgsLegendLayerFile*> QgsLegendLayer::legendLayerFiles()
181+
void QgsLegendLayer::updateLayerSymbologySettings(const QgsMapLayer* mapLayer)
163182
{
164-
std::list<QgsLegendLayerFile*> list;
165-
QTreeWidgetItem* llfgroup = QTreeWidgetItem::child(0); //the legend layer file group
166-
if(!llfgroup)
183+
if(mapLayer)
167184
{
168-
return list;
169-
}
170-
QgsLegendItem* llf = dynamic_cast<QgsLegendItem*>(llfgroup->child(0));
171-
if(!llf)
172-
{
173-
return list;
174-
}
175-
QgsLegendLayerFile* legendlayerfile = 0;
176-
do
177-
{
178-
legendlayerfile = dynamic_cast<QgsLegendLayerFile*>(llf);
179-
if(legendlayerfile)
185+
//find all layers
186+
std::list<QgsMapLayer*> theMapLayers = mapLayers();
187+
for(std::list<QgsMapLayer*>::iterator it = theMapLayers.begin(); it != theMapLayers.end(); ++it)
180188
{
181-
list.push_back(legendlayerfile);
189+
if((*it) != mapLayer)
190+
{
191+
(*it)->copySymbologySettings(*mapLayer);
192+
}
182193
}
183194
}
184-
while(llf = llf->nextSibling());
185-
return list;
186195
}

‎src/legend/qgslegendlayer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Container for layer, including layer file(s), symbology class breaks and propert
3535
3636
@author Tim Sutton
3737
*/
38-
class QgsLegendLayer : public QgsLegendItem, public QObject //for signal/ slot
38+
class QgsLegendLayer : public QgsLegendItem
3939
{
4040
public:
4141
QgsLegendLayer(QTreeWidgetItem * ,QString);
@@ -55,6 +55,10 @@ class QgsLegendLayer : public QgsLegendItem, public QObject //for signal/ slot
5555
std::list<QgsMapLayer*> mapLayers();
5656
/**Returns the legend layer file items associated with this legend layer*/
5757
std::list<QgsLegendLayerFile*> legendLayerFiles();
58+
/**Copies the symbology settings of the layer to all maplayers in the QgsLegendLayerFileGroup.
59+
This method should be called whenever a layer in this group changes it symbology settings
60+
(normally from QgsMapLayer::refreshLegend)*/
61+
void updateLayerSymbologySettings(const QgsMapLayer* mapLayer);
5862
};
5963

6064
#endif

‎src/legend/qgslegendlayerfilegroup.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ bool QgsLegendLayerFileGroup::insert(QgsLegendItem* newItem, bool changesettings
8484
}
8585
QgsMapLayer* newLayer = (dynamic_cast<QgsLegendLayerFile*>(newItem))->layer();
8686
QTreeWidgetItem* nexts = nextSibling();
87-
if(nexts)
87+
/*if(nexts)
8888
{
8989
QgsLegendSymbologyGroup* sg = dynamic_cast<QgsLegendSymbologyGroup*>(nexts);
9090
if(sg)
9191
{
9292
newLayer->setLegendSymbologyGroupParent(sg);
9393
newLayer->refreshLegend();
9494
}
95-
}
95+
}*/
9696
return true;
9797
}
9898
//if there are already legend layer files, copy the symbology settings if the two layers are
@@ -130,14 +130,14 @@ bool QgsLegendLayerFileGroup::insert(QgsLegendItem* newItem, bool changesettings
130130
return true;
131131
}
132132
QTreeWidgetItem* nexts = nextSibling();
133-
if(nexts)
133+
/*if(nexts)
134134
{
135135
QgsLegendSymbologyGroup* sg = dynamic_cast<QgsLegendSymbologyGroup*>(nexts);
136136
if(sg)
137137
{
138138
newLayer->setLegendSymbologyGroupParent(sg);
139139
}
140-
}
140+
}*/
141141
return true;
142142
}
143143
else
@@ -151,3 +151,16 @@ bool QgsLegendLayerFileGroup::insert(QgsLegendItem* newItem, bool changesettings
151151
}
152152
}
153153

154+
bool QgsLegendLayerFileGroup::containsLegendLayerFile(const QgsLegendLayerFile* llf) const
155+
{
156+
bool result = false;
157+
for(int i = 0; i < childCount(); ++i)
158+
{
159+
if(llf == child(i))
160+
{
161+
result = true;
162+
break;
163+
}
164+
}
165+
return result;
166+
}

‎src/legend/qgslegendlayerfilegroup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "qgslegenditem.h"
1919

20+
class QgsLegendLayerFile;
21+
2022
class QgsLegendLayerFileGroup: public QgsLegendItem
2123
{
2224
public:
@@ -25,4 +27,6 @@ class QgsLegendLayerFileGroup: public QgsLegendItem
2527
DRAG_ACTION accept(LEGEND_ITEM_TYPE type);
2628
QgsLegendItem::DRAG_ACTION accept(const QgsLegendItem* li) const;
2729
bool insert(QgsLegendItem* theItem, bool changesettings = true);
30+
/**Returns true if llf is a childelement*/
31+
bool containsLegendLayerFile(const QgsLegendLayerFile* llf) const;
2832
};

‎src/legend/qgslegendsymbologygroup.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,43 +64,3 @@ int QgsLegendSymbologyGroup::compare (QTreeWidgetItem * i,int col, bool ascendin
6464
}
6565
}
6666

67-
void QgsLegendSymbologyGroup::updateLayerSymbologySettings(const QgsMapLayer* thelayer)
68-
{
69-
//find the legend layer group node
70-
QgsLegendItem* parent = dynamic_cast<QgsLegendItem*>(this->parent());
71-
if(!parent)
72-
{
73-
return;
74-
}
75-
QgsLegendItem* sibling = 0;
76-
QgsLegendLayerFileGroup* group = 0;
77-
for(sibling = parent->firstChild(); sibling != 0; sibling = sibling->nextSibling())
78-
{
79-
group = dynamic_cast<QgsLegendLayerFileGroup*>(sibling);
80-
if(group)
81-
{
82-
break;
83-
}
84-
}
85-
86-
if(!group)
87-
{
88-
return;
89-
}
90-
91-
//go through all the entries and apply QgsMapLayer::copySymbologySettings
92-
QgsLegendLayerFile* f = 0;
93-
QgsMapLayer* mylayer = 0;
94-
for(sibling = group->firstChild(); sibling != 0; sibling = sibling->nextSibling())
95-
{
96-
f = dynamic_cast<QgsLegendLayerFile*>(sibling);
97-
if( f && (f->layer() != thelayer))
98-
{
99-
mylayer = f->layer();
100-
if(mylayer)
101-
{
102-
mylayer->copySymbologySettings(*thelayer);
103-
}
104-
}
105-
}
106-
}

‎src/legend/qgslegendsymbologygroup.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ class QgsLegendSymbologyGroup : public QgsLegendItem
3939
* @note The symbology group must always be the second in the list
4040
*/
4141
int compare (QTreeWidgetItem * i,int col, bool ascending);
42-
/**Copies the symbology settings of the layer to all maplayers in the QgsLegendLayerFileGroup.
43-
This method should be called whenever a layer in this group changes it symbology settings
44-
(normally from QgsMapLayer::refreshLegend)*/
45-
void updateLayerSymbologySettings(const QgsMapLayer* thelayer);
4642
};
4743

4844
#endif

‎src/raster/qgsrasterlayer.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ When compiling you can make sure DEBUG is defined by including -DDEBUG in the gc
5959
wish to see edbug messages printed to stdout.
6060
6161
*/
62-
#include "qgslegendsymbologygroup.h"
63-
#include "qgslegendsymbologyitem.h"
62+
#include "qgslegend.h"
63+
#include "qgslegendlayer.h"
6464
#include "qgsrasterlayer.h"
6565
#include "qgsmaptopixel.h"
6666
//Added by qt3to4:
@@ -5201,17 +5201,12 @@ QString QgsRasterLayer::layerTypeIconPath()
52015201

52025202
void QgsRasterLayer::refreshLegend()
52035203
{
5204-
if(mLegendSymbologyGroupParent)
5204+
if(mLegend)
52055205
{
5206-
//first remove the existing child item (currently there is always one for rasterlayers)
5207-
QTreeWidgetItem* myChild = mLegendSymbologyGroupParent->child(0);
5208-
delete myChild;
5209-
5210-
QgsLegendSymbologyItem* item = new QgsLegendSymbologyItem(mLegendSymbologyGroupParent, "");
5211-
QIcon theIcon(getLegendQPixmap(true));
5212-
item->setIcon(0, theIcon);
5213-
//copy the symbology changes for the other layers in the same symbology group
5214-
mLegendSymbologyGroupParent->updateLayerSymbologySettings(this);
5206+
std::list< std::pair<QString, QIcon*> > itemList;
5207+
QIcon* theIcon = new QIcon(getLegendQPixmap(true));
5208+
itemList.push_back(std::make_pair("", theIcon));
5209+
mLegend->changeSymbologySettings(getLayerID(), &itemList);
52155210
}
52165211
}
52175212

0 commit comments

Comments
 (0)
Please sign in to comment.