forked from blank63/j3dview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qt.py
189 lines (133 loc) · 5.9 KB
/
qt.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
from PyQt5 import QtCore,QtWidgets
class ComboBox(QtWidgets.QComboBox):
def setItems(self,items):
self.items = items
self.clear()
self.addItems([item.name for item in items])
for i,item in enumerate(items):
self.setItemData(i,item)
def setValue(self,value):
self.setCurrentIndex(self.items.index(value))
class PropertyUndoCommand(QtWidgets.QUndoCommand):
def __init__(self,property_owner,property_name,old_value,new_value):
super().__init__()
self.property_owner = property_owner
self.property_name = property_name
self.old_value = old_value
self.new_value = new_value
#FIXME: Set the text property to something?
def redo(self):
setattr(self.property_owner,self.property_name,self.new_value)
def undo(self):
setattr(self.property_owner,self.property_name,self.old_value)
class PropertyWidget:
def __init__(self):
self.property_owner = None
self.property_name = None
self.property_changed_signal = None
self.undo_stack = None
def bindProperty(self,property_owner,property_name,property_changed_signal):
if self.property_changed_signal is not None:
self.property_changed_signal.disconnect(self.setValue)
self.property_owner = property_owner
self.property_name = property_name
self.setValue(getattr(self.property_owner,self.property_name))
self.property_changed_signal = property_changed_signal
self.property_changed_signal.connect(self.setValue)
def setUndoStack(self,undo_stack):
self.undo_stack = undo_stack
def updatePropertyValue(self,value):
if self.property_owner is None: return
old_value = getattr(self.property_owner,self.property_name)
if value == old_value: return
if self.undo_stack is not None:
self.undo_stack.push(PropertyUndoCommand(self.property_owner,self.property_name,old_value,value))
else:
setattr(self.property_owner,self.property_name,value)
class PropertyLineEdit(QtWidgets.QLineEdit,PropertyWidget):
def __init__(self,*args,**kwargs):
QtWidgets.QLineEdit.__init__(self,*args,**kwargs)
PropertyWidget.__init__(self)
self.editingFinished.connect(self.on_editingFinished)
@QtCore.pyqtSlot(str)
def setValue(self,value):
self.setText(value)
@QtCore.pyqtSlot()
def on_editingFinished(self):
self.updatePropertyValue(self.text())
class PropertyComboBox(ComboBox,PropertyWidget):
def __init__(self,*args,**kwargs):
ComboBox.__init__(self,*args,**kwargs)
PropertyWidget.__init__(self)
self.currentIndexChanged.connect(self.on_currentIndexChanged)
@QtCore.pyqtSlot(int)
def on_currentIndexChanged(self,index):
self.updatePropertyValue(self.itemData(index))
class PropertySpinBox(QtWidgets.QSpinBox,PropertyWidget):
def __init__(self,*args,**kwargs):
QtWidgets.QSpinBox.__init__(self,*args,**kwargs)
PropertyWidget.__init__(self)
self.setKeyboardTracking(False)
self.valueChanged.connect(self.updatePropertyValue)
class PropertyDoubleSpinBox(QtWidgets.QDoubleSpinBox,PropertyWidget):
def __init__(self,*args,**kwargs):
QtWidgets.QSpinBox.__init__(self,*args,**kwargs)
PropertyWidget.__init__(self)
self.setKeyboardTracking(False)
self.valueChanged.connect(self.updatePropertyValue)
class Property:
def __init__(self,property_type=object):
self.property_type = property_type
@staticmethod
def create_getter(property_name):
def getter(property_owner):
return getattr(property_owner,'_' + property_name)
return getter
@staticmethod
def create_setter(property_name):
def setter(property_owner,value):
setattr(property_owner,'_' + property_name,value)
return setter
def create_full_setter(self,property_name,notify_name):
internal_setter = self.create_setter(property_name)
def setter(property_owner,value):
try:
old_value = getattr(property_owner,property_name)
except AttributeError:
pass
else:
if value == old_value: return
internal_setter(property_owner,value)
getattr(property_owner,notify_name).emit(value)
return setter
class PropertyOwnerMetaClass(type(QtCore.QObject)):
def __new__(metacls,cls,bases,classdict):
property_items = [(key,value) for key,value in classdict.items() if isinstance(value,Property)]
for name,property_placeholder in property_items:
metacls.insert_property(classdict,name,property_placeholder)
return super().__new__(metacls,cls,bases,classdict)
@staticmethod
def insert_property(classdict,name,property_placeholder):
notify_name = name + '_changed'
notify = QtCore.pyqtSignal(property_placeholder.property_type)
classdict[notify_name] = notify
getter = property_placeholder.create_getter(name)
setter = property_placeholder.create_full_setter(name,notify_name)
classdict[name] = property(getter,setter)
class Wrapper(QtCore.QObject,metaclass=PropertyOwnerMetaClass):
class Property(Property):
@staticmethod
def create_getter(property_name):
def getter(property_owner):
return getattr(property_owner.wrapped_object,property_name)
return getter
@staticmethod
def create_setter(property_name):
def setter(property_owner,value):
setattr(property_owner.wrapped_object,property_name,value)
return setter
def __init__(self,wrapped_object,*args,**kwargs):
super().__init__(*args,**kwargs)
self.wrapped_object = wrapped_object
def __getattr__(self,name):
return getattr(self.wrapped_object,name)