forked from enricofer/go2streetview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go2streetviewDialog.py
419 lines (358 loc) · 17.4 KB
/
go2streetviewDialog.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""
/***************************************************************************
go2streetview
A QGIS plugin
-------------------
begin :
copyright :
email :
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from PyQt5 import Qt, QtCore, QtWidgets, QtGui, QtWebKit, QtWebKitWidgets, QtXml, QtNetwork, uic
from qgis import core, utils, gui
import json
import os
import html.parser as HTMLParser
import xml.sax.saxutils
import resources_rc
MAIN_DIALOG_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_go2streetview.ui'))
NOTES_DIALOG_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_snapshotNotes.ui'))
LICENSE_DIALOG_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_go2streetviewLicense.ui'))
DUM_DIALOG_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_go2streetviewDum.ui'))
INFOBOX_DIALOG_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_infoBox.ui'))
# create the view dialog
class go2streetviewDialog(QtWidgets.QDockWidget, MAIN_DIALOG_CLASS):
focus_in = QtCore.pyqtSignal(int, name='focusIn')
closed_ev = QtCore.pyqtSignal(int, name='closed')
resized_ev = QtCore.pyqtSignal(int, name='resized')
enter_ev = QtCore.pyqtSignal(int, name='enter')
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
def closeEvent(self, event):
self.closed_ev.emit(1)
def resizeEvent (self, event):
self.resized_ev.emit(1)
def enterEvent (self,event):
self.enter_ev.emit(1)
# create the annotation dialog
class snapshotNotesDialog(QtWidgets.QDialog, NOTES_DIALOG_CLASS):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
# create the License dialog
class snapshotLicenseDialog(QtWidgets.QDialog, LICENSE_DIALOG_CLASS):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
# create the dummy widget
class dumWidget(QtWidgets.QDialog, DUM_DIALOG_CLASS):
enter_ev = QtCore.pyqtSignal(int, name='enter')
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
def enterEvent (self,event):
self.enter_ev.emit(1)
#create the infobox dialog
class infobox (QtWidgets.QDialog, INFOBOX_DIALOG_CLASS):
defined = QtCore.pyqtSignal()
def __init__(self,parentModule):
QtWidgets.QDialog.__init__(self)
# Set up the user interface from Designer.
#self.ui = Ui_Dialog()
self.parentModule = parentModule
self.iface = parentModule.iface
self.setupUi(self)
self.editInfoBoxHtml.clicked.connect(self.editInfoBoxHtmlAction)
self.editIconPath.clicked.connect(self.editIconPathAction)
self.editInfoField.clicked.connect(self.editInfoFieldAction)
self.enableInfoBoxCheckbox.stateChanged.connect(self.enableInfoBoxAction)
self.enableInfoLayerCheckbox.stateChanged.connect(self.enableInfoLayerAction)
self.applyButton.clicked.connect(self.acceptInfoBoxState)
self.cancelButton.clicked.connect(self.rejectInfoBoxState)
self.progressBar.hide()
self.iconPath.setText(self.tr("Icon Path"))
self.layersCombo.clear()
self.distanceBuffer.setText("")
self.distanceBuffer.setValidator(QtGui.QIntValidator(1,1000,self))
self.infoBoxIni = {'infoLayerEnabled': None,'infoBoxTemplate': u'','infoField': '','infoBoxEnabled': None,'iconPath': '','infoLayer': '','distanceBuffer':'100',"mapCommandsEnabled":None}
self.layerSet = {}
self.infoIndex = None
def enableInfoLayerAction(self,state):
if self.enableInfoLayerCheckbox.isChecked():
self.layersCombo.setEnabled(True)
self.infoField.setEnabled(True)
self.iconPath.setEnabled(True)
self.enableInfoBoxCheckbox.setEnabled(True)
self.mapCommandsCheck.setEnabled(True)
self.enableInfoBoxAction(None)
self.distanceBuffer.setEnabled(True)
self.editInfoField.setEnabled(True)
self.editIconPath.setEnabled(True)
else:
self.layersCombo.setEnabled(False)
self.infoField.setEnabled(False)
self.iconPath.setEnabled(False)
self.enableInfoBoxCheckbox.setEnabled(False)
self.mapCommandsCheck.setEnabled(False)
self.infoboxHtml.setEnabled(False)
self.editInfoBoxHtml.setEnabled(False)
self.distanceBuffer.setEnabled(False)
self.editInfoField.setEnabled(False)
self.editIconPath.setEnabled(False)
self.parentModule.disableControlShape()
def enableInfoBoxAction(self,state):
if self.enableInfoBoxCheckbox.isChecked():
self.infoboxHtml.setEnabled(True)
self.editInfoBoxHtml.setEnabled(True)
self.mapCommandsCheck.setEnabled(True)
else:
self.infoboxHtml.setEnabled(False)
self.editInfoBoxHtml.setEnabled(False)
self.mapCommandsCheck.setEnabled(False)
def layersComboAction(self,idx):
txt = self.layersCombo.currentText()
if txt and txt != "" and txt != self.tr("Select Info Layer"):
self.infoBoxIni["infoLayer"] = txt
#set dialog to default
units = self.layerSet[txt].crs().mapUnits()
if units == core.QgsUnitTypes.DistanceMeters:
dValue = '100'
uStr = self.tr("(Meters)")
elif units == core.QgsUnitTypes.DistanceFeet:
dValue = '300'
uStr = self.tr("(Feet)")
elif units == core.QgsUnitTypes.DistanceDegrees:
dValue = '0.000899838928832'
uStr = self.tr("(Degrees)")
elif units == core.QgsUnitTypes.DistanceUnknownUnit:
dValue = ''
uStr = self.tr("(Unknown unit)")
elif units == core.QgsUnitTypes.DistanceNauticalMiles:
dValue = ''
uStr = self.tr("(Nautical Miles)")
self.distanceBuffer.setText(dValue)
self.labelDistanceBuffer.setText(self.tr("Distance buffer ") + uStr)
self.infoField.clear()
self.infoboxHtml.clear()
self.iconPath.clear()
self.enableInfoBoxCheckbox.setCheckState(QtCore.Qt.Unchecked)
#self.infoBoxIni = {'infoLayerEnabled': None,'infoBoxTemplate': u'','infoField': '','infoBoxEnabled': None,'iconPath': '','infoLayer': '','distanceBuffer':'100'}
self.saveIni()
def loadPointLayers(self,default = None):
self.layerSet = {}
for layer_name, layer in core.QgsProject.instance().mapLayers().items():
if layer.type() == core.QgsMapLayer.VectorLayer and (layer.geometryType() in (core.QgsWkbTypes.PointGeometry, core.QgsWkbTypes.LineGeometry, core.QgsWkbTypes.PolygonGeometry)):
self.layerSet[layer.name()] = layer
if default:
if default in self.layerSet.keys():
defaultLayer = default
else:
defaultLayer = None
else:
defaultLayer = None
self.populateComboBox(self.layersCombo,self.layerSet.keys(),predef=defaultLayer,sort = True,msg=self.tr("Select Info Layer"))
self.layersCombo.activated.connect(self.layersComboAction)
def loadFields(self,layer,default = None):
if layer and layer != "":
fieldNames = [field.name() for field in layer.pendingFields() ]
if default:
if default in fieldNames:
defaultField = default
else:
defaultField = None
else:
defaultField = None
self.populateComboBox(self.fieldsCombo,fieldNames,predef=defaultField,msg=self.tr("Select Info Field"))
#self.fieldsCombo.activated.connect(self.fieldsComboAction)
def editInfoBoxHtmlAction(self):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys():
self.QEX = gui.QgsExpressionBuilderDialog(self.layerSet[self.infoBoxIni["infoLayer"]],"Insert expression",None)
self.QEX.setExpressionText(self.infoboxHtml.textCursor().selectedText().strip('[%').strip('%]').strip())
if self.QEX.exec_():
self.infoboxHtml.insertPlainText('[% {} %]'.format(self.QEX.expressionText()))
def editInfoFieldAction(self):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys():
self.QEX = gui.QgsExpressionBuilderDialog(self.layerSet[self.infoBoxIni["infoLayer"]],"Insert expression",None)
self.QEX.setExpressionText(self.infoField.text().strip('[%').strip('%]').strip())
if self.QEX.exec_():
self.infoField.setText('[% {} %]'.format(self.QEX.expressionText()))
def editIconPathAction(self):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys():
self.QEX = gui.QgsExpressionBuilderDialog(self.layerSet[self.infoBoxIni["infoLayer"]],"Insert expression",None)
self.QEX.setExpressionText(self.iconPath.text().strip('[%').strip('%]').strip())
if self.QEX.exec_():
self.iconPath.setText('[% {} %]'.format(self.QEX.expressionText()))
def populateComboBox(self,combo,list,predef = None,sort = None,msg = ""):
#procedure to fill specified combobox with provided list
combo.clear()
model=QtGui.QStandardItemModel(combo)
for elem in list:
try:
item = QtGui.QStandardItem(elem)
except TypeError:
item = QtGui.QStandardItem(str(elem))
model.appendRow(item)
if sort:
model.sort(0)
combo.setModel(model)
idx = combo.findText(predef)
if idx != -1:
combo.setCurrentIndex(idx)
else:
combo.insertItem(0,msg)
combo.setCurrentIndex(0)
def copyToTextBox(self,valid):
if valid:
self.infoboxHtml.setPlainText (self.QEX.expressionText())
def get_featureContext(self,feat):
infoboxContext = core.QgsExpressionContext()
infoboxContext.appendScopes(core.QgsExpressionContextUtils.globalProjectLayerScopes(self.getInfolayer()))
infoboxContext.setFeature(feat)
infoboxContext.setFields(self.getInfolayer().fields())
return infoboxContext
def getHtml(self,feat):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys() and self.enableInfoBoxCheckbox.isChecked():
htmlExp = core.QgsExpression()
html = htmlExp.replaceExpressionText(self.infoboxHtml.toPlainText().replace("\n",""), self.get_featureContext(feat))
if html:
html = html.replace("\n","")
html = html.replace('"',"")
html = html.replace("'","")
return html
else:
return ""
else:
return ""
def getInfoField(self,feat):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys() and self.infoField.text() != "":
infoFieldExp = core.QgsExpression()
content = infoFieldExp.replaceExpressionText(self.infoField.text(), self.get_featureContext(feat))
if content:
content = content.replace("\n","")
content = content.replace('"',"")
content = content.replace("'","")
return content
return ""
def getFeatId(self,feat):
if self.mapCommandsCheck.isChecked():
return feat.id()
else:
return 0
def getIconPath(self,feat):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys() and self.iconPath.text() != "":
iconPathExp = core.QgsExpression()
content = iconPathExp.replaceExpressionText(self.iconPath.text().replace("\n",""), self.get_featureContext(feat))
return content
return ""
def getFieldContent(self,feat):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys():
idx = self.layerSet[self.infoBoxIni["infoLayer"]].fieldNameIndex(self.infoBoxIni["infoField"])
return feat.attributes()[idx]
def getInfolayer(self):
if self.infoBoxIni["infoLayer"] in self.layerSet.keys():
return self.layerSet[self.infoBoxIni["infoLayer"]]
else:
return None
def isEnabled(self):
return self.infoBoxIni["infoLayerEnabled"]
def infoBoxEnabled(self):
return self.infoBoxIni["infoLayerEnabled"]
def getDistanceBuffer(self):
return float(self.infoBoxIni["distanceBuffer"])
def restoreIni(self):
#prjFileInfo = QtCore.QFileInfo(core.QgsProject.instance().fileName())
#iniFileInfo = QtCore.QFileInfo(os.path.join(prjFileInfo.path(),prjFileInfo.baseName()+".gsv"))
stored_settings = core.QgsExpressionContextUtils.projectScope(core.QgsProject.instance()).variable('go2sv_infolayer_settings')
if stored_settings:
self.infoBoxIni = json.loads(stored_settings)
self.loadPointLayers(default = self.infoBoxIni["infoLayer"])
self.infoField.setText(self.infoBoxIni["infoField"])
else:
self.infoBoxIni = {'infoLayerEnabled': None,'infoBoxTemplate': u'','infoField': '','infoBoxEnabled': None,'iconPath': '','infoLayer': '','distanceBuffer':'100',"mapCommandsEnabled":None}
self.loadPointLayers()
if self.infoBoxIni["infoLayerEnabled"]:
self.enableInfoLayerCheckbox.setCheckState(QtCore.Qt.Checked)
else:
self.enableInfoLayerCheckbox.setCheckState(QtCore.Qt.Unchecked)
self.iconPath.setText(self.infoBoxIni["iconPath"])
self.distanceBuffer.setText(self.infoBoxIni["distanceBuffer"])
if self.infoBoxIni["infoBoxEnabled"]:
self.enableInfoBoxCheckbox.setCheckState(QtCore.Qt.Checked)
else:
self.enableInfoBoxCheckbox.setCheckState(QtCore.Qt.Unchecked)
if self.infoBoxIni["mapCommandsEnabled"]:
self.mapCommandsCheck.setCheckState(QtCore.Qt.Checked)
else:
self.enableInfoBoxCheckbox.setCheckState(QtCore.Qt.Unchecked)
html_parser = HTMLParser.HTMLParser()
self.infoboxHtml.setPlainText(html_parser.unescape(self.infoBoxIni["infoBoxTemplate"]))
self.enableInfoLayerAction(True)
if self.infoIndex and self.enableInfoLayerCheckbox.isChecked():
self.updateSpatialIndex()
self.defined.emit()
def saveIni(self):
self.infoBoxIni["infoLayerEnabled"] = self.enableInfoLayerCheckbox.isChecked()
if self.layersCombo.currentText() != self.tr("Select Info Layer"):
self.infoBoxIni["infoLayer"] = self.layersCombo.currentText()
else:
self.infoBoxIni["infoLayer"] = ""
self.infoBoxIni["infoField"] = self.infoField.text()
if self.iconPath.text() != self.tr("Icon Path"):
self.infoBoxIni["iconPath"] = self.iconPath.text()
else:
self.infoBoxIni["iconPath"] = ""
self.infoBoxIni["distanceBuffer"] = self.distanceBuffer.text()
self.infoBoxIni["infoBoxEnabled"] = self.enableInfoBoxCheckbox.isChecked()
self.infoBoxIni["mapCommandsEnabled"] = self.mapCommandsCheck.isChecked()
self.infoBoxIni["infoBoxTemplate"] = xml.sax.saxutils.escape(self.infoboxHtml.toPlainText())
core.QgsExpressionContextUtils.setProjectVariable(core.QgsProject.instance(), 'go2sv_infolayer_settings', json.dumps(self.infoBoxIni))
def showEvent(self,event):
self.raise_()
self.restoreIni()
def getContextFeatures(self,point):
dist = self.getDistanceBuffer()
context = core.QgsRectangle(point.x()-dist,point.y()-dist,point.x()+dist,point.y()+dist)
try:
return self.infoIndex.intersects(context)
except:
return []
def updateSpatialIndex(self):
if self.enableInfoLayerCheckbox.isChecked():
self.infoIndex = core.QgsSpatialIndex ()
self.progressBar.show()
self.progressBar.setRange(0,self.getInfolayer().featureCount ())
infoFeats = self.getInfolayer().getFeatures()
processed = 0
for feat in infoFeats:
self.infoIndex.insertFeature(feat)
self.progressBar.setValue(processed)
processed += 1
self.progressBar.hide()
self.progressBar.reset()
else:
self.infoIndex = None
def acceptInfoBoxState(self):
self.saveIni()
try:
self.layersCombo.activated.disconnect(self.layersComboAction)
except:
pass
self.updateSpatialIndex()
self.hide()
self.defined.emit()
def rejectInfoBoxState(self):
self.hide()