Skip to content

Commit 6bef78f

Browse files
committed
Allow buiding of QgsLegendModel from layer tree
1 parent 36b4f16 commit 6bef78f

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

python/core/composer/qgslegendmodel.sip

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ class QgsLegendModel : QStandardItemModel
1919
QgsLegendModel();
2020
~QgsLegendModel();
2121

22-
/**Sets layer set and groups*/
23-
void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList<QString> > >& groupInfo );
22+
/** Set layers and groups from a layer tree
23+
* @note added in 2.6
24+
*/
25+
void setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup );
26+
/** Sets layer set and groups
27+
* @deprecated in 2.6
28+
*/
29+
void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList<QString> > >& groupInfo ) /Deprecated/;
2430
void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" );
2531
/**Adds a group
2632
@param text name of group (defaults to translation of "Group")
2733
@param position insertion position (toplevel position (or -1 if it should be placed at the end of the legend).
2834
@returns a pointer to the added group
2935
*/
30-
QStandardItem *addGroup( QString text = QString::null, int position = -1 );
36+
QStandardItem *addGroup( QString text = QString::null, int position = -1, QStandardItem* parentItem = 0 );
3137

3238
/**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/
3339
void updateItem( QStandardItem* item );
@@ -63,7 +69,7 @@ class QgsLegendModel : QStandardItemModel
6369

6470
public slots:
6571
void removeLayer( const QString& layerId );
66-
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "" );
72+
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "", QStandardItem* parentItem = 0 );
6773

6874
signals:
6975
void layersChanged();

src/app/composer/qgscomposerlegendwidget.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#include <QFontDialog>
2727
#include <QColorDialog>
2828

29-
#include "qgsapplegendinterface.h"
3029
#include "qgisapp.h"
30+
#include "qgsapplication.h"
3131
#include "qgsmapcanvas.h"
3232
#include "qgsmaplayerregistry.h"
3333
#include "qgsmaprenderer.h"
34-
#include "qgsapplication.h"
34+
#include "qgsproject.h"
3535
#include "qgsvectorlayer.h"
3636

3737
#include <QMessageBox>
@@ -916,9 +916,7 @@ void QgsComposerLegendWidget::updateLegend()
916916

917917

918918
//and also group info
919-
QgsAppLegendInterface legendIface( app->layerTreeView() );
920-
QList< GroupLayerInfo > groupInfo = legendIface.groupLayerRelationship();
921-
mLegend->model()->setLayerSetAndGroups( layerIdList, groupInfo );
919+
mLegend->model()->setLayerSetAndGroups( QgsProject::instance()->layerTreeRoot() );
922920
mLegend->endCommand();
923921
}
924922
}

src/core/composer/qgslegendmodel.cpp

+37-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgslegendmodel.h"
1919
#include "qgscomposerlegenditem.h"
2020
#include "qgsfield.h"
21+
#include "qgslayertree.h"
2122
#include "qgsmaplayer.h"
2223
#include "qgsmaplayerregistry.h"
2324
#include "qgsrasterlayer.h"
@@ -50,6 +51,31 @@ QgsLegendModel::~QgsLegendModel()
5051
{
5152
}
5253

54+
void QgsLegendModel::setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup )
55+
{
56+
clear();
57+
addGroupFromLayerTree( rootGroup, invisibleRootItem() );
58+
}
59+
60+
void QgsLegendModel::addGroupFromLayerTree( QgsLayerTreeGroup* parentGroup, QStandardItem* parentItem )
61+
{
62+
foreach ( QgsLayerTreeNode* node, parentGroup->children() )
63+
{
64+
if ( QgsLayerTree::isGroup( node ) )
65+
{
66+
QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node );
67+
QStandardItem* groupItem = addGroup( nodeGroup->name(), -1, parentItem );
68+
addGroupFromLayerTree( nodeGroup, groupItem );
69+
}
70+
else if ( QgsLayerTree::isLayer( node ) )
71+
{
72+
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
73+
if ( nodeLayer->layer() )
74+
addLayer( nodeLayer->layer(), -1, QString(), parentItem );
75+
}
76+
}
77+
}
78+
5379
void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo )
5480
{
5581
setLayerSet( layerIds );
@@ -115,21 +141,24 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom
115141
}
116142
}
117143

118-
QStandardItem* QgsLegendModel::addGroup( QString text, int position )
144+
QStandardItem* QgsLegendModel::addGroup( QString text, int position, QStandardItem* parentItem )
119145
{
120146
if ( text.isNull() )
121147
text = tr( "Group" );
122148

149+
if ( !parentItem )
150+
parentItem = invisibleRootItem();
151+
123152
QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text );
124153
groupItem->setUserText( text );
125154

126155
if ( position == -1 )
127156
{
128-
position = invisibleRootItem()->rowCount();
157+
position = parentItem->rowCount();
129158
}
130159
QList<QStandardItem *> itemsList;
131160
itemsList << groupItem << new QgsComposerStyleItem( groupItem );
132-
invisibleRootItem()->insertRow( position, itemsList );
161+
parentItem->insertRow( position, itemsList );
133162

134163
emit layersChanged();
135164
return groupItem;
@@ -524,13 +553,16 @@ void QgsLegendModel::removeLayer( const QString& layerId )
524553
}
525554
}
526555

527-
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule )
556+
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule, QStandardItem* parentItem )
528557
{
529558
if ( !theMapLayer )
530559
{
531560
return;
532561
}
533562

563+
if ( !parentItem )
564+
parentItem = invisibleRootItem();
565+
534566
QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() );
535567
if ( theMapLayer->title() != "" )
536568
{
@@ -543,7 +575,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator
543575

544576
QList<QStandardItem *> itemsList;
545577
itemsList << layerItem << new QgsComposerStyleItem( layerItem );
546-
invisibleRootItem()->appendRow( itemsList );
578+
parentItem->appendRow( itemsList );
547579

548580
switch ( theMapLayer->type() )
549581
{

src/core/composer/qgslegendmodel.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
class QDomDocument;
2626
class QDomElement;
27+
class QgsLayerTreeGroup;
2728
class QgsMapLayer;
2829
class QgsSymbolV2;
2930
class QgsVectorLayer;
@@ -52,15 +53,21 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
5253
QgsLegendModel();
5354
~QgsLegendModel();
5455

55-
/**Sets layer set and groups*/
56-
void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo );
56+
/** Set layers and groups from a layer tree
57+
* @note added in 2.6
58+
*/
59+
void setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup );
60+
/** Sets layer set and groups
61+
* @deprecated in 2.6
62+
*/
63+
Q_DECL_DEPRECATED void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo );
5764
void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" );
5865
/**Adds a group
5966
@param text name of group (defaults to translation of "Group")
6067
@param position insertion position (toplevel position (or -1 if it should be placed at the end of the legend).
6168
@returns a pointer to the added group
6269
*/
63-
QStandardItem *addGroup( QString text = QString::null, int position = -1 );
70+
QStandardItem *addGroup( QString text = QString::null, int position = -1, QStandardItem* parentItem = 0 );
6471

6572
/**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/
6673
void updateItem( QStandardItem* item );
@@ -97,7 +104,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
97104

98105
public slots:
99106
void removeLayer( const QString& layerId );
100-
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "" );
107+
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "", QStandardItem* parentItem = 0 );
101108

102109
private slots:
103110
void updateLayer();
@@ -117,6 +124,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
117124
void updateSymbolV2ItemText( QStandardItem* symbolItem );
118125
void updateRasterSymbolItemText( QStandardItem* symbolItem );
119126

127+
void addGroupFromLayerTree( QgsLayerTreeGroup* parentGroup, QStandardItem* parentItem );
120128

121129
protected:
122130
QStringList mLayerIds;

0 commit comments

Comments
 (0)