@@ -628,9 +628,17 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
628
628
{
629
629
theMenu.addAction ( tr ( " Re&name" ), this , SLOT ( openEditor () ) );
630
630
}
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
631
639
}
632
640
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 () ) );
634
642
theMenu.addAction ( QgisApp::getThemeIcon ( " /mActionExpandTree.png" ), tr ( " &Expand all" ), this , SLOT ( expandAll () ) );
635
643
theMenu.addAction ( QgisApp::getThemeIcon ( " /mActionCollapseTree.png" ), tr ( " &Collapse all" ), this , SLOT ( collapseAll () ) );
636
644
@@ -2401,3 +2409,39 @@ void QgsLegend::toggleDrawingOrderUpdate()
2401
2409
{
2402
2410
setUpdateDrawingOrder ( !mUpdateDrawingOrder );
2403
2411
}
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
+
0 commit comments