Skip to content
Permalink
Browse files

Merge pull request #2723 from mhugo/fix_vlayer_ui

Fix virtual layer creation GUI
  • Loading branch information
nyalldawson committed Jan 26, 2016
2 parents 99b9a49 + f8d5938 commit 36b6a16603ed23eb8491bc6dc69213b93fc6ea10
@@ -3798,8 +3798,8 @@ void QgisApp::addVirtualLayer()
}
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 ) ) );
connect( dts, SIGNAL( replaceVectorLayer( QString, QString, QString, QString ) ),
this, SLOT( replaceSelectedVectorLayer( QString, QString, QString, QString ) ) );
dts->exec();
delete dts;
} // QgisApp::addVirtualLayer()
@@ -3809,13 +3809,12 @@ void QgisApp::addSelectedVectorLayer( const QString& uri, const QString& layerNa
addVectorLayer( uri, layerName, provider );
} // QgisApp:addSelectedVectorLayer

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

QgsVectorLayer* oldLayer = static_cast<QgsVectorLayer*>( selected[0] );
QgsVectorLayer* oldLayer = static_cast<QgsVectorLayer*>( old );
QgsVectorLayer* newLayer = new QgsVectorLayer( uri, layerName, provider );
if ( !newLayer || !newLayer->isValid() )
return;
@@ -716,7 +716,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! 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 );
void replaceSelectedVectorLayer( const QString& oldId, const QString& uri, const QString& layerName, const QString& provider );
//#ifdef HAVE_MSSQL
//! Add a MSSQL layer to the map
void addMssqlLayer();
@@ -23,6 +23,9 @@ email : hugo dot mercier at oslandia dot com
#include <qgsvectordataprovider.h>
#include <qgsmaplayerregistry.h>
#include <qgsgenericprojectionselector.h>
#include <layertree/qgslayertreemodel.h>
#include <layertree/qgslayertreegroup.h>
#include <layertree/qgslayertreelayer.h>

#include <QUrl>
#include <Qsci/qscilexer.h>
@@ -41,41 +44,33 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
QgsLayerTreeView* treeView = parent->findChild<QgsLayerTreeView*>( "theLayerTreeView" );
if ( treeView )
{
QList<QgsMapLayer*> selected = treeView->selectedLayers();
if ( selected.size() == 1 && selected[0]->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( selected[0] )->providerType() == "virtual" )
QgsLayerTreeModel* model = qobject_cast<QgsLayerTreeModel*>( treeView->model() );
foreach ( QgsLayerTreeLayer* layer, model->rootGroup()->findLayers() )
{
// reuse the configuration of this virtual layer
mReplaceLayer->setEnabled( true );

QgsVirtualLayerDefinition def = QgsVirtualLayerDefinition::fromUrl( QUrl::fromEncoded( selected[0]->source().toUtf8() ) );

if ( !def.query().isEmpty() )
if ( layer->layer()->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( layer->layer() )->providerType() == "virtual" )
{
mQueryEdit->setText( def.query() );
// store layer's id as user data
mLayerNameCombo->addItem( layer->layer()->name(), layer->layer()->id() );
}
}
}

if ( !def.uid().isEmpty() )
{
mUIDColumnNameChck->setChecked( true );
mUIDField->setText( def.uid() );
}
if ( mLayerNameCombo->count() == 0 )
mLayerNameCombo->addItem( "virtual_layer" );

if ( def.geometryWkbType() == QgsWKBTypes::NoGeometry )
{
mNoGeometryRadio->setChecked( true );
}
else if ( def.hasDefinedGeometry() )
{
mGeometryRadio->setChecked( true );
mSrid = def.geometrySrid();
QgsCoordinateReferenceSystem crs( def.geometrySrid() );
mCRS->setText( crs.authid() );
mGeometryType->setCurrentIndex( static_cast<long>( def.geometryWkbType() ) - 1 );
mGeometryField->setText( def.geometryField() );
}
// select the current layer, if any
if ( treeView )
{
QList<QgsMapLayer*> selected = treeView->selectedLayers();
if ( selected.size() == 1 && selected[0]->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( selected[0] )->providerType() == "virtual" )
{
mLayerNameCombo->setCurrentIndex( mLayerNameCombo->findData( selected[0]->id() ) );
}
}

QObject::connect( mLayerNameCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onLayerComboChanged( int ) ) );
onLayerComboChanged( mLayerNameCombo->currentIndex() );

// configure auto completion with SQL functions
QsciAPIs* apis = new QsciAPIs( mQueryEdit->lexer() );

