Ejemplo n.º 1
0
    def paint(self, painter, option, index):
        item = index.data(role=QtCore.Qt.UserRole)
        if not item:
            return

        self.initStyleOption(option, index)

        icon_size = self.parent().iconSize()

        lines = []
        if isinstance(item, plexdevices.media.Episode):
            lines.append(item.grandparent_title)
            lines.append(item.title)
            lines.append('S{} E{}'.format(item.parent_index, item.index))
        elif isinstance(item, plexdevices.media.Album):
            lines.append(item.parent_title)
            lines.append(item.title)
        elif isinstance(item, plexdevices.media.Movie):
            lines.append(item.title)
            lines.append(str(item.year))
        elif isinstance(item, plexdevices.media.Track):
            lines.append(item.title)
            lines.append(timestamp_from_ms(item.duration, minimal=True))
        else:
            lines.append(item.title)

        icon_rect = QtCore.QRect(
            option.rect.topLeft(),
            option.rect.topRight() + QtCore.QPoint(0, icon_size.height())
        )

        background_rect = QtCore.QRect(
            option.rect.topLeft(),
            icon_rect.bottomRight() +
            QtCore.QPoint(0, self.title_font_metrics.height() +
                          self.summary_font_metrics.height() * (len(lines) - 1))
        )

        text_rect = QtCore.QRect(option.rect.topLeft(), option.rect.bottomRight())
        text_rect.setTop(icon_rect.bottom())

        # Background
        if option.state & QtWidgets.QStyle.State_Selected:
            painter.fillRect(background_rect, option.palette.highlight())
        elif option.state & QtWidgets.QStyle.State_MouseOver:
            brush = option.palette.base()
            if brush.color().lightness() > 127:
                brush.setColor(brush.color().darker(120))
            else:
                brush.setColor(brush.color().lighter(120))
            painter.fillRect(background_rect, brush)

        # Icon
        thumb = index.data(role=QtCore.Qt.DecorationRole)
        if thumb and not thumb.isNull():
            if thumb.height() > thumb.width():
                scaled = thumb.scaledToWidth(icon_size.height(),
                                             QtCore.Qt.SmoothTransformation)
                scaled = scaled.copy(0, scaled.height() / 2 - icon_size.height() / 2,
                                     icon_size.height(), icon_size.height())
            elif thumb.height() < thumb.width():
                scaled = thumb.scaledToHeight(icon_size.height(),
                                              QtCore.Qt.SmoothTransformation)
            else:
                scaled = thumb.scaledToHeight(
                    icon_size.height(), QtCore.Qt.SmoothTransformation)
            if item.markable:
                draw_progress_bar(item, scaled, height=6)
                draw_unwatched_indicator(item, scaled, size=0.20)
            QtWidgets.QApplication.style().drawItemPixmap(
                painter, option.rect, QtCore.Qt.AlignTop | QtCore.Qt.AlignHCenter, scaled)

        # Title
        painter.save()
        painter.setBrush(option.palette.highlightedText() if
                         option.state & QtWidgets.QStyle.State_Selected else
                         option.palette.text())
        painter.setFont(self.title_font)

        elided_text = painter.fontMetrics().elidedText(
            lines[0], QtCore.Qt.ElideRight, option.rect.width())
        painter.drawText(text_rect, QtCore.Qt.AlignLeft, elided_text)
        text_rect.setTop(text_rect.top() + painter.fontMetrics().height())
        painter.restore()
        painter.save()
        if painter.pen().color().lightness() > 127:
            painter.setPen(QtGui.QPen(painter.pen().color().darker()))
        else:
            painter.setPen(QtGui.QPen(painter.pen().color().lighter()))
        painter.setFont(self.summary_font)

        # extra lines
        for line in lines[1:]:
            elided_text = painter.fontMetrics().elidedText(
                line, QtCore.Qt.ElideRight, option.rect.width())
            painter.drawText(text_rect, QtCore.Qt.AlignLeft, elided_text)
            text_rect.setTop(text_rect.top() + painter.fontMetrics().height())

        painter.restore()
