Skip to content

Commit 36b6a16

Browse files
committedJan 26, 2016
Merge pull request #2723 from mhugo/fix_vlayer_ui
Fix virtual layer creation GUI
2 parents 99b9a49 + f8d5938 commit 36b6a16

File tree

5 files changed

+98
-57
lines changed

5 files changed

+98
-57
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,8 +3798,8 @@ void QgisApp::addVirtualLayer()
37983798
}
37993799
connect( dts, SIGNAL( addVectorLayer( QString, QString, QString ) ),
38003800
this, SLOT( addSelectedVectorLayer( QString, QString, QString ) ) );
3801-
connect( dts, SIGNAL( replaceVectorLayer( QString, QString, QString ) ),
3802-
this, SLOT( replaceSelectedVectorLayer( QString, QString, QString ) ) );
3801+
connect( dts, SIGNAL( replaceVectorLayer( QString, QString, QString, QString ) ),
3802+
this, SLOT( replaceSelectedVectorLayer( QString, QString, QString, QString ) ) );
38033803
dts->exec();
38043804
delete dts;
38053805
} // QgisApp::addVirtualLayer()
@@ -3809,13 +3809,12 @@ void QgisApp::addSelectedVectorLayer( const QString& uri, const QString& layerNa
38093809
addVectorLayer( uri, layerName, provider );
38103810
} // QgisApp:addSelectedVectorLayer
38113811

3812-
void QgisApp::replaceSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider )
3812+
void QgisApp::replaceSelectedVectorLayer( const QString& oldId, const QString& uri, const QString& layerName, const QString& provider )
38133813
{
3814-
QList<QgsMapLayer*> selected = mLayerTreeView->selectedLayers();
3815-
if ( selected.size() != 1 && selected[0]->type() != QgsMapLayer::VectorLayer )
3814+
QgsMapLayer* old = QgsMapLayerRegistry::instance()->mapLayer( oldId );
3815+
if ( !old )
38163816
return;
3817-
3818-
QgsVectorLayer* oldLayer = static_cast<QgsVectorLayer*>( selected[0] );
3817+
QgsVectorLayer* oldLayer = static_cast<QgsVectorLayer*>( old );
38193818
QgsVectorLayer* newLayer = new QgsVectorLayer( uri, layerName, provider );
38203819
if ( !newLayer || !newLayer->isValid() )
38213820
return;

‎src/app/qgisapp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
716716
//! Add a vector layer defined by uri, layer name, data source uri
717717
void addSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider );
718718
//! Replace the selected layer by a vector layer defined by uri, layer name, data source uri
719-
void replaceSelectedVectorLayer( const QString& uri, const QString& layerName, const QString& provider );
719+
void replaceSelectedVectorLayer( const QString& oldId, const QString& uri, const QString& layerName, const QString& provider );
720720
//#ifdef HAVE_MSSQL
721721
//! Add a MSSQL layer to the map
722722
void addMssqlLayer();

‎src/providers/virtual/qgsvirtuallayersourceselect.cpp

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ email : hugo dot mercier at oslandia dot com
2323
#include <qgsvectordataprovider.h>
2424
#include <qgsmaplayerregistry.h>
2525
#include <qgsgenericprojectionselector.h>
26+
#include <layertree/qgslayertreemodel.h>
27+
#include <layertree/qgslayertreegroup.h>
28+
#include <layertree/qgslayertreelayer.h>
2629

2730
#include <QUrl>
2831
#include <Qsci/qscilexer.h>
@@ -41,41 +44,33 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
4144
QgsLayerTreeView* treeView = parent->findChild<QgsLayerTreeView*>( "theLayerTreeView" );
4245
if ( treeView )
4346
{
44-
QList<QgsMapLayer*> selected = treeView->selectedLayers();
45-
if ( selected.size() == 1 && selected[0]->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( selected[0] )->providerType() == "virtual" )
47+
QgsLayerTreeModel* model = qobject_cast<QgsLayerTreeModel*>( treeView->model() );
48+
foreach ( QgsLayerTreeLayer* layer, model->rootGroup()->findLayers() )
4649
{
47-
// reuse the configuration of this virtual layer
48-
mReplaceLayer->setEnabled( true );
49-
50-
QgsVirtualLayerDefinition def = QgsVirtualLayerDefinition::fromUrl( QUrl::fromEncoded( selected[0]->source().toUtf8() ) );
51-
52-
if ( !def.query().isEmpty() )
50+
if ( layer->layer()->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( layer->layer() )->providerType() == "virtual" )
5351
{
54-
mQueryEdit->setText( def.query() );
52+
// store layer's id as user data
53+
mLayerNameCombo->addItem( layer->layer()->name(), layer->layer()->id() );
5554
}
55+
}
56+
}
5657

