def _handle_text(self, layout, row): param = self.controls.get(row, None) if param is None: label = QLabel() label.setOpenExternalLinks(True) label.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) if row.startswith('info:'): label.setText(row[5:]) font = QFont() font.setPointSize(10) label.setFont(font) label.setAccessibleName('info') elif row.startswith('h1:'): label.setText(row[3:]) font = QFont() font.setPointSize(17) font.setBold(True) label.setFont(font) label.setAccessibleName('h1') elif row.startswith('h2:'): label.setText(row[3:]) font = QFont() font.setPointSize(16) font.setBold(True) label.setFont(font) label.setAccessibleName('h2') elif row.startswith('h3:'): label.setText(row[3:]) font = QFont() font.setPointSize(15) font.setBold(True) label.setFont(font) label.setAccessibleName('h3') elif row.startswith('h4:'): label.setText(row[3:]) font = QFont() font.setPointSize(14) font.setBold(True) label.setFont(font) label.setAccessibleName('h4') elif row.startswith('h5:'): label.setText(row[3:]) font = QFont() font.setPointSize(12) font.setBold(True) label.setFont(font) label.setAccessibleName('h5') else: label.setText(row) label.setAccessibleName('msg') label.setToolTip(label.text()) layout.addWidget(label) else: param.parent = self param.name = row layout.addWidget(param.form)
def generate_panel(self, formset): """ Generate a panel for the module form with all the controls formset format example: [('_video', '_arenas', '_run'), {"Player":['_threshold', "_player", "=", "_results", "_query"], "Background image":[(' ', '_selectBackground', '_paintBackground'), '_image']}, "_progress"] tuple: will display the controls in the same horizontal line list: will display the controls in the same vertical line dict: will display the controls in a tab widget '||': will plit the controls in a horizontal line '=': will plit the controls in a vertical line @param formset: Form configuration @type formset: list """ control = None if '=' in formset or isinstance(formset, hsplitter): control = QSplitter(QtCore.Qt.Vertical) index = list(formset).index('=') first_panel = self.generate_panel(formset[0:index]) second_panel = self.generate_panel(formset[index + 1:]) control.addWidget(first_panel) control.addWidget(second_panel) self._splitters.append(control) return control elif '||' in formset or isinstance(formset, vsplitter): control = QSplitter(QtCore.Qt.Horizontal) index = list(formset).index('||') first_panel = self.generate_panel(formset[0:index]) second_panel = self.generate_panel(formset[index + 1:]) control.addWidget(first_panel) control.addWidget(second_panel) if isinstance(formset, vsplitter): sizes = [formset.left_width, formset.right_width] control.setSizes(sizes) self._splitters.append(control) return control control = QFrame(self) layout = None if type(formset) is tuple: layout = QHBoxLayout() for row in formset: if isinstance(row, (list, tuple, vsplitter, hsplitter)): panel = self.generate_panel(row) layout.addWidget(panel) elif row == " ": spacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) layout.addItem(spacer) elif type(row) is dict: c = self.generate_tabs(row) layout.addWidget(c) self._tabs.append(c) else: param = self.controls.get(row, None) if param is None: label = QLabel() label.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) # layout.addWidget( label ) if row.startswith('info:'): label.setText(row[5:]) font = QFont() font.setPointSize(10) label.setFont(font) label.setAccessibleName('info') elif row.startswith('h1:'): label.setText(row[3:]) font = QFont() font.setPointSize(17) font.setBold(True) label.setFont(font) label.setAccessibleName('h1') elif row.startswith('h2:'): label.setText(row[3:]) font = QFont() font.setPointSize(16) font.setBold(True) label.setFont(font) label.setAccessibleName('h2') elif row.startswith('h3:'): label.setText(row[3:]) font = QFont() font.setPointSize(15) font.setBold(True) label.setFont(font) label.setAccessibleName('h3') elif row.startswith('h4:'): label.setText(row[3:]) font = QFont() font.setPointSize(14) font.setBold(True) label.setFont(font) label.setAccessibleName('h4') elif row.startswith('h5:'): label.setText(row[3:]) font = QFont() font.setPointSize(12) font.setBold(True) label.setFont(font) label.setAccessibleName('h5') else: label.setText(row) font = QFont() font.setPointSize(10) label.setFont(font) label.setAccessibleName('msg') label.setToolTip(label.text()) layout.addWidget(label) else: param.parent = self param.name = row layout.addWidget(param.form) elif type(formset) is list: layout = QVBoxLayout() for row in formset: if isinstance(row, (list, tuple, vsplitter, hsplitter)): panel = self.generate_panel(row) layout.addWidget(panel) elif row == " ": spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) layout.addItem(spacer) elif type(row) is dict: c = self.generate_tabs(row) layout.addWidget(c) self._tabs.append(c) else: param = self.controls.get(row, None) if param is None: label = QLabel() label.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) label.resize(30, 30) # layout.addWidget( label ) if row.startswith('info:'): label.setText(row[5:]) font = QFont() font.setPointSize(10) label.setFont(font) label.setAccessibleName('info') elif row.startswith('h1:'): label.setText(row[3:]) font = QFont() font.setPointSize(17) font.setBold(True) label.setFont(font) label.setAccessibleName('h1') elif row.startswith('h2:'): label.setText(row[3:]) font = QFont() font.setPointSize(16) font.setBold(True) label.setFont(font) label.setAccessibleName('h2') elif row.startswith('h3:'): label.setText(row[3:]) font = QFont() font.setPointSize(15) font.setBold(True) label.setFont(font) label.setAccessibleName('h3') elif row.startswith('h4:'): label.setText(row[3:]) font = QFont() font.setPointSize(14) font.setBold(True) label.setFont(font) label.setAccessibleName('h4') elif row.startswith('h5:'): label.setText(row[3:]) font = QFont() font.setPointSize(12) font.setBold(True) label.setFont(font) label.setAccessibleName('h5') else: label.setText(row) font = QFont() font.setPointSize(10) label.setFont(font) label.setAccessibleName('msg') label.setToolTip(label.text()) layout.addWidget(label) else: param.parent = self param.name = row layout.addWidget(param.form) if _api.USED_API == _api.QT_API_PYQT5: layout.setContentsMargins(0, 0, 0, 0) elif _api.USED_API == _api.QT_API_PYQT4: layout.setMargin(0) control.setLayout(layout) return control
class SelectEncodingsWidget(QWidget): """ Popup widget for selecting a set of text encodings for use in other parts of the GUI. """ def __init__(self, *args, headingText="", **kwargs): super().__init__(*args, **kwargs) self.setLayout(QVBoxLayout()) self.__top_label = QLabel(headingText, visible=bool(headingText), objectName="-top-heading-text") self.__top_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) self.__model = model = encodings_model() self.__model.setParent(self) self.__view = view = EncodingsView( self, uniformItemSizes=True, editTriggers=QListView.NoEditTriggers) self.__view.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred) view.setModel(model) self.layout().addWidget(self.__top_label) self.layout().addWidget(view) buttons = QDialogButtonBox( standardButtons=QDialogButtonBox.RestoreDefaults) b = buttons.addButton("Select all", QDialogButtonBox.ActionRole) b.clicked.connect(self.selectAll) b = buttons.button(QDialogButtonBox.RestoreDefaults) b.clicked.connect(self.reset) self.layout().addWidget(buttons) self.setAttribute(Qt.WA_MacSmallSize) def headingText(self): # type: () -> str return self.__top_label.text() def setHeadingText(self, text): # type: (str) -> None self.__top_label.setText(text) self.__top_label.setVisible(bool(text)) def setModel(self, model): if self.__model is not None: if self.__model.parent() is self: self.__model.deleteLater() self.__model = None self.__view.setModel(model) self.__model = model def model(self): return self.__model def selectedEncodings(self): # type: () -> List[str] """ Return a list of currently selected (checked) encodings. """ model = self.__model res = [] for i in range(model.rowCount()): data = model.itemData(model.index(i, 0)) if data.get(Qt.CheckStateRole) == Qt.Checked and \ EncodingNameRole in data: res.append(data[EncodingNameRole]) return res @Slot() def selectAll(self): """ Select all encodings. """ model = self.__model for i in range(model.rowCount()): item = model.item(i) item.setCheckState(Qt.Checked) @Slot() def clearAll(self): """ Clear (uncheck) all encodings. """ model = self.__model for i in range(model.rowCount()): item = model.item(i) item.setCheckState(Qt.Checked) @Slot() def reset(self): """ Reset the encodings model to the default selected set. """ model = self.__model for i in range(model.rowCount()): item = model.item(i) co = item.data(CodecInfoRole) if isinstance(co, codecs.CodecInfo): state = co.name in DEFAULT_ENCODINGS item.setCheckState(Qt.Checked if state else Qt.Unchecked)
class ControlCombo(ControlBase, QWidget): """This class represents a wrapper to the combo box""" def __init__(self, label='', default=None, helptext=None): QWidget.__init__(self) ControlBase.__init__(self, label, default, helptext) ########################################################################## ############ Functions ################################################### ########################################################################## def init_form(self): self._layout = QHBoxLayout() self._combo = QComboBox(self.form) if self._label is not None: self._combolabel = QLabel(self.form) self._layout.addWidget(self._combolabel) self._combolabel.setAccessibleName('ControlCombo-label') self.label = self._label else: self._combolabel = None self._layout.addWidget(self._combo) self.form.setLayout(self._layout) self._combo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self._layout.setContentsMargins(0, 0, 0, 0) self.form.setContentsMargins(0, 0, 0, 0) self.form.setMinimumHeight(38) self.form.setMaximumHeight(38) self.form.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) self._combo.currentIndexChanged.connect(self._currentIndexChanged) self._combo.activated.connect(self._activated) self._combo.highlighted.connect(self._highlighted) self._combo.editTextChanged.connect(self._editTextChanged) self._items = {} self._addingItem = False def clear(self): self._items = {} self._value = None self._combo.clear() def add_item(self, label, value=None): self._addingItem = True if value is not None: if not (value in self._items.values()): self._combo.addItem(label) else: if not (label in self._items.keys()): self._combo.addItem(label) firstValue = False if self._items == {}: firstValue = True if value is None: self._items[str(label)] = label else: self._items[str(label)] = value self._addingItem = False if firstValue: self.value = self._items[label] def __add__(self, val): if isinstance(val, tuple): self.add_item(val[0], val[1]) else: self.add_item(val) return self def get_item_index_by_name(self, item_name): """ Returns the index of the item containing the given name :param item_name: item name in combo box :type item_name: string """ return self._combo.findText(item_name) def count(self): return self._combo.count() def show(self): """ Show the control """ QWidget.show(self) def hide(self): """ Hide the control """ QWidget.hide(self) ########################################################################## ############ Events ###################################################### ########################################################################## def current_index_changed_event(self, index): """Called when the user chooses an item in the combobox and the selected choice is different from the last one selected. @index: item's index """ pass def activated_event(self, index): """Called when the user chooses an item in the combobox. Note that this signal happens even when the choice is not changed @index: item's index """ pass def highlighted_event(self, index): pass def edittext_changed_event(self, text): pass ########################################################################## ############ PROPERTIES ################################################## ########################################################################## @property def form(self): return self @property def current_index(self): return self._combo.currentIndex() @current_index.setter def current_index(self, value): self._combo.setCurrentIndex(value) @property def items(self): return self._items.items() @property def value(self): return self._value @value.setter def value(self, value): for key, val in self.items: if value == val: index = self._combo.findText(key) self._combo.setCurrentIndex(index) if self._value != value: self.changed_event() self._value = val @property def text(self): return str(self._combo.currentText()) @text.setter def text(self, value): for key, val in self.items: if value == key: self.value = val break @property def label(self): if self._combolabel: return self._combolabel.text() else: return None @label.setter def label(self, value): """ Label of the control, if applies @type value: string """ if self._combolabel: self._combolabel.setText(value) ########################################################################## ############ Private functions ########################################### ########################################################################## def _activated(self, index): if not self._addingItem: item = self._combo.currentText() if len(item) >= 1: ControlBase.value.fset(self, self._items[str(item)]) self.activated_event(index) def _highlighted(self, index): """Called when an item in the combobox popup list is highlighted by the user. @index: item's index """ self.highlighted_event(index) def _editTextChanged(self, text): self.edittext_changed_event(text) def _currentIndexChanged(self, index): if not self._addingItem: item = self._combo.currentText() if len(item) >= 1: ControlBase.value.fset(self, self._items[str(item)]) self.current_index_changed_event(index)