@@ -23,6 +23,9 @@ email : hugo dot mercier at oslandia dot com
23
23
#include < qgsvectordataprovider.h>
24
24
#include < qgsmaplayerregistry.h>
25
25
#include < qgsgenericprojectionselector.h>
26
+ #include < layertree/qgslayertreemodel.h>
27
+ #include < layertree/qgslayertreegroup.h>
28
+ #include < layertree/qgslayertreelayer.h>
26
29
27
30
#include < QUrl>
28
31
#include < Qsci/qscilexer.h>
@@ -41,41 +44,33 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
41
44
QgsLayerTreeView* treeView = parent->findChild <QgsLayerTreeView*>( " theLayerTreeView" );
42
45
if ( treeView )
43
46
{
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 () )
46
49
{
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" )
53
51
{
54
- mQueryEdit ->setText ( def.query () );
52
+ // store layer's id as user data
53
+ mLayerNameCombo ->addItem ( layer->layer ()->name (), layer->layer ()->id () );
55
54
}
55
+ }
56
+ }
56
57
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" );
62
60
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 () ) );
76
68
}
77
69
}
78
70
71
+ QObject::connect ( mLayerNameCombo , SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( onLayerComboChanged ( int ) ) );
72
+ onLayerComboChanged ( mLayerNameCombo ->currentIndex () );
73
+
79
74
// configure auto completion with SQL functions
80
75
QsciAPIs* apis = new QsciAPIs ( mQueryEdit ->lexer () );
81
76
@@ -109,13 +104,47 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
109
104
mQueryEdit ->lexer ()->setAPIs ( apis );
110
105
111
106
mQueryEdit ->setWrapMode ( QsciScintilla::WrapWord );
112
-
113
107
}
114
108
115
109
QgsVirtualLayerSourceSelect::~QgsVirtualLayerSourceSelect ()
116
110
{
117
111
}
118
112
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
+
119
148
void QgsVirtualLayerSourceSelect::onBrowseCRS ()
120
149
{
121
150
QgsGenericProjectionSelector crsSelector ( this );
@@ -174,17 +203,28 @@ void QgsVirtualLayerSourceSelect::onTestQuery()
174
203
void QgsVirtualLayerSourceSelect::on_buttonBox_accepted ()
175
204
{
176
205
QString layerName = " virtual_layer" ;
177
- if ( ! mLayerName ->text ().isEmpty () )
206
+ int idx = mLayerNameCombo ->currentIndex ();
207
+ if ( idx != -1 && !mLayerNameCombo ->currentText ().isEmpty () )
178
208
{
179
- layerName = mLayerName -> text ();
209
+ layerName = mLayerNameCombo -> currentText ();
180
210
}
181
211
182
212
QgsVirtualLayerDefinition def = getVirtualLayerDef ();
183
213
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" );
188
228
}
189
229
190
230
QGISEXTERN QgsVirtualLayerSourceSelect *selectWidget ( QWidget *parent, Qt::WindowFlags fl )
0 commit comments