Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
defer legend invalidation to avoid reprocessing when multiple layers …
…are involved
  • Loading branch information
jef-n committed Sep 21, 2015
1 parent 9cbfeea commit 2a1cacd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
8 changes: 5 additions & 3 deletions python/core/layertree/qgslayertreemodel.sip
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -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 )
Expand All @@ -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()
Expand Down Expand Up @@ -682,7 +685,7 @@ void QgsLayerTreeModel::nodeLayerLoaded()
if ( !nodeLayer )
return;

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

Expand Down Expand Up @@ -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
//
Expand Down
24 changes: 14 additions & 10 deletions src/core/layertree/qgslayertreemodel.h
Expand Up @@ -19,6 +19,7 @@
#include <QAbstractItemModel>
#include <QFont>
#include <QIcon>
#include <QTimer>

class QgsLayerTreeNode;
class QgsLayerTreeGroup;
Expand All @@ -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
Expand Down Expand Up @@ -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 )

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

void legendNodeDataChanged();

void invalidateLegendMapBasedData();

protected:
void removeLegendFromLayer( QgsLayerTreeLayer* nodeLayer );
void addLegendToLayer( QgsLayerTreeLayer* nodeL );
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 2a1cacd

Please sign in to comment.