Skip to content

Commit

Permalink
Nicer placement of item actions
Browse files Browse the repository at this point in the history
- Make sure Add 3d map appears directly after Add Map
- Place Dynamic Text menu directly after Add Label action
  • Loading branch information
nyalldawson committed Jan 11, 2021
1 parent 4d3a022 commit b7cf210
Showing 1 changed file with 82 additions and 11 deletions.
93 changes: 82 additions & 11 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -408,7 +408,16 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla


connect( mActionClose, &QAction::triggered, this, &QWidget::close ); connect( mActionClose, &QAction::triggered, this, &QWidget::close );


mDynamicTextMenu = new QMenu( tr( "Dynamic Text" ), this ); // populate with initial items...
const QList< int > itemMetadataIds = QgsGui::layoutItemGuiRegistry()->itemMetadataIds();
for ( int id : itemMetadataIds )
{
itemTypeAdded( id );
}
//..and listen out for new item types
connect( QgsGui::layoutItemGuiRegistry(), &QgsLayoutItemGuiRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );

mDynamicTextMenu = new QMenu( tr( "Add Dynamic Text" ), this );


connect( mDynamicTextMenu, &QMenu::aboutToShow, this, [ = ] connect( mDynamicTextMenu, &QMenu::aboutToShow, this, [ = ]
{ {
Expand All @@ -423,16 +432,24 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
} ); } );
} ); } );


mItemMenu->addMenu( mDynamicTextMenu ); // not so nice hack to insert dynamic text menu near the label item

int index = 0;
// populate with initial items... for ( QAction *action : mItemMenu->actions() )
const QList< int > itemMetadataIds = QgsGui::layoutItemGuiRegistry()->itemMetadataIds();
for ( int id : itemMetadataIds )
{ {
itemTypeAdded( id ); if ( action->data().isValid() && QgsGui::layoutItemGuiRegistry()->itemMetadata( action->data().toInt() )->type() == QgsLayoutItemRegistry::LayoutLabel )
{
if ( QAction *before = mItemMenu->actions().value( index + 1 ) )
{
mItemMenu->insertMenu( before, mDynamicTextMenu );
}
else
{
mItemMenu->addMenu( mDynamicTextMenu );
}
break;
}
index++;
} }
//..and listen out for new item types
connect( QgsGui::layoutItemGuiRegistry(), &QgsLayoutItemGuiRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );


QToolButton *orderingToolButton = new QToolButton( this ); QToolButton *orderingToolButton = new QToolButton( this );
orderingToolButton->setPopupMode( QToolButton::InstantPopup ); orderingToolButton->setPopupMode( QToolButton::InstantPopup );
Expand Down Expand Up @@ -1632,12 +1649,66 @@ void QgsLayoutDesignerDialog::itemTypeAdded( int id )
if ( itemSubmenu ) if ( itemSubmenu )
itemSubmenu->addAction( action ); itemSubmenu->addAction( action );
else else
mItemMenu->addAction( action ); {
// a not nice hack to manually place 3d layout map entry in the right place
if ( QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->type() == QgsLayoutItemRegistry::Layout3DMap )
{
// find position of normal add map action
int index = 0;
for ( QAction *existingAction : mItemMenu->actions() )
{
if ( existingAction->data().isValid() && QgsGui::layoutItemGuiRegistry()->itemMetadata( existingAction->data().toInt() )->type() == QgsLayoutItemRegistry::LayoutMap )
{
if ( QAction *before = mItemMenu->actions().value( index + 1 ) )
{
mItemMenu->insertAction( before, action );
}
else
{
mItemMenu->addAction( action );
}
break;
}
index++;
}
}
else
{
mItemMenu->addAction( action );
}
}


if ( groupButton ) if ( groupButton )
groupButton->addAction( action ); groupButton->addAction( action );
else else
mToolsToolbar->addAction( action ); {
// a not nice hack to manually place 3d layout map entry in the right place
if ( QgsGui::layoutItemGuiRegistry()->itemMetadata( id )->type() == QgsLayoutItemRegistry::Layout3DMap )
{
// find position of normal add map action
int index = 0;
for ( QAction *existingAction : mToolsToolbar->actions() )
{
if ( existingAction->data().isValid() && QgsGui::layoutItemGuiRegistry()->itemMetadata( existingAction->data().toInt() )->type() == QgsLayoutItemRegistry::LayoutMap )
{
if ( QAction *before = mToolsToolbar->actions().value( index + 1 ) )
{
mToolsToolbar->insertAction( before, action );
}
else
{
mToolsToolbar->addAction( action );
}
break;
}
index++;
}
}
else
{
mToolsToolbar->addAction( action );
}
}


connect( action, &QAction::triggered, this, [this, id, nodeBased]() connect( action, &QAction::triggered, this, [this, id, nodeBased]()
{ {
Expand Down

0 comments on commit b7cf210

Please sign in to comment.