def Build_Tab(group, source_key, default, projector, projectordb, edit=False): """ Create the radio button page for a tab. Dictionary will be a 1-key entry where key=tab to setup, val=list of inputs. source_key: {"groupkey1": {"key11": "key11-text", "key12": "key12-text", ... }, "groupkey2": {"key21": "key21-text", "key22": "key22-text", .... }, ... } :param group: Button group widget to add buttons to :param source_key: Dictionary of sources for radio buttons :param default: Default radio button to check :param projector: Projector instance :param projectordb: ProjectorDB instance for session :param edit: If we're editing the source text """ buttonchecked = False widget = QWidget() layout = QFormLayout() if edit else QVBoxLayout() layout.setSpacing(10) widget.setLayout(layout) tempkey = list(source_key.keys())[0] # Should only be 1 key sourcelist = list(source_key[tempkey]) sourcelist.sort() button_count = len(sourcelist) if edit: for key in sourcelist: item = QLineEdit() item.setObjectName('source_key_%s' % key) source_item = projectordb.get_source_by_code(code=key, projector_id=projector.db_item.id) if source_item is None: item.setText(PJLINK_DEFAULT_CODES[key]) else: item.setText(source_item.text) layout.addRow(PJLINK_DEFAULT_CODES[key], item) group.append(item) else: for key in sourcelist: source_item = projectordb.get_source_by_code(code=key, projector_id=projector.db_item.id) if source_item is None: text = source_key[tempkey][key] else: text = source_item.text itemwidget = QRadioButton(text) itemwidget.setAutoExclusive(True) if default == key: itemwidget.setChecked(True) buttonchecked = itemwidget.isChecked() or buttonchecked group.addButton(itemwidget, int(key)) layout.addWidget(itemwidget) layout.addStretch() return widget, button_count, buttonchecked
class SourceSelectSingle(QDialog): """ Class for handling selecting the source for the projector to use. Uses single dialog interface. """ def __init__(self, parent, projectordb, edit=False): """ Build the source select dialog. :param projectordb: ProjectorDB session to use """ log.debug('Initializing SourceSelectSingle()') self.projectordb = projectordb super(SourceSelectSingle, self).__init__(parent) self.edit = edit if self.edit: title = translate('OpenLP.SourceSelectForm', 'Edit Projector Source Text') else: title = translate('OpenLP.SourceSelectForm', 'Select Projector Source') self.setObjectName('source_select_single') self.setWindowIcon(build_icon(':/icon/openlp-log-32x32.png')) self.setModal(True) self.edit = edit def exec_(self, projector, edit=False): """ Override initial method so we can build the tabs. :param projector: Projector instance to build source list from """ self.projector = projector self.layout = QFormLayout() if self.edit else QVBoxLayout() self.layout.setObjectName('source_select_tabs_layout') self.layout.setSpacing(10) self.setLayout(self.layout) self.setMinimumWidth(350) self.button_group = [] if self.edit else QButtonGroup() self.source_text = self.projectordb.get_source_list(projector=projector) keys = list(self.source_text.keys()) keys.sort() key_count = len(keys) button_list = [] if self.edit: for key in keys: item = QLineEdit() item.setObjectName('source_key_%s' % key) source_item = self.projectordb.get_source_by_code(code=key, projector_id=self.projector.db_item.id) if source_item is None: item.setText(PJLINK_DEFAULT_CODES[key]) else: item.old_text = item.text() item.setText(source_item.text) self.layout.addRow(PJLINK_DEFAULT_CODES[key], item) self.button_group.append(item) self.button_box = QDialogButtonBox(QtGui.QDialogButtonBox.Reset | QtGui.QDialogButtonBox.Discard | QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) else: for key in keys: source_text = self.projectordb.get_source_by_code(code=key, projector_id=self.projector.db_item.id) text = self.source_text[key] if source_text is None else source_text.text button = QtGui.QRadioButton(text) button.setChecked(True if key == projector.source else False) self.layout.addWidget(button) self.button_group.addButton(button, int(key)) button_list.append(key) self.button_box = QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) self.button_box.clicked.connect(self.button_clicked) self.layout.addWidget(self.button_box) self.setMinimumHeight(key_count*25) set_button_tooltip(self.button_box) selected = super(SourceSelectSingle, self).exec_() return selected @pyqtSlot(object) def button_clicked(self, button): """ Checks which button was clicked :param button: Button that was clicked :returns: Ok: Saves current edits Delete: Resets text to last-saved text Reset: Reset all text to PJLink default text Cancel: Cancel text edit """ if self.button_box.standardButton(button) == self.button_box.Cancel: self.done(0) elif self.button_box.standardButton(button) == self.button_box.Reset: self.done(100) elif self.button_box.standardButton(button) == self.button_box.Discard: self.delete_sources() elif self.button_box.standardButton(button) == self.button_box.Ok: return self.accept_me() else: return 100 def delete_sources(self): msg = QtGui.QMessageBox() msg.setText(translate('OpenLP.SourceSelectForm', 'Delete entries for this projector')) msg.setInformativeText(translate('OpenLP.SourceSelectForm', 'Are you sure you want to delete ALL user-defined ' 'source input text for this projector?')) msg.setStandardButtons(msg.Cancel | msg.Ok) msg.setDefaultButton(msg.Cancel) ans = msg.exec_() if ans == msg.Cancel: return self.projectordb.delete_all_objects(ProjectorSource, ProjectorSource.projector_id == self.projector.db_item.id) self.done(100) @pyqtSlot() def accept_me(self): """ Slot to accept 'OK' button """ projector = self.projector.db_item if self.edit: for key in self.button_group: code = key.objectName().split("_")[-1] text = key.text().strip() if key.text().strip().lower() == PJLINK_DEFAULT_CODES[code].strip().lower(): continue item = self.projectordb.get_source_by_code(code=code, projector_id=projector.id) if item is None: log.debug("(%s) Adding new source text %s: %s" % (projector.ip, code, text)) item = ProjectorSource(projector_id=projector.id, code=code, text=text) else: item.text = text log.debug('(%s) Updating source code %s with text="%s"' % (projector.ip, item.code, item.text)) self.projectordb.add_source(item) selected = 0 else: selected = self.button_group.checkedId() log.debug('SourceSelectDialog().accepted() Setting source to %s' % selected) self.done(selected)