Skip to content
Permalink
Browse files

Merge branch 'drawing_order_embedded'

  • Loading branch information
mhugent committed Nov 21, 2012
2 parents 0893db7 + d673606 commit b177abf1871b7bca22b46a58e32657ce2fe2445f
@@ -20,6 +20,8 @@
#include "qgsmaplayer.h"
#include "qgslogger.h"
#include "qgslegend.h"
#include "qgslegendgroup.h"
#include "qgslegendlayer.h"
#include "qgslegendlayer.h"
#include "qgsproject.h"

@@ -33,10 +35,6 @@ QgsLayerOrder::QgsLayerOrder( QgsLegend *legend, QWidget * parent, const char *n
{
setObjectName( name );

// track visibility changed in legend
connect( mLegend, SIGNAL( itemChanged( QTreeWidgetItem *, int ) ),
this, SLOT( legendItemChanged( QTreeWidgetItem *, int ) ) );

// track if legend mode changes
connect( mLegend, SIGNAL( updateDrawingOrderChecked( bool ) ),
this, SLOT( updateDrawingOrderChecked( bool ) ) );
@@ -85,13 +83,14 @@ void QgsLayerOrder::refreshLayerList()
{
clear();

foreach ( QgsLegendLayer *layer, mLegend->legendLayers() )
QList<DrawingOrderInfo> drawingOrderList = mLegend->drawingOrder();
QList<DrawingOrderInfo>::const_iterator it = drawingOrderList.constBegin();
for ( ; it != drawingOrderList.constEnd(); ++it )
{
QListWidgetItem *item = new QListWidgetItem( layer->layer()->name() );
item->setData( 1, QString::number( layer->drawingOrder() ) );
item->setData( Qt::UserRole, qVariantFromValue( qobject_cast<QObject*>( layer->layer() ) ) );
item->setCheckState( layer->isVisible() ? Qt::Checked : Qt::Unchecked );
QgsDebugMsg( QString( "add item=%1 at %2" ).arg( item ? item->text() : "(null item)" ).arg( count() ) );
QListWidgetItem *item = new QListWidgetItem( it->name );
item->setCheckState( it->checked ? Qt::Checked : Qt::Unchecked );
item->setData( Qt::UserRole, it->id );
item->setData( Qt::UserRole + 1, it->embeddedGroup );
addItem( item );
}
}
@@ -113,35 +112,28 @@ QListWidgetItem *QgsLayerOrder::layerItem( QgsMapLayer *layer ) const

void QgsLayerOrder::itemChanged( QListWidgetItem *item )
{
QgsDebugMsg( "Entering." );
QgsDebugMsg( QString( "item=%1" ).arg( item ? item->text() : "(null item)" ) );

QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( item->data( Qt::UserRole ).value<QObject*>() );
mLegend->setLayerVisible( layer, item->checkState() == Qt::Checked );

QString name = item->text();
QString id = item->data( Qt::UserRole ).toString();
bool embeddedGroup = item->data( Qt::UserRole + 1 ).toBool();
if ( embeddedGroup )
{
QgsLegendGroup* grp = mLegend->findLegendGroup( name, id );
if ( grp )
{
grp->setCheckState( 0, item->checkState() );
}
}
else
{
QgsLegendLayer* ll = mLegend->findLegendLayer( id );
if ( ll )
{
ll->setCheckState( 0, item->checkState() );
}
}
updateLayerOrder();
}

void QgsLayerOrder::legendItemChanged( QTreeWidgetItem *item, int col )
{
QgsDebugMsg( "Entering." );

if ( col != 0 )
return;

QgsDebugMsg( QString( "legendItem changed=%1" ).arg( item ? item->text( 0 ) : "(null item)" ) );

QgsLegendLayer *ll = dynamic_cast< QgsLegendLayer * >( item );
if ( !ll )
return;

QListWidgetItem *lwi = layerItem( ll->layer() );
if ( !lwi )
return;

lwi->setCheckState( item->checkState( col ) );
}

void QgsLayerOrder::mousePressEvent( QMouseEvent * e )
{
QgsDebugMsg( "Entering." );
@@ -261,14 +253,24 @@ void QgsLayerOrder::updateLayerOrder()
if ( !isEnabled() )
return;

QList<QgsMapLayer *> layers;
QList<DrawingOrderInfo> drawingOrder;

for ( int i = 0; i < count(); i++ )
{
layers << qobject_cast<QgsMapLayer *>( item( i )->data( Qt::UserRole ).value<QObject*>() );
QListWidgetItem* listItem = item( i );
if ( !listItem )
{
continue;
}
DrawingOrderInfo info;
info.name = listItem->text();
info.id = listItem->data( Qt::UserRole ).toString();
info.checked = listItem->checkState() == Qt::Checked;
info.embeddedGroup = listItem->data( Qt::UserRole + 1 ).toBool();
drawingOrder.push_back( info );
}

mLegend->setDrawingOrder( layers );
mLegend->setDrawingOrder( drawingOrder );
}

void QgsLayerOrder::hideLine()
@@ -48,7 +48,6 @@ class QgsLayerOrder : public QListWidget
private slots:
void updateDrawingOrderChecked( bool );
void itemChanged( QListWidgetItem * );
void legendItemChanged( QTreeWidgetItem *, int );
void refreshLayerList();

private:
@@ -841,6 +841,9 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
group = new QgsLegendGroup( this, groupName );
}

group->setEmbedded( true );
group->setProjectPath( projectFilePath );

QFont groupFont;
groupFont.setItalic( true );
group->setFont( 0, groupFont );
@@ -1146,6 +1149,68 @@ QList<QgsLegendLayer *> QgsLegend::legendLayers()
}
}

