-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImportAutoFieldsDialog.py
299 lines (258 loc) · 13.7 KB
/
ImportAutoFieldsDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding:utf-8 -*-
"""
/***************************************************************************
AutoFields
A QGIS plugin
Automatic attribute updates when creating or modifying vector features
-------------------
begin : 2017-04-11
copyright : (C) 2017 by Germán Carrillo (GeoTux)
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os.path
import ntpath
import re
import json
from functools import partial
from osgeo import ogr
from qgis.core import QgsMapLayerRegistry, QgsMapLayer, QgsVectorDataProvider
from PyQt4.QtCore import Qt, QSettings
from PyQt4.QtGui import ( QApplication, QDialog, QDialogButtonBox, QComboBox,
QTableWidgetItem, QFileDialog, QMessageBox, QIcon,
QPixmap, QLabel )
import resources_rc
from Ui_Import_AutoFields import Ui_ImportAutoFieldsDialog
class ImportAutoFieldsDialog( QDialog, Ui_ImportAutoFieldsDialog ):
def __init__( self, parent, autoFieldManager, messageManager, listAutoFields, filePath, bCalculateOnExisting ):
QDialog.__init__( self, parent )
self.setupUi( self )
self.setModal( True )
self.parent = parent
self.autoFieldManager = autoFieldManager
self.messageManager = messageManager
self.listAutoFields = listAutoFields
self.filePath = filePath
self.bCalculateOnExisting = bCalculateOnExisting
self.listCandidates = self.getCandidates( listAutoFields )
self.layers = []
layers = QgsMapLayerRegistry.instance().mapLayers().values()
for layer in layers:
if layer.type() == QgsMapLayer.VectorLayer:
if layer.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
self.layers.append( layer )
self.tblAutoFields.setColumnWidth( 0, 120 )
self.tblAutoFields.setColumnWidth( 3, 80 )
self.tblAutoFields.setColumnWidth( 4, 150 )
self.tblAutoFields.setColumnWidth( 5, 120 )
self.populateAutoFieldsTable()
def populateAutoFieldsTable( self ):
self.tblAutoFields.clearContents()
self.tblAutoFields.setRowCount( 0 )
self.tblAutoFields.setColumnCount( 6 )
for i in range(len(self.listAutoFields)):
self.addAutoFieldToAutoFieldsTable( self.listAutoFields[i], self.listCandidates[i] )
def addAutoFieldToAutoFieldsTable( self, autoField, candidateLayer ):
""" Add a whole row to the AutoFields table """
row = self.tblAutoFields.rowCount()
self.tblAutoFields.insertRow( row )
layerName = self.ogrLayerName( autoField['layer'] )
item = QTableWidgetItem( layerName if layerName else autoField['layer'] )
item.setData( Qt.UserRole, autoField['layer'] )
item.setData( Qt.ToolTipRole, autoField['layer'] )
self.tblAutoFields.setItem( row, 0, item )
item = QTableWidgetItem( autoField['field'] )
self.tblAutoFields.setItem( row, 1, item )
item = QTableWidgetItem( autoField['expression'] )
self.tblAutoFields.setItem( row, 2, item )
layerCombo = QComboBox()
layerCombo.addItem( '[Select a layer]', None )
for layer in self.layers:
layerCombo.addItem( layer.name(), layer.id() )
if candidateLayer:
layerCombo.setCurrentIndex( layerCombo.findData( candidateLayer.id() ) )
layerCombo.currentIndexChanged.connect( partial( self.layerOrFieldCombosChanged, row ) )
self.tblAutoFields.setCellWidget( row, 4, layerCombo )
fieldCombo = QComboBox()
fieldCombo.addItem( '[Select a field]', None )
if layerCombo.currentIndex() != 0:
for field in candidateLayer.fields():
fieldCombo.addItem( field.name() )
fieldIndex = fieldCombo.findText( autoField['field'] )
fieldCombo.setCurrentIndex( fieldIndex if fieldIndex != -1 else 0 )
fieldCombo.currentIndexChanged.connect( partial( self.layerOrFieldCombosChanged, None ) )
self.tblAutoFields.setCellWidget( row, 5, fieldCombo )
label = self.getLabelWithArrow( layerCombo.currentIndex(), fieldCombo.currentIndex(), candidateLayer, autoField['field'] )
self.tblAutoFields.setCellWidget( row, 3, label )
self.layerOrFieldCombosChanged( None, None ) # Validate initial load of AutoFields/Layers
def getLabelWithArrow( self, indexLayerCombo, indexFieldCombo, layer, fieldName ):
label = QLabel()
if indexLayerCombo != 0 and indexFieldCombo != 0:
if self.autoFieldManager.isFieldAnAutoField( layer, fieldName ):
label.setPixmap( QPixmap( ":/plugins/AutoFields/icons/orange_arrow.png" ) )
label.setToolTip( u'This AutoField will overwrite an existing AutoField in {}/{}'.format(layer.name(),fieldName) )
else:
label.setPixmap( QPixmap( ":/plugins/AutoFields/icons/green_arrow.png" ) )
label.setToolTip( 'AutoField correctly assigned' )
else:
label.setPixmap( QPixmap( ":/plugins/AutoFields/icons/gray_arrow.png" ) )
label.setToolTip( 'This AutoField will not be assigned to any layer/field' )
label.setAlignment( Qt.AlignCenter )
return label
def ogrLayerName( self, uri ):
"""Borrowed and adapted from
https://github.com/qgis/QGIS/blob/master/python/plugins/processing/tools/vector.py
"""
if os.path.isfile(uri):
return os.path.basename(os.path.splitext(uri)[0])
if ' table=' in uri:
# table="schema"."table"
re_table_schema = re.compile(' table="([^"]*)"\\."([^"]*)"')
r = re_table_schema.search(uri)
if r:
return r.groups()[0] + '.' + r.groups()[1]
# table="table"
re_table = re.compile(' table="([^"]*)"')
r = re_table.search(uri)
if r:
return r.groups()[0]
# table='table'
re_table = re.compile(" table='([^']*)'")
r = re_table.search(uri)
if r:
return r.groups()[0]
elif 'layername' in uri:
regex = re.compile('(layername=)([^|]*)')
r = regex.search(uri)
return r.groups()[1]
fields = uri.split('|')
basePath = fields[0]
fields = fields[1:]
layerid = 0
for f in fields:
if f.startswith('layername='):
return f.split('=')[1]
if f.startswith('layerid='):
layerid = int(f.split('=')[1])
ds = ogr.Open(basePath)
if not ds:
# Check if we can get basename from non-existing layer
baseName = os.path.basename(uri)
if baseName:
if baseName != uri:
ext = os.path.splitext( baseName )
if len(ext) == 2:
return ext[0]
else: # Maybe a Windows path and plugin runnign on GNU/Linux?
baseName = ntpath.basename(uri)
if baseName:
ext = ntpath.splitext( baseName )
if len(ext) == 2:
return ext[0]
return None
ly = ds.GetLayer(layerid)
if not ly:
return None
name = ly.GetName()
ds = None
return name
def getCandidates( self, listAF ):
listCandidates = []
for af in listAF:
afLayerName = self.ogrLayerName( af['layer'] )
layers = QgsMapLayerRegistry.instance().mapLayersByName(afLayerName)
layers = [l for l in layers if l.type() == QgsMapLayer.VectorLayer and l.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures]
if not layers: # Check layer source
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
if layer.type() == QgsMapLayer.VectorLayer:
if self.ogrLayerName( layer.source() ) == afLayerName:
if layer.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
layers.append( layer )
if not layers:
listCandidates.append( None )
continue # No candidate layer for this AF
# Now check fields
bFieldFound = False
for layer in layers:
if layer.fieldNameIndex( af['field'] ) != -1: # We got a candidate layer/field pair
bFieldFound = True
listCandidates.append( layer )
break
if not bFieldFound:
listCandidates.append( layers[0] ) # If no field found, propose layer anyways
return listCandidates
def layerOrFieldCombosChanged( self, callerRow=None, idx=None ):
""" SLOT. Handles both layer or field selection changed SIGNALS updating
all rows in the table. Namely, this function:
+ updates assignation arrows,
+ enables/disables OK button accordingly, and
+ updates field comboBox if layer was changed.
"""
assigned = []
bEnableOkButton = True
for row in range(self.tblAutoFields.rowCount()):
indexLayerCombo = self.tblAutoFields.cellWidget( row, 4 ).currentIndex()
indexFieldCombo = self.tblAutoFields.cellWidget( row, 5 ).currentIndex()
layerId = self.tblAutoFields.cellWidget( row, 4 ).itemData( indexLayerCombo )
fieldName = self.tblAutoFields.cellWidget( row, 5 ).itemText( indexFieldCombo )
layer = QgsMapLayerRegistry.instance().mapLayer( layerId )
# Update fieldCombo if necessary
if callerRow is not None and row == callerRow:
fieldCombo = self.tblAutoFields.cellWidget( row, 5 )
fieldCombo.blockSignals(True)
fieldCombo.clear()
fieldCombo.addItem( '[Select a field]', None )
if indexLayerCombo != 0:
autoFieldFieldName = self.tblAutoFields.item( row, 1 ).text()
for field in layer.fields():
fieldCombo.addItem( field.name() )
fieldIndex = fieldCombo.findText( autoFieldFieldName )
fieldCombo.setCurrentIndex( fieldIndex if fieldIndex != -1 else 0 )
fieldCombo.blockSignals(False)
# Update fieldCombo status info
indexFieldCombo = self.tblAutoFields.cellWidget( row, 5 ).currentIndex()
fieldName = self.tblAutoFields.cellWidget( row, 5 ).itemText( indexFieldCombo )
# Arrows
label = QLabel()
if indexLayerCombo != 0 and indexFieldCombo != 0 and layerId + "_" + fieldName in assigned:
label.setPixmap( QPixmap( ":/plugins/AutoFields/icons/red_arrow.png" ) )
label.setToolTip( 'Target layer/field pair already selected. You cannot assign two AutoFields to the same layer/field pair.' )
label.setAlignment( Qt.AlignCenter )
bEnableOkButton = False
else:
if indexLayerCombo != 0 and indexFieldCombo != 0:
assigned.append( layerId + "_" + fieldName )
label = self.getLabelWithArrow( indexLayerCombo, indexFieldCombo, layer, fieldName )
self.tblAutoFields.setCellWidget( row, 3, label )
self.buttonBox.button( QDialogButtonBox.Ok ).setEnabled( bEnableOkButton )
def accept( self ):
# Create/Overwrite configured AutoFields to given layer/field pairs
importedCount = 0
for row in range(self.tblAutoFields.rowCount()):
indexLayerCombo = self.tblAutoFields.cellWidget( row, 4 ).currentIndex()
indexFieldCombo = self.tblAutoFields.cellWidget( row, 5 ).currentIndex()
layerId = self.tblAutoFields.cellWidget( row, 4 ).itemData( indexLayerCombo )
fieldName = self.tblAutoFields.cellWidget( row, 5 ).itemText( indexFieldCombo )
expression = self.tblAutoFields.item( row, 2 ).text()
layer = QgsMapLayerRegistry.instance().mapLayer( layerId )
if indexLayerCombo != 0 and indexFieldCombo != 0:
importedCount += 1
if self.autoFieldManager.isFieldAnAutoField( layer, fieldName ):
self.autoFieldManager.overwriteAutoField( layer, fieldName, expression, calculateOnExisting=self.bCalculateOnExisting )
else:
self.autoFieldManager.createAutoField( layer, fieldName, expression, calculateOnExisting=self.bCalculateOnExisting )
if importedCount:
self.messageManager.show( QApplication.translate( "ImportAutoFields",
u"Configured AutoFields have been imported into QGIS from file {}.".format( self.filePath ) ) )
else:
self.messageManager.show( QApplication.translate( "ImportAutoFields",
u"None of the AutoFields from file {} has been imported into QGIS.".format( self.filePath ) ) )
self.done( 1 )