Skip to content

Commit

Permalink
Update to duplicate layer(s)
Browse files Browse the repository at this point in the history
- Better information about unsupported types
- Action unavailable in contextual menu for individually selected unsupported layers
  • Loading branch information
dakcarto committed Nov 3, 2012
1 parent ab2442b commit bb426f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
13 changes: 12 additions & 1 deletion src/app/legend/qgslegendlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
theMenu.addAction( QgsApplication::getThemeIcon( "/mActionRemoveLayer.png" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );

// duplicate layer
theMenu.addAction( QgsApplication::getThemeIcon( "/mActionAddMap.png" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );
QAction* duplicateLayersAction = theMenu.addAction( QgsApplication::getThemeIcon( "/mActionAddMap.png" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );

// set layer crs
theMenu.addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );
Expand Down Expand Up @@ -460,6 +460,12 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
}
}

// disable duplication of memory layers
if ( vlayer->storageType() == "Memory storage" && legend()->selectedLayers().count() == 1 )
{
duplicateLayersAction->setEnabled( false );
}

// save as vector file
theMenu.addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) );

Expand All @@ -485,6 +491,11 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
{
theMenu.addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) );
}
else if ( lyr->type() == QgsMapLayer::PluginLayer && legend()->selectedLayers().count() == 1 )
{
// disable duplication of plugin layers
duplicateLayersAction->setEnabled( false );
}

// properties goes on bottom of menu for consistency with normal ui standards
// e.g. kde stuff
Expand Down
62 changes: 38 additions & 24 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5363,31 +5363,49 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
}

mMapCanvas->freeze();
// int startCount = QgsMapLayerRegistry::instance()->count();
QgsMapLayer *dupLayer;
QString layerDupName, unSppType;

foreach ( QgsMapLayer * selectedLyr, selectedLyrs )
{
dupLayer = 0;
QString layerDupName = selectedLyr->name() + " " + tr( "copy" );
unSppType = QString( "" );
layerDupName = selectedLyr->name() + " " + tr( "copy" );

// setup for placing duplicated layer below source layer, regardless of group depth
mMapLegend->blockSignals( true );
mMapLegend->setCurrentLayer( selectedLyr );
if ( !mMapLegend->setCurrentLayer( selectedLyr ) )
{
mMapLegend->blockSignals( false );
continue; // legend item doesn't exist for map layer
}
mMapLegend->blockSignals( false );
QTreeWidgetItem *sourceItem = mMapLegend->currentItem();
QgsLegendLayer *sourcellayer = mMapLegend->currentLegendLayer();

if ( selectedLyr->type() == QgsMapLayer::PluginLayer )
{
unSppType = tr( "Plugin layer" );
}

// duplicate the layer's basic parameters

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( selectedLyr );
// TODO: check for other layer types that won't duplicate correctly
// currently memory and plugin layers are skipped
if ( vlayer && vlayer->storageType() != "Memory storage" )
if ( unSppType.isEmpty() )
{
dupLayer = new QgsVectorLayer( vlayer->source(), layerDupName, vlayer->providerType() );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( selectedLyr );
// TODO: check for other layer types that won't duplicate correctly
// currently memory and plugin layers are skipped
if ( vlayer && vlayer->storageType() == "Memory storage" )
{
unSppType = tr( "Memory layer" );
}
else if ( vlayer )
{
dupLayer = new QgsVectorLayer( vlayer->source(), layerDupName, vlayer->providerType() );
}
}

if ( !dupLayer )

if ( unSppType.isEmpty() && !dupLayer )
{
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer*>( selectedLyr );
if ( rlayer )
Expand All @@ -5396,18 +5414,21 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
}
}

if ( dupLayer && !dupLayer->isValid() )
if ( unSppType.isEmpty() && dupLayer && !dupLayer->isValid() )
{
// addMapLayer() also checks layer validity, but do it now to skip canvas refresh
QgsDebugMsg( "Duplicated layer was invalid" );
QMessageBox::information( this,
tr( "Invalid Layer" ),
tr( "%1\n\nDuplication resulted in invalid layer." ).arg( selectedLyr->name() ) );
continue;
}

if ( !dupLayer )
if ( !unSppType.isEmpty() || !dupLayer )
{
QMessageBox::information( this,
tr( "Unsupported Layer" ),
tr( "%1\n\nDuplication of layer type is unsupported." ).arg( selectedLyr->name() ) );
tr( "%1\n%2\n\nDuplication of layer type is unsupported." )
.arg( selectedLyr->name() )
.arg( !unSppType.isEmpty() ? QString( " (" ) + unSppType + QString( ")" ) : "" ) );
continue;
}

Expand All @@ -5421,15 +5442,13 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
pasteStyle( dupLayer );

// move layer to just below source layer
QTreeWidgetItem *dupItem = mMapLegend->currentItem();
mMapLegend->moveItem( dupItem, sourceItem );
QgsLegendLayer *dupllayer = mMapLegend->currentLegendLayer();
mMapLegend->moveItem( dupllayer, sourcellayer );

// always set duplicated layers to not visible
// so layer can be configured before being turned on,
// and no map canvas refresh needed when doing multiple duplications
mMapLegend->setLayerVisible( dupLayer, false );
// OR, set visible property from source layer? (will require canvas refresh)
//mMapLegend->setLayerVisible( dupLayer, mMapLegend->layerCheckState( selectedLyr ) == Qt::Checked );
}

dupLayer = 0;
Expand All @@ -5438,11 +5457,6 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
qApp->processEvents();

mMapCanvas->freeze( false );
// if ( QgsMapLayerRegistry::instance()->count() > startCount )
// {
// statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
// mMapCanvas->refresh();
// }
}

void QgisApp::setLayerCRS()
Expand Down

0 comments on commit bb426f4

Please sign in to comment.