QList<DrawingOrderInfo> QgsLegend::drawingOrder()
{
QMap<int, DrawingOrderInfo> drawingOrder;
QSet<QgsLegendLayer*> embeddedGroupChildren;
int nEntries = 0;

QTreeWidgetItemIterator it( this );
while ( *it )
{
QgsLegendLayer* llayer = dynamic_cast<QgsLegendLayer *>( *it );
QgsLegendGroup* lgroup = dynamic_cast<QgsLegendGroup *>( *it );
if ( llayer )
{
if ( !embeddedGroupChildren.contains( llayer ) )
{
DrawingOrderInfo dInfo;
dInfo.name = llayer->layerName();
dInfo.id = llayer->layer()->id();
dInfo.checked = ( llayer->checkState( 0 ) == Qt::Checked );
dInfo.embeddedGroup = false;
if ( mUpdateDrawingOrder )
{
drawingOrder.insertMulti( nEntries, dInfo );
}
else
{
drawingOrder.insertMulti( llayer->drawingOrder(), dInfo );
}
++nEntries;
}
}
else if ( lgroup )
{
if ( lgroup->isEmbedded() && !( lgroup->parent() ) )
{
QList<QgsLegendLayer*> groupLayers = lgroup->legendLayers();
QList<QgsLegendLayer*>::const_iterator groupLayerIt = groupLayers.constBegin();
for ( ; groupLayerIt != groupLayers.constEnd(); ++groupLayerIt )
{
embeddedGroupChildren.insert( *groupLayerIt );
}
DrawingOrderInfo dInfo;
dInfo.name = lgroup->text( 0 );
dInfo.id = lgroup->projectPath();
dInfo.checked = ( lgroup->checkState( 0 ) != Qt::Unchecked );
dInfo.embeddedGroup = true;
if ( mUpdateDrawingOrder )
{
drawingOrder.insertMulti( nEntries, dInfo );
}
else
{
drawingOrder.insertMulti( lgroup->drawingOrder(), dInfo );
}
++nEntries;
}
}
++it;
}
return drawingOrder.values();
}

