def createEditor(self, parent, options, idx): if not self._is_device_rc(idx): return super(MCTreeItemDelegate, self).createEditor(parent, options, idx) combo = QtWidgets.QComboBox(parent) combo = AutoPopupComboBox(parent) combo.addItem("RC on", True) combo.addItem("RC off", False) # Hack to make the combobox commit immediately after the user selects # an item. def on_combo_activated(index): self.commitData.emit(combo) self.closeEditor.emit(combo, QtWidgets.QAbstractItemDelegate.NoHint) combo.activated.connect(on_combo_activated) return combo
def __init__(self, bus=None, available_addresses=bm.ALL_DEVICE_ADDRESSES, known_idcs=list(), allow_custom_idcs=True, title="Add Device", selected_bus=None, selected_address=None, selected_idc=None, assigned_name=None, parent=None): """ Dialog constructor. If `bus' is given the dialog will use it as the selected bus and won't allow the user to change the bus. `available_addresses` should be a list of (bus, address) pairs. These address pairs will be available for the user to choose from. `known_idcs` should be a list of (idc, name) pairs. If `allow_custom_idcs` is True the user may enter a custom IDC, otherwise only IDCs from `known_idcs` are selectable. If `known_idcs' is empty and `allow_custom_idcs' is False this method will raise an exception. If `selected_bus` and `selected_address` is not None the corresponding item will be preselected in the GUI. If `selected_idc` is not None the device type will be set using the given IDC and will not be modifyable. """ if len(known_idcs) == 0 and not allow_custom_idcs: raise RuntimeError("No devices to choose from") if len(available_addresses) == 0 or ( bus is not None and util.ilen_destructive( filter(lambda x: x[0] == bus, available_addresses)) == 0): raise RuntimeError("No addresses available") super(AddDeviceDialog, self).__init__(parent) self.log = util.make_logging_source_adapter(__name__, self) self.setWindowTitle(title) self._result = None self.allow_custom_idcs = allow_custom_idcs self.bus_combo = QtWidgets.QComboBox() self.bus_combo.addItems([str(i) for i in bm.BUS_RANGE]) self.address_combos = [QtWidgets.QComboBox() for i in bm.BUS_RANGE] for b, a in sorted(available_addresses): self.address_combos[b].addItem("%X" % a, a) self.address_combo_stack = QtWidgets.QStackedWidget() for combo in self.address_combos: self.address_combo_stack.addWidget(combo) self.bus_combo.activated.connect( self.address_combo_stack.setCurrentIndex) self.idc_combo = EatReturnCombo() for idc, name in sorted(known_idcs, key=lambda x: x[1]): self.idc_combo.addItem("%s (%d)" % (name, idc), idc) if self.idc_combo.findData(idc) < 0: self.idc_combo.addItem(str(idc), idc) if selected_idc is not None: self.log.debug("selected_idc=%d, idx=%d", selected_idc, self.idc_combo.findData(selected_idc)) self.idc_combo.setCurrentIndex( self.idc_combo.findData(selected_idc)) self.idc_combo.setEnabled(False) if bus is not None: self.bus_combo.setCurrentIndex(bus) self.address_combo_stack.setCurrentIndex(bus) self.name_input = QtWidgets.QLineEdit() if assigned_name is not None: self.name_input.setText(assigned_name) self.button_box = QtWidgets.QDialogButtonBox( QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) if allow_custom_idcs: self.idc_combo.setEditable(True) self.idc_combo.setValidator(QtGui.QIntValidator(1, 99)) ok_button = self.button_box.button(QtWidgets.QDialogButtonBox.Ok) ok_button.setEnabled(len(known_idcs)) def combo_index_changed(): ok_button.setEnabled(True) def combo_le_text_edited(): ok_button.setEnabled( self.idc_combo.lineEdit().hasAcceptableInput()) self.idc_combo.currentIndexChanged.connect(combo_index_changed) self.idc_combo.lineEdit().textEdited.connect(combo_le_text_edited) if selected_bus is not None: assert selected_bus in bm.BUS_RANGE self.bus_combo.setCurrentIndex(selected_bus) self.address_combo_stack.setCurrentIndex(selected_bus) if selected_address is not None: assert selected_address in bm.DEV_RANGE combo = self.address_combo_stack.currentWidget() combo.setCurrentIndex(combo.findText("%X" % selected_address)) def accept(): bus = self.bus_combo.currentIndex() address = int(self.address_combos[bus].itemData( self.address_combos[bus].currentIndex())) if self.allow_custom_idcs and self.idc_combo.lineEdit( ).hasAcceptableInput(): idc = int(self.idc_combo.lineEdit().text()) else: idx = self.idc_combo.currentIndex() idc = int(self.idc_combo.itemData(idx)) name = self.name_input.text() self._result = AddDeviceDialog.Result(bus, address, idc, name) super(AddDeviceDialog, self).accept() self.button_box.accepted.connect(accept) self.button_box.rejected.connect(self.reject) layout = QtWidgets.QFormLayout(self) layout.addRow("Bus", self.bus_combo) layout.addRow("Address", self.address_combo_stack) layout.addRow("IDC", self.idc_combo) layout.addRow("Name", self.name_input) layout.addRow(self.button_box)