Skip to content

Commit

Permalink
Merge pull request #217 from endmax/master
Browse files Browse the repository at this point in the history
[FEATURE] The QgslegendInterface implementation updated to allow access to items and groups at all levels.
  • Loading branch information
timlinux committed Sep 11, 2012
2 parents 597e770 + 318dd7c commit 815f973
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
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
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
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 815f973

Please sign in to comment.