|
20 | 20 | #include "qgslayertree.h" |
21 | 21 |
|
22 | 22 | #include "qgsproject.h" |
| 23 | +#include "qgslogger.h" |
| 24 | +#include <QAction> |
23 | 25 |
|
24 | 26 | QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root, QObject *parent ) |
25 | 27 | : QObject( parent ) |
@@ -63,11 +65,17 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers ) |
63 | 65 |
|
64 | 66 | void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds ) |
65 | 67 | { |
| 68 | + QgsDebugMsg( QString( "%1 layers will be removed, enabled:%2" ).arg( layerIds.count() ).arg( mEnabled ) ); |
| 69 | + |
66 | 70 | if ( !mEnabled ) |
67 | 71 | return; |
68 | 72 |
|
69 | 73 | foreach ( QString layerId, layerIds ) |
70 | 74 | { |
| 75 | + QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId ); |
| 76 | + if ( mapLayer ) |
| 77 | + removeLegendLayerActionsForLayer( mapLayer ); |
| 78 | + |
71 | 79 | QgsLayerTreeLayer* nodeLayer = mRoot->findLayer( layerId ); |
72 | 80 | if ( nodeLayer ) |
73 | 81 | qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent() )->removeChildNode( nodeLayer ); |
@@ -111,5 +119,79 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren() |
111 | 119 | toRemove << layerId; |
112 | 120 | mLayerIdsForRemoval.clear(); |
113 | 121 |
|
| 122 | + QgsDebugMsg( QString( "%1 layers will be removed" ).arg( toRemove.count() ) ); |
| 123 | + |
114 | 124 | QgsMapLayerRegistry::instance()->removeMapLayers( toRemove ); |
115 | 125 | } |
| 126 | + |
| 127 | +void QgsLayerTreeRegistryBridge::addLegendLayerAction( QAction* action, QString menu, QString id, |
| 128 | + QgsMapLayer::LayerType type, bool allLayers ) |
| 129 | +{ |
| 130 | + mLegendLayerActionMap[type].append( LegendLayerAction( action, menu, id, allLayers ) ); |
| 131 | +} |
| 132 | + |
| 133 | +bool QgsLayerTreeRegistryBridge::removeLegendLayerAction( QAction* action ) |
| 134 | +{ |
| 135 | + QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it; |
| 136 | + for ( it = mLegendLayerActionMap.begin(); |
| 137 | + it != mLegendLayerActionMap.end(); ++it ) |
| 138 | + { |
| 139 | + for ( int i = 0; i < it->count(); i++ ) |
| 140 | + { |
| 141 | + if (( *it )[i].action == action ) |
| 142 | + { |
| 143 | + ( *it ).removeAt( i ); |
| 144 | + return true; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return false; |
| 149 | +} |
| 150 | + |
| 151 | +void QgsLayerTreeRegistryBridge::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer ) |
| 152 | +{ |
| 153 | + legendLayerActions( layer->type() ); |
| 154 | + if ( !action || !layer || ! mLegendLayerActionMap.contains( layer->type() ) ) |
| 155 | + return; |
| 156 | + |
| 157 | + QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it |
| 158 | + = mLegendLayerActionMap.find( layer->type() ); |
| 159 | + for ( int i = 0; i < it->count(); i++ ) |
| 160 | + { |
| 161 | + if ( ( *it )[i].action == action ) |
| 162 | + { |
| 163 | + ( *it )[i].layers.append( layer ); |
| 164 | + return; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +void QgsLayerTreeRegistryBridge::removeLegendLayerActionsForLayer( QgsMapLayer* layer ) |
| 170 | +{ |
| 171 | + if ( ! layer || ! mLegendLayerActionMap.contains( layer->type() ) ) |
| 172 | + return; |
| 173 | + |
| 174 | + QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it |
| 175 | + = mLegendLayerActionMap.find( layer->type() ); |
| 176 | + for ( int i = 0; i < it->count(); i++ ) |
| 177 | + { |
| 178 | + ( *it )[i].layers.removeAll( layer ); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +QList< LegendLayerAction > QgsLayerTreeRegistryBridge::legendLayerActions( QgsMapLayer::LayerType type ) const |
| 183 | +{ |
| 184 | +#ifdef QGISDEBUG |
| 185 | + if ( mLegendLayerActionMap.contains( type ) ) |
| 186 | + { |
| 187 | + QgsDebugMsg( QString("legendLayerActions for layers of type %1:").arg( type ) ); |
| 188 | + |
| 189 | + foreach ( LegendLayerAction lyrAction, mLegendLayerActionMap[ type ] ) |
| 190 | + { |
| 191 | + QgsDebugMsg( QString("%1/%2 - %3 layers").arg( lyrAction.menu ).arg( lyrAction.action->text() ).arg( lyrAction.layers.count()) ); |
| 192 | + } |
| 193 | + } |
| 194 | +#endif |
| 195 | + |
| 196 | + return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ; |
| 197 | +} |
0 commit comments