Skip to content

Commit

Permalink
It's FAR too expensive to construct a QgsSettings object for every sy…
Browse files Browse the repository at this point in the history
…mbol node, especially for complex

projects. So only read the valid size ranges once, and store them for subsequent use
  • Loading branch information
nyalldawson committed Dec 9, 2020
1 parent e779bf9 commit da51a5a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Expand Up @@ -293,6 +293,9 @@ and allowing interaction with the symbol / renderer.
%End
public:

static double MINIMUM_SIZE;
static double MAXIMUM_SIZE;

QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsSymbolLegendNode.
Expand Down
3 changes: 3 additions & 0 deletions src/app/options/qgsoptions.cpp
Expand Up @@ -38,6 +38,7 @@
#include "qgsexpressioncontextutils.h"
#include "qgslocaldefaultsettings.h"
#include "qgsnumericformatwidget.h"
#include "qgslayertreemodellegendnode.h"

#include "qgsattributetablefiltermodel.h"
#include "qgslocalizeddatapathregistry.h"
Expand Down Expand Up @@ -1570,6 +1571,8 @@ void QgsOptions::saveOptions()

mSettings->setValue( QStringLiteral( "/qgis/legendsymbolMinimumSize" ), mLegendSymbolMinimumSizeSpinBox->value() );
mSettings->setValue( QStringLiteral( "/qgis/legendsymbolMaximumSize" ), mLegendSymbolMaximumSizeSpinBox->value() );
QgsSymbolLegendNode::MINIMUM_SIZE = mLegendSymbolMinimumSizeSpinBox->value();
QgsSymbolLegendNode::MAXIMUM_SIZE = mLegendSymbolMaximumSizeSpinBox->value();

mSettings->setValue( QStringLiteral( "/qgis/defaultLegendGraphicResolution" ), mLegendGraphicResolutionSpinBox->value() );
mSettings->setValue( QStringLiteral( "/qgis/mapTipsDelay" ), mMapTipsDelaySpinBox->value() );
Expand Down
21 changes: 15 additions & 6 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -287,6 +287,9 @@ void QgsLayerTreeModelLegendNode::toggleAllItems()

// -------------------------------------------------------------------------

double QgsSymbolLegendNode::MINIMUM_SIZE = -1.0;
double QgsSymbolLegendNode::MAXIMUM_SIZE = -1.0;

QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent )
: QgsLayerTreeModelLegendNode( nodeLayer, parent )
, mItem( item )
Expand All @@ -295,9 +298,14 @@ QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const Qg
const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
mIconSize = QSize( iconSize, iconSize );

QgsSettings settings;
mSymbolMinimumSize = settings.value( "/qgis/legendsymbolMinimumSize", 0.5 ).toDouble();
mSymbolMaximumSize = settings.value( "/qgis/legendsymbolMaximumSize", 20.0 ).toDouble();
if ( MINIMUM_SIZE < 0 )
{
// it's FAR too expensive to construct a QgsSettings object for every symbol node, especially for complex
// projects. So only read the valid size ranges once, and store them for subsequent use
QgsSettings settings;
MINIMUM_SIZE = settings.value( "/qgis/legendsymbolMinimumSize", 0.5 ).toDouble();
MAXIMUM_SIZE = settings.value( "/qgis/legendsymbolMaximumSize", 20.0 ).toDouble();
}

updateLabel();
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
Expand Down Expand Up @@ -334,7 +342,7 @@ QSize QgsSymbolLegendNode::minimumIconSize( QgsRenderContext *context ) const
// unusued width, height variables
double width = 0.0;
double height = 0.0;
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), mSymbolMinimumSize, mSymbolMaximumSize, context, width, height ) );
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context, width, height ) );
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerUtils::symbolPreviewPixmap( symbol ? symbol.get() : mItem.symbol(), QSize( largeIconSize, largeIconSize ), 0,
context ).toImage(),
Expand All @@ -345,7 +353,7 @@ QSize QgsSymbolLegendNode::minimumIconSize( QgsRenderContext *context ) const
{
double width = 0.0;
double height = 0.0;
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), mSymbolMinimumSize, mSymbolMaximumSize, context, width, height ) );
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context, width, height ) );
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerUtils::symbolPreviewPixmap( symbol ? symbol.get() : mItem.symbol(), QSize( minSz.width(), largeIconSize ), 0,
context ).toImage(),
Expand Down Expand Up @@ -506,7 +514,7 @@ QVariant QgsSymbolLegendNode::data( int role ) const
// unusued width, height variables
double width = 0.0;
double height = 0.0;
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), mSymbolMinimumSize, mSymbolMaximumSize, context.get(), width, height ) );
std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context.get(), width, height ) );
pix = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol ? symbol.get() : mItem.symbol(), mIconSize, 0, context.get() );

if ( !mTextOnSymbolLabel.isEmpty() && context )
Expand Down Expand Up @@ -1416,3 +1424,4 @@ void QgsDataDefinedSizeLegendNode::cacheImage() const
mImage = mSettings->collapsedLegendImage( *context );
}
}

6 changes: 3 additions & 3 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -341,9 +341,11 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode
{
Q_OBJECT


public:

static double MINIMUM_SIZE;
static double MAXIMUM_SIZE;

/**
* Constructor for QgsSymbolLegendNode.
* \param nodeLayer layer node
Expand Down Expand Up @@ -497,8 +499,6 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode
bool mSymbolUsesMapUnits;

QSize mIconSize;
double mSymbolMinimumSize = 0.5;
double mSymbolMaximumSize = 20.0;

QString mTextOnSymbolLabel;
QgsTextFormat mTextOnSymbolTextFormat;
Expand Down

0 comments on commit da51a5a

Please sign in to comment.