Skip to content
Permalink
Browse files

Merge pull request #217 from endmax/master

[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 815f97377e9acb8ac31637253912864631f388da
Showing with 111 additions and 13 deletions.
  1. +47 −8 src/app/legend/qgsapplegendinterface.cpp
  2. +1 −0 src/app/legend/qgsapplegendinterface.h
  3. +63 −5 src/app/legend/qgslegend.cpp
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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 )
@@ -94,6 +94,7 @@ class QgsAppLegendInterface : public QgsLegendInterface

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

#endif //QGSLEGENDAPPIFACE_H
@@ -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()
@@ -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 );
@@ -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 );

0 comments on commit 815f973

Please sign in to comment.
You can’t perform that action at this time.