[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
This commit is contained in:
Nyall Dawson 2017-10-10 09:48:43 +10:00
parent fdc0f75066
commit 53e24f1951

View File

@ -15,6 +15,7 @@
#include "qgslayoutappmenuprovider.h"
#include "qgslayoutitempage.h"
#include "qgslayoutitemgroup.h"
#include "qgslayoutdesignerdialog.h"
#include "qgslayout.h"
#include <QMenu>
@ -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 )