Skip to content
Permalink
Browse files

defer legend invalidation to avoid reprocessing when multiple layers …

…are involved
  • Loading branch information
jef-n committed Sep 21, 2015
1 parent 9cbfeea commit 2a1cacd5f4fc053c8f6d31bf64609a21af7979b5
@@ -51,9 +51,11 @@ class QgsLayerTreeModel : QAbstractItemModel
enum Flag
{
// display flags
ShowLegend, //!< Add legend nodes for layer nodes
ShowSymbology, //!< deprecated - use ShowLegend
ShowLegendAsTree, //!< For legends that support it, will show them in a tree instead of a list (needs also ShowLegend). Added in 2.8
ShowLegend, //!< Add legend nodes for layer nodes
ShowSymbology, //!< deprecated - use ShowLegend
ShowRasterPreviewIcon, //!< Will use real preview of raster layer as icon (may be slow)
ShowLegendAsTree, //!< For legends that support it, will show them in a tree instead of a list (needs also ShowLegend). Added in 2.8
DeferredLegendInvalidation, //!< defer legend model invalidation

// behavioral flags
AllowNodeReorder, //!< Allow reordering with drag'n'drop
@@ -35,7 +35,7 @@
QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent )
: QAbstractItemModel( parent )
, mRootNode( rootNode )
, mFlags( ShowLegend | AllowLegendChangeState )
, mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
, mAutoCollapseLegendNodesCount( -1 )
, mLegendFilterByScale( 0 )
, mLegendMapViewMupp( 0 )
@@ -45,6 +45,9 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
connectToRootNode();

mFontLayer.setBold( true );

connect( &mDeferLegendInvalidationTimer, SIGNAL( timeout() ), this, SLOT( invalidateLegendMapBasedData() ) );
mDeferLegendInvalidationTimer.setSingleShot( true );
}

QgsLayerTreeModel::~QgsLayerTreeModel()
@@ -682,7 +685,7 @@ void QgsLayerTreeModel::nodeLayerLoaded()
if ( !nodeLayer )
return;

// deffered connection to the layer
// deferred connection to the layer
connectToLayer( nodeLayer );
}

@@ -1303,6 +1306,16 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::layerLegendNodes( QgsLaye

void QgsLayerTreeModel::legendInvalidateMapBasedData()
{
if ( !testFlag( DeferredLegendInvalidation ) )
invalidateLegendMapBasedData();
else
mDeferLegendInvalidationTimer.start( 1000 );
}

void QgsLayerTreeModel::invalidateLegendMapBasedData()
{
QgsDebugCall;

// we have varying icon sizes, and we want icon to be centered and
// text to be left aligned, so we have to compute the max width of icons
//
@@ -19,6 +19,7 @@
#include <QAbstractItemModel>
#include <QFont>
#include <QIcon>
#include <QTimer>

class QgsLayerTreeNode;
class QgsLayerTreeGroup;
@@ -28,7 +29,6 @@ class QgsMapHitTest;
class QgsMapLayer;
class QgsMapSettings;


/**
* The QgsLayerTreeModel class is model implementation for Qt item views framework.
* The model can be used in any QTreeView, it is however recommended to use it
@@ -72,17 +72,18 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
enum Flag
{
// display flags
ShowLegend = 0x0001, //!< Add legend nodes for layer nodes
ShowSymbology = 0x0001, //!< deprecated - use ShowLegend
ShowRasterPreviewIcon = 0x0002, //!< Will use real preview of raster layer as icon (may be slow)
ShowLegendAsTree = 0x0004, //!< For legends that support it, will show them in a tree instead of a list (needs also ShowLegend). Added in 2.8
ShowLegend = 0x0001, //!< Add legend nodes for layer nodes
ShowSymbology = 0x0001, //!< deprecated - use ShowLegend
ShowRasterPreviewIcon = 0x0002, //!< Will use real preview of raster layer as icon (may be slow)
ShowLegendAsTree = 0x0004, //!< For legends that support it, will show them in a tree instead of a list (needs also ShowLegend). Added in 2.8
DeferredLegendInvalidation = 0x0008, //!< defer legend model invalidation

// behavioral flags
AllowNodeReorder = 0x1000, //!< Allow reordering with drag'n'drop
AllowNodeRename = 0x2000, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility = 0x4000, //!< Allow user to set node visibility with a check box
AllowLegendChangeState = 0x8000, //!< Allow check boxes for legend nodes (if supported by layer's legend)
AllowSymbologyChangeState = 0x8000, //!< deprecated - use AllowLegendChangeState
AllowNodeReorder = 0x1000, //!< Allow reordering with drag'n'drop
AllowNodeRename = 0x2000, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility = 0x4000, //!< Allow user to set node visibility with a check box
AllowLegendChangeState = 0x8000, //!< Allow check boxes for legend nodes (if supported by layer's legend)
AllowSymbologyChangeState = 0x8000, //!< deprecated - use AllowLegendChangeState
};
Q_DECLARE_FLAGS( Flags, Flag )

@@ -204,6 +205,8 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel

void legendNodeDataChanged();

void invalidateLegendMapBasedData();

protected:
void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
void addLegendToLayer( QgsLayerTreeLayer* nodeL );
@@ -295,6 +298,7 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
double mLegendMapViewMupp;
int mLegendMapViewDpi;
double mLegendMapViewScale;
QTimer mDeferLegendInvalidationTimer;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayerTreeModel::Flags )

0 comments on commit 2a1cacd

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