From 53e24f1951471f2881d423e67de610cfa292102c Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 10 Oct 2017 09:48:43 +1000 Subject: [PATCH] [FEATURE][layouts] Add group/ungroup action to layout context menu Group/ungroup actions show only when an appropriate selection is present. I.e. "group" appears when a selection of at least two items is present, and "ungroup" appears only when at least one of the selected items is a group. Refs #1830 --- src/app/layout/qgslayoutappmenuprovider.cpp | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/app/layout/qgslayoutappmenuprovider.cpp b/src/app/layout/qgslayoutappmenuprovider.cpp index 1c590746a2f..7677e31e0e0 100644 --- a/src/app/layout/qgslayoutappmenuprovider.cpp +++ b/src/app/layout/qgslayoutappmenuprovider.cpp @@ -15,6 +15,7 @@ #include "qgslayoutappmenuprovider.h" #include "qgslayoutitempage.h" +#include "qgslayoutitemgroup.h" #include "qgslayoutdesignerdialog.h" #include "qgslayout.h" #include @@ -31,6 +32,46 @@ QMenu *QgsLayoutAppMenuProvider::createContextMenu( QWidget *parent, QgsLayout * { QMenu *menu = new QMenu( parent ); + const QList< QgsLayoutItem * > selectedItems = layout->selectedLayoutItems(); + if ( !selectedItems.empty() ) + { + bool addedGroupAction = false; + if ( selectedItems.count() > 1 ) + { + QAction *groupAction = new QAction( tr( "Group" ), menu ); + connect( groupAction, &QAction::triggered, this, [this]() + { + mDesigner->view()->groupSelectedItems(); + } ); + menu->addAction( groupAction ); + addedGroupAction = true; + } + bool foundSelectedGroup = false; + QList< QgsLayoutItemGroup * > groups; + layout->layoutItems( groups ); + for ( QgsLayoutItemGroup *group : qgsAsConst( groups ) ) + { + if ( group->isSelected() ) + { + foundSelectedGroup = true; + break; + } + } + if ( foundSelectedGroup ) + { + QAction *ungroupAction = new QAction( tr( "Ungroup" ), menu ); + connect( ungroupAction, &QAction::triggered, this, [this]() + { + mDesigner->view()->ungroupSelectedItems(); + } ); + menu->addAction( ungroupAction ); + addedGroupAction = true; + } + + if ( addedGroupAction ) + menu->addSeparator(); + } + // is a page under the mouse? QgsLayoutItemPage *page = layout->pageCollection()->pageAtPoint( layoutPoint ); if ( page )