Skip to content

Commit 815f973

Browse files
committed
Merge pull request #217 from endmax/master
[FEATURE] The QgslegendInterface implementation updated to allow access to items and groups at all levels.
2 parents 597e770 + 318dd7c commit 815f973

3 files changed

Lines changed: 111 additions & 13 deletions

File tree

‎src/app/legend/qgsapplegendinterface.cpp‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ void QgsAppLegendInterface::updateIndex( QModelIndex oldIndex, QModelIndex newIn
6666

6767
void QgsAppLegendInterface::setGroupExpanded( int groupIndex, bool expand )
6868
{
69-
mLegend->setExpanded( mLegend->model()->index( groupIndex, 0 ), expand );
69+
QTreeWidgetItem * item = getItem (groupIndex);
70+
if ( !item )
71+
{
72+
return;
73+
}
74+
75+
item->setExpanded(expand);
7076
}
7177

7278
void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible )
@@ -76,8 +82,29 @@ void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible )
7682
return;
7783
}
7884

79-
Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked;
80-
mLegend->topLevelItem( groupIndex )->setCheckState( 0, state );
85+
Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked;
86+
getItem (groupIndex)->setCheckState( 0, state );
87+
}
88+
89+
QTreeWidgetItem *QgsAppLegendInterface::getItem(int itemIndex)
90+
{
91+
int itemCount = 0;
92+
for (QTreeWidgetItem* theItem = mLegend->firstItem(); theItem; theItem = mLegend->nextItem( theItem ) )
93+
{
94+
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
95+
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
96+
if (itemCount == itemIndex)
97+
{
98+
return theItem;
99+
}
100+
else
101+
{
102+
itemCount = itemCount + 1;
103+
}
104+
}
105+
}
106+
107+
return NULL;
81108
}
82109

83110
void QgsAppLegendInterface::setLayerVisible( QgsMapLayer * ml, bool visible )
@@ -101,14 +128,26 @@ QList< GroupLayerInfo > QgsAppLegendInterface::groupLayerRelationship()
101128

102129
bool QgsAppLegendInterface::groupExists( int groupIndex )
103130
{
104-
QModelIndex mi = mLegend->model()->index( groupIndex, 0 );
105-
return ( mi.isValid() &&
106-
mLegend->isLegendGroup( mi ) );
131+
QTreeWidgetItem * item = getItem (groupIndex);
132+
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( item );
133+
134+
if ( !legendItem )
135+
{
136+
return false;
137+
}
138+
139+
return legendItem->type() == QgsLegendItem::LEGEND_GROUP;
107140
}
108141

109142
bool QgsAppLegendInterface::isGroupExpanded( int groupIndex )
110143
{
111-
return mLegend->isExpanded( mLegend->model()->index( groupIndex, 0 ) );
144+
QTreeWidgetItem * item = getItem (groupIndex);
145+
if ( !item )
146+
{
147+
return false;
148+
}
149+
150+
return item->isExpanded();
112151
}
113152

114153
bool QgsAppLegendInterface::isGroupVisible( int groupIndex )
@@ -118,7 +157,7 @@ bool QgsAppLegendInterface::isGroupVisible( int groupIndex )
118157
return false;
119158
}
120159

121-
return ( Qt::Checked == mLegend->topLevelItem( groupIndex )->checkState( 0 ) );
160+
return ( Qt::Checked == getItem( groupIndex )->checkState( 0 ) );
122161
}
123162

124163
bool QgsAppLegendInterface::isLayerVisible( QgsMapLayer * ml )

‎src/app/legend/qgsapplegendinterface.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class QgsAppLegendInterface : public QgsLegendInterface
9494

9595
//! Pointer to QgsLegend object
9696
QgsLegend *mLegend;
97+
QTreeWidgetItem *getItem(int itemIndex);
9798
};
9899

99100
#endif //QGSLEGENDAPPIFACE_H

‎src/app/legend/qgslegend.cpp‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,27 @@ int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )
231231

232232
int QgsLegend::addGroup( QString name, bool expand, int groupIndex )
233233
{
234-
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
235-
return addGroup( name, expand, lg );
234+
QTreeWidgetItem * parentItem = invisibleRootItem();
235+
236+
int itemCount = 0;
237+
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
238+
{
239+
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
240+
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
241+
if (itemCount == groupIndex)
242+
{
243+
// this is the matching group
244+
parentItem = legendItem;
245+
break;
246+
}
247+
else
248+
{
249+
itemCount = itemCount + 1;
250+
}
251+
}
252+
}
253+
254+
return addGroup( name, expand, parentItem );
236255
}
237256

238257
void QgsLegend::removeAll()
@@ -276,7 +295,26 @@ void QgsLegend::setLayersVisible( bool visible )
276295

277296
void QgsLegend::removeGroup( int groupIndex )
278297
{
279-
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
298+
QgsLegendGroup * lg = NULL;
299+
int itemCount = 0;
300+
301+
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
302+
{
303+
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
304+
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
305+
if (itemCount == groupIndex)
306+
{
307+
// this is the matching group
308+
lg = dynamic_cast<QgsLegendGroup*>( legendItem );
309+
break;
310+
}
311+
else
312+
{
313+
itemCount = itemCount + 1;
314+
}
315+
}
316+
}
317+
280318
if ( lg )
281319
{
282320
removeGroup( lg );
@@ -1252,8 +1290,28 @@ void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
12521290
if ( !layer )
12531291
return;
12541292

1255-
QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
1256-
if ( !group )
1293+
int itemCount = 0;
1294+
QgsLegendGroup *group = NULL;
1295+
1296+
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
1297+
{
1298+
1299+
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
1300+
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
1301+
if (itemCount == groupIndex)
1302+
{
1303+
// this is the matching group
1304+
group = dynamic_cast<QgsLegendGroup*>( legendItem );
1305+
break;
1306+
}
1307+
else
1308+
{
1309+
itemCount = itemCount + 1;
1310+
}
1311+
}
1312+
}
1313+
1314+
if ( group == NULL)
12571315
return;
12581316

12591317
insertItem( layer, group );

0 commit comments

Comments
 (0)