- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDigitalLockUi.py
More file actions
Latest commit
311 lines (266 loc) · 13.8 KB
/
Copy pathDigitalLockUi.py
File metadata and controls
311 lines (266 loc) · 13.8 KB
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
# *****************************************************************
# IonControl: Copyright 2016 Sandia Corporation
# This Software is released under the GPL license detailed
# in the file "license.txt" in the top-level IonControl directory
# *****************************************************************
importsys
importlogging
importos
importyaml
fromPyQt5importQtCore, QtGui, QtWidgets
importPyQt5.uic
frommylogging.ExceptionLogButtonimportExceptionLogButton
frommylogging.LoggerLevelsUiimportLoggerLevelsUi
frommyloggingimportLoggingSetup#@UnusedImport
fromdigitalLock.controller.ControllerClientimportController
fromguiimportSettingsDialog
frommodulesimportDataDirectory
frompersistimportconfigshelve
fromuiModulesimportMagnitudeParameter#@UnusedImport
fromdigitalLock.uiimportRepetitionRate_rc#@UnusedImport
fromProjectConfig.ProjectimportProject, ProjectInfoUi
fromtraceimportTraceui
fromtraceimportpens
fromdigitalLock.LockControlimportLockControl
fromdigitalLock.TraceControlimportTraceControl
fromdigitalLock.LockStatusimportLockStatus
frompyqtgraph.dockareaimportDockArea, Dock
fromuiModules.CoordinatePlotWidgetimportCoordinatePlotWidget
importctypes
setID=ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID
WidgetContainerForm, WidgetContainerBase=PyQt5.uic.loadUiType(r'digitalLock\ui\DigitalLockUi.ui')
classDigitalLockUi(WidgetContainerBase, WidgetContainerForm):
levelNameList= ["debug", "info", "warning", "error", "critical"]
levelValueList= [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]
plotConfigurationChanged=QtCore.pyqtSignal( object )
def__init__(self, config, project):
self.config=config
self.project=project
super(DigitalLockUi, self).__init__()
self.settings=SettingsDialog.Settings()
self.deviceSerial=config.get('Settings.deviceSerial')
self.deviceDescription=config.get('Settings.deviceDescription')
self.loggingLevel=config.get('Settings.loggingLevel', logging.INFO)
self.consoleMaximumLines=config.get('Settings.consoleMaximumLines', 0)
self.dockWidgetList=list()
ifself.loggingLevelnotinself.levelValueList: self.loggingLevel=logging.INFO
self.plotDict=dict()
def__enter__(self):
self.pulser=Controller()
returnself
def__exit__(self, excepttype, value, traceback):
self.pulser.shutdown()
returnFalse
defsetupUi(self, parent):
super(DigitalLockUi, self).setupUi(parent)
self.dockWidgetConsole.hide()
self.loggerUi=LoggerLevelsUi(self.config)
self.loggerUi.setupUi(self.loggerUi)
self.setupAsDockWidget(self.loggerUi, "Logging", QtCore.Qt.NoDockWidgetArea)
logger=logging.getLogger()
self.toolBar.addWidget(ExceptionLogButton())
# Setup Console Dockwidget
self.levelComboBox.addItems(self.levelNameList)
# self.levelComboBox.currentIndexChanged[int].connect( self.setLoggingLevel )
self.levelComboBox.setCurrentIndex( self.levelValueList.index(self.loggingLevel) )
# self.consoleClearButton.clicked.connect( self.onClearConsole )
# self.linesSpinBox.valueChanged.connect( self.onConsoleMaximumLinesChanged )
self.parent=parent
self.tabList=list()
self.tabDict=dict()
# initialize FPGA settings
self.settingsDialog=SettingsDialog.SettingsDialog(self.pulser, self.config, self.parent)
self.settingsDialog.setupUi()
self.settings=self.settingsDialog.settings
try:
configFile=os.path.splitext(self.settings.bitfile)[0] +'.yml'
withopen(configFile, 'r') asf:
lockConfig=yaml.load(f)
self.settings.onBoardADCEncoding=lockConfig['onBoardADCEncoding']
exceptExceptionase:
logger.error('unable to load config file: {0}'.format(e))
self.settings.onBoardADCEncoding=None
self.setupPlots()
# Traceui
self.penicons=pens.penicons().penicons()
self.traceui=Traceui.Traceui(self.penicons, self.config, "Main", self.plotDict)
self.traceui.setupUi(self.traceui)
self.setupAsDockWidget(self.traceui, "Traces", QtCore.Qt.LeftDockWidgetArea)
# lock status
self.lockStatus=LockStatus(self.pulser, self.config, self.traceui, self.plotDict, self.settings, self.parent)
self.lockStatus.setupUi()
self.setupAsDockWidget(self.lockStatus, "Status", QtCore.Qt.RightDockWidgetArea)
self.plotConfigurationChanged.connect( self.lockStatus.onPlotConfigurationChanged )
# Trace control
self.traceControl=TraceControl(self.pulser, self.config, self.traceui, self.plotDict, self.parent)
self.traceControl.setupUi()
self.setupAsDockWidget(self.traceControl, "Trace Control", QtCore.Qt.RightDockWidgetArea)
self.plotConfigurationChanged.connect( self.traceControl.onPlotConfigurationChanged )
# Lock control
self.lockControl=LockControl(self.pulser, self.config, self.settings, self.parent)
self.lockControl.dataChanged.connect( self.lockStatus.onControlChanged )
self.lockControl.dataChanged.connect( self.traceControl.onControlChanged )
self.lockControl.setupUi()
self.lockStatus.newDataAvailable.connect( self.lockControl.onStreamData )
self.traceControl.newDataAvailable.connect( self.lockControl.onTraceData )
self.setupAsDockWidget(self.lockControl, "Control", QtCore.Qt.RightDockWidgetArea)
self.actionSave.triggered.connect(self.onSave)
self.actionSettings.triggered.connect(self.onSettings)
self.actionExit.triggered.connect(self.onClose)
self.actionProject.triggered.connect( self.onProjectSelection)
self.addPlot=QtWidgets.QAction( QtGui.QIcon(":/openicon/icons/add-plot.png"), "Add new plot", self)
self.addPlot.setToolTip("Add new plot")
self.addPlot.triggered.connect(self.onAddPlot)
self.toolBar.addAction(self.addPlot)
self.removePlot=QtWidgets.QAction( QtGui.QIcon(":/openicon/icons/remove-plot.png"), "Remove a plot", self)
self.removePlot.setToolTip("Remove a plot")
self.removePlot.triggered.connect(self.onRemovePlot)
self.toolBar.addAction(self.removePlot)
self.renamePlot=QtWidgets.QAction( QtGui.QIcon(":/openicon/icons/rename-plot.png"), "Rename a plot", self)
self.renamePlot.setToolTip("Rename a plot")
self.renamePlot.triggered.connect(self.onRenamePlot)
self.toolBar.addAction(self.renamePlot)
self.setWindowTitle( "Digital Lock ({0})".format(self.project) )
if'MainWindow.State'inself.config:
self.parent.restoreState(self.config['MainWindow.State'])
self.initMenu()
try:
if'pyqtgraph-dockareastate'inself.config:
self.area.restoreState(self.config['pyqtgraph-dockareastate'])
exceptExceptionase:
logger.error("Cannot restore dock state in experiment {0}. Exception occurred: ".format(self.experimentName) +str(e))
QtCore.QTimer.singleShot(300000, self.onCommitConfig )
defonCommitConfig(self):
self.saveConfig()
self.config.saveConfig()
QtCore.QTimer.singleShot(300000, self.onCommitConfig )
defsetupPlots(self):
self.area=DockArea()
self.setCentralWidget(self.area)
self.plotDict=dict()
# initialize all the plot windows we want
plotNames=self.config.get( 'PlotNames', ['History', 'Scope'] )
iflen(plotNames)<1:
plotNames.append('History')
fornameinplotNames:
dock=Dock(name)
widget=CoordinatePlotWidget(self)
view=widget._graphicsView
self.area.addDock(dock, "bottom")
dock.addWidget(widget)
self.plotDict[name] = {"dock":dock, "widget":widget, "view":view}
defsetupAsDockWidget(self, widget, name, area=QtCore.Qt.RightDockWidgetArea, stackAbove=None, stackBelow=None ):
dock=QtWidgets.QDockWidget(name)
dock.setObjectName(name)
dock.setWidget( widget )
self.addDockWidget(area, dock )
self.dockWidgetList.append( dock )
ifstackAboveisnotNone:
self.tabifyDockWidget( stackAbove, dock )
elifstackBelowisnotNone:
self.tabifyDockWidget( dock, stackBelow )
returndock
defonAddPlot(self):
name, ok=QtWidgets.QInputDialog.getText(self, 'Plot Name', 'Please enter a plot name: ')
ifok:
name=str(name)
dock=Dock(name)
widget=CoordinatePlotWidget(self)
view=widget._graphicsView
self.area.addDock(dock, "bottom")
dock.addWidget(widget)
self.plotDict[name] = {"dock":dock, "widget":widget, "view":view}
self.plotConfigurationChanged.emit( self.plotDict )
defonRemovePlot(self):
logger=logging.getLogger(__name__)
iflen(self.plotDict) >0:
name, ok=QtWidgets.QInputDialog.getItem(self, "Select Plot", "Please select which plot to remove: ", list(self.plotDict.keys()), editable=False)
ifok:
name=str(name)
self.plotDict[name]["dock"].close()
delself.plotDict[name]
self.plotConfigurationChanged.emit( self.plotDict )
else:
logger.info("There are no plots which can be removed")
defonRenamePlot(self):
logger=logging.getLogger(__name__)
iflen(self.plotDict) >0:
name, ok=QtWidgets.QInputDialog.getItem(self, "Select Plot", "Please select which plot to rename: ", list(self.plotDict.keys()), editable=False)
ifok:
newName, newOk=QtWidgets.QInputDialog.getText(self, 'New Plot Name', 'Please enter a new plot name: ')
ifnewOk:
name=str(name)
newName=str(newName)
self.plotDict[name]["dock"].label.setText(str(newName))
self.plotDict[newName] =self.plotDict[name]
delself.plotDict[name]
self.plotConfigurationChanged.emit( self.plotDict )
else:
logger.info("There are no plots which can be renamed")
defonProjectSelection(self):
ui=ProjectInfoUi(self.project)
ui.show()
ui.exec_()
defonSettings(self):
self.settingsDialog.show()
defonSave(self):
logger=logging.getLogger(__name__)
logger.info( "Saving config" )
filename, _=DataDirectory.DataDirectory().sequencefile("digitalLock-configuration.db")
self.saveConfig()
self.config.saveConfig(filename)
defonMessageWrite(self,message,level=logging.DEBUG):
iflevel>=self.loggingLevel:
cursor=self.textEditConsole.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
iflevel<logging.ERROR:
self.textEditConsole.setTextColor(QtCore.Qt.black)
else:
self.textEditConsole.setTextColor(QtCore.Qt.red)
cursor.insertText(message)
self.textEditConsole.setTextCursor(cursor)
self.textEditConsole.ensureCursorVisible()
defonClose(self):
self.parent.close()
defcloseEvent(self, e):
logger=logging.getLogger("")
logger.debug( "Saving Configuration" )
self.saveConfig()
self.settingsDialog.done(0)
self.lockControl.closeEvent(e)
definitMenu(self):
self.menuView.clear()
fordockinself.dockWidgetList:
self.menuView.addAction(dock.toggleViewAction())
defsaveConfig(self):
self.config['MainWindow.State'] =self.parent.saveState()
fortabinself.tabList:
tab.saveConfig()
self.config['Settings.deviceSerial'] =self.settings.deviceSerial
self.config['Settings.deviceDescription'] =self.settings.deviceDescription
self.config['MainWindow.pos'] =self.pos()
self.config['MainWindow.size'] =self.size()
self.config['Settings.loggingLevel'] =self.loggingLevel
self.config['Settings.consoleMaximumLines'] =self.consoleMaximumLines
self.config['PlotNames'] =list(self.plotDict.keys())
self.config['pyqtgraph-dockareastate'] =self.area.saveState()
self.settingsDialog.saveConfig()
self.loggerUi.saveConfig()
self.lockControl.saveConfig()
self.lockStatus.saveConfig()
self.traceControl.saveConfig()
if__name__=="__main__":
app=QtWidgets.QApplication(sys.argv)
project=Project() #loads in the project through the config files/config GUIs
logger=logging.getLogger("")
setID('TrappedIons.DigitalLockUi') #Makes the icon in the Windows taskbar match the icon set in Qt Designer
overrideConfigFile=project.projectConfig.get('configurationFile')
overrideFileType= {'.yml': 'yaml', '.yaml': 'yaml', '.db': 'sqlite'}.get(os.path.splitext(overrideConfigFile)[1], 'sqlite') ifoverrideConfigFileelseNone
loadFromDate=project.projectConfig.get('configurationFile')
withconfigshelve.configshelve(project.dbConnection, loadFromDate=project.projectConfig.get('loadFromDateTime', None),
filename=overrideConfigFile, filetype=overrideFileType) asconfig:
withDigitalLockUi(config, project) asui:
ui.setupUi(ui)
LoggingSetup.qtHandler.textWritten.connect(ui.onMessageWrite)
ui.show()
sys.exit(app.exec_())