-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIElements.py
407 lines (316 loc) · 11.8 KB
/
GUIElements.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
from PyQt4 import QtGui, QtCore
from copy import copy
#import FlatCAMApp
import re
import logging
log = logging.getLogger('base')
class RadioSet(QtGui.QWidget):
def __init__(self, choices, orientation='horizontal', parent=None):
"""
The choices are specified as a list of dictionaries containing:
* 'label': Shown in the UI
* 'value': The value returned is selected
:param choices: List of choices. See description.
:param orientation: 'horizontal' (default) of 'vertical'.
:param parent: Qt parent widget.
:type choices: list
"""
super(RadioSet, self).__init__(parent)
self.choices = copy(choices)
if orientation == 'horizontal':
layout = QtGui.QHBoxLayout()
else:
layout = QtGui.QVBoxLayout()
group = QtGui.QButtonGroup(self)
for choice in self.choices:
choice['radio'] = QtGui.QRadioButton(choice['label'])
group.addButton(choice['radio'])
layout.addWidget(choice['radio'], stretch=0)
choice['radio'].toggled.connect(self.on_toggle)
layout.addStretch()
self.setLayout(layout)
self.group_toggle_fn = lambda: None
def on_toggle(self):
log.debug("Radio toggled")
radio = self.sender()
if radio.isChecked():
self.group_toggle_fn()
return
def get_value(self):
for choice in self.choices:
if choice['radio'].isChecked():
return choice['value']
log.error("No button was toggled in RadioSet.")
return None
def set_value(self, val):
for choice in self.choices:
if choice['value'] == val:
choice['radio'].setChecked(True)
return
log.error("Value given is not part of this RadioSet: %s" % str(val))
class LengthEntry(QtGui.QLineEdit):
def __init__(self, output_units='IN', parent=None):
super(LengthEntry, self).__init__(parent)
self.output_units = output_units
self.format_re = re.compile(r"^([^\s]+)(?:\s([a-zA-Z]+))?$")
# Unit conversion table OUTPUT-INPUT
self.scales = {
'IN': {'IN': 1.0,
'MM': 1/25.4},
'MM': {'IN': 25.4,
'MM': 1.0}
}
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(LengthEntry, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(LengthEntry, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def returnPressed(self, *args, **kwargs):
val = self.get_value()
if val is not None:
self.set_text(str(val))
else:
log.warning("Could not interpret entry: %s" % self.get_text())
def get_value(self):
raw = str(self.text()).strip(' ')
# match = self.format_re.search(raw)
try:
units = raw[-2:]
units = self.scales[self.output_units][units.upper()]
value = raw[:-2]
return float(eval(value))*units
except IndexError:
value = raw
return float(eval(value))
except KeyError:
value = raw
return float(eval(value))
except:
log.warning("Could not parse value in entry: %s" % str(raw))
return None
def set_value(self, val):
self.setText(str(val))
class FloatEntry(QtGui.QLineEdit):
def __init__(self, parent=None):
super(FloatEntry, self).__init__(parent)
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(FloatEntry, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(FloatEntry, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def returnPressed(self, *args, **kwargs):
val = self.get_value()
if val is not None:
self.set_text(str(val))
else:
log.warning("Could not interpret entry: %s" % self.text())
def get_value(self):
raw = str(self.text()).strip(' ')
try:
evaled = eval(raw)
except:
log.error("Could not evaluate: %s" % str(raw))
return None
return float(evaled)
def set_value(self, val):
self.setText("%.6f" % val)
class IntEntry(QtGui.QLineEdit):
def __init__(self, parent=None, allow_empty=False, empty_val=None):
super(IntEntry, self).__init__(parent)
self.allow_empty = allow_empty
self.empty_val = empty_val
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(IntEntry, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(IntEntry, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def get_value(self):
if self.allow_empty:
if str(self.text()) == "":
return self.empty_val
return int(self.text())
def set_value(self, val):
if val == self.empty_val and self.allow_empty:
self.setText("")
return
self.setText(str(val))
class FCEntry(QtGui.QLineEdit):
def __init__(self, parent=None):
super(FCEntry, self).__init__(parent)
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(FCEntry, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(FCEntry, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def get_value(self):
return str(self.text())
def set_value(self, val):
self.setText(str(val))
class EvalEntry(QtGui.QLineEdit):
def __init__(self, parent=None):
super(EvalEntry, self).__init__(parent)
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(EvalEntry, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(EvalEntry, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def returnPressed(self, *args, **kwargs):
val = self.get_value()
if val is not None:
self.setText(str(val))
else:
log.warning("Could not interpret entry: %s" % self.get_text())
def get_value(self):
raw = str(self.text()).strip(' ')
try:
return eval(raw)
except:
log.error("Could not evaluate: %s" % str(raw))
return None
def set_value(self, val):
self.setText(str(val))
class FCCheckBox(QtGui.QCheckBox):
def __init__(self, label='', parent=None):
super(FCCheckBox, self).__init__(str(label), parent)
def get_value(self):
return self.isChecked()
def set_value(self, val):
self.setChecked(val)
def toggle(self):
self.set_value(not self.get_value())
class FCTextArea(QtGui.QPlainTextEdit):
def __init__(self, parent=None):
super(FCTextArea, self).__init__(parent)
def set_value(self, val):
self.setPlainText(val)
def get_value(self):
return str(self.toPlainText())
class FCInputDialog(QtGui.QInputDialog):
def __init__(self, parent=None, ok=False, val=None):
super(FCInputDialog, self).__init__(parent)
self.allow_empty = ok
self.empty_val = val
self.readyToEdit = True
def mousePressEvent(self, e, Parent=None):
# required to deselect on 2nd click
super(FCInputDialog, self).mousePressEvent(e)
if self.readyToEdit:
self.selectAll()
self.readyToEdit = False
def focusOutEvent(self, e):
# required to remove cursor on focusOut
super(FCInputDialog, self).focusOutEvent(e)
self.deselect()
self.readyToEdit = True
def get_value(self, title=None, message=None, min=None, max=None, decimals=None):
if title is None:
title = "FlatCAM action"
if message is None:
message = "Please enter the value: "
if min is None:
min = 0.0
if max is None:
max = 100.0
if decimals is None:
decimals = 1
self.val,self.ok = self.getDouble(self, title, message, min=min,
max=max, decimals=decimals)
return [self.val,self.ok]
def set_value(self, val):
pass
class FCButton(QtGui.QPushButton):
def __init__(self, parent=None):
super(FCButton, self).__init__(parent)
def get_value(self):
return self.isChecked()
def set_value(self, val):
self.setText(str(val))
class VerticalScrollArea(QtGui.QScrollArea):
"""
This widget extends QtGui.QScrollArea to make a vertical-only
scroll area that also expands horizontally to accomodate
its contents.
"""
def __init__(self, parent=None):
QtGui.QScrollArea.__init__(self, parent=parent)
self.setWidgetResizable(True)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
def eventFilter(self, source, event):
"""
The event filter gets automatically installed when setWidget()
is called.
:param source:
:param event:
:return:
"""
if event.type() == QtCore.QEvent.Resize and source == self.widget():
# log.debug("VerticalScrollArea: Widget resized:")
# log.debug(" minimumSizeHint().width() = %d" % self.widget().minimumSizeHint().width())
# log.debug(" verticalScrollBar().width() = %d" % self.verticalScrollBar().width())
self.setMinimumWidth(self.widget().sizeHint().width() +
self.verticalScrollBar().sizeHint().width())
# if self.verticalScrollBar().isVisible():
# log.debug(" Scroll bar visible")
# self.setMinimumWidth(self.widget().minimumSizeHint().width() +
# self.verticalScrollBar().width())
# else:
# log.debug(" Scroll bar hidden")
# self.setMinimumWidth(self.widget().minimumSizeHint().width())
return QtGui.QWidget.eventFilter(self, source, event)
class OptionalInputSection:
def __init__(self, cb, optinputs):
"""
Associates the a checkbox with a set of inputs.
:param cb: Checkbox that enables the optional inputs.
:param optinputs: List of widgets that are optional.
:return:
"""
assert isinstance(cb, FCCheckBox), \
"Expected an FCCheckBox, got %s" % type(cb)
self.cb = cb
self.optinputs = optinputs
self.on_cb_change()
self.cb.stateChanged.connect(self.on_cb_change)
def on_cb_change(self):
if self.cb.checkState():
for widget in self.optinputs:
widget.setEnabled(True)
else:
for widget in self.optinputs:
widget.setEnabled(False)