-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
propwidget.py
540 lines (411 loc) · 18.2 KB
/
propwidget.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# -*- coding: utf-8 -*-
# (C) 2014 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2014-01-06
import os
from PyQt5.QtCore import Qt, QDir, QEvent, QObject, QVariant
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QComboBox, QFileDialog, QWidget
from qgis.core import QgsApplication, QgsFieldProxyModel, QgsProject
from .ui.widgetComboEdit import Ui_ComboEditWidget
from .utils import selectColor, getDEMLayersInProject, shortTextFromSelectedLayerIds
class WVT:
# widget value type
SIZE = 1
ANGLE = 2
OTHERS = 3
class WidgetFuncBase:
def __init__(self, widget, mapLayer):
self.widget = widget
self.mapLayer = mapLayer
def setup(self, name=None, editLabel="", lineEdit="", placeholderText="", toolButton=False, checkBox=False):
# setup widgets
b = bool(name is not None)
self.widget.label_1.setVisible(b)
self.widget.comboBox.setVisible(b)
self.widget.checkBox.setVisible(b and checkBox)
if name:
self.widget.label_1.setText(name)
if editLabel:
self.widget.label_2.setText(editLabel)
self.widget.label_2.setVisible(bool(editLabel))
self.widget.expression.setExpression(lineEdit or "")
self.widget.expression.setLayer(None)
b = bool(lineEdit is not None)
self.widget.expression.setVisible(b)
if b:
self.setPlaceholderText(placeholderText)
self.widget.toolButton.setVisible(toolButton)
if toolButton and self.widget.toolButton.icon():
self.widget.toolButton.setIcon(QIcon())
def dispose(self):
pass
def comboBoxSelectionChanged(self, index):
pass
def toolButtonClicked(self):
pass
def setPlaceholderText(self, text):
try:
lineEdit = self.widget.expressionComboBox().lineEdit()
lineEdit.setPlaceholderText(text)
lineEdit.setToolTip(text)
except:
pass
def values(self):
return {"type": self.widget.funcType,
"comboData": self.widget.comboBox.itemData(self.widget.comboBox.currentIndex()),
"comboText": self.widget.comboBox.currentText(),
"editText": self.widget.expression.expression()}
def setValues(self, vals):
index = self.widget.comboBox.findData(vals["comboData"])
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
self.widget.expression.setExpression(vals["editText"])
@classmethod
def numericalFields(cls, layer):
numeric_fields = []
for i, field in enumerate(layer.fields()):
if field.type() in [QVariant.Double, QVariant.Int, QVariant.LongLong, QVariant.UInt, QVariant.ULongLong]:
numeric_fields.append([i, field.name()])
return numeric_fields
class ExpressionWidgetFunc(WidgetFuncBase):
def __init__(self, widget, mapLayer):
WidgetFuncBase.__init__(self, widget, mapLayer)
self.valType = None
def setup(self, options=None):
""" options: name, valType, label, defaultValue """
options = options or {}
valType = options.get("valType", WVT.SIZE)
if valType == self.valType:
val = self.widget.expression.expression()
else:
val = str(options.get("defVal", 0))
self.valType = valType
WidgetFuncBase.setup(self, editLabel=options.get("name", ""), lineEdit=val)
self.widget.comboBox.clear()
self.widget.comboBox.addItem("Expression")
self.widget.expression.setFilters(QgsFieldProxyModel.Numeric)
self.widget.expression.setLayer(self.mapLayer)
class ColorWidgetFunc(WidgetFuncBase):
FEATURE = 1
RANDOM = 2
EXPRESSION = 3
def setup(self, options=None):
""" options: defaultValue """
WidgetFuncBase.setup(self, "Color", lineEdit=None)
options = options or {}
self.widget.comboBox.clear()
self.widget.comboBox.addItem("Feature style", ColorWidgetFunc.FEATURE)
self.widget.comboBox.addItem("Random", ColorWidgetFunc.RANDOM)
self.widget.comboBox.addItem("Expression", ColorWidgetFunc.EXPRESSION)
self.widget.expression.setExpression(str(options.get("defVal", "")))
self.widget.expression.setFilters(QgsFieldProxyModel.String)
self.setPlaceholderText("e.g. color_rgb(255, 127, 0), '#FF7F00'")
self.widget.expression.setFilters(QgsFieldProxyModel.String | QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong)
self.widget.expression.setLayer(self.mapLayer)
self.widget.toolButton.setIcon(QgsApplication.getThemeIcon("mIconColorSwatches.svg"))
def comboBoxSelectionChanged(self, index):
itemData = self.widget.comboBox.itemData(index)
isRGB = itemData == ColorWidgetFunc.EXPRESSION
self.widget.label_2.setVisible(isRGB)
self.widget.expression.setVisible(isRGB)
self.widget.toolButton.setVisible(isRGB)
def toolButtonClicked(self):
color = selectColor()
if color:
self.widget.expression.setExpression("'" + color.name().replace("#", "0x") + "'")
def setValues(self, vals):
index = self.widget.comboBox.findData(vals["comboData"])
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
self.widget.comboBoxSelectionChanged(index) # make sure to update visibility
self.widget.expression.setExpression(vals["editText"])
class OptionalColorWidgetFunc(ColorWidgetFunc):
def setup(self, options=None):
""" options: name, itemText, defaultValue """
options = options or {}
ColorWidgetFunc.setup(self, options)
self.widget.label_1.setText(options.get("name", "Color"))
itemText = options.get("itemText", {})
if itemText.get(None, "") is not None:
self.widget.comboBox.insertItem(0, "None", None)
for id, text in itemText.items():
index = self.widget.comboBox.findData(id)
if index != -1:
self.widget.comboBox.setItemText(index, text)
index = self.widget.comboBox.findData(options.get("defVal"))
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
class FilePathWidgetFunc(WidgetFuncBase):
FILEPATH = 1
def setup(self, options=None):
""" options: name, label, defaultValue, filterString, allowURL """
options = options or {}
self.lineEditLabel = options.get("label", "")
WidgetFuncBase.setup(self, options.get("name", ""), editLabel=self.lineEditLabel, toolButton=True)
self.widget.expression.setExpression(str(options.get("defVal", "")))
self.widget.comboBox.clear()
if options.get("allowURL"):
self.widget.comboBox.addItem("File path or URL", FilePathWidgetFunc.FILEPATH)
else:
self.widget.comboBox.addItem("File path", FilePathWidgetFunc.FILEPATH)
self.widget.expression.setFilters(QgsFieldProxyModel.String)
self.widget.expression.setLayer(self.mapLayer)
self.filterString = options.get("filterString", "")
def toolButtonClicked(self):
workdir = os.path.split(self.widget.expression.expression())[0]
if not workdir:
workdir = QgsProject.instance().homePath()
if not workdir:
workdir = QDir.homePath()
filepath, _ = QFileDialog.getOpenFileName(None, "Select a file", workdir, self.filterString)
if filepath:
self.widget.expression.setExpression("'" + filepath + "'")
class HeightWidgetFunc(WidgetFuncBase):
def setup(self, options=None):
""" options: name, defaultValue, defaultValue """
options = options or {}
WidgetFuncBase.setup(self, options.get("name", "Mode"))
self.defaultValue = options.get("defVal", 0)
# set up combo box
comboBox = self.widget.comboBox
comboBox.clear()
comboBox.addItem("Absolute")
for lyr in getDEMLayersInProject():
comboBox.addItem('Relative to "{0}" layer'.format(lyr.name()), lyr.id())
# z value if layer has
# if layer and layer.wkbType() in [QgsWkbTypes.Point25D, QgsWkbTypes.LineString25D, QgsWkbTypes.MultiPoint25D, QgsWkbTypes.MultiLineString25D]:
# comboBox.addItem("Z value", HeightWidgetFunc.Z_VALUE)
# comboBox.insertSeparator(1)
defaultValue = options.get("defVal")
if defaultValue:
index = comboBox.findData(defaultValue)
if index != -1:
comboBox.setCurrentIndex(index)
def comboBoxSelectionChanged(self, index):
if self.widget.comboBox.itemData(index):
label = "Addend"
defaultValue = 0
else:
label = "Altitude"
defaultValue = self.defaultValue
self.widget.label_2.setText(label)
self.widget.expression.setExpression(str(defaultValue))
def isCurrentItemRelativeHeight(self):
return self.widget.comboBox.itemData(self.widget.comboBox.currentIndex()) is None
class LabelHeightWidgetFunc(WidgetFuncBase):
ABSOLUTE = 0
RELATIVE = 1
def setup(self, options=None):
""" options: defaultValue """
WidgetFuncBase.setup(self, "Label height")
options = options or {}
self.widget.comboBox.clear()
self.widget.comboBox.addItem("Relative", self.RELATIVE)
self.widget.comboBox.addItem("Absolute", self.ABSOLUTE)
self.widget.expression.setFilters(QgsFieldProxyModel.Numeric)
self.widget.expression.setLayer(self.mapLayer)
self.defaultValue = options.get("defVal")
if self.defaultValue is not None:
self.widget.expression.setExpression(str(self.defaultValue))
class OpacityWidgetFunc(WidgetFuncBase):
FEATURE = 1
EXPRESSION = 2
def setup(self, options=None):
WidgetFuncBase.setup(self, "Opacity", lineEdit=None)
options = options or {}
self.widget.comboBox.clear()
self.widget.comboBox.addItem("Feature style", OpacityWidgetFunc.FEATURE)
self.widget.comboBox.addItem("Expression", OpacityWidgetFunc.EXPRESSION)
self.widget.expression.setFilters(QgsFieldProxyModel.Numeric)
self.widget.expression.setLayer(self.mapLayer)
self.setPlaceholderText("numeric (percentage. 0 - 100)")
def comboBoxSelectionChanged(self, index):
itemData = self.widget.comboBox.itemData(index)
isValue = itemData == OpacityWidgetFunc.EXPRESSION
self.widget.label_2.setVisible(isValue)
self.widget.expression.setVisible(isValue)
def setValues(self, vals):
index = self.widget.comboBox.findData(vals["comboData"])
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
self.widget.comboBoxSelectionChanged(index) # make sure to update visibility
self.widget.expression.setExpression(vals["editText"])
class ColorTextureWidgetFunc(ColorWidgetFunc):
MAP_CANVAS = 10
LAYER = 11
def __init__(self, widget):
ColorWidgetFunc.__init__(self, widget)
self.layerIds = []
self.mapSettings = None
def setup(self, options=None):
""" options: mapSettings """
options = options or {}
self.mapSettings = options.get("mapSettings")
ColorWidgetFunc.setup(self, options)
self.widget.label_1.setText("Color/Texture")
comboBox = self.widget.comboBox
comboBox.insertSeparator(comboBox.count())
comboBox.addItem("Map canvas image", ColorTextureWidgetFunc.MAP_CANVAS)
comboBox.addItem("Layer image", ColorTextureWidgetFunc.LAYER)
self.updateLineEdit()
def comboBoxSelectionChanged(self, index):
itemData = self.widget.comboBox.itemData(index)
isRGB = bool(itemData == ColorWidgetFunc.EXPRESSION)
isLayer = bool(itemData == ColorTextureWidgetFunc.LAYER)
self.widget.label_2.setText("Layers" if isLayer else "Value")
self.widget.label_2.setVisible(isRGB or isLayer)
# self.widget.expression.setPlaceholderText("0xrrggbb" if isRGB else "")
# self.widget.expression.setReadOnly(isLayer)
self.widget.expression.setVisible(isRGB or isLayer)
if isRGB:
self.widget.expression.setExpression("")
elif isLayer:
self.updateLineEdit()
self.widget.toolButton.setVisible(isRGB or isLayer)
def toolButtonClicked(self):
itemData = self.widget.comboBox.itemData(self.widget.comboBox.currentIndex())
if itemData == ColorWidgetFunc.EXPRESSION:
ColorWidgetFunc.toolButtonClicked(self)
return
# ColorTextureWidgetFunc.LAYER
from .layerselectdialog import LayerSelectDialog
dialog = LayerSelectDialog(self.widget)
dialog.initTree(self.layerIds)
dialog.setMapSettings(self.mapSettings)
if dialog.exec_():
self.layerIds = dialog.visibleLayerIds()
self.updateLineEdit()
def updateLineEdit(self):
self.widget.expression.setExpression(shortTextFromSelectedLayerIds(self.layerIds))
def values(self):
v = ColorWidgetFunc.values(self)
if self.layerIds:
v["layerIds"] = self.layerIds
return v
def setValues(self, vals):
self.layerIds = vals.get("layerIds", [])
ColorWidgetFunc.setValues(self, vals)
class CheckBoxWidgetFunc(WidgetFuncBase):
def setup(self, options=None):
""" options: name, defaultValue, connectTo """
options = options or {}
WidgetFuncBase.setup(self, options.get("name", ""), checkBox=True)
self.setLayoutVisible(False)
checked = options.get("defVal", False)
# connect with widgets
self.connectedWidgets = []
for w in options.get("connectTo", []):
w.setEnabled(checked)
self.widget.checkBox.toggled.connect(w.setEnabled)
self.connectedWidgets.append(w)
def dispose(self):
self.setLayoutVisible(True)
for w in self.connectedWidgets:
self.widget.checkBox.toggled.disconnect(w.setEnabled)
w.setEnabled(True)
def values(self):
return {"type": self.widget.funcType,
"checkBox": self.widget.checkBox.isChecked()}
def setValues(self, vals):
checked = vals.get("checkBox", False)
self.widget.checkBox.setChecked(checked)
for w in self.connectedWidgets:
w.setEnabled(checked)
def setLayoutVisible(self, visible):
self.widget.label_2.setVisible(visible)
self.widget.comboBox.setVisible(visible)
self.widget.expression.setVisible(visible)
self.widget.toolButton.setVisible(visible)
class ComboBoxWidgetFunc(WidgetFuncBase):
def setup(self, options=None):
""" options: name, items, defaultValue"""
options = options or {}
WidgetFuncBase.setup(self, options.get("name", ""), lineEdit=None)
self.widget.comboBox.clear()
for item in options.get("items", []):
self.widget.comboBox.addItem(item, item)
def_val = options.get("defVal")
if def_val:
index = self.widget.comboBox.findData(def_val)
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
def values(self):
return {"type": self.widget.funcType,
"comboData": self.widget.comboBox.itemData(self.widget.comboBox.currentIndex())}
def setValues(self, vals):
index = self.widget.comboBox.findData(vals["comboData"])
if index != -1:
self.widget.comboBox.setCurrentIndex(index)
class PropertyWidget(QWidget, Ui_ComboEditWidget):
# function types
EXPRESSION = 1
COLOR = 2
FILEPATH = 3
HEIGHT = 4 # OBSOLETE
OPACITY = 5
LABEL_HEIGHT = 6
OPTIONAL_COLOR = 7
COLOR_TEXTURE = 8 # OBSOLETE
CHECKBOX = 9
COMBOBOX = 10
type2funcClass = {EXPRESSION: ExpressionWidgetFunc,
COLOR: ColorWidgetFunc,
FILEPATH: FilePathWidgetFunc,
HEIGHT: HeightWidgetFunc, # OBSOLETE
LABEL_HEIGHT: LabelHeightWidgetFunc,
OPACITY: OpacityWidgetFunc,
OPTIONAL_COLOR: OptionalColorWidgetFunc,
COLOR_TEXTURE: ColorTextureWidgetFunc, # OBSOLETE
CHECKBOX: CheckBoxWidgetFunc,
COMBOBOX: ComboBoxWidgetFunc}
FIELDTYPE_ALL = 0
FIELDTYPE_NUMBER = 1
FIELDTYPE_STRING = 2
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.comboBox.currentIndexChanged.connect(self.comboBoxSelectionChanged)
self.toolButton.clicked.connect(self.toolButtonClicked)
self.funcType = None
self.func = None
self.hasValues = False
# install event filter
self.enterKeyFilter = EnterKeyEventFilter(self)
for w in self.expression.findChildren(QComboBox):
w.installEventFilter(self.enterKeyFilter)
def setup(self, funcType, mapLayer=None, options=None):
if self.func:
self.func.dispose()
if self.func is None or self.funcType != funcType:
self.func = self.type2funcClass[funcType](self, mapLayer)
self.funcType = funcType
self.func.setup(options)
self.setVisible(True)
self.hasValues = True
def expressionComboBox(self):
for w in self.expression.findChildren(QComboBox):
return w
def comboBoxSelectionChanged(self, index):
if self.func and index != -1:
self.func.comboBoxSelectionChanged(index)
def toolButtonClicked(self):
if self.func:
self.func.toolButtonClicked()
def hide(self):
self.hasValues = False
QWidget.hide(self)
def values(self):
if self.func and self.hasValues:
return self.func.values()
else:
return {}
def setValues(self, vals):
if self.func:
self.func.setValues(vals)
class EnterKeyEventFilter(QObject):
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress and (event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter):
event.ignore()
return True
return QObject.eventFilter(self, obj, event)