def paint(self, painter: QtGui.QPainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: checked: bool = index.data(Qt.DisplayRole) options = QStyleOptionButton() if (index.flags() & Qt.ItemIsEditable) > 0: options.state |= QStyle.State_Enabled else: options.state |= QStyle.State_ReadOnly if checked: options.state |= QStyle.State_On else: options.state |= QStyle.State_Off options.rect = self.__getCheckboxRect(option) if not (index.flags() & Qt.ItemIsEditable): options.state |= QStyle.State_ReadOnly QApplication.style().drawControl(QStyle.CE_CheckBox, options, painter)
def editorEvent(self, event: QEvent, model: 'PropModel', option: QStyleOptionViewItem, index: QModelIndex) -> bool: """ Change the data in the model and the state of the checkbox if the user presses the left mouse button or presses Key_Space or Key_Select and this cell is editable. Otherwise do nothing. :param event: The event that will take place to trigger the editor Event. :type event: QEvent :param model: The model that our delegate will render. :type model: PropModel :param option: Option for the kind've event that takes place. :type option: QStyleOptionViewItem :param index: Index of the events. :type index: QModelIndex :return: true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. :rtype: bool """ event.type() if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0: return False data = index.internalPointer() if index.column() != 1 or not isinstance( data, Property) or data.getType() != bool: return QStyledItemDelegate.editorEvent(self, event, model, option, index) if data.isReadOnly(): return False # Do not change the checkbox-state if event.type() == QEvent.MouseButtonPress: return False if event.type() == QEvent.MouseButtonRelease or event.type( ) == QEvent.MouseButtonDblClick: if event.button( ) != QtCore.Qt.LeftButton or not self.getCheckBoxRect( option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != QtCore.Qt.Key_Space and event.key( ) != QtCore.Qt.Key_Select: return False else: return False # Change the checkbox-state checkbox = QCheckBox('temp') checkbox.setChecked(not data.getValue()) self.setModelData(checkbox, model, index) return True
def _fakom_item_pressed(self, prx_index: QModelIndex): if not prx_index.flags() & Qt.ItemIsSelectable: return self.fakom_tree.model().clear_filter() already_selected = False current_fa_name = prx_index.siblingAtColumn(Kg.NAME).data(Qt.DisplayRole) current_trim_idx = self.get_index_group_parent(prx_index) current_model_code = current_trim_idx.siblingAtColumn(Kg.VALUE).data(Qt.DisplayRole) # -- Lookup if index is already selected for model_code, fa_name_ls in self.wizard.session.data.fakom_selection.items(): if current_model_code == model_code: if current_fa_name in fa_name_ls: already_selected = True if already_selected: # Remove entry self.wizard.session.data.fakom_selection[current_model_code].remove(current_fa_name) if not self.wizard.session.data.fakom_selection[current_model_code]: self.wizard.session.data.fakom_selection.pop(current_model_code) else: # Add entry self.wizard.session.data.fakom_selection.update( {current_model_code: (self.wizard.session.data.fakom_selection.get(current_model_code) or []) + [current_fa_name] } ) # -- Style selected items with checkmark src_idx_selection_ls = list() for model_code, fa_src_idx, fa_item in self.iter_all_fakom_items(): if fa_src_idx.data(Qt.DisplayRole) in (self.wizard.session.data.fakom_selection.get(model_code) or []): self._style_index_checked(fa_src_idx) src_idx_selection_ls.append(fa_src_idx) else: if fa_src_idx.data(Qt.DecorationRole): self.fakom_tree.model().sourceModel().setData(fa_src_idx, QIcon(), Qt.DecorationRole) self.fakom_tree.model().apply_last_filter() # Empty selection if not src_idx_selection_ls: self.wizard.session.data.fakom_selection = dict() self.completeChanged.emit() self.result_tree.clear() return self.populate_result_tree(src_idx_selection_ls) self.completeChanged.emit()
def editorEvent(self, event: QEvent, model: QAbstractItemModel, option: QStyleOptionViewItem, index: QModelIndex) -> bool: if not (index.flags() & Qt.ItemIsEditable) > 0: return False if event.type() == QEvent.MouseButtonRelease: if event.button() != Qt.LeftButton: # or not self.__getCheckboxRect(option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != Qt.Key_Space and event.key() != Qt.Key_Select: return False else: return False self.setModelData(None, model, index) return True # super().editorEvent(event, model, option, index)
def paint(self, painter: QStylePainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: """ Paint a checkbox without the label. :param painter: This draws the widget. :type painter: QStylePainter :param option: Option for the style of checkbox. :type option: QStyleOptionViewItem :param index: Index for the painted checkbox. :type index: QModelIndex :return: None :rtype: NoneType """ QStyledItemDelegate.paint(self, painter, option, index) if index.column() == 1 and isinstance( index.internalPointer(), Property) and index.internalPointer().getType() == bool: checked = index.internalPointer().getValue() check_box_style_option = QtWidgets.QStyleOptionButton() if (index.flags() & QtCore.Qt.ItemIsEditable) > 0: check_box_style_option.state |= QtWidgets.QStyle.State_Enabled else: check_box_style_option.state |= QtWidgets.QStyle.State_ReadOnly if checked: check_box_style_option.state |= QtWidgets.QStyle.State_On else: check_box_style_option.state |= QtWidgets.QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) check_box_style_option.state |= QtWidgets.QStyle.State_Enabled QtWidgets.QApplication.style().drawControl( QtWidgets.QStyle.CE_CheckBox, check_box_style_option, painter)