Ejemplo n.º 2
0
    def paint(self, painter, option, index):
        self.initStyleOption(option, index)
        item = index.data(role=QtCore.Qt.UserRole)

        icon_size = self.parent().iconSize()
        thumb_rect = QtCore.QRect(option.rect.topLeft(), icon_size)

        if item.__class__.__name__ == 'HubsItem':
            title_text = hub_title(item)
        elif isinstance(item, plexdevices.hubs.Hub):
            title_text = item.title.upper()
        else:
            title_text = title(item)

        # Background
        if option.state & QtWidgets.QStyle.State_Selected:
            painter.fillRect(option.rect, option.palette.highlight())
        elif option.state & QtWidgets.QStyle.State_MouseOver:
            brush = option.palette.base()
            if brush.color().lightness() > 127:
                brush.setColor(brush.color().darker(120))
            else:
                brush.setColor(brush.color().lighter(120))
            painter.fillRect(option.rect, brush)

        # Icon
        thumb = index.data(role=QtCore.Qt.DecorationRole)
        if thumb and not thumb.isNull():
            if thumb.width() > thumb.height():
                scaled = thumb.scaledToHeight(
                    icon_size.height(), QtCore.Qt.SmoothTransformation)
                scaled = scaled.copy(scaled.width() / 2 - icon_size.height() / 2, 0,
                                     icon_size.height(), icon_size.height())
            elif thumb.width() < thumb.height():
                scaled = thumb.scaledToWidth(
                    icon_size.height(), QtCore.Qt.SmoothTransformation)
                scaled = scaled.copy(0, scaled.height() / 2 - icon_size.height() / 2,
                                     icon_size.height(), icon_size.height())
            else:
                scaled = thumb.scaledToHeight(
                    icon_size.height(), QtCore.Qt.SmoothTransformation)
            if item.markable:
                draw_progress_bar(item, scaled, height=6)
                draw_unwatched_indicator(item, scaled, size=0.20)
            QtWidgets.QApplication.style().drawItemPixmap(
                painter, option.rect,
                QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter,
                scaled)

        text_rect = QtCore.QRect(option.rect.topLeft(), option.rect.bottomRight())
        text_rect.setLeft(thumb_rect.right() + 4)

        # Right Title
        if hasattr(item, 'duration'):
            line_text = timestamp_from_ms(item.duration, minimal=True)
            painter.save()
            if painter.pen().color().value() < 128:
                painter.setPen(QtGui.QPen(painter.pen().color().lighter()))
            else:
                painter.setPen(QtGui.QPen(painter.pen().color().darker(150)))
            painter.setFont(self.summary_font)

            painter.drawText(text_rect, QtCore.Qt.AlignRight, line_text)

            bounding_rect = painter.boundingRect(text_rect, QtCore.Qt.AlignRight, line_text)
            text_rect.setRight(bounding_rect.left())
            painter.restore()

        # Title Line
        painter.save()
        if option.state & QtWidgets.QStyle.State_Selected:
            painter.setBrush(option.palette.highlightedText())
        else:
            painter.setBrush(option.palette.text())
        painter.setFont(self.title_font)

        elided_text = painter.fontMetrics().elidedText(
            title_text, QtCore.Qt.ElideRight, text_rect.width())
        painter.drawText(text_rect, QtCore.Qt.AlignLeft, elided_text)

        text_rect.setRight(option.rect.right())
        text_rect.setTop(option.rect.top() + painter.fontMetrics().height())

        painter.restore()

        # Summary text. wrap and elide
        if hasattr(item, 'summary'):
            painter.save()
            painter.setFont(self.summary_font)
            summary_text = item.summary
            if not summary_text or text_rect.height() < painter.fontMetrics().height():
                painter.restore()
                return
            if painter.pen().color().value() < 128:
                painter.setPen(QtGui.QPen(painter.pen().color().lighter()))
            else:
                painter.setPen(QtGui.QPen(painter.pen().color().darker(150)))
            elide_text(painter, text_rect, summary_text)
            painter.restore()

        self.last_icon_size = self.parent().iconSize().height()