class VoltageGlobalAdjust(VoltageGlobalAdjustForm, VoltageGlobalAdjustBase): updateOutput = QtCore.pyqtSignal(object, object) outputChannelsChanged = QtCore.pyqtSignal(object) _channelParams = {} def __init__(self, config, globalDict, parent=None): VoltageGlobalAdjustForm.__init__(self) VoltageGlobalAdjustBase.__init__(self, parent) self.config = config self.configname = 'VoltageGlobalAdjust.Settings' self.settings = self.config.get(self.configname, Settings()) self.globalAdjustDict = SequenceDict() self.myLabelList = list() self.myBoxList = list() self.historyCategory = 'VoltageGlobalAdjust' self.adjustHistoryName = None self.globalDict = globalDict self.adjustCache = self.config.get(self.configname + ".cache", dict()) self.savedValue = defaultdict(lambda: None) self.displayValueObservable = defaultdict(lambda: Observable()) def setupUi(self, parent): VoltageGlobalAdjustForm.setupUi(self, parent) self.gainBox.setValue(self.settings.gain) self.gainBox.valueChanged.connect(self.onGainChanged) self.tableModel = VoltageGlobalAdjustTableModel( self.globalAdjustDict, self.globalDict) self.tableView.setModel(self.tableModel) self.tableView.setSortingEnabled(True) # triggers sorting self.delegate = MagnitudeSpinBoxDelegate(self.globalDict) self.tableView.setItemDelegateForColumn(1, self.delegate) def onGainChanged(self, gain): self.settings.gain = gain self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def setupGlobalAdjust(self, name, adjustDict): if name != self.adjustHistoryName: self.adjustCache[self.adjustHistoryName] = [ v.data for v in list(self.globalAdjustDict.values()) ] self.settings.gainCache[ self.adjustHistoryName] = self.settings.gain self.settings.gain = self.settings.gainCache.get( name, self.settings.gain) if name in self.adjustCache: for data in self.adjustCache[name]: if data[0] in adjustDict: adjustDict[data[0]].data = data self.adjustHistoryName = name self.globalAdjustDict = adjustDict for name, adjust in self.globalAdjustDict.items(): try: adjust.valueChanged.connect(self.onValueChanged, QtCore.Qt.UniqueConnection) except: pass self.tableModel.setGlobalAdjust(adjustDict) self.outputChannelsChanged.emit(self.outputChannels()) self.gainBox.setValue(self.settings.gain) #self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def onValueChanged(self, name, value, string, origin): if origin == 'recalculate': self.tableModel.valueRecalcualted(name) self.globalAdjustDict[name]._value = float( self.globalAdjustDict[name]._value) self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def saveConfig(self): self.config[self.configname] = self.settings self.adjustCache[self.adjustHistoryName] = [ v.data for v in list(self.globalAdjustDict.values()) ] self.config[self.configname + ".cache"] = self.adjustCache def setValue(self, channel, value): self.globalAdjustDict[channel].value = value return value def getValue(self, channel): return self.globalAdjustDict[channel].value def currentValue(self, channel): return self.globalAdjustDict[channel].value def saveValue(self, channel): self.savedValue[channel] = self.globalAdjustDict[channel].value def restoreValue(self, channel): if self.savedValue[channel] is not None: self.globalAdjustDict[channel].value = self.savedValue[channel] return True def strValue(self, channel): adjust = self.globalAdjustDict[channel] return adjust.string if adjust.hasDependency else None def setStrValue(self, channel, value): pass def outputChannels(self): self._outputChannels = dict( ((channelName, VoltageOutputChannel( self, None, channelName, self.globalDict, )) for channelName in self.globalAdjustDict.keys())) return self._outputChannels
class InstrumentLoggingDisplayTableModel(QtCore.QAbstractTableModel): valueChanged = QtCore.pyqtSignal(str, object) def __init__(self, controlUi, config, parameterList=None, parent=None): super(InstrumentLoggingDisplayTableModel, self).__init__(parent) self.names = list() self.controlUi = controlUi self.config = config self.headerLookup = ['Name', 'Raw', 'Decimated', 'Calibrated'] self.dataLookup = { (QtCore.Qt.DisplayRole, 0): lambda row: self.data.keyAt(row), (QtCore.Qt.DisplayRole, 1): lambda row: str(self.data.at(row).raw), (QtCore.Qt.DisplayRole, 3): lambda row: str(self.data.at(row).calibrated), (QtCore.Qt.DisplayRole, 2): lambda row: str(self.data.at(row).decimated), (QtCore.Qt.FontRole, 0): lambda row: QtGui.QFont( "MS Shell Dlg 2", self.fontsizeCache[ (self.data.keyAt(row), 0)]), (QtCore.Qt.FontRole, 1): lambda row: QtGui.QFont( "MS Shell Dlg 2", self.fontsizeCache[ (self.data.keyAt(row), 1)]), (QtCore.Qt.FontRole, 2): lambda row: QtGui.QFont( "MS Shell Dlg 2", self.fontsizeCache[ (self.data.keyAt(row), 2)]), (QtCore.Qt.FontRole, 3): lambda row: QtGui.QFont( "MS Shell Dlg 2", self.fontsizeCache[ (self.data.keyAt(row), 3)]) } self.data = SequenceDict() self.fontsizeCache = self.config.get( "InstrumentLoggingDisplayTableModel.FontsizeCache", defaultdict(defaultFontsize)) self.inputChannels = dict() def resize(self, index, keyboardkey): if keyboardkey == QtCore.Qt.Key_Equal: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] = 10 elif keyboardkey == QtCore.Qt.Key_Plus: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] += 1 elif keyboardkey == QtCore.Qt.Key_Minus: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] -= 1 self.dataChanged.emit(index, index) def setInputChannels(self, inputChannels): self.beginResetModel() # drop everything that is not in the enabled parameter keys for key in list(self.data.keys()): if key not in inputChannels: self.data.pop(key) channel = self.inputChannels.pop(key) channel.newData.disconnect(self.updateHandler) for key, channel in inputChannels.items(): if key not in self.data: self.data[key] = InputData() channel.newData.connect(self.updateHandler) self.inputChannels[key] = channel self.endResetModel() def rowCount(self, parent=QtCore.QModelIndex()): return len(self.data) def columnCount(self, parent=QtCore.QModelIndex()): return 4 def data(self, index, role): if index.isValid(): return self.dataLookup.get((role, index.column()), lambda row: None)(index.row()) return None def flags(self, index): return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable def headerData(self, section, orientation, role): if (role == QtCore.Qt.DisplayRole) and (orientation == QtCore.Qt.Horizontal): return self.headerLookup[section] return None #QtCore.QVariant() def updateHandler(self, name, data): if name in self.data and data is not None: self.data[name].raw = data[1] index = self.data.index(name) leftInd = self.createIndex(index, 1) rightInd = self.createIndex(index, 3) self.dataChanged.emit(leftInd, rightInd) def update(self, key, value): if key in self.data: self.data[key].update(value) index = self.data.index(key) leftInd = self.createIndex(index, 1) rightInd = self.createIndex(index, 3) self.dataChanged.emit(leftInd, rightInd) def saveConfig(self): self.config[ "InstrumentLoggingDisplayTableModel.FontsizeCache"] = self.fontsizeCache
class InstrumentLoggingDisplayTableModel( QtCore.QAbstractTableModel ): valueChanged = QtCore.pyqtSignal(str, object) def __init__(self, controlUi, config, parameterList=None, parent=None): super(InstrumentLoggingDisplayTableModel, self).__init__(parent) self.names = list() self.controlUi = controlUi self.config = config self.headerLookup = ['Name', 'Raw', 'Decimated', 'Calibrated'] self.dataLookup = { (QtCore.Qt.DisplayRole, 0): lambda row: self.data.keyAt(row), (QtCore.Qt.DisplayRole, 1): lambda row: str(self.data.at(row).raw), (QtCore.Qt.DisplayRole, 3): lambda row: str(self.data.at(row).calibrated), (QtCore.Qt.DisplayRole, 2): lambda row: str(self.data.at(row).decimated), (QtCore.Qt.FontRole, 0): lambda row: QtGui.QFont("MS Shell Dlg 2", self.fontsizeCache[(self.data.keyAt(row), 0)]), (QtCore.Qt.FontRole, 1): lambda row: QtGui.QFont("MS Shell Dlg 2", self.fontsizeCache[(self.data.keyAt(row), 1)]), (QtCore.Qt.FontRole, 2): lambda row: QtGui.QFont("MS Shell Dlg 2", self.fontsizeCache[(self.data.keyAt(row), 2)]), (QtCore.Qt.FontRole, 3): lambda row: QtGui.QFont("MS Shell Dlg 2", self.fontsizeCache[(self.data.keyAt(row), 3)]) } self.data = SequenceDict() self.fontsizeCache = self.config.get("InstrumentLoggingDisplayTableModel.FontsizeCache", defaultdict(defaultFontsize)) self.inputChannels = dict() def resize(self, index, keyboardkey): if keyboardkey==QtCore.Qt.Key_Equal: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] = 10 elif keyboardkey==QtCore.Qt.Key_Plus: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] += 1 elif keyboardkey==QtCore.Qt.Key_Minus: self.fontsizeCache[(self.data.keyAt(index.row()), index.column())] -= 1 self.dataChanged.emit(index, index) def setInputChannels(self, inputChannels ): self.beginResetModel() # drop everything that is not in the enabled parameter keys for key in list(self.data.keys()): if key not in inputChannels: self.data.pop(key) channel = self.inputChannels.pop(key) channel.newData.disconnect(self.updateHandler) for key, channel in inputChannels.items(): if key not in self.data: self.data[key] = InputData() channel.newData.connect(self.updateHandler) self.inputChannels[key] = channel self.endResetModel() def rowCount(self, parent=QtCore.QModelIndex()): return len(self.data) def columnCount(self, parent=QtCore.QModelIndex()): return 4 def data(self, index, role): if index.isValid(): return self.dataLookup.get( (role, index.column()), lambda row: None)(index.row()) return None def flags(self, index ): return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable def headerData(self, section, orientation, role ): if (role == QtCore.Qt.DisplayRole) and (orientation == QtCore.Qt.Horizontal): return self.headerLookup[section] return None #QtCore.QVariant() def updateHandler(self, name, data): if name in self.data and data is not None: self.data[name].raw = data[1] index = self.data.index(name) leftInd = self.createIndex(index, 1) rightInd = self.createIndex(index, 3) self.dataChanged.emit(leftInd, rightInd) def update(self, key, value): if key in self.data: self.data[key].update(value) index = self.data.index(key) leftInd = self.createIndex(index, 1) rightInd = self.createIndex(index, 3) self.dataChanged.emit(leftInd, rightInd) def saveConfig(self): self.config["InstrumentLoggingDisplayTableModel.FontsizeCache"] = self.fontsizeCache
class VoltageGlobalAdjust(VoltageGlobalAdjustForm, VoltageGlobalAdjustBase ): updateOutput = QtCore.pyqtSignal(object, object) outputChannelsChanged = QtCore.pyqtSignal(object) _channelParams = {} def __init__(self, config, globalDict, parent=None): VoltageGlobalAdjustForm.__init__(self) VoltageGlobalAdjustBase.__init__(self, parent) self.config = config self.configname = 'VoltageGlobalAdjust.Settings' self.settings = self.config.get(self.configname, Settings()) self.globalAdjustDict = SequenceDict() self.myLabelList = list() self.myBoxList = list() self.historyCategory = 'VoltageGlobalAdjust' self.adjustHistoryName = None self.globalDict = globalDict self.adjustCache = self.config.get(self.configname+".cache", dict()) self.savedValue = defaultdict( lambda: None ) self.displayValueObservable = defaultdict( lambda: Observable() ) def setupUi(self, parent): VoltageGlobalAdjustForm.setupUi(self, parent) self.gainBox.setValue(self.settings.gain) self.gainBox.valueChanged.connect( self.onGainChanged ) self.tableModel = VoltageGlobalAdjustTableModel( self.globalAdjustDict, self.globalDict ) self.tableView.setModel( self.tableModel ) self.tableView.setSortingEnabled(True) # triggers sorting self.delegate = MagnitudeSpinBoxDelegate(self.globalDict) self.tableView.setItemDelegateForColumn(1, self.delegate) def onGainChanged(self, gain): self.settings.gain = gain self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def setupGlobalAdjust(self, name, adjustDict): if name!=self.adjustHistoryName: self.adjustCache[self.adjustHistoryName] = [v.data for v in list(self.globalAdjustDict.values())] self.settings.gainCache[self.adjustHistoryName] = self.settings.gain self.settings.gain = self.settings.gainCache.get( name, self.settings.gain ) if name in self.adjustCache: for data in self.adjustCache[name]: if data[0] in adjustDict: adjustDict[data[0]].data = data self.adjustHistoryName = name self.globalAdjustDict = adjustDict for name, adjust in self.globalAdjustDict.items(): try: adjust.valueChanged.connect(self.onValueChanged, QtCore.Qt.UniqueConnection) except: pass self.tableModel.setGlobalAdjust( adjustDict ) self.outputChannelsChanged.emit( self.outputChannels() ) self.gainBox.setValue(self.settings.gain) #self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def onValueChanged(self, name, value, string, origin): if origin=='recalculate': self.tableModel.valueRecalcualted(name) self.globalAdjustDict[name]._value = float(self.globalAdjustDict[name]._value) self.updateOutput.emit(self.globalAdjustDict, self.settings.gain) def saveConfig(self): self.config[self.configname] = self.settings self.adjustCache[self.adjustHistoryName] = [v.data for v in list(self.globalAdjustDict.values())] self.config[self.configname+".cache"] = self.adjustCache def setValue(self, channel, value): self.globalAdjustDict[channel].value = value return value def getValue(self, channel): return self.globalAdjustDict[channel].value def currentValue(self, channel): return self.globalAdjustDict[channel].value def saveValue(self, channel): self.savedValue[channel] = self.globalAdjustDict[channel].value def restoreValue(self, channel): if self.savedValue[channel] is not None: self.globalAdjustDict[channel].value = self.savedValue[channel] return True def strValue(self, channel): adjust = self.globalAdjustDict[channel] return adjust.string if adjust.hasDependency else None def setStrValue(self, channel, value): pass def outputChannels(self): self._outputChannels = dict(( (channelName, VoltageOutputChannel(self, None, channelName, self.globalDict, )) for channelName in self.globalAdjustDict.keys() )) return self._outputChannels