Skip to content

Commit

Permalink
Fix crash when extracting symbols from project with layout legend
Browse files Browse the repository at this point in the history
Legend must be initialized before calling style visitor function

Fixes #57609
  • Loading branch information
nyalldawson committed Jul 23, 2024
1 parent 4891bda commit 2fb8519
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Returns whether the legend should automatically resize to fit its contents.
Returns the legend model.
%End


void setAutoUpdateModel( bool autoUpdate );
%Docstring
Sets whether the legend content should auto update to reflect changes in the project's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Returns whether the legend should automatically resize to fit its contents.
Returns the legend model.
%End


void setAutoUpdateModel( bool autoUpdate );
%Docstring
Sets whether the legend content should auto update to reflect changes in the project's
Expand Down
15 changes: 11 additions & 4 deletions src/core/layout/qgslayoutitemlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,13 @@ void QgsLayoutItemLegend::setCustomLayerTree( QgsLayerTree *rootGroup )
mCustomLayerTree.reset( rootGroup );
}

void QgsLayoutItemLegend::ensureModelIsInitialized()
void QgsLayoutItemLegend::ensureModelIsInitialized() const
{
if ( mDeferLegendModelInitialization )
{
mDeferLegendModelInitialization = false;
setCustomLayerTree( mCustomLayerTree.release() );
QgsLayoutItemLegend *mutableThis = const_cast< QgsLayoutItemLegend * >( this );
mutableThis->mDeferLegendModelInitialization = false;
mutableThis->setCustomLayerTree( mutableThis->mCustomLayerTree.release() );
}
}

Expand All @@ -334,6 +335,12 @@ QgsLegendModel *QgsLayoutItemLegend::model()
return mLegendModel.get();
}

const QgsLegendModel *QgsLayoutItemLegend::model() const
{
ensureModelIsInitialized();
return mLegendModel.get();
}

void QgsLayoutItemLegend::setAutoUpdateModel( bool autoUpdate )
{
if ( autoUpdate == autoUpdateModel() )
Expand Down Expand Up @@ -1345,7 +1352,7 @@ bool QgsLayoutItemLegend::accept( QgsStyleEntityVisitorInterface *visitor ) cons
}
return true;
};
return visit( mLegendModel->rootGroup( ) );
return visit( model()->rootGroup( ) );
}

bool QgsLayoutItemLegend::isRefreshing() const
Expand Down
9 changes: 8 additions & 1 deletion src/core/layout/qgslayoutitemlegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
*/
QgsLegendModel *model();

/**
* Returns the legend model.
*
* \note Not available in Python bindings
*/
const QgsLegendModel *model() const SIP_SKIP;

/**
* Sets whether the legend content should auto update to reflect changes in the project's
* layer tree.
Expand Down Expand Up @@ -634,7 +641,7 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem

void setModelStyleOverrides( const QMap<QString, QString> &overrides );

void ensureModelIsInitialized();
void ensureModelIsInitialized() const;
std::unique_ptr< QgsLegendModel > mLegendModel;
std::unique_ptr< QgsLayerTree > mCustomLayerTree;
bool mDeferLegendModelInitialization = true;
Expand Down

0 comments on commit 2fb8519

Please sign in to comment.