Skip to content
Permalink
Browse files

Don't crash when trying to insert dynamic text into an empty report

Fixes #42579
  • Loading branch information
nyalldawson committed Apr 3, 2021
1 parent 56f7812 commit cb6981dbff58c27d285fc8f16cc5979693f3eb74
Showing with 20 additions and 13 deletions.
  1. +10 −7 src/app/layout/qgslayoutdesignerdialog.cpp
  2. +10 −6 src/gui/layout/qgslayoutlabelwidget.cpp
@@ -425,14 +425,17 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
connect( mDynamicTextMenu, &QMenu::aboutToShow, this, [ = ]
{
mDynamicTextMenu->clear();
// we need to rebuild this on each show, as the content varies depending on other available items...
QgsLayoutLabelWidget::buildInsertDynamicTextMenu( mLayout, mDynamicTextMenu, [ = ]( const QString & expression )
if ( mLayout )
{
activateNewItemCreationTool( QgsGui::layoutItemGuiRegistry()->metadataIdForItemType( QgsLayoutItemRegistry::LayoutLabel ), false );
QVariantMap properties;
properties.insert( QStringLiteral( "expression" ), expression );
mAddItemTool->setCustomProperties( properties );
} );
// we need to rebuild this on each show, as the content varies depending on other available items...
QgsLayoutLabelWidget::buildInsertDynamicTextMenu( mLayout, mDynamicTextMenu, [ = ]( const QString & expression )
{
activateNewItemCreationTool( QgsGui::layoutItemGuiRegistry()->metadataIdForItemType( QgsLayoutItemRegistry::LayoutLabel ), false );
QVariantMap properties;
properties.insert( QStringLiteral( "expression" ), expression );
mAddItemTool->setCustomProperties( properties );
} );
}
} );

// not so nice hack to insert dynamic text menu near the label item
@@ -75,13 +75,16 @@ QgsLayoutLabelWidget::QgsLayoutLabelWidget( QgsLayoutItemLabel *label )
connect( mDynamicTextMenu, &QMenu::aboutToShow, this, [ = ]
{
mDynamicTextMenu->clear();
// we need to rebuild this on each show, as the content varies depending on other available items...
buildInsertDynamicTextMenu( mLabel->layout(), mDynamicTextMenu, [ = ]( const QString & expression )
if ( mLabel->layout() )
{
mLabel->beginCommand( tr( "Insert dynamic text" ) );
mTextEdit->insertPlainText( "[%" + expression + "%]" );
mLabel->endCommand();
} );
// we need to rebuild this on each show, as the content varies depending on other available items...
buildInsertDynamicTextMenu( mLabel->layout(), mDynamicTextMenu, [ = ]( const QString & expression )
{
mLabel->beginCommand( tr( "Insert dynamic text" ) );
mTextEdit->insertPlainText( "[%" + expression + "%]" );
mLabel->endCommand();
} );
}
} );

}
@@ -94,6 +97,7 @@ void QgsLayoutLabelWidget::setMasterLayout( QgsMasterLayoutInterface *masterLayo

void QgsLayoutLabelWidget::buildInsertDynamicTextMenu( QgsLayout *layout, QMenu *menu, const std::function<void ( const QString & )> &callback )
{
Q_ASSERT( layout );
auto addExpression = [&callback]( QMenu * menu, const QString & name, const QString & expression )
{
QAction *action = new QAction( name, menu );

0 comments on commit cb6981d

Please sign in to comment.