Skip to content

Commit bb426f4

Browse files
committed
Update to duplicate layer(s)
- Better information about unsupported types - Action unavailable in contextual menu for individually selected unsupported layers
1 parent ab2442b commit bb426f4

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

src/app/legend/qgslegendlayer.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
431431
theMenu.addAction( QgsApplication::getThemeIcon( "/mActionRemoveLayer.png" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );
432432

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

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

463+
// disable duplication of memory layers
464+
if ( vlayer->storageType() == "Memory storage" && legend()->selectedLayers().count() == 1 )
465+
{
466+
duplicateLayersAction->setEnabled( false );
467+
}
468+
463469
// save as vector file
464470
theMenu.addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) );
465471

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

489500
// properties goes on bottom of menu for consistency with normal ui standards
490501
// e.g. kde stuff

src/app/qgisapp.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,31 +5363,49 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
53635363
}
53645364

53655365
mMapCanvas->freeze();
5366-
// int startCount = QgsMapLayerRegistry::instance()->count();
53675366
QgsMapLayer *dupLayer;
5367+
QString layerDupName, unSppType;
53685368

53695369
foreach ( QgsMapLayer * selectedLyr, selectedLyrs )
53705370
{
53715371
dupLayer = 0;
5372-
QString layerDupName = selectedLyr->name() + " " + tr( "copy" );
5372+
unSppType = QString( "" );
5373+
layerDupName = selectedLyr->name() + " " + tr( "copy" );
53735374

53745375
// setup for placing duplicated layer below source layer, regardless of group depth
53755376
mMapLegend->blockSignals( true );
5376-
mMapLegend->setCurrentLayer( selectedLyr );
5377+
if ( !mMapLegend->setCurrentLayer( selectedLyr ) )
5378+
{
5379+
mMapLegend->blockSignals( false );
5380+
continue; // legend item doesn't exist for map layer
5381+
}
53775382
mMapLegend->blockSignals( false );
5378-
QTreeWidgetItem *sourceItem = mMapLegend->currentItem();
5383+
QgsLegendLayer *sourcellayer = mMapLegend->currentLegendLayer();
5384+
5385+
if ( selectedLyr->type() == QgsMapLayer::PluginLayer )
5386+
{
5387+
unSppType = tr( "Plugin layer" );
5388+
}
53795389

53805390
// duplicate the layer's basic parameters
53815391

5382-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( selectedLyr );
5383-
// TODO: check for other layer types that won't duplicate correctly
5384-
// currently memory and plugin layers are skipped
5385-
if ( vlayer && vlayer->storageType() != "Memory storage" )
5392+
if ( unSppType.isEmpty() )
53865393
{
5387-
dupLayer = new QgsVectorLayer( vlayer->source(), layerDupName, vlayer->providerType() );
5394+
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( selectedLyr );
5395+
// TODO: check for other layer types that won't duplicate correctly
5396+
// currently memory and plugin layers are skipped
5397+
if ( vlayer && vlayer->storageType() == "Memory storage" )
5398+
{
5399+
unSppType = tr( "Memory layer" );
5400+
}
5401+
else if ( vlayer )
5402+
{
5403+
dupLayer = new QgsVectorLayer( vlayer->source(), layerDupName, vlayer->providerType() );
5404+
}
53885405
}
53895406

5390-
if ( !dupLayer )
5407+
5408+
if ( unSppType.isEmpty() && !dupLayer )
53915409
{
53925410
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer*>( selectedLyr );
53935411
if ( rlayer )
@@ -5396,18 +5414,21 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
53965414
}
53975415
}
53985416

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

5406-
if ( !dupLayer )
5425+
if ( !unSppType.isEmpty() || !dupLayer )
54075426
{
54085427
QMessageBox::information( this,
54095428
tr( "Unsupported Layer" ),
5410-
tr( "%1\n\nDuplication of layer type is unsupported." ).arg( selectedLyr->name() ) );
5429+
tr( "%1\n%2\n\nDuplication of layer type is unsupported." )
5430+
.arg( selectedLyr->name() )
5431+
.arg( !unSppType.isEmpty() ? QString( " (" ) + unSppType + QString( ")" ) : "" ) );
54115432
continue;
54125433
}
54135434

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

54235444
// move layer to just below source layer
5424-
QTreeWidgetItem *dupItem = mMapLegend->currentItem();
5425-
mMapLegend->moveItem( dupItem, sourceItem );
5445+
QgsLegendLayer *dupllayer = mMapLegend->currentLegendLayer();
5446+
mMapLegend->moveItem( dupllayer, sourcellayer );
54265447

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

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

54405459
mMapCanvas->freeze( false );
5441-
// if ( QgsMapLayerRegistry::instance()->count() > startCount )
5442-
// {
5443-
// statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
5444-
// mMapCanvas->refresh();
5445-
// }
54465460
}
54475461

54485462
void QgisApp::setLayerCRS()

0 commit comments

Comments
 (0)