Exemplo n.º 1
0
class ImageViewer(QWidget):
    def __init__(self):
        super(ImageViewer, self).__init__()
        self.painter = QPainter()
        self.my_pen = QPen(QColor("red"))
        self.my_pen.setWidth(5)

        self.my_brush = QBrush(QColor("#123456"))

        self.photo = QPixmap()
        self.photo_rect = QRect()

    def set_pixmap(self, image_path):
        self.photo.load(image_path)
        self.repaint()

    def paintEvent(self, event):
        self.painter.begin(self)
        self.draw()
        self.painter.end()

    def draw(self):
        rect = self.rect()

        self.painter.setPen(self.my_pen)
        self.painter.setBrush(self.my_brush)

        photo = self.photo.scaled(QSize(rect.width(), rect.height()), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.photo_rect.setRect(rect.x(), rect.y(), photo.width(), photo.height())
        self.photo_rect.moveCenter(rect.center())

        self.painter.drawPixmap(self.photo_rect, photo)
Exemplo n.º 2
0
class MovieListDelegate(QItemDelegate):
    def __init__(self):
        super(MovieListDelegate, self).__init__()

        self.poster_rect = QRect()
        self.content_box = QRect()
        self.date_rect = QRect()
        self.user_score_rect = QRect()
        self.description_rect = QRect()
        self.vote_average = QRect()

        self.outline = QPen(QColor(0, 0, 0, 30))
        self.font_pen = QPen(QColor(0, 0, 0, 150))
        self.poster_background = QBrush(QColor("black"))
        self.favorited_background = QBrush(QColor("lightBlue"))

        self.font = QFont()

        self.title_font = QFont()
        self.title_font.setPixelSize(20)
        self.title_font.setBold(True)

    def paint(self, painter, option, index):
        rect = option.rect
        movie = index.data(Qt.UserRole)
        poster = index.data(Qt.UserRole + 1)

        painter.setPen(self.outline)

        if movie.favorited:
            painter.setBrush(self.favorited_background)
        else:
            painter.setBrush(Qt.NoBrush)

        painter.drawRect(rect)

        # draw poster
        self.poster_rect.setRect(rect.x() + 2,
                                 rect.y() + 2, poster.width(),
                                 poster.height() - 4)
        painter.setPen(Qt.NoPen)
        painter.setBrush(self.poster_background)
        painter.drawPixmap(self.poster_rect, poster)

        # content box
        content_box_width = (rect.width() - self.poster_rect.width()) - 14
        self.content_box.setRect(self.poster_rect.right() + 10,
                                 rect.y() + 2, content_box_width,
                                 poster.height() - 4)

        # draw title
        painter.setPen(self.font_pen)
        painter.setFont(self.title_font)
        painter.drawText(self.content_box, movie.original_title)

        # draw release date
        painter.setFont(self.font)
        self.date_rect.setRect(self.content_box.x(),
                               self.content_box.y() + 25,
                               self.content_box.width(), 15)
        painter.drawText(self.date_rect, movie.release_date)

        # director/writer
        self.vote_average.setRect(self.content_box.x(),
                                  self.date_rect.bottom() + 2,
                                  self.content_box.width(), 15)
        painter.drawText(self.vote_average,
                         f"Vote average: {movie.vote_average}")

        # description
        self.description_rect.setRect(self.content_box.x(),
                                      self.vote_average.bottom() + 20,
                                      self.content_box.width(), rect.height())
        painter.drawText(self.description_rect, movie.overview)