Skip to content

Commit 00b1958

Browse files
committed
Update to duplicate layer(s)
- Duplicate layer always created below source layer, and at same nested group depth - Duplicate layer is always set to not visible (much faster, canvas refresh not needed when duplicating many layers)
1 parent 9df95b1 commit 00b1958

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/app/qgisapp.cpp

+32-2
Original file line numberDiff line numberDiff line change
@@ -5362,12 +5362,20 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
53625362
}
53635363

53645364
mMapCanvas->freeze();
5365+
// int startCount = QgsMapLayerRegistry::instance()->count();
53655366
QgsMapLayer *dupLayer;
5367+
53665368
foreach ( QgsMapLayer * selectedLyr, selectedLyrs )
53675369
{
53685370
dupLayer = 0;
53695371
QString layerDupName = selectedLyr->name() + " " + tr( "copy" );
53705372

5373+
// setup for placing duplicated layer below source layer, regardless of group depth
5374+
mMapLegend->blockSignals( true );
5375+
mMapLegend->setCurrentLayer( selectedLyr );
5376+
mMapLegend->blockSignals( false );
5377+
QTreeWidgetItem *sourceItem = mMapLegend->currentItem();
5378+
53715379
// duplicate the layer's basic parameters
53725380

53735381
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( selectedLyr );
@@ -5389,6 +5397,7 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
53895397

53905398
if ( dupLayer && !dupLayer->isValid() )
53915399
{
5400+
// addMapLayer() also checks layer validity, but do it now to skip canvas refresh
53925401
QgsDebugMsg( "Duplicated layer was invalid" );
53935402
continue;
53945403
}
@@ -5402,16 +5411,37 @@ void QgisApp::duplicateLayers( QList<QgsMapLayer *> lyrList )
54025411
}
54035412

54045413
// add layer to project and layer registry
5405-
addMapLayer( dupLayer );
5414+
QList<QgsMapLayer *> myList;
5415+
myList << dupLayer;
5416+
QgsMapLayerRegistry::instance()->addMapLayers( myList );
54065417

54075418
// duplicate the layer style
54085419
copyStyle( selectedLyr );
54095420
pasteStyle( dupLayer );
5421+
5422+
// move layer to just below source layer
5423+
QTreeWidgetItem *dupItem = mMapLegend->currentItem();
5424+
mMapLegend->moveItem( dupItem, sourceItem );
5425+
5426+
// always set duplicated layers to not visible
5427+
// so layer can be configured before being turned on,
5428+
// and no map canvas refresh needed when doing multiple duplications
5429+
mMapLegend->setLayerVisible( dupLayer, false );
5430+
// OR, set visible property from source layer? (will require canvas refresh)
5431+
//mMapLegend->setLayerVisible( dupLayer, mMapLegend->layerCheckState( selectedLyr ) == Qt::Checked );
54105432
}
54115433

54125434
dupLayer = 0;
5435+
5436+
// update UI
5437+
qApp->processEvents();
5438+
54135439
mMapCanvas->freeze( false );
5414-
mMapCanvas->refresh();
5440+
// if ( QgsMapLayerRegistry::instance()->count() > startCount )
5441+
// {
5442+
// statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
5443+
// mMapCanvas->refresh();
5444+
// }
54155445
}
54165446

54175447
void QgisApp::setLayerCRS()

0 commit comments

Comments
 (0)