Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 cb6981d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/gui/layout/qgslayoutlabelwidget.cpp
Expand Up @@ -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();
} );
}
} );

}
Expand All @@ -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 );
Expand Down

0 comments on commit cb6981d

Please sign in to comment.