Skip to content
Permalink
Browse files
[FEATURE] allow selection and removal of several layers at once
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14167 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 28, 2010
1 parent e752db7 commit d197b48dc27d54409ca20fb546924a78be620924
Showing with 64 additions and 14 deletions.
  1. +41 −0 src/app/legend/qgslegend.cpp
  2. +7 −0 src/app/legend/qgslegend.h
  3. +16 −14 src/app/qgisapp.cpp
@@ -93,6 +93,7 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
mInsertionLine->setPalette( pal );

setSortingEnabled( false );
setSelectionMode( QAbstractItemView::ExtendedSelection );
setDragEnabled( false );
setAutoScroll( true );
QFont f( "Arial", 10, QFont::Normal );
@@ -600,6 +601,20 @@ QgsMapLayer* QgsLegend::currentLayer()
}
}

QList<QgsMapLayer *> QgsLegend::selectedLayers()
{
QList<QgsMapLayer *> layers;

foreach( QTreeWidgetItem *item, selectedItems() )
{
QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
if ( ll )
layers << ll->layer();
}

return layers;
}

bool QgsLegend::setCurrentLayer( QgsMapLayer *layer )
{
QgsLegendLayer *ll = findLegendLayer( layer );
@@ -1746,3 +1761,29 @@ void QgsLegend::refreshCheckStates()
}
}

void QgsLegend::removeSelectedLayers()
{
// Turn off rendering to improve speed.
bool renderFlagState = mMapCanvas->renderFlag();
mMapCanvas->setRenderFlag( false );

foreach( QTreeWidgetItem *item, selectedItems() )
{
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item );
if ( lg )
{
removeGroup( lg );
continue;
}

QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
if ( ll && ll->layer() )
{
QgsMapLayerRegistry::instance()->removeMapLayer( ll->layer()->getLayerID() );
continue;
}
}

// Turn on rendering (if it was on previously)
mMapCanvas->setRenderFlag( renderFlagState );
}
@@ -109,6 +109,10 @@ class QgsLegend : public QTreeWidget
Else, 0 is returned.*/
QgsMapLayer* currentLayer();

/*!Returns the currently selected layer QgsLegendLayers.
Else, an empty list is returned.*/
QList<QgsMapLayer *> selectedLayers();

/*!set the current layer
returns true if the layer exists, false otherwise*/
bool setCurrentLayer( QgsMapLayer *layer );
@@ -263,6 +267,9 @@ class QgsLegend : public QTreeWidget

/**Updates check states when the map canvas layer set is changed */
void refreshCheckStates();

/** Remove selected layers */
void removeSelectedLayers();
protected:

/*!Event handler for mouse movements.
@@ -1072,9 +1072,9 @@ void QgisApp::createActions()
connect( mActionLayerSelectionSaveAs, SIGNAL( triggered() ), this, SLOT( saveSelectionAsVectorFile() ) );
mActionLayerSelectionSaveAs->setEnabled( false );

mActionRemoveLayer = new QAction( getThemeIcon( "mActionRemoveLayer.png" ), tr( "Remove Layer" ), this );
shortcuts->registerAction( mActionRemoveLayer, tr( "Ctrl+D", "Remove a Layer" ) );
mActionRemoveLayer->setStatusTip( tr( "Remove a Layer" ) );
mActionRemoveLayer = new QAction( getThemeIcon( "mActionRemoveLayer.png" ), tr( "Remove Layer(s)" ), this );
shortcuts->registerAction( mActionRemoveLayer, tr( "Ctrl+D", "Remove Layer(s)" ) );
mActionRemoveLayer->setStatusTip( tr( "Remove Layer(s)" ) );
connect( mActionRemoveLayer, SIGNAL( triggered() ), this, SLOT( removeLayer() ) );
mActionRemoveLayer->setEnabled( false );

@@ -3964,7 +3964,7 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection )
if ( !mMapLegend )
return;

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( activeLayer() );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( activeLayer() ); // FIXME: output of multiple layers at once?
if ( !vlayer )
return;

@@ -4741,7 +4741,7 @@ void QgisApp::saveEdits()
if ( mMapCanvas && mMapCanvas->isDrawing() )
return;

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( activeLayer() );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( activeLayer() ); // FIXME: save edits of all selected layers
if ( !vlayer || !vlayer->isEditable() || !vlayer->isModified() )
return;

@@ -4989,18 +4989,20 @@ void QgisApp::removeLayer()
return;
}

QgsMapLayer *layer = activeLayer();

if ( !layer )
if ( !mMapLegend )
{
return;
}

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( layer );
if ( vlayer && vlayer->isEditable() && !toggleEditing( vlayer, true ) )
return;
foreach( QgsMapLayer *layer, mMapLegend->selectedLayers() )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( layer );
if ( vlayer && vlayer->isEditable() && !toggleEditing( vlayer, true ) )
return;
}

mMapLegend->removeSelectedLayers();

QgsMapLayerRegistry::instance()->removeMapLayer( layer->getLayerID() );
mMapCanvas->refresh();
}

@@ -5903,6 +5905,8 @@ void QgisApp::selectionChanged( QgsMapLayer *layer )

void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
{
mActionRemoveLayer->setEnabled( mMapLegend && mMapLegend->selectedItems().size() > 0 );

if ( !layer )
{
mActionSelect->setEnabled( false );
@@ -5917,7 +5921,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionSaveEdits->setEnabled( false );
mActionLayerSaveAs->setEnabled( false );
mActionLayerSelectionSaveAs->setEnabled( false );
mActionRemoveLayer->setEnabled( false );
mActionLayerProperties->setEnabled( false );
mActionLayerSubsetString->setEnabled( false );
mActionAddToOverview->setEnabled( false );
@@ -5951,7 +5954,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
return;
}

mActionRemoveLayer->setEnabled( true );
mActionLayerProperties->setEnabled( true );
mActionAddToOverview->setEnabled( true );

0 comments on commit d197b48

Please sign in to comment.