Skip to content

Commit

Permalink
Add a basic GUI for virtual layer creation and add it to the main app
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Jan 7, 2016
1 parent f351715 commit 7fb9392
Show file tree
Hide file tree
Showing 9 changed files with 946 additions and 15 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -128,6 +128,7 @@
<file>themes/default/mActionAddWfsLayer.svg</file>
<file>themes/default/mActionAddWmsLayer.svg</file>
<file>themes/default/mActionAddDelimitedTextLayer.svg</file>
<file>themes/default/mActionAddVirtualLayer.svg</file>
<file>themes/default/mActionAlignBottom.png</file>
<file>themes/default/mActionAlignHCenter.png</file>
<file>themes/default/mActionAlignLeft.png</file>
Expand Down
276 changes: 276 additions & 0 deletions images/themes/default/mActionAddVirtualLayer.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1415,6 +1415,7 @@ void QgisApp::createActions()
connect( mActionAddWcsLayer, SIGNAL( triggered() ), this, SLOT( addWcsLayer() ) );
connect( mActionAddWfsLayer, SIGNAL( triggered() ), this, SLOT( addWfsLayer() ) );
connect( mActionAddDelimitedText, SIGNAL( triggered() ), this, SLOT( addDelimitedTextLayer() ) );
connect( mActionAddVirtualLayer, SIGNAL( triggered() ), this, SLOT( addVirtualLayer() ) );
connect( mActionOpenTable, SIGNAL( triggered() ), this, SLOT( attributeTable() ) );
connect( mActionOpenFieldCalc, SIGNAL( triggered() ), this, SLOT( fieldCalculator() ) );
connect( mActionToggleEditing, SIGNAL( triggered() ), this, SLOT( toggleEditing() ) );
Expand Down Expand Up @@ -3758,11 +3759,74 @@ void QgisApp::addDelimitedTextLayer()
delete dts;
} // QgisApp::addDelimitedTextLayer()

void QgisApp::addVirtualLayer()
{
// show the Delimited text dialog
QDialog *dts = dynamic_cast<QDialog*>( QgsProviderRegistry::instance()->selectWidget( "virtual", this ) );
if ( !dts )
{
QMessageBox::warning( this, tr( "Virtual layer" ), tr( "Cannot get virtual layer select dialog from provider." ) );
return;
}
connect( dts, SIGNAL( addVectorLayer( QString, QString, QString ) ),
this, SLOT( addSelectedVectorLayer( QString, QString, QString ) ) );
connect( dts, SIGNAL( replaceVectorLayer( QString, QString, QString ) ),
this, SLOT( replaceSelectedVectorLayer( QString, QString, QString ) ) );
dts->exec();
delete dts;
} // QgisApp::addVirtualLayer()

void QgisApp::addSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider )
{
addVectorLayer( uri, layerName, provider );
} // QgisApp:addSelectedVectorLayer

void QgisApp::replaceSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider )
{
QList<QgsMapLayer*> selected = mLayerTreeView->selectedLayers();
if ( selected.size() != 1 && selected[0]->type() != QgsMapLayer::VectorLayer )
return;

QgsVectorLayer* oldLayer = static_cast<QgsVectorLayer*>( selected[0] );
QgsVectorLayer* newLayer = new QgsVectorLayer( uri, layerName, provider );
if ( !newLayer || !newLayer->isValid() )
return;

QgsMapLayerRegistry::instance()->addMapLayer( newLayer, /*addToLegend*/ false, /*takeOwnership*/ true );
// copy symbology, if possible
if ( oldLayer->geometryType() == newLayer->geometryType() )
{
QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
"qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
QDomDocument doc( documentType );
QDomElement rootNode = doc.createElement( "qgis" );
rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
doc.appendChild( rootNode );
QString errorMsg;
oldLayer->writeSymbology( rootNode, doc, errorMsg );
newLayer->readSymbology( rootNode, errorMsg );
}

// get the index in its parent for the current layer
QgsLayerTreeLayer* inTree = QgsProject::instance()->layerTreeRoot()->findLayer( oldLayer->id() );
int idx = 0;
foreach ( QgsLayerTreeNode* vl, inTree->parent()->children() )
{
if ( vl->nodeType() == QgsLayerTreeNode::NodeLayer && static_cast<QgsLayerTreeLayer*>( vl )->layer() == oldLayer )
{
break;
}
idx++;
}
// insert the new layer
QgsLayerTreeGroup* parent = static_cast<QgsLayerTreeGroup*>( inTree->parent() ) ? static_cast<QgsLayerTreeGroup*>( inTree->parent() ) : QgsProject::instance()->layerTreeRoot();
parent->insertLayer( idx, newLayer );
// remove the current layer
QgsMapLayerRegistry::instance()->removeMapLayer( oldLayer );
} // QgisApp:replaceSelectedVectorLayer

void QgisApp::addMssqlLayer()
{
#ifdef HAVE_MSSQL
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -710,6 +710,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void addDelimitedTextLayer();
//! Add a vector layer defined by uri, layer name, data source uri
void addSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider );
//! Replace the selected layer by a vector layer defined by uri, layer name, data source uri
void replaceSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider );
//#ifdef HAVE_MSSQL
//! Add a MSSQL layer to the map
void addMssqlLayer();
Expand All @@ -718,6 +720,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Add a Oracle layer to the map
void addOracleLayer();
//#endif
//! Add a virtual layer
void addVirtualLayer();
//! toggles whether the current selected layer is in overview or not
void isInOverview();
//! Store the position for map tool tip
Expand Down

0 comments on commit 7fb9392

Please sign in to comment.