57-
if ( !def.uid().isEmpty() )
58-
{
59-
mUIDColumnNameChck->setChecked( true );
60-
mUIDField->setText( def.uid() );
61-
}
58+
if ( mLayerNameCombo->count() == 0 )
59+
mLayerNameCombo->addItem( "virtual_layer" );
6260

63-
if ( def.geometryWkbType() == QgsWKBTypes::NoGeometry )
64-
{
65-
mNoGeometryRadio->setChecked( true );
66-
}
67-
else if ( def.hasDefinedGeometry() )
68-
{
69-
mGeometryRadio->setChecked( true );
70-
mSrid = def.geometrySrid();
71-
QgsCoordinateReferenceSystem crs( def.geometrySrid() );
72-
mCRS->setText( crs.authid() );
73-
mGeometryType->setCurrentIndex( static_cast<long>( def.geometryWkbType() ) - 1 );
74-
mGeometryField->setText( def.geometryField() );
75-
}
61+
// select the current layer, if any
62+
if ( treeView )
63+
{
64+
QList<QgsMapLayer*> selected = treeView->selectedLayers();
65+
if ( selected.size() == 1 && selected[0]->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer*>( selected[0] )->providerType() == "virtual" )
66+
{
67+
mLayerNameCombo->setCurrentIndex( mLayerNameCombo->findData( selected[0]->id() ) );
7668
}
7769
}
7870

71+
QObject::connect( mLayerNameCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onLayerComboChanged( int ) ) );
72+
onLayerComboChanged( mLayerNameCombo->currentIndex() );
73+
7974
// configure auto completion with SQL functions
8075
QsciAPIs* apis = new QsciAPIs( mQueryEdit->lexer() );
8176

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

111106
mQueryEdit->setWrapMode( QsciScintilla::WrapWord );
112-
113107
}
114108

115109
QgsVirtualLayerSourceSelect::~QgsVirtualLayerSourceSelect()
116110
{
117111
}
118112

113+
void QgsVirtualLayerSourceSelect::onLayerComboChanged( int idx )
114+
{
115+
if ( idx == -1 )
116+
return;
117+
118+
QString lid = mLayerNameCombo->itemData( idx ).toString();
119+
QgsVectorLayer* l = static_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( lid ) );
120+
QgsVirtualLayerDefinition def = QgsVirtualLayerDefinition::fromUrl( QUrl::fromEncoded( l->source().toUtf8() ) );
121+
122+
if ( !def.query().isEmpty() )
123+
{
124+
mQueryEdit->setText( def.query() );
125+
}
126+
127+
if ( !def.uid().isEmpty() )
128+
{
129+
mUIDColumnNameChck->setChecked( true );
130+
mUIDField->setText( def.uid() );
131+
}
132+
133+
if ( def.geometryWkbType() == QgsWKBTypes::NoGeometry )
134+
{
135+
mNoGeometryRadio->setChecked( true );
136+
}
137+
else if ( def.hasDefinedGeometry() )
138+
{
139+
mGeometryRadio->setChecked( true );
140+
mSrid = def.geometrySrid();
141+
QgsCoordinateReferenceSystem crs( def.geometrySrid() );
142+
mCRS->setText( crs.authid() );
143+
mGeometryType->setCurrentIndex( static_cast<long>( def.geometryWkbType() ) - 1 );
144+
mGeometryField->setText( def.geometryField() );
145+
}
146+
}
147+
119148
void QgsVirtualLayerSourceSelect::onBrowseCRS()
120149
{
121150
QgsGenericProjectionSelector crsSelector( this );
@@ -174,17 +203,28 @@ void QgsVirtualLayerSourceSelect::onTestQuery()
174203
void QgsVirtualLayerSourceSelect::on_buttonBox_accepted()
175204
{
176205
QString layerName = "virtual_layer";
177-
if ( ! mLayerName->text().isEmpty() )
206+
int idx = mLayerNameCombo->currentIndex();
207+
if ( idx != -1 && !mLayerNameCombo->currentText().isEmpty() )
178208
{
179-
layerName = mLayerName->text();
209+
layerName = mLayerNameCombo->currentText();
180210
}
181211

182212
QgsVirtualLayerDefinition def = getVirtualLayerDef();
183213

184-
if ( mReplaceLayer->isEnabled() && mReplaceLayer->isChecked() )
185-
emit replaceVectorLayer( def.toString(), layerName, "virtual" );
186-
else
187-
emit addVectorLayer( def.toString(), layerName, "virtual" );
214+
if ( idx != -1 )
215+
{
216+
QString id( mLayerNameCombo->itemData( idx ).toString() );
217+
if ( !id.isEmpty() && mLayerNameCombo->currentText() == QgsMapLayerRegistry::instance()->mapLayer( id )->name() )
218+
{
219+
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 );
220+
if ( r == QMessageBox::Yes )
221+
{
222+
emit replaceVectorLayer( id, def.toString(), layerName, "virtual" );
223+
return;
224+
}
225+
}
226+
}
227+
emit addVectorLayer( def.toString(), layerName, "virtual" );
188228
}
189229

