Skip to content

Commit

Permalink
removed the limitation to top leve items for group indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
endmax committed Aug 27, 2012
1 parent 078f5b8 commit 318dd7c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
55 changes: 47 additions & 8 deletions src/app/legend/qgsapplegendinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ void QgsAppLegendInterface::updateIndex( QModelIndex oldIndex, QModelIndex newIn

void QgsAppLegendInterface::setGroupExpanded( int groupIndex, bool expand )
{
mLegend->setExpanded( mLegend->model()->index( groupIndex, 0 ), expand );
QTreeWidgetItem * item = getItem (groupIndex);
if ( !item )
{
return;
}

item->setExpanded(expand);
}

void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible )
Expand All @@ -76,8 +82,29 @@ void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible )
return;
}

Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked;
mLegend->topLevelItem( groupIndex )->setCheckState( 0, state );
Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked;
getItem (groupIndex)->setCheckState( 0, state );
}

QTreeWidgetItem *QgsAppLegendInterface::getItem(int itemIndex)
{
int itemCount = 0;
for (QTreeWidgetItem* theItem = mLegend->firstItem(); theItem; theItem = mLegend->nextItem( theItem ) )
{
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
if (itemCount == itemIndex)
{
return theItem;
}
else
{
itemCount = itemCount + 1;
}
}
}

return NULL;
}

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

bool QgsAppLegendInterface::groupExists( int groupIndex )
{
QModelIndex mi = mLegend->model()->index( groupIndex, 0 );
return ( mi.isValid() &&
mLegend->isLegendGroup( mi ) );
QTreeWidgetItem * item = getItem (groupIndex);
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( item );

if ( !legendItem )
{
return false;
}

return legendItem->type() == QgsLegendItem::LEGEND_GROUP;
}

bool QgsAppLegendInterface::isGroupExpanded( int groupIndex )
{
return mLegend->isExpanded( mLegend->model()->index( groupIndex, 0 ) );
QTreeWidgetItem * item = getItem (groupIndex);
if ( !item )
{
return false;
}

return item->isExpanded();
}

bool QgsAppLegendInterface::isGroupVisible( int groupIndex )
Expand All @@ -118,7 +157,7 @@ bool QgsAppLegendInterface::isGroupVisible( int groupIndex )
return false;
}

return ( Qt::Checked == mLegend->topLevelItem( groupIndex )->checkState( 0 ) );
return ( Qt::Checked == getItem( groupIndex )->checkState( 0 ) );
}

bool QgsAppLegendInterface::isLayerVisible( QgsMapLayer * ml )
Expand Down
1 change: 1 addition & 0 deletions src/app/legend/qgsapplegendinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class QgsAppLegendInterface : public QgsLegendInterface

//! Pointer to QgsLegend object
QgsLegend *mLegend;
QTreeWidgetItem *getItem(int itemIndex);
};

#endif //QGSLEGENDAPPIFACE_H
68 changes: 63 additions & 5 deletions src/app/legend/qgslegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,27 @@ int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )

int QgsLegend::addGroup( QString name, bool expand, int groupIndex )
{
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
return addGroup( name, expand, lg );
QTreeWidgetItem * parentItem = invisibleRootItem();

int itemCount = 0;
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
{
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
if (itemCount == groupIndex)
{
// this is the matching group
parentItem = legendItem;
break;
}
else
{
itemCount = itemCount + 1;
}
}
}

return addGroup( name, expand, parentItem );
}

void QgsLegend::removeAll()
Expand Down Expand Up @@ -276,7 +295,26 @@ void QgsLegend::setLayersVisible( bool visible )

void QgsLegend::removeGroup( int groupIndex )
{
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
QgsLegendGroup * lg = NULL;
int itemCount = 0;

for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
{
QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
if (itemCount == groupIndex)
{
// this is the matching group
lg = dynamic_cast<QgsLegendGroup*>( legendItem );
break;
}
else
{
itemCount = itemCount + 1;
}
}
}

if ( lg )
{
removeGroup( lg );
Expand Down Expand Up @@ -1252,8 +1290,28 @@ void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
if ( !layer )
return;

QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
if ( !group )
int itemCount = 0;
QgsLegendGroup *group = NULL;

for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
{

QgsLegendItem* legendItem = dynamic_cast<QgsLegendItem *>( theItem );
if (legendItem->type() == QgsLegendItem::LEGEND_GROUP) {
if (itemCount == groupIndex)
{
// this is the matching group
group = dynamic_cast<QgsLegendGroup*>( legendItem );
break;
}
else
{
itemCount = itemCount + 1;
}
}
}

if ( group == NULL)
return;

insertItem( layer, group );
Expand Down

0 comments on commit 318dd7c

Please sign in to comment.