def mouseReleaseEvent(self, event): """ Обрабатываем нажатие на иконку 'Интересно/Скучно' как нажатие на обычный чекбокс Решение достаточно грубое, но в данной ситуации вполне себе работает. Перехватывать нужно отпускание мыши, потому что в QAbstractItemView редактирование запускается именно по отпусканию. """ if event.pos().x() < settings.entryIconWidth(): item = self.itemAt(event.pos()) item.setSelected(True) rect = self.visualItemRect(item) iconRect = QRect(rect.left(), rect.top(), settings.entryIconWidth(), settings.entryIconHeight()) if iconRect.contains(event.pos()): if item.checkState() == Qt.Checked: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) else: QListWidget.mousePressEvent(self, event)
def mouseReleaseEvent(self, event): """ Обрабатываем нажатие на иконку 'Интересно/Скучно' как нажатие на обычный чекбокс Решение достаточно грубое, но в данной ситуации вполне себе работает. Перехватывать нужно отпускание мыши, потому что в QAbstractItemView редактирование запускается именно по отпусканию. """ item = self.itemAt(event.pos()) if self._itemIsCategory(item) and event.pos().x() < 20: item.setExpanded(not item.isExpanded()) elif item is not None and event.pos().x() < settings.entryIconWidth(): item.setSelected(True) rect = self.visualItemRect(item) iconRect = QRect(rect.left(), rect.top() + (rect.height() - settings.entryIconHeight()) / 2, settings.entryIconWidth(), settings.entryIconHeight()) if iconRect.contains(event.pos()): self._invertItemCheckState(item) else: QTreeWidget.mouseReleaseEvent(self, event)
def _drawEntryRow(self, painter, option, index): """ Отрисовка строки с записью """ fontColor = Qt.black if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) fontColor = Qt.white elif settings.useAlternateRowColors() and index.row() % 2 == 1: painter.fillRect(option.rect, QColor('#DDEBFF')) indexIsNotLastInCategory = index.sibling(index.row() + 1, index.column()).isValid() if indexIsNotLastInCategory: painter.setPen(QColor(250, 250, 250)) painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight()) iconHeight = settings.entryIconHeight() if settings.oneLineEntry(): y = (option.rect.height() - iconHeight) / 2 else: y = settings.entryTopBottomMargin() iconPoint = QPoint(option.rect.left(), option.rect.top() + y) if index.data(Qt.CheckStateRole): painter.drawPixmap(iconPoint, QPixmap(":/icons/interesting.svg")) else: painter.drawPixmap(iconPoint, QPixmap(":/icons/boring.svg")) painter.setFont(index.data(Qt.FontRole)) painter.setPen(fontColor) baseLine = (option.rect.height() + option.fontMetrics.ascent()) / 2 blog, title = tuple(i.strip() for i in index.data().split("/", 1)) fontMetrics = QFontMetrics(index.data(Qt.FontRole)) totalWidth = option.rect.width() iconWidth = settings.entryIconWidth() + settings.entryLeftMargin() blogWidth = fontMetrics.width(blog) titleWidth = totalWidth - iconWidth - blogWidth if settings.oneLineEntry(): textY = blogY = option.rect.top() + baseLine textX = option.rect.left() + iconWidth blogX = option.rect.right() - blogWidth elidedTitle = fontMetrics.elidedText(title, Qt.ElideRight, titleWidth) else: textX = blogX = option.rect.left() + iconWidth textY = option.rect.top() + 2 * fontMetrics.ascent() blogY = option.rect.top() + fontMetrics.ascent() elidedTitle = fontMetrics.elidedText(title, Qt.ElideRight, totalWidth - iconWidth) painter.drawText(textX, textY, elidedTitle) painter.setPen(Qt.lightGray) painter.drawText(blogX, blogY, blog)