@@ -1415,6 +1415,7 @@ void QgisApp::createActions()
1415
1415
connect ( mActionAddWcsLayer , SIGNAL ( triggered () ), this , SLOT ( addWcsLayer () ) );
1416
1416
connect ( mActionAddWfsLayer , SIGNAL ( triggered () ), this , SLOT ( addWfsLayer () ) );
1417
1417
connect ( mActionAddDelimitedText , SIGNAL ( triggered () ), this , SLOT ( addDelimitedTextLayer () ) );
1418
+ connect ( mActionAddVirtualLayer , SIGNAL ( triggered () ), this , SLOT ( addVirtualLayer () ) );
1418
1419
connect ( mActionOpenTable , SIGNAL ( triggered () ), this , SLOT ( attributeTable () ) );
1419
1420
connect ( mActionOpenFieldCalc , SIGNAL ( triggered () ), this , SLOT ( fieldCalculator () ) );
1420
1421
connect ( mActionToggleEditing , SIGNAL ( triggered () ), this , SLOT ( toggleEditing () ) );
@@ -3758,11 +3759,74 @@ void QgisApp::addDelimitedTextLayer()
3758
3759
delete dts;
3759
3760
} // QgisApp::addDelimitedTextLayer()
3760
3761
3762
+ void QgisApp::addVirtualLayer ()
3763
+ {
3764
+ // show the Delimited text dialog
3765
+ QDialog *dts = dynamic_cast <QDialog*>( QgsProviderRegistry::instance ()->selectWidget ( " virtual" , this ) );
3766
+ if ( !dts )
3767
+ {
3768
+ QMessageBox::warning ( this , tr ( " Virtual layer" ), tr ( " Cannot get virtual layer select dialog from provider." ) );
3769
+ return ;
3770
+ }
3771
+ connect ( dts, SIGNAL ( addVectorLayer ( QString, QString, QString ) ),
3772
+ this , SLOT ( addSelectedVectorLayer ( QString, QString, QString ) ) );
3773
+ connect ( dts, SIGNAL ( replaceVectorLayer ( QString, QString, QString ) ),
3774
+ this , SLOT ( replaceSelectedVectorLayer ( QString, QString, QString ) ) );
3775
+ dts->exec ();
3776
+ delete dts;
3777
+ } // QgisApp::addVirtualLayer()
3778
+
3761
3779
void QgisApp::addSelectedVectorLayer ( const QString& uri, const QString& layerName, const QString& provider )
3762
3780
{
3763
3781
addVectorLayer ( uri, layerName, provider );
3764
3782
} // QgisApp:addSelectedVectorLayer
3765
3783
3784
+ void QgisApp::replaceSelectedVectorLayer ( const QString& uri, const QString& layerName, const QString& provider )
3785
+ {
3786
+ QList<QgsMapLayer*> selected = mLayerTreeView ->selectedLayers ();
3787
+ if ( selected.size () != 1 && selected[0 ]->type () != QgsMapLayer::VectorLayer )
3788
+ return ;
3789
+
3790
+ QgsVectorLayer* oldLayer = static_cast <QgsVectorLayer*>( selected[0 ] );
3791
+ QgsVectorLayer* newLayer = new QgsVectorLayer ( uri, layerName, provider );
3792
+ if ( !newLayer || !newLayer->isValid () )
3793
+ return ;
3794
+
3795
+ QgsMapLayerRegistry::instance ()->addMapLayer ( newLayer, /* addToLegend*/ false , /* takeOwnership*/ true );
3796
+ // copy symbology, if possible
3797
+ if ( oldLayer->geometryType () == newLayer->geometryType () )
3798
+ {
3799
+ QDomImplementation DomImplementation;
3800
+ QDomDocumentType documentType =
3801
+ DomImplementation.createDocumentType (
3802
+ " qgis" , " http://mrcc.com/qgis.dtd" , " SYSTEM" );
3803
+ QDomDocument doc ( documentType );
3804
+ QDomElement rootNode = doc.createElement ( " qgis" );
3805
+ rootNode.setAttribute ( " version" , QString ( " %1" ).arg ( QGis::QGIS_VERSION ) );
3806
+ doc.appendChild ( rootNode );
3807
+ QString errorMsg;
3808
+ oldLayer->writeSymbology ( rootNode, doc, errorMsg );
3809
+ newLayer->readSymbology ( rootNode, errorMsg );
3810
+ }
3811
+
3812
+ // get the index in its parent for the current layer
3813
+ QgsLayerTreeLayer* inTree = QgsProject::instance ()->layerTreeRoot ()->findLayer ( oldLayer->id () );
3814
+ int idx = 0 ;
3815
+ foreach ( QgsLayerTreeNode* vl, inTree->parent ()->children () )
3816
+ {
3817
+ if ( vl->nodeType () == QgsLayerTreeNode::NodeLayer && static_cast <QgsLayerTreeLayer*>( vl )->layer () == oldLayer )
3818
+ {
3819
+ break ;
3820
+ }
3821
+ idx++;
3822
+ }
3823
+ // insert the new layer
3824
+ QgsLayerTreeGroup* parent = static_cast <QgsLayerTreeGroup*>( inTree->parent () ) ? static_cast <QgsLayerTreeGroup*>( inTree->parent () ) : QgsProject::instance ()->layerTreeRoot ();
3825
+ parent->insertLayer ( idx, newLayer );
3826
+ // remove the current layer
3827
+ QgsMapLayerRegistry::instance ()->removeMapLayer ( oldLayer );
3828
+ } // QgisApp:replaceSelectedVectorLayer
3829
+
3766
3830
void QgisApp::addMssqlLayer ()
3767
3831
{
3768
3832
#ifdef HAVE_MSSQL
0 commit comments