-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_rectf.py
116 lines (88 loc) · 4.45 KB
/
my_rectf.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
from pyqtgraph.parametertree import registerParameterType
from pyqtgraph.parametertree.parameterTypes.basetypes import ParameterItem
from qgis.PyQt.QtCore import QRectF
from qgis.PyQt.QtWidgets import QHBoxLayout, QSizePolicy, QSpacerItem, QWidget
from .my_group import MyGroupParameter, MyGroupParameterItem
from .my_preview_label import MyPreviewLabel
registerParameterType('myGroup', MyGroupParameter, override=True)
class RectPreviewLabel(MyPreviewLabel):
def __init__(self, param):
super().__init__()
param.sigValueChanging.connect(self.onRectChanging)
opts = param.opts
self.decimals = opts.get('decimals', 5)
val = opts.get('value', QRectF())
self.onRectChanging(None, val)
def onRectChanging(self, _, val): # unused param replaced by _
rect = QRectF(val) # make local copy
d = self.decimals
if rect.isNull():
self.setText('Unrestricted')
else:
Xmin = rect.left()
Xmax = rect.right()
Ymin = rect.top()
Ymax = rect.bottom()
self.setText(f'x:({Xmin:.{d}g}¦{Xmax:.{d}g}), y:({Ymin:.{d}g}¦{Ymax:.{d}g})')
self.update()
class MyRectParameterItem(MyGroupParameterItem):
def __init__(self, param, depth):
super().__init__(param, depth)
self.itemWidget = QWidget()
spacerItem = QSpacerItem(5, 5, QSizePolicy.Fixed, QSizePolicy.Fixed)
self.label = RectPreviewLabel(param)
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(2) # spacing between elements
layout.addSpacerItem(spacerItem)
layout.addWidget(self.label)
self.itemWidget.setLayout(layout)
def treeWidgetChanged(self):
ParameterItem.treeWidgetChanged(self)
tw = self.treeWidget()
if tw is None:
return
tw.setItemWidget(self, 1, self.itemWidget)
class MyRectParameter(MyGroupParameter):
itemClass = MyRectParameterItem
def __init__(self, **opts):
# opts['expanded'] = False # to overrule user-requested options
# opts['flat'] = True
MyGroupParameter.__init__(self, **opts)
if 'children' in opts:
raise KeyError('Cannot set "children" argument in MyRect Parameter opts')
d = opts.get('decimals', 5)
e = opts.get('enabled', True)
r = opts.get('readonly', False)
self.rect = opts.get('value', QRectF())
self.addChild(dict(name='Xmin', type='myFloat', decimals=d, enabled=e, readonly=r, value=self.rect.left(), default=self.rect.left())) # myFloat
self.addChild(dict(name='Xmax', type='myFloat', decimals=d, enabled=e, readonly=r, value=self.rect.right(), default=self.rect.right())) # myFloat
self.addChild(dict(name='Ymin', type='myFloat', decimals=d, enabled=e, readonly=r, value=self.rect.top(), default=self.rect.top())) # myFloat
self.addChild(dict(name='Ymax', type='myFloat', decimals=d, enabled=e, readonly=r, value=self.rect.bottom(), default=self.rect.bottom())) # myFloat
self.parXmin = self.child('Xmin')
self.parXmax = self.child('Xmax')
self.parYmin = self.child('Ymin')
self.parYmax = self.child('Ymax')
self.parXmin.sigValueChanged.connect(self.changed)
self.parXmax.sigValueChanged.connect(self.changed)
self.parYmin.sigValueChanged.connect(self.changed)
self.parYmax.sigValueChanged.connect(self.changed)
def setOpts(self, **opts):
self.parXmin.setOpts(**opts)
self.parXmax.setOpts(**opts)
self.parYmin.setOpts(**opts)
self.parYmax.setOpts(**opts)
# update the values of the four children
def changed(self):
xmin = self.parXmin.value()
xmax = self.parXmax.value()
self.rect.setLeft(min(xmin, xmax)) # always set first
self.rect.setRight(max(xmin, xmax))
ymin = self.parYmin.value()
ymax = self.parYmax.value()
self.rect.setTop(min(ymin, ymax)) # always set first
self.rect.setBottom(max(ymin, ymax))
self.sigValueChanging.emit(self, self.rect)
def value(self):
return self.rect
registerParameterType('myRectF', MyRectParameter, override=True)