-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomwidgets.py
312 lines (269 loc) · 9.37 KB
/
customwidgets.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
'''
A Modules for customized widgets that enable the actual displays
'''
import os
from os.path import join
from inspect import getmembers, isclass
from importlib.util import spec_from_file_location #, module_from_spec
from PyQt5 import QtCore
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import QLabel, QMainWindow, QListWidgetItem, QDoubleSpinBox, QWidget
from PyQt5.QtWidgets import QTreeWidgetItem
import pyqtgraph as pg
from Interfaces.Base_Recipe_Dialog import Ui_RecipeDialog
from Interfaces.Base_Tip_Selection_Dialog import Ui_TipSelectionDialog
import recipe
class CustomViewBox(pg.ViewBox):
'''
Viewbox that allows for selecting range, taken from PyQtGraphs documented examples
'''
def __init__(self, *args, **kwds):
kwds['enableMenu'] = False
pg.ViewBox.__init__(self, *args, **kwds)
self.setMouseMode(self.RectMode)
#
## reimplement right-click to zoom out
def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
self.autoRange()
#
## reimplement mouseDragEvent to disable continuous axis zoom
def mouseDragEvent(self, ev, axis=None):
if axis is not None and ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev, axis=axis)
#
#
class BaseMainWindow(QMainWindow):
'''
Need this stupid wrapper because Qt Designer does not inhert from QMainWindow so the references
get convoluted. Call thisInstance.setSubRef(self) from the class inheriting from QtDesigner and
then this base can pass events to that instance, such as the closeEvent.
'''
def setSubRef(self, ref):
'''
Call this when setting up the subclass (inherited from the Qt Designer code) in order to
reference it from the main Window itself.
'''
self.UIsubclass = ref
#
def closeEvent(self, event):
if hasattr(self, 'UIsubclass'):
self.UIsubclass.closeEvent(event)
else:
event.accept()
#
#
class BaseStatusWidget(QWidget):
'''
Need this stupid wrapper because Qt Designer does not inhert from QWidget so the references
get convoluted. Call thisInstance.setGUIRef(self) from the class inheriting from QtDesigner and
then this base can pass events to the mainWindow
'''
def setGUIRef(self, ref):
'''
Call this when setting up the subclass (inherited from the Qt Designer code) in order to
reference it from the main Window itself.
'''
self.GUIref = ref
#
def closeEvent(self, event):
if hasattr(self, 'GUIref'):
self.GUIref.close()
event.ignore()
else:
event.accept()
#
#
class RecipeDialog(Ui_RecipeDialog):
'''
A Dialog box to select the Recipe to load.
'''
def loadRecipes(self, directory):
recipe_members = dict(getmembers(recipe, isclass))
self.items = dict()
for filename in os.listdir(directory):
if filename.endswith('.py') and filename != "__init__.py":
spec = spec_from_file_location(filename,join(directory, filename))
module = spec.loader.load_module()
for name, obj in getmembers(module, isclass):
if name not in recipe_members:
key = name.replace('_', ' ')
self.items[key] = obj
self.recipeListWidget.addItem(QListWidgetItem(key))
#
def setupUi(self, parent):
super().setupUi(parent)
self.cancelled = True
self.parent = parent
self.loadButton.clicked.connect(self.loadCallback)
self.cancelButton.clicked.connect(self.cancelCallback)
self.loadLastCheckBox.toggled.connect(self.loadLastCallback)
self.loadSpecificCheckBox.toggled.connect(self.loadSpecificCallback)
self.load_last = True
self.parent.setWindowIcon(QIcon(join('Interfaces','images','squid_tip.png')))
#
def getRecipe(self):
'''
Return the recipe class, returns None if cancelled.
'''
if self.cancelled:
return None
key = self.recipeListWidget.currentItem().text()
return self.items[key]
#
def getLoadState(self):
'''
Get the options for loading the previous parameters.
Returns None is the parameters of the last run are to be used. Returns the SQUID name
for loading a specific SQUID.
'''
if self.load_last:
return None
else:
return str(self.loadSpecificLineEdit.text())
#
def loadCallback(self):
self.cancelled = False
self.parent.close()
#
def cancelCallback(self):
self.cancelled = True
self.parent.close()
#
def loadLastCallback(self):
if self.loadLastCheckBox.isChecked():
self.loadSpecificCheckBox.setChecked(False)
self.load_last = True
#
def loadSpecificCallback(self):
if self.loadSpecificCheckBox.isChecked():
self.loadLastCheckBox.setChecked(False)
self.load_last = False
#
#
class TipSelectionDialog(Ui_TipSelectionDialog):
'''
A Dialog box to select a tip to load data from
'''
def setupUi(self, parent):
super().setupUi(parent)
self.cancelled = True
self.parent = parent
self.loadButton.clicked.connect(self.loadCallback)
self.cancelButton.clicked.connect(self.cancelCallback)
self.parent.setWindowIcon(QIcon(join('Interfaces','images','squid_tip.png')))
self.loadTips()
#
def loadTips(self, directory='..\database'):
self.treeWidget.setHeaderLabels(["Select a Deposition"])
for file in os.listdir(directory):
if file.endswith(".csv"):
name = file.replace('_params.csv','')
name = name.replace('_v', ' v')
name = name.replace('_', ' ')
name = name.replace('-', '.')
recipeItem = QTreeWidgetItem([name])
with open(join(directory, file), 'r') as reader:
lines = reader.readlines()
for i in range(1,len(lines)):
ln = lines[i].split(',')
tip = QTreeWidgetItem([ln[1]])
recipeItem.addChild(tip)
self.treeWidget.addTopLevelItem(recipeItem)
#
#
def getTip(self):
'''
Return the recipe class, returns None if cancelled.
'''
if self.cancelled:
return None, None
else:
current = self.treeWidget.currentItem()
parent = current.parent()
if parent is None: # If you just selected a recipe, don't load
return None, None
else:
recipe = parent.text(0)
recipe = recipe.replace(' v', '_v')
recipe = recipe.replace(' ', '_')
recipe = recipe.replace('.', '-')
return recipe, current.text(0)
#
def loadCallback(self):
self.cancelled = False
self.parent.close()
#
def cancelCallback(self):
self.cancelled = True
self.parent.close()
#
#
class CustomSpinBox(QDoubleSpinBox):
def textFromValue(self, value):
return str(value)
#
#
class VarEntry(QWidget):
'''
A simple widget to display a value with a label
'''
def __init__(self, parent, label, units="", width=300, height=35, labelwidth=150, valuewidth=100, unitswidth=50):
super().__init__(parent)
self.setMaximumSize(labelwidth+valuewidth+unitswidth, height)
self.value = 0.0
self.precision = 3
font = QFont()
font.setPointSize(14)
self.staticLabel = QLabel(self)
self.staticLabel.setGeometry(QtCore.QRect(0, 5, labelwidth, height-10))
self.staticLabel.setFont(font)
#self.staticLabel.setStyleSheet("background-color: green")
self.dynamicLabel = QLabel(self)
self.dynamicLabel.setGeometry(QtCore.QRect(labelwidth, 5, valuewidth, height-10))
self.dynamicLabel.setFont(font)
self.setLabel(label)
#self.dynamicLabel.setStyleSheet("background-color: blue")
self.unitsLabel = QLabel(self)
self.unitsLabel.setGeometry(QtCore.QRect(labelwidth+valuewidth, 5, unitswidth, height-10))
self.unitsLabel.setFont(font)
self.setUnits(units)
#self.unitsLabel.setStyleSheet("background-color: yellow")
self.show()
#
def setLabel(self, lbl):
'''
Set the text of the label.
Args:
lbl (str) : The Label
'''
self.label = lbl
self.staticLabel.setText(str(lbl))
def setValue(self, val):
'''
Set the numeric value of the label.
Args:
val (float) : The value
'''
self.value = val
if isinstance(val, float):
if val <= 0.001 and val != 0.0:
s = "{:.2E}".format(val)
else:
s = str(round(val, self.precision))
else:
s = str(val)
self.dynamicLabel.setText(s)
#
def setUnits(self, unit):
'''
Set the units of the value
Args:
unit (str) : The unit
'''
self.units = unit
self.unitsLabel.setText(str(self.units))
#
#