class AliasWidget(QFrame): def __init__(self, parent, name, selections, preselect): QFrame.__init__(self, parent) self.name = name self.selections = selections layout = QHBoxLayout() layout.addWidget(QLabel(name, self)) self.combo = QComboBox(self) self.combo.addItems(selections) if preselect in selections: self.combo.setCurrentIndex(selections.index(preselect)) else: self.combo.setCurrentIndex(0) layout.addWidget(self.combo) layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) def setSelections(self, selections, preselect): if selections != self.selections: self.selections = selections self.combo.clear() self.combo.addItems(selections) if preselect in selections: self.combo.setCurrentIndex(selections.index(preselect)) else: self.combo.setCurrentIndex(0) def getSelection(self): return self.combo.currentText()
class Detector(MeasElement): """Element for selecting detector distance, depending on selector.""" CACHE_KEY = 'detector/presets' SORT_KEY = lambda self, x: num_sort(x) LABEL = 'Detector' _allvalues = None def clientUpdate(self, client): self._allvalues = client.getDeviceParam(*self.CACHE_KEY.split('/')) self._values = [] def createWidget(self, parent, client): self.clientUpdate(client) self._widget = QComboBox(parent) self._updateWidget() self._widget.currentIndexChanged.connect(self._updateValue) return self._widget def _updateWidget(self): self._widget.clear() self._widget.addItems(self._values) if self.value in self._values: self._widget.setCurrentIndex(self._values.index(self.value)) def otherChanged(self, eltype, value): if eltype == 'selector' and self._allvalues is not None: self._values = sorted(self._allvalues[value], key=self.SORT_KEY) if self.value not in self._values: if self._values: self.value = self._values[0] else: self.value = None if self._widget is not None: self._updateWidget() def _updateValue(self, index): self.value = self._values[index] self.changed.emit(self.value)