Skip to content

Commit d2b9e95

Browse files
ArunmozhiNathanW2
Arunmozhi
authored andcommitted
[FEATURE] Group selected layers on right click
1 parent d2775ba commit d2b9e95

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/app/legend/qgslegend.cpp

+45-1
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,17 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
628628
{
629629
theMenu.addAction( tr( "Re&name" ), this, SLOT( openEditor() ) );
630630
}
631+
//
632+
// Option to group layers, if the selection is more than one
633+
//
634+
if( selectedLayers().length() > 1 )
635+
{
636+
theMenu.addAction( tr( "&Group selected" ), this, SLOT( groupSelectedLayers() ) );
637+
}
638+
// ends here
631639
}
632640

633-
theMenu.addAction( QgisApp::getThemeIcon( "/folder_new.png" ), tr( "&Add group" ), this, SLOT( addGroupToCurrentItem() ) );
641+
theMenu.addAction( QgisApp::getThemeIcon( "/folder_new.png" ), tr( "&Add new group" ), this, SLOT( addGroupToCurrentItem() ) );
634642
theMenu.addAction( QgisApp::getThemeIcon( "/mActionExpandTree.png" ), tr( "&Expand all" ), this, SLOT( expandAll() ) );
635643
theMenu.addAction( QgisApp::getThemeIcon( "/mActionCollapseTree.png" ), tr( "&Collapse all" ), this, SLOT( collapseAll() ) );
636644

@@ -2401,3 +2409,39 @@ void QgsLegend::toggleDrawingOrderUpdate()
24012409
{
24022410
setUpdateDrawingOrder( !mUpdateDrawingOrder );
24032411
}
2412+
2413+
void QgsLegend::groupSelectedLayers()
2414+
{
2415+
//avoid multiple refreshes of map canvas because of itemChanged signal
2416+
blockSignals( true );
2417+
2418+
QTreeWidgetItem * parent;
2419+
foreach( QTreeWidgetItem* item, selectedItems() )
2420+
{
2421+
parent = item->parent();
2422+
}
2423+
QgsLegendGroup *group;
2424+
2425+
if( parent )
2426+
{
2427+
group = new QgsLegendGroup( parent, tr( "sub-group" ) );
2428+
}
2429+
else
2430+
{
2431+
group = new QgsLegendGroup( this, tr( "group" ) );
2432+
}
2433+
2434+
foreach( QTreeWidgetItem * item, selectedItems() )
2435+
{
2436+
QgsLegendLayer* layer = dynamic_cast<QgsLegendLayer *>( item );
2437+
if ( layer )
2438+
{
2439+
insertItem( item, group );
2440+
}
2441+
}
2442+
editItem( group, 0 );
2443+
2444+
blockSignals( false );
2445+
2446+
}
2447+

src/app/legend/qgslegend.h

+3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ class QgsLegend : public QTreeWidget
313313
/** Update drawing order */
314314
void unsetUpdateDrawingOrder( bool dontUpdateDrawingOrder ) { setUpdateDrawingOrder( !dontUpdateDrawingOrder ); }
315315

316+
/** Create a new group for the selected items **/
317+
void groupSelectedLayers();
318+
316319
protected:
317320

318321
/*!Event handler for mouse movements.

0 commit comments

Comments
 (0)