Skip to content
Permalink
Browse files

legend updates:

- when enabling explicit drawing order, intially take legend order
- when loading projects with explicit drawing order enabled,
  verify that the order is actually fully set and take legend
  order otherwise (fixes #7623)
- store/restore active layer
  • Loading branch information
jef-n committed May 25, 2013
1 parent 9054ede commit fc58edd6310e01e6b7143c91f60099b18e51daa8
Showing with 63 additions and 5 deletions.
  1. +60 −4 src/app/legend/qgslegend.cpp
  2. +3 −1 src/app/legend/qgslegend.h
@@ -34,6 +34,7 @@
#include "qgsvectorlayer.h"
#include "qgsgenericprojectionselector.h"
#include "qgsclipboard.h"
#include "qgsmessagelog.h"

#include <QFont>
#include <QDomDocument>
@@ -1453,7 +1454,7 @@ void QgsLegend::setDrawingOrder( QList<QgsMapLayer *> layers )
updateMapCanvasLayerSet();
}

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

@@ -1680,7 +1681,7 @@ bool QgsLegend::writeXML( QDomNode &legendnode, QDomDocument &document )

bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDocument &document )
{
foreach ( QTreeWidgetItem * currentItem, items )
foreach ( QTreeWidgetItem *currentItem, items )
{
QgsLegendItem *item = dynamic_cast<QgsLegendItem *>( currentItem );
if ( !item )
@@ -1827,6 +1828,9 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
bkMapExtent = mMapCanvas->extent();
}

QString activeLayerId = node.toElement().attribute( "activeLayer" );
QgsLegendLayer *activeLayer = 0;

const QDomNodeList &l = node.childNodes();
for ( int i = 0; i < l.count(); i++ )
{
@@ -1899,6 +1903,9 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
continue;
}

if ( currentLayer->layer() && currentLayer->layer()->id() == activeLayerId )
activeLayer = currentLayer;

if ( currentLayer->layer() && !QgsProject::instance()->layerIsEmbedded( currentLayer->layer()->id() ).isEmpty() )
{
// start with already set font style
@@ -1959,22 +1966,40 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
}
}

if ( activeLayer )
setCurrentItem( activeLayer );

//restore canvas extent (could be changed by addLayer calls)
if ( !bkMapExtent.isEmpty() )
{
mMapCanvas->setExtent( bkMapExtent );
}

return true;
}

bool QgsLegend::readXML( QDomNode& legendnode )
{
clear(); //remove all items first
mEmbeddedGroups.clear();
mUpdateDrawingOrder = legendnode.toElement().attribute( "updateDrawingOrder", "true" ) == "true";

mUpdateDrawingOrder = true;

if( !readXML( 0, legendnode ) )
return false;

if( legendnode.toElement().attribute( "updateDrawingOrder", "true" ) != "true" )
{
if( !verifyDrawingOrder() )
QgsMessageLog::logMessage( tr( "Not fully defined drawing order set to legend order." ), tr( "Legend" ), QgsMessageLog::WARNING );

mUpdateDrawingOrder = false;
}

emit updateDrawingOrderChecked( mUpdateDrawingOrder );
emit updateDrawingOrderUnchecked( !mUpdateDrawingOrder );
return readXML( 0, legendnode );

return true;
}

QgsLegendLayer* QgsLegend::readLayerFromXML( QDomElement& childelem, bool& isOpen )
@@ -2794,6 +2819,8 @@ void QgsLegend::writeProject( QDomDocument & doc )

QDomElement mapcanvasNode = doc.createElement( "legend" );
mapcanvasNode.setAttribute( "updateDrawingOrder", mUpdateDrawingOrder ? "true" : "false" );
if ( currentLayer() )
mapcanvasNode.setAttribute( "activeLayer", currentLayer()->id() );
qgisNode.appendChild( mapcanvasNode );
writeXML( mapcanvasNode, doc );
}
@@ -2946,11 +2973,40 @@ bool QgsLegend::groupEmbedded( QTreeWidgetItem* item ) const
return mEmbeddedGroups.contains( gItem->text( 0 ) );
}

bool QgsLegend::verifyDrawingOrder()
{
Q_ASSERT( mUpdateDrawingOrder );

// check if the drawing order wasn't already initially set
bool hasUndefinedOrder = false;
for ( QTreeWidgetItemIterator it( this ); *it && !hasUndefinedOrder; it++ )
{
QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( *it );
hasUndefinedOrder |= ll && ll->drawingOrder() < 0;
}

if( !hasUndefinedOrder )
return true;

int i = 0;
foreach ( QgsLegendLayer *ll, legendLayers() )
{
ll->setDrawingOrder( i++ );
}

return false;
}

void QgsLegend::setUpdateDrawingOrder( bool updateDrawingOrder )
{
if ( mUpdateDrawingOrder == updateDrawingOrder )
return;

if ( !updateDrawingOrder )
{
verifyDrawingOrder();
}

mUpdateDrawingOrder = updateDrawingOrder;

QgsProject::instance()->dirty( true );
@@ -577,7 +577,7 @@ class QgsLegend : public QTreeWidget

} mPixmaps;

//! Widget that holds the indicator line //
//! Widget that holds the indicator line
QWidget *mInsertionLine;

QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > > mLegendLayerActionMap;
@@ -588,6 +588,8 @@ class QgsLegend : public QTreeWidget

void updateGroupCheckStates( QTreeWidgetItem *item );

bool verifyDrawingOrder();

signals:
void itemAdded( QModelIndex index );
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );

0 comments on commit fc58edd

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