@@ -66,7 +66,13 @@ void QgsAppLegendInterface::updateIndex( QModelIndex oldIndex, QModelIndex newIn
6666
6767void 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
7278void 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
83110void QgsAppLegendInterface::setLayerVisible ( QgsMapLayer * ml, bool visible )
@@ -101,14 +128,26 @@ QList< GroupLayerInfo > QgsAppLegendInterface::groupLayerRelationship()
101128
102129bool 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
109142bool 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
114153bool 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
124163bool QgsAppLegendInterface::isLayerVisible ( QgsMapLayer * ml )
0 commit comments