@@ -366,6 +366,7 @@ void QgsLegend::removeLayers( QStringList theLayers )
366
366
{
367
367
invLayerRemoved = true ;
368
368
}
369
+ removeLegendLayerActionsForLayer ( ll->layer () );
369
370
removeItem ( ll );
370
371
delete ll;
371
372
break ;
@@ -761,7 +762,73 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
761
762
{
762
763
if ( li->type () == QgsLegendItem::LEGEND_LAYER )
763
764
{
764
- qobject_cast<QgsLegendLayer*>( li )->addToPopupMenu ( theMenu );
765
+ QgsLegendLayer* lyr = qobject_cast<QgsLegendLayer*>( li );
766
+ lyr->addToPopupMenu ( theMenu );
767
+
768
+ // add custom layer actions
769
+ QList< LegendLayerAction > actions = legendLayerActions ( lyr->layer ()->type () );
770
+ if ( ! actions.isEmpty () )
771
+ {
772
+ theMenu.addSeparator ();
773
+ QList<QMenu*> theMenus;
774
+ for ( int i = 0 ; i < actions.count (); i++ )
775
+ {
776
+ if ( actions[i].allLayers || actions[i].layers .contains ( lyr->layer () ) )
777
+ {
778
+ if ( actions[i].menu .isEmpty () )
779
+ {
780
+ theMenu.addAction ( actions[i].action );
781
+ }
782
+ else
783
+ {
784
+ // find or create menu for given menu name
785
+ // adapted from QgisApp::getPluginMenu( QString menuName )
786
+ QString menuName = actions[i].menu ;
787
+ #ifdef Q_WS_MAC
788
+ // Mac doesn't have '&' keyboard shortcuts.
789
+ menuName.remove ( QChar ( ' &' ) );
790
+ #endif
791
+ QAction* before = 0 ;
792
+ QMenu* newMenu = 0 ;
793
+ QString dst = menuName;
794
+ dst.remove ( QChar ( ' &' ) );
795
+ foreach ( QMenu* menu, theMenus )
796
+ {
797
+ QString src = menu->title ();
798
+ src.remove ( QChar ( ' &' ) );
799
+ int comp = dst.localeAwareCompare ( src );
800
+ if ( comp < 0 )
801
+ {
802
+ // Add item before this one
803
+ before = menu->menuAction ();
804
+ break ;
805
+ }
806
+ else if ( comp == 0 )
807
+ {
808
+ // Plugin menu item already exists
809
+ newMenu = menu;
810
+ break ;
811
+ }
812
+ }
813
+ if ( ! newMenu )
814
+ {
815
+ // It doesn't exist, so create
816
+ newMenu = new QMenu ( menuName, this );
817
+ theMenus.append ( newMenu );
818
+ // Where to put it? - we worked that out above...
819
+ theMenu.insertMenu ( before, newMenu );
820
+ }
821
+ // QMenu* menu = getMenu( actions[i].menu, &theBeforeSep, &theAfterSep, &theMenu );
822
+ newMenu->addAction ( actions[i].action );
823
+ }
824
+ }
825
+ }
826
+ theMenu.addSeparator ();
827
+ }
828
+
829
+ // properties goes on bottom of menu for consistency with normal ui standards
830
+ // e.g. kde stuff
831
+ theMenu.addAction ( tr ( " &Properties" ), QgisApp::instance (), SLOT ( layerProperties () ) );
765
832
766
833
if ( li->parent () && !parentGroupEmbedded ( li ) )
767
834
{
@@ -2969,3 +3036,61 @@ void QgsLegend::groupSelectedLayers()
2969
3036
}
2970
3037
}
2971
3038
3039
+ void QgsLegend::addLegendLayerAction ( QAction* action, QString menu, QString id,
3040
+ QgsMapLayer::LayerType type, bool allLayers )
3041
+ {
3042
+ mLegendLayerActionMap [type].append ( LegendLayerAction ( action, menu, id, allLayers ) );
3043
+ }
3044
+
3045
+ bool QgsLegend::removeLegendLayerAction ( QAction* action )
3046
+ {
3047
+ QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
3048
+ for ( it = mLegendLayerActionMap .begin ();
3049
+ it != mLegendLayerActionMap .end (); ++it )
3050
+ {
3051
+ for ( int i = 0 ; i < it->count (); i++ )
3052
+ {
3053
+ if (( *it )[i].action == action )
3054
+ {
3055
+ ( *it ).removeAt ( i );
3056
+ return true ;
3057
+ }
3058
+ }
3059
+ }
3060
+ return false ;
3061
+ }
3062
+
3063
+ void QgsLegend::addLegendLayerActionForLayer ( QAction* action, QgsMapLayer* layer )
3064
+ {
3065
+ QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
3066
+ for ( it = mLegendLayerActionMap .begin ();
3067
+ it != mLegendLayerActionMap .end (); ++it )
3068
+ {
3069
+ for ( int i = 0 ; i < it->count (); i++ )
3070
+ {
3071
+ if (( *it )[i].action == action )
3072
+ {
3073
+ ( *it )[i].layers .append ( layer );
3074
+ return ;
3075
+ }
3076
+ }
3077
+ }
3078
+ }
3079
+
3080
+ void QgsLegend::removeLegendLayerActionsForLayer ( QgsMapLayer* layer )
3081
+ {
3082
+ QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
3083
+ for ( it = mLegendLayerActionMap .begin ();
3084
+ it != mLegendLayerActionMap .end (); ++it )
3085
+ {
3086
+ for ( int i = 0 ; i < it->count (); i++ )
3087
+ {
3088
+ ( *it )[i].layers .removeAll ( layer );
3089
+ }
3090
+ }
3091
+ }
3092
+
3093
+ QList< LegendLayerAction > QgsLegend::legendLayerActions ( QgsMapLayer::LayerType type ) const
3094
+ {
3095
+ return mLegendLayerActionMap .contains ( type ) ? mLegendLayerActionMap .value ( type ) : QList< LegendLayerAction >() ;
3096
+ }
0 commit comments