190230
QGISEXTERN QgsVirtualLayerSourceSelect *selectWidget( QWidget *parent, Qt::WindowFlags fl )

‎src/providers/virtual/qgsvirtuallayersourceselect.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ class QgsVirtualLayerSourceSelect : public QDialog, private Ui::QgsVirtualLayerS
3939
void on_buttonBox_accepted();
4040
void onTestQuery();
4141
void onBrowseCRS();
42+
void onLayerComboChanged( int );
4243

4344
signals:
45+
/** Source, name, provider */
4446
void addVectorLayer( QString, QString, QString );
45-
void replaceVectorLayer( QString, QString, QString );
47+
/** Old_id, source, name, provider */
48+
void replaceVectorLayer( QString, QString, QString, QString );
4649

4750
private:
4851
QgsVirtualLayerDefinition getVirtualLayerDef();

‎src/providers/virtual/qgsvirtuallayersourceselectbase.ui

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@
1717
<item>
1818
<layout class="QHBoxLayout" name="horizontalLayout_4">
1919
<item>
20-
<widget class="QLabel" name="label_4">
20+
<widget class="QLabel" name="label_2">
2121
<property name="text">
2222
<string>Layer name</string>
2323
</property>
2424
</widget>
2525
</item>
2626
<item>
27-
<widget class="QLineEdit" name="mLayerName">
28-
<property name="text">
29-
<string>virtual_layer</string>
27+
<widget class="QComboBox" name="mLayerNameCombo">
28+
<property name="editable">
29+
<bool>true</bool>
3030
</property>
3131
</widget>
3232
</item>
3333
<item>
34-
<widget class="QCheckBox" name="mReplaceLayer">
35-
<property name="enabled">
36-
<bool>false</bool>
34+
<spacer name="horizontalSpacer">
35+
<property name="orientation">
36+
<enum>Qt::Horizontal</enum>
3737
</property>
38-
<property name="text">
39-
<string>Replace</string>
40-
</property>
41-
<property name="checked">
42-
<bool>true</bool>
38+
<property name="sizeHint" stdset="0">
39+
<size>
40+
<width>40</width>
41+
<height>20</height>
42+
</size>
4343
</property>
44-
</widget>
44+
</spacer>
4545
</item>
4646
</layout>
4747
</item>
@@ -257,7 +257,6 @@
257257
</customwidget>
258258
</customwidgets>
259259
<tabstops>
260-
<tabstop>mLayerName</tabstop>
261260
<tabstop>mGeometryGroup</tabstop>
262261
<tabstop>buttonBox</tabstop>
263262
<tabstop>mGeometryRadio</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.