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

(cherry picked from commit da51a5a)
  • Loading branch information
nyalldawson committed Dec 21, 2020
1 parent 622ca71 commit c40c55c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Expand Up @@ -257,6 +257,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 @@ -242,6 +242,9 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set

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

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 @@ -250,9 +253,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();
connect( qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ), &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );
Expand Down Expand Up @@ -287,7 +295,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 @@ -298,7 +306,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 @@ -468,7 +476,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 @@ -1307,3 +1315,4 @@ void QgsDataDefinedSizeLegendNode::cacheImage() const
mImage = mSettings->collapsedLegendImage( *context );
}
}

6 changes: 3 additions & 3 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -307,9 +307,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 @@ -489,8 +491,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 c40c55c

Please sign in to comment.