@@ -109,13 +104,47 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
mQueryEdit->lexer()->setAPIs( apis );

mQueryEdit->setWrapMode( QsciScintilla::WrapWord );

}

QgsVirtualLayerSourceSelect::~QgsVirtualLayerSourceSelect()
{
}

void QgsVirtualLayerSourceSelect::onLayerComboChanged( int idx )
{
if ( idx == -1 )
return;

QString lid = mLayerNameCombo->itemData( idx ).toString();
QgsVectorLayer* l = static_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( lid ) );
QgsVirtualLayerDefinition def = QgsVirtualLayerDefinition::fromUrl( QUrl::fromEncoded( l->source().toUtf8() ) );

if ( !def.query().isEmpty() )
{
mQueryEdit->setText( def.query() );
}

if ( !def.uid().isEmpty() )
{
mUIDColumnNameChck->setChecked( true );
mUIDField->setText( def.uid() );
}

if ( def.geometryWkbType() == QgsWKBTypes::NoGeometry )
{
mNoGeometryRadio->setChecked( true );
}
else if ( def.hasDefinedGeometry() )
{
mGeometryRadio->setChecked( true );
mSrid = def.geometrySrid();
QgsCoordinateReferenceSystem crs( def.geometrySrid() );
mCRS->setText( crs.authid() );
mGeometryType->setCurrentIndex( static_cast<long>( def.geometryWkbType() ) - 1 );
mGeometryField->setText( def.geometryField() );
}
}

void QgsVirtualLayerSourceSelect::onBrowseCRS()
{
QgsGenericProjectionSelector crsSelector( this );
@@ -174,17 +203,28 @@ void QgsVirtualLayerSourceSelect::onTestQuery()
void QgsVirtualLayerSourceSelect::on_buttonBox_accepted()
{
QString layerName = "virtual_layer";
if ( ! mLayerName->text().isEmpty() )
int idx = mLayerNameCombo->currentIndex();
if ( idx != -1 && !mLayerNameCombo->currentText().isEmpty() )
{
layerName = mLayerName->text();
layerName = mLayerNameCombo->currentText();
}

QgsVirtualLayerDefinition def = getVirtualLayerDef();

if ( mReplaceLayer->isEnabled() && mReplaceLayer->isChecked() )
emit replaceVectorLayer( def.toString(), layerName, "virtual" );
else
emit addVectorLayer( def.toString(), layerName, "virtual" );
if ( idx != -1 )
{
QString id( mLayerNameCombo->itemData( idx ).toString() );
if ( !id.isEmpty() && mLayerNameCombo->currentText() == QgsMapLayerRegistry::instance()->mapLayer( id )->name() )
{
int r = QMessageBox::warning( nullptr, tr( "Warning" ), tr( "A virtual layer of this name already exists, would you like to overwrite it ?" ), QMessageBox::Yes | QMessageBox::No );
if ( r == QMessageBox::Yes )
{
emit replaceVectorLayer( id, def.toString(), layerName, "virtual" );
return;
}
}
}
emit addVectorLayer( def.toString(), layerName, "virtual" );
}

QGISEXTERN QgsVirtualLayerSourceSelect *selectWidget( QWidget *parent, Qt::WindowFlags fl )
@@ -39,10 +39,13 @@ class QgsVirtualLayerSourceSelect : public QDialog, private Ui::QgsVirtualLayerS
void on_buttonBox_accepted();
void onTestQuery();
void onBrowseCRS();
void onLayerComboChanged( int );

signals:
/** Source, name, provider */
void addVectorLayer( QString, QString, QString );
void replaceVectorLayer( QString, QString, QString );
/** Old_id, source, name, provider */
void replaceVectorLayer( QString, QString, QString, QString );

private:
QgsVirtualLayerDefinition getVirtualLayerDef();
@@ -17,31 +17,31 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_4">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Layer name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mLayerName">
<property name="text">
<string>virtual_layer</string>
<widget class="QComboBox" name="mLayerNameCombo">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="mReplaceLayer">
<property name="enabled">
<bool>false</bool>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="text">
<string>Replace</string>
</property>
<property name="checked">
<bool>true</bool>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</widget>
</spacer>
</item>
</layout>
</item>
@@ -257,7 +257,6 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mLayerName</tabstop>
<tabstop>mGeometryGroup</tabstop>
<tabstop>buttonBox</tabstop>
<tabstop>mGeometryRadio</tabstop>

0 comments on commit 36b6a16

Please sign in to comment.
You can’t perform that action at this time.