QList<QgsMapLayer *> QgsLegend::layers()
{
QList<QgsMapLayer *> ls;
@@ -1160,14 +1225,58 @@ QList<QgsMapLayer *> QgsLegend::layers()

QList<QgsMapCanvasLayer> QgsLegend::canvasLayers()
{
QList<QgsMapCanvasLayer> ls;
QMap<int, QgsMapCanvasLayer> layers;
QSet<QgsLegendLayer*> embeddedGroupChildren;
int nEntries = 0;

foreach ( QgsLegendLayer *l, legendLayers() )
QTreeWidgetItemIterator it( this );
while ( *it )
{
ls << l->canvasLayer();
QgsLegendLayer* llayer = dynamic_cast<QgsLegendLayer *>( *it );
QgsLegendGroup* lgroup = dynamic_cast<QgsLegendGroup *>( *it );
if ( llayer && !embeddedGroupChildren.contains( llayer ) )
{
QgsMapCanvasLayer canvasLayer = llayer->canvasLayer();
if ( mUpdateDrawingOrder )
{
layers.insertMulti( nEntries + embeddedGroupChildren.size(), canvasLayer );
}
else
{
layers.insertMulti( llayer->drawingOrder(), canvasLayer );
}
}
else if ( lgroup )
{
if ( lgroup->isEmbedded() )
{
int groupDrawingOrder = lgroup->drawingOrder();
QList<QgsLegendLayer*> groupLayers = lgroup->legendLayers();
for ( int i = groupLayers.size() - 1; i >= 0; --i )
{
QgsLegendLayer* ll = groupLayers.at( i );
if ( !ll || embeddedGroupChildren.contains( ll ) )
{
continue;
}

if ( mUpdateDrawingOrder )
{
layers.insertMulti( nEntries, ll->canvasLayer() );
}
else
{
layers.insertMulti( groupDrawingOrder, ll->canvasLayer() );
}
embeddedGroupChildren.insert( ll );
}
}
}
++it;
++nEntries;
}

return ls;
return layers.values();
}

void QgsLegend::setDrawingOrder( QList<QgsMapLayer *> layers )
@@ -1186,6 +1295,44 @@ void QgsLegend::setDrawingOrder( QList<QgsMapLayer *> layers )
updateMapCanvasLayerSet();
}

void QgsLegend::setDrawingOrder( const QList<DrawingOrderInfo>& order )
{
QList<QgsMapCanvasLayer> layers;

QList<DrawingOrderInfo>::const_iterator orderIt = order.constBegin();
int i = 0;
for ( ; orderIt != order.constEnd(); ++orderIt )
{
if ( orderIt->embeddedGroup )
{
//find group
QgsLegendGroup* group = findLegendGroup( orderIt->name, orderIt->id );
if ( group )
{
group->setDrawingOrder( i );
QList<QgsLegendLayer*> groupLayers = group->legendLayers();
QList<QgsLegendLayer*>::iterator groupIt = groupLayers.begin();
for ( ; groupIt != groupLayers.end(); ++groupIt )
{
layers.push_back(( *groupIt )->canvasLayer() );
}
++i;
}
}
else
{
QgsLegendLayer *ll = findLegendLayer( orderIt->id );
if ( ll )
{
ll->setDrawingOrder( i );
layers.push_back( ll->canvasLayer() );
++i;
}
}
}
mMapCanvas->setLayerSet( layers );
}

bool QgsLegend::setCurrentLayer( QgsMapLayer *layer )
{
QgsLegendLayer *ll = findLegendLayer( layer );
@@ -1405,6 +1552,11 @@ bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDo
{
legendgroupnode.setAttribute( "embedded", 1 );
legendgroupnode.setAttribute( "project", QgsProject::instance()->writePath( embedIt.value() ) );
QgsLegendGroup* group = dynamic_cast<QgsLegendGroup*>( item );
if ( group )
{
legendgroupnode.setAttribute( "drawingOrder", group->drawingOrder() );
}
}
else
{
@@ -1531,6 +1683,10 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
if ( childelem.attribute( "embedded" ) == "1" )
{
theGroup = addEmbeddedGroup( name, QgsProject::instance()->readPath( childelem.attribute( "project" ) ) );
if ( childelem.hasAttribute( "drawingOrder" ) )
{
theGroup->setDrawingOrder( childelem.attribute( "drawingOrder" ).toInt() );
}
updateGroupCheckStates( theGroup );
}
else
@@ -1742,6 +1898,23 @@ QgsLegendLayer* QgsLegend::findLegendLayer( const QgsMapLayer *layer )
return 0;
}

QgsLegendGroup* QgsLegend::findLegendGroup( const QString& name, const QString& projectPath )
{
QgsLegendGroup* group = 0;
for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
{
group = dynamic_cast<QgsLegendGroup*>( theItem );
if ( group )
{
if ( group->text( 0 ) == name && group->projectPath() == projectPath )
{
return group;
}
}
}
return 0;
}


void QgsLegend::adjustIconSize()
{

0 comments on commit b177abf

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