Example #1
0
 def paintEvent(self, event):
     """
     Render the UI.
     """
     # first render the label
     QtGui.QLabel.paintEvent(self, event)
     
     if self.playable and self.interactive:
         # now render a pixmap on top
         painter = QtGui.QPainter()
         painter.begin(self)
         try:
             # set up semi transparent backdrop
             painter.setRenderHint(QtGui.QPainter.Antialiasing)
             
             # draw image
             painter.translate((painter.device().width() / 2) - (self._play_icon.width()/2), 
                               (painter.device().height() / 2) - (self._play_icon.height()/2) )
             
             if self._hover:
                 painter.drawPixmap( QtCore.QPoint(0, 0), self._play_icon)
             else:
                 painter.drawPixmap( QtCore.QPoint(0, 0), self._play_icon_inactive)
                 
         finally:
             painter.end()
Example #2
0
    def paintEvent(self, paint_event):
        """
        Paint Event override
        """
        # paint multiple values indicator
        if self._multiple_values == True:
            p = QtGui.QPainter(self)
            p.drawPixmap(
                0,
                0,
                self.width(),
                self.height(),
                self._no_thumb_pixmap,
                0,
                0,
                self._no_thumb_pixmap.width(),
                self._no_thumb_pixmap.height(),
            )
            p.fillRect(0, 0, self.width(), self.height(), QtGui.QColor(42, 42, 42, 237))
            p.setFont(QtGui.QFont("Arial", 15, QtGui.QFont.Bold))
            pen = QtGui.QPen(QtGui.QColor("#18A7E3"))
            p.setPen(pen)
            p.drawText(self.rect(), QtCore.Qt.AlignCenter, "Multiple Values")

        else:
            # paint thumbnail
            QtGui.QLabel.paintEvent(self, paint_event)
    def paintEvent(self, event):
        """
        Paint the widget

        :param event:    The QPaintEvent event
        """
        # call the base paint event:
        QtGui.QWidget.paintEvent(self, event)

        painter = QtGui.QPainter()
        painter.begin(self)
        try:
            painter.setRenderHint(QtGui.QPainter.Antialiasing)

            # use the foreground colour to paint with:
            fg_col = self.palette().color(self.foregroundRole())
            pen = QtGui.QPen(fg_col)
            pen.setWidth(self.line_width)
            painter.setPen(pen)

            border = self.border + int(math.ceil(self.line_width / 2.0))
            r = QtCore.QRect(0, 0, self.width(), self.height())
            r.adjust(border, border, -border, -border)

            # draw the arc:
            painter.drawArc(r, -self._start_angle * 16, self.arc_length * 16)
            r = None

        finally:
            painter.end()
            painter = None
Example #4
0
def create_round_thumbnail(image):
    """
    Create a 200 px wide circle thumbnail
    
    :param image: QImage representing a thumbnail
    :returns: Round QPixmap
    """
    CANVAS_SIZE = 200

    # get the 512 base image
    base_image = QtGui.QPixmap(CANVAS_SIZE, CANVAS_SIZE)
    base_image.fill(QtCore.Qt.transparent)

    # now attempt to load the image
    # pixmap will be a null pixmap if load fails
    thumb = QtGui.QPixmap.fromImage(image)

    if not thumb.isNull():

        # scale it down to fit inside a frame of maximum 512x512
        thumb_scaled = thumb.scaled(CANVAS_SIZE, CANVAS_SIZE,
                                    QtCore.Qt.KeepAspectRatioByExpanding,
                                    QtCore.Qt.SmoothTransformation)

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)
        painter = QtGui.QPainter(base_image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(brush)
        painter.drawEllipse(0, 0, CANVAS_SIZE, CANVAS_SIZE)
        painter.end()

    return base_image
Example #5
0
    def paintEvent(self, event):
        """
        Render the UI.
        """
        if self._mode == self.MODE_OFF:
            return
        
        painter = QtGui.QPainter()
        painter.begin(self)
        try:
            # set up semi transparent backdrop
            painter.setRenderHint(QtGui.QPainter.Antialiasing)
            overlay_color = QtGui.QColor(30, 30, 30, 100)
            painter.setBrush( QtGui.QBrush(overlay_color))
            painter.setPen(QtGui.QPen(overlay_color))
            painter.drawRect(0, 0, painter.device().width(), painter.device().height())

            # show the spinner
            painter.translate((painter.device().width() / 2) - 10, 
                              (painter.device().height() / 2) - 10)
            
            pen = QtGui.QPen(QtGui.QColor(self._bundle.style_constants["SG_HIGHLIGHT_COLOR"]))
            pen.setWidth(1)
            painter.setPen(pen)

            r = QtCore.QRectF(0.0, 0.0, 20.0, 20.0)
            start_angle = (0 + self._spin_angle) * 4 * 16
            span_angle = 340 * 16 

            painter.drawArc(r, start_angle, span_angle)
            
        finally:
            painter.end()
    def _get_default_thumbnail(self, sg_entity):
        """
        Get the default icon for the specified entity.

        :param sg_entity:   A Shotgun entity dictionary for the entity to get the
                            icon for.
        :returns:           A QIcon for the entity if available.  For Step entities, a swatch 
                            representing the step colour is returned.  If no icon is available 
                            for the entity type then the default icon is returned
        """
        if sg_entity.get("type") == "Step":
            # special case handling for steps to return a colour swatch:
            step_id = sg_entity.get("id")
            if step_id != None:
                # get the colour from the cache:
                if step_id not in ShotgunEntityModel._SG_STEP_COLOURS:
                    ShotgunEntityModel._SG_STEP_COLOURS[step_id] = None
                    # refresh cache:
                    bundle = sgtk.platform.current_bundle()
                    try:
                        sg_steps = bundle.shotgun.find("Step", [], ["color"])
                        for sg_step in sg_steps:
                            colour = None
                            try:
                                colour = tuple([
                                    int(c)
                                    for c in sg_step.get("color").split(",")
                                ])
                            except:
                                pass
                            ShotgunEntityModel._SG_STEP_COLOURS[
                                sg_step["id"]] = colour
                    except:
                        pass
                colour = ShotgunEntityModel._SG_STEP_COLOURS[step_id]

                if colour and isinstance(colour, tuple) and len(colour) == 3:
                    # get the icon for this colour from the cache:
                    if colour not in self._step_swatch_icons:
                        # build icon and add to cache:
                        pm = QtGui.QPixmap(16, 16)
                        pm.fill(QtCore.Qt.transparent)
                        painter = QtGui.QPainter(pm)
                        try:
                            painter.setBrush(
                                QtGui.QBrush(
                                    QtGui.QColor(colour[0], colour[1],
                                                 colour[2])))
                            painter.setPen(QtCore.Qt.black)
                            painter.drawRect(2, 2, 12, 12)
                        finally:
                            painter.end()
                        self._step_swatch_icons[colour] = QtGui.QIcon(pm)

                    # return the icon:
                    return self._step_swatch_icons[colour]

        # just return the entity icon or the default icon if there is no entity icon:
        return self.get_entity_icon(
            sg_entity.get("type")) or self._default_icon
    def paintEvent(self, event):
        """
        Handles painting the text fade overlay.

        Overrides the same method from ``QtGui.QWidget``
        """
        painter = QtGui.QPainter(self)

        # calculate the rectangle to paint the overlay.
        # it should only be
        gradient_rect = QtCore.QRect(
            60,  # stay to the right of the thumbnail
            event.rect().bottom() - 8,  # only 8 pixels high from the bottom
            event.rect().right() + 1,  # ensure covers full width
            event.rect().bottom(),
        )

        # vertical gradient
        gradient = QtGui.QLinearGradient(gradient_rect.topLeft(),
                                         gradient_rect.bottomLeft())

        # transparent -> base color, in first 15% of height of rect
        gradient.setColorAt(0, QtGui.QColor(0, 0, 0, 0))
        gradient.setColorAt(0.15, self.palette().base().color())

        # paint it
        painter.fillRect(gradient_rect, gradient)
        painter.end()
    def paintEvent(self, pe):

        opt = QtGui.QStyleOption()
        opt.initFrom(self)
        p = QtGui.QPainter(self)
        s = self.style()

        s.drawPrimitive(QtGui.QStyle.PE_Widget, opt, p, self)
Example #9
0
def create_overlayed_publish_thumbnail(image):
    """
    Given a shotgun thumbnail, create a publish icon
    with the thumbnail composited onto a centered otherwise empty canvas.
    This will return a 512x400 pixmap object.


    :param image: QImage containing a thumbnail
    :returns: QPixmap with a 512x400 px image
    """

    CANVAS_WIDTH = 512
    CANVAS_HEIGHT = 400
    CORNER_RADIUS = 10

    # get the 512 base image
    base_image = QtGui.QPixmap(CANVAS_WIDTH, CANVAS_HEIGHT)
    base_image.fill(QtCore.Qt.transparent)

    # now attempt to load the image
    # pixmap will be a null pixmap if load fails
    thumb = QtGui.QPixmap.fromImage(image)

    if not thumb.isNull():

        # scale it down to fit inside a frame of maximum 512x512
        thumb_scaled = thumb.scaled(CANVAS_WIDTH, CANVAS_HEIGHT,
                                    QtCore.Qt.KeepAspectRatio,
                                    QtCore.Qt.SmoothTransformation)

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)

        painter = QtGui.QPainter(base_image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(brush)

        # figure out the offsets in order to center the thumb
        height_difference = CANVAS_HEIGHT - thumb_scaled.height()
        width_difference = CANVAS_WIDTH - thumb_scaled.width()

        # center it horizontally
        inlay_offset_w = (width_difference / 2) + (CORNER_RADIUS / 2)
        # center it vertically
        inlay_offset_h = (height_difference / 2) + (CORNER_RADIUS / 2)

        # note how we have to compensate for the corner radius
        painter.translate(inlay_offset_w, inlay_offset_h)
        painter.drawRoundedRect(0, 0,
                                thumb_scaled.width() - CORNER_RADIUS,
                                thumb_scaled.height() - CORNER_RADIUS,
                                CORNER_RADIUS, CORNER_RADIUS)

        painter.end()

    return base_image
    def paintEvent(self, event):
        QtGui.QPushButton.paintEvent(self, event)

        if self.version:
            painter = QtGui.QPainter(self)
            painter.setPen(QtGui.QPen(QtGui.QColor(255, 255, 255, 255)))
            painter.drawText(self.rect().adjusted(3, 0, 0, 0),
                             QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter,
                             str(self.version))
Example #11
0
def create_rectangular_thumbnail(thumb):
    """
    Scale a given pixmap down to a given resolution

    :param thumb: pixmap to scale
    :returns: scaled thumbnail
    """
    # TODO: this would be great to add to the qtwidgets framework

    CANVAS_WIDTH = 48
    CANVAS_HEIGHT = 38

    if thumb.isNull():
        # to be safe, if thumb is null, use the empty/default thumbnail
        thumb = QtGui.QPixmap(
            ":/tk_framework_qtwidgets.global_search_widget/no_thumbnail.png"
        )

    # get the 512 base image
    base_image = QtGui.QPixmap(CANVAS_WIDTH, CANVAS_HEIGHT)
    base_image.fill(QtCore.Qt.transparent)

    # scale it down to fit inside a frame of maximum 512x400
    thumb_scaled = thumb.scaled(
        CANVAS_WIDTH,
        CANVAS_HEIGHT,
        QtCore.Qt.KeepAspectRatioByExpanding,
        QtCore.Qt.SmoothTransformation,
    )

    # now composite the thumbnail on top of the base image
    # bottom align it to make it look nice
    thumb_img = thumb_scaled.toImage()
    brush = QtGui.QBrush(thumb_img)

    painter = QtGui.QPainter(base_image)
    painter.setRenderHint(QtGui.QPainter.Antialiasing)
    painter.setBrush(brush)

    # figure out the offset height wise in order to center the thumb
    height_difference = CANVAS_HEIGHT - thumb_scaled.height()
    width_difference = CANVAS_WIDTH - thumb_scaled.width()

    # center it with wise
    inlay_offset_w = width_difference / 2
    # bottom height wise
    # inlay_offset_h = height_difference+CORNER_RADIUS
    inlay_offset_h = height_difference / 2

    # note how we have to compensate for the corner radius
    painter.translate(inlay_offset_w, inlay_offset_h)
    painter.drawRect(0, 0, thumb_scaled.width(), thumb_scaled.height())

    painter.end()

    return base_image
Example #12
0
 def print_(self):
     dialog = QtGui.QPrintDialog(self.printer, self)
     if dialog.exec_():
         painter = QtGui.QPainter(self.printer)
         rect = painter.viewport()
         size = self.imageLabel.pixmap().size()
         size.scale(rect.size(), QtCore.Qt.KeepAspectRatio)
         painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
         painter.setWindow(self.imageLabel.pixmap().rect())
         painter.drawPixmap(0, 0, self.imageLabel.pixmap())
Example #13
0
    def resizeImage(self, image, newSize):
        if image.size() == newSize:
            return

        newImage = QtGui.QImage(newSize, QtGui.QImage.Format_RGB32)
        newImage.fill(QtGui.qRgb(255, 255, 255))
        painter = QtGui.QPainter(newImage)
        painter.drawImage(QtCore.QPoint(0, 0), image)

        self.image = newImage
Example #14
0
    def drawLineTo(self, endPoint):
        painter = QtGui.QPainter(self.image)
        painter.setPen(
            QtGui.QPen(self.myPenColor, self.myPenWidth, QtCore.Qt.SolidLine,
                       QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
        painter.drawLine(self.lastPoint, endPoint)
        self.modified = True

        rad = self.myPenWidth / 2 + 2
        self.update(
            QtCore.QRect(self.lastPoint, endPoint).normalized().adjusted(
                -rad, -rad, +rad, +rad))
        self.lastPoint = QtCore.QPoint(endPoint)
Example #15
0
    def print_(self):
        printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)

        printDialog = QtGui.QPrintDialog(printer, self)
        if printDialog.exec_() == QtGui.QDialog.Accepted:
            painter = QtGui.QPainter(printer)
            rect = painter.viewport()
            size = self.image.size()
            size.scale(rect.size(), QtCore.Qt.KeepAspectRatio)
            painter.setViewport(rect.x(), rect.y(), size.width(),
                                size.height())
            painter.setWindow(self.image.rect())
            painter.drawImage(0, 0, self.image)
            painter.end()
Example #16
0
def create_rectangular_512x400_thumbnail(image):
    """
    Given a QImage shotgun thumbnail, create a rectangular icon
    with the thumbnail composited onto a centered otherwise empty canvas.
    This will return a 512x400 pixmap object.

    :param image: QImage source image
    :returns: QPixmap rectangular thumbnail on a 512x400 rect backdrop
    """
    CANVAS_WIDTH = 512
    CANVAS_HEIGHT = 400
    CORNER_RADIUS = 10

    # get the 512 base image
    base_image = QtGui.QPixmap(CANVAS_WIDTH, CANVAS_HEIGHT)
    base_image.fill(QtCore.Qt.transparent)

    # now attempt to load the image
    # pixmap will be a null pixmap if load fails
    thumb = QtGui.QPixmap.fromImage(image)

    if not thumb.isNull():

        # scale it down to fit inside a frame of maximum 512x512
        thumb_scaled = thumb.scaled(
            CANVAS_WIDTH,
            CANVAS_HEIGHT,
            QtCore.Qt.KeepAspectRatioByExpanding,
            QtCore.Qt.SmoothTransformation,
        )

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)

        painter = QtGui.QPainter(base_image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(brush)
        painter.setPen(QtGui.QPen())

        painter.drawRoundedRect(
            0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, CORNER_RADIUS, CORNER_RADIUS
        )

        painter.end()

    return base_image
    def paint_line_numbers(self, event):
        """Paint the line numbers for the input widget.

        :param event:  paint event object.
        """

        if not self._show_line_numbers:
            return

        # paint on the line number area
        painter = QtGui.QPainter(self._line_number_area)

        line_num_rect = event.rect()

        # fill it with the line number base color
        painter.fillRect(line_num_rect, self._line_number_area_base_color())

        painter.setPen(self.palette().base().color())
        painter.drawLine(line_num_rect.topLeft(), line_num_rect.bottomLeft())
        painter.drawLine(line_num_rect.topLeft(), line_num_rect.topRight())
        painter.drawLine(line_num_rect.bottomLeft(),
                         line_num_rect.bottomRight())

        # ---- process the visible blocks

        block = self.firstVisibleBlock()
        block_num = block.blockNumber()

        top = int(
            self.blockBoundingGeometry(block).translated(
                self.contentOffset()).top())

        bottom = top + int(self.blockBoundingRect(block).height())

        while block.isValid() and top <= line_num_rect.bottom():

            if block.isVisible() and bottom >= line_num_rect.top():

                num = str(block_num + 1)
                painter.setPen(self._line_number_color())
                painter.drawText(-2, top, self._line_number_area.width(),
                                 self.fontMetrics().height(),
                                 QtCore.Qt.AlignRight, num)

            block = block.next()
            top = bottom
            bottom = top + int(self.blockBoundingRect(block).height())
            block_num += 1
Example #18
0
def __create_rounded_rect_thumbnail(image, canvas_width, canvas_height,
                                    radius):
    """
    Given a qimage shotgun thumbnail, create a publish icon
    with the thumbnail composited onto a centered otherwise empty canvas.
    The thumbnail will be taking up all the space in the image.

    :param image: QImage to load thumbnail from
    :param canvas_width: Width of image to generate, in pixels
    :param canvas_height: Heiht of image to generate, in pixels
    :param radius: Corner radius of image to generate, in pixels
    :returns: QPixmap object
    """
    # get the base image
    base_image = QtGui.QPixmap(canvas_width, canvas_height)
    base_image.fill(QtCore.Qt.transparent)

    # now attempt to load the image
    # pixmap will be a null pixmap if load fails
    thumb = QtGui.QPixmap.fromImage(image)

    if not thumb.isNull():

        # scale it down to fit inside a frame
        thumb_scaled = thumb.scaled(
            canvas_width,
            canvas_height,
            QtCore.Qt.KeepAspectRatioByExpanding,
            QtCore.Qt.SmoothTransformation,
        )

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)

        painter = QtGui.QPainter(base_image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(brush)
        painter.setPen(QtGui.QPen())

        painter.drawRoundedRect(0, 0, canvas_width, canvas_height, radius,
                                radius)

        painter.end()

    return base_image
Example #19
0
def create_overlayed_user_publish_thumbnail(publish_pixmap, user_pixmap):
    """
    Creates a sqaure 75x75 thumbnail with an optional overlayed pixmap.
    """
    # create a 100x100 base image
    base_image = QtGui.QPixmap(75, 75)
    base_image.fill(QtCore.Qt.transparent)

    painter = QtGui.QPainter(base_image)
    painter.setRenderHint(QtGui.QPainter.Antialiasing)

    # scale down the thumb
    if not publish_pixmap.isNull():
        thumb_scaled = publish_pixmap.scaled(
            75, 75,
            QtCore.Qt.KeepAspectRatioByExpanding,
            QtCore.Qt.SmoothTransformation)

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)
        painter.save()
        painter.setBrush(brush)
        painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))
        painter.drawRect(0, 0, 75, 75)
        painter.restore()

    if user_pixmap and not user_pixmap.isNull():

        # overlay the user picture on top of the thumbnail
        user_scaled = user_pixmap.scaled(
            30, 30,
            QtCore.Qt.KeepAspectRatioByExpanding,
            QtCore.Qt.SmoothTransformation)
        user_img = user_scaled.toImage()
        user_brush = QtGui.QBrush(user_img)
        painter.save()
        painter.translate(42, 42)
        painter.setBrush(user_brush)
        painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))
        painter.drawRect(0, 0, 30, 30)
        painter.restore()

    painter.end()

    return base_image
Example #20
0
    def __format_thumbnail(self, pixmap_obj):
        """
        Given a screengrab, create a thumbnail object, scaled to 96x75 px
        and with a subtle rounded frame.
        
        :param pixmap_obj: input screenshot
        :returns: 96x75px pixmap 
        """
        CANVAS_WIDTH = 96
        CANVAS_HEIGHT = 75
        CORNER_RADIUS = 6
    
        # get the 512 base image
        base_image = QtGui.QPixmap(CANVAS_WIDTH, CANVAS_HEIGHT)
        base_image.fill(QtCore.Qt.transparent)
        
        # scale it down to fit inside a frame of maximum 512x512
        thumb_scaled = pixmap_obj.scaled(CANVAS_WIDTH, 
                                         CANVAS_HEIGHT, 
                                         QtCore.Qt.KeepAspectRatioByExpanding, 
                                         QtCore.Qt.SmoothTransformation)  

        # now composite the thumbnail on top of the base image
        # bottom align it to make it look nice
        thumb_img = thumb_scaled.toImage()
        brush = QtGui.QBrush(thumb_img)
        
        painter = QtGui.QPainter(base_image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(brush)

        pen = QtGui.QPen(QtGui.QColor("#2C93E2"))
        pen.setWidth(3)
        painter.setPen(pen)
        
        # note how we have to compensate for the corner radius
        painter.drawRoundedRect(0,  
                                0, 
                                CANVAS_WIDTH, 
                                CANVAS_HEIGHT, 
                                CORNER_RADIUS, 
                                CORNER_RADIUS)
        
        painter.end()
        
        return base_image        
Example #21
0
    def paintEvent(self, paint_event):
       """
       Paints the line plain text editor and adds a placeholder on bottom right corner when multiple values are detected.
       """
 
       # If the box does not have focus, draw <multiple values> placeholder when self._show_placeholder is true, even if the widget has text
       if not self.hasFocus() and self._show_placeholder == True:
           p = QtGui.QPainter(self.viewport())

           # right placeholder note in blue
           col = QtGui.QColor(24,167,227) # blue
           p.setPen(QtGui.QPen(col))
           p.setBrush(QtGui.QBrush(col))

           p.drawText(self.rect(),QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft, self._placeholder_text)
 
       else:
           QtGui.QPlainTextEdit.paintEvent(self, paint_event)
Example #22
0
 def paintEvent(self, event):
     """
     Render the UI.
     """
     if self._mode == self.MODE_OFF:
         return
     
     painter = QtGui.QPainter()
     painter.begin(self)
     try:
         # set up semi transparent backdrop
         painter.setRenderHint(QtGui.QPainter.Antialiasing)
         overlay_color = QtGui.QColor(30, 30, 30, 160)
         painter.setBrush( QtGui.QBrush(overlay_color))
         painter.setPen(QtGui.QPen(overlay_color))
         painter.drawRect(0, 0, painter.device().width(), painter.device().height())            
     finally:
         painter.end()
Example #23
0
    def paintEvent(self, event):
        """
        Paint event
        """
        # Convert click and current mouse positions to local space.
        mouse_pos = self.mapFromGlobal(QtGui.QCursor.pos())
        click_pos = None
        if self._click_pos is not None:
            click_pos = self.mapFromGlobal(self._click_pos)

        painter = QtGui.QPainter(self)

        # Draw background. Aside from aesthetics, this makes the full
        # tool region accept mouse events.
        painter.setBrush(QtGui.QColor(0, 0, 0, self._opacity))
        painter.setPen(QtCore.Qt.NoPen)
        painter.drawRect(event.rect())

        # Clear the capture area
        if click_pos is not None:
            capture_rect = QtCore.QRect(click_pos, mouse_pos)
            painter.setCompositionMode(QtGui.QPainter.CompositionMode_Clear)
            painter.drawRect(capture_rect)
            painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)

        pen = QtGui.QPen(QtGui.QColor(255, 255, 255, 64), 1, QtCore.Qt.DotLine)
        painter.setPen(pen)

        # Draw cropping markers at click position
        if click_pos is not None:
            painter.drawLine(
                event.rect().left(), click_pos.y(), event.rect().right(), click_pos.y()
            )
            painter.drawLine(
                click_pos.x(), event.rect().top(), click_pos.x(), event.rect().bottom()
            )

        # Draw cropping markers at current mouse position
        painter.drawLine(
            event.rect().left(), mouse_pos.y(), event.rect().right(), mouse_pos.y()
        )
        painter.drawLine(
            mouse_pos.x(), event.rect().top(), mouse_pos.x(), event.rect().bottom()
        )
Example #24
0
    def paintEvent(self, paint_event):
        """
        Paints the line plain text editor and adds a placeholder on bottom right corner when multiple values are detected.
        """
        super(PublishDescriptionEditQt4, self).paintEvent(paint_event)

        if self.placeholderVisible():
            painter = QtGui.QPainter(self.viewport())
            colour = self.palette().text().color()
            colour.setAlpha(128)
            painter.setPen(colour)
            painter.setClipRect(self.rect())
            margin = self.document().documentMargin()
            textRect = self.viewport().rect().adjusted(margin, margin, 0, 0)
            painter.drawText(
                textRect,
                QtCore.Qt.AlignTop | QtCore.Qt.TextWordWrap,
                self.placeholderText(),
            )
    def setPixmap(self, pixmap):
        
        # scale the pixmap down to fit
        if pixmap.height() > 40 or pixmap.width() > 60:
            # scale it down to 120x80
            pixmap = pixmap.scaled( QtCore.QSize(60,40), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)
        
        # now slap it on top of a 120x80 transparent canvas
        rendered_pixmap = QtGui.QPixmap(60, 40)
        rendered_pixmap.fill(QtCore.Qt.transparent)

        w_offset = (60 - pixmap.width()) / 2
        h_offset = (40 - pixmap.height()) / 2
        
        painter = QtGui.QPainter(rendered_pixmap)
        painter.drawPixmap(w_offset, h_offset, pixmap)
        painter.end()
        
        # and finally assign it
        QtGui.QLabel.setPixmap(self, rendered_pixmap)
Example #26
0
    def paintEvent(self, paint_event):
        """
        Paints the line plain text editor and adds a placeholder on bottom right corner when multiple values are detected.
        """

        # If the box does not have focus, draw <multiple values> placeholder when self._show_placeholder is true, even if the widget has text
        if not self.hasFocus() and self._show_multiple_values is True:
            p = QtGui.QPainter(self.viewport())

            # right placeholder note in blue
            col = QtGui.QColor(self._highlight)  # blue
            p.setPen(QtGui.QPen(col))
            p.setBrush(QtGui.QBrush(col))

            p.drawText(
                self.rect(),
                QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft,
                self._multiple_values_text,
            )

        else:
            super(PublishDescriptionEditBase, self).paintEvent(paint_event)
    def generate_badge_pixmap(self, badge_color):
        """
        Generate a badge QPixmap from a QColor. This hook method is used to generate a badge image
        when a badge hook returns a QColor. Thus, by overloading this method, it's possible to
        customize what the generated badges will look like when get_work_file_badge or
        get_publish_badge return a QColor.

        :param QColor badge_color: The color of the badge to generate a pixmap for.

        :returns: A QPixmap of the badge to be used.
        """
        # We want to multiply the color onto the (white) badge_default dot to
        # generate a nice looking badge.
        badge = QtGui.QPixmap(":/tk-multi-workfiles2/badge_default.png")
        painter = QtGui.QPainter()
        painter.begin(badge)
        try:
            painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceIn)
            painter.fillRect(badge.rect(), badge_color)
        finally:
            painter.end()
        return badge
Example #28
0
    def createOverlayPixmap(self, alpha=45):

        px = float(self.attachmentPixmap.width()) / 3.0
        py = float(self.attachmentPixmap.height()) / 3.0

        pf = px
        if px > py:
            pf = py

        overPix = QtGui.QPixmap(self.overImagePath).scaled(
            pf, pf, QtCore.Qt.KeepAspectRatio)
        self.resultPix = QtGui.QPixmap(self.attachmentPixmap.width(),
                                       self.attachmentPixmap.height())
        self.resultPix.fill(QtCore.Qt.transparent)

        painter = QtGui.QPainter(self.resultPix)
        painter.drawPixmap(0, 0, self.attachmentPixmap)
        painter.fillRect(0, 0, self.attachmentPixmap.width(),
                         self.attachmentPixmap.height(),
                         QtGui.QColor(0, 0, 0, alpha))
        painter.drawPixmap(px, py, overPix)
        del painter
Example #29
0
    def paintEvent(self, event):
        """
        Override the Qt method.

        Highlight the background color on mouse hover.
        """

        super(ChoicesFilterItemWidget, self).paintEvent(event)

        option = QtGui.QStyleOption()
        option.initFrom(self)
        painter = QtGui.QPainter()
        painter.begin(self)
        try:
            if option.state & QtGui.QStyle.State_MouseOver:
                painter.setRenderHint(QtGui.QPainter.Antialiasing)
                hover_color = option.palette.highlight().color()
                painter.setBrush(QtGui.QBrush(hover_color))
                painter.setPen(QtGui.QPen(hover_color))
                painter.drawRect(
                    0, 0, painter.device().width(), painter.device().height()
                )
        finally:
            painter.end()
    def paintEvent(self, event):
        """
        Overriden base method that gets called whenever the view needs repainting.

        :param event:    The QPaintEvent containing information about the event
        """
        if not self.model():
            return

        # make sure item rects are up to date:
        self._update_item_info()

        row_count = self.model().rowCount()
        if row_count != len(self._item_info):
            # this shouldn't ever happen but just incase it does then
            # we shouldn't paint anything as we'll probably get exceptions!
            bundle = sgtk.platform.current_bundle()
            bundle.log_warning(
                "Unable to paint the Grouped List View as the internal cache is out of sync!"
            )
            return

        # build lookups for the group widgets:
        group_widgets_by_row = {}
        for widget, row in self._group_widget_rows.items():
            if row < row_count:
                group_widgets_by_row[row] = widget
        unused_group_widgets = []
        for widget in self._group_widgets:
            if widget not in group_widgets_by_row.values():
                unused_group_widgets.append(widget)
        next_unused_group_widget_idx = 0
        self._group_widget_rows = {}
        group_widgets_to_resize = []

        # pull out the viewport size and offsets:
        update_rect = event.rect()
        viewport_rect = self.viewport().rect()
        viewport_offset = (-self.horizontalOffset(), -self.verticalOffset())

        # start painting:
        painter = QtGui.QPainter(self.viewport())
        try:
            painter.setRenderHints(QtGui.QPainter.Antialiasing
                                   | QtGui.QPainter.TextAntialiasing)

            # keep track of the y-offset as we go:
            y_offset = self._border.height()
            for row, item_info in enumerate(self._item_info):

                # get valid model index:
                index = self.model().index(row, 0, self.rootIndex())

                # get the rectangle and translate into the correct relative location:
                rect = item_info.rect.translated(viewport_offset[0],
                                                 viewport_offset[1] + y_offset)

                # test to see if the rectangle exists within the viewport:
                grp_widget = group_widgets_by_row.get(row)
                if rect.isValid and rect.intersects(viewport_rect):
                    # the group widget is visible:
                    if not grp_widget:
                        if next_unused_group_widget_idx < len(
                                unused_group_widgets):
                            grp_widget = unused_group_widgets[
                                next_unused_group_widget_idx]
                            next_unused_group_widget_idx += 1
                        else:
                            # need to create a new group widget and hook up the signals:
                            if hasattr(self.itemDelegate(),
                                       "create_group_widget"):
                                grp_widget = self.itemDelegate(
                                ).create_group_widget(self.viewport())
                            if grp_widget:
                                self._group_widgets.append(grp_widget)
                                grp_widget.toggle_expanded.connect(
                                    self._on_group_expanded_toggled)

                    if grp_widget:
                        if grp_widget.geometry() != rect:
                            # add widget to list to be resized later:
                            group_widgets_to_resize.append((grp_widget, rect))

                        # set up this widget for this index:
                        grp_widget.set_expanded(not item_info.collapsed)
                        grp_widget.set_item(index)
                        grp_widget.show()
                        self._group_widget_rows[grp_widget] = row

                elif grp_widget:
                    # group widget is hidden!
                    unused_group_widgets.append(grp_widget)

                # add the group rectangle height to the y-offset
                y_offset += rect.height()

                if not item_info.collapsed:
                    # draw any children:
                    num_child_rows = self.model().rowCount(index)
                    if len(item_info.child_info) == num_child_rows:
                        # draw all children
                        for child_row, (_, _, child_rect) in enumerate(
                                item_info.child_info):
                            # figure out index and update rect:
                            child_index = self.model().index(
                                child_row, 0, index)

                            child_rect = child_rect.translated(
                                viewport_offset[0],
                                viewport_offset[1] + y_offset)
                            if not child_rect.isValid or not child_rect.intersects(
                                    update_rect):
                                # no need to draw!
                                continue

                            # set up the rendering options:
                            # option = self.viewOptions())
                            # (AD) - using self.viewOptions() to get the view style options seems
                            # to return an invalid item in some versions of PySide/PyQt!  I think
                            # it's returning a QtGui.QStyleOptionViewItem even though the
                            # underlying C++ object is a QtGui.QStyleOptionViewItemV2 or higher.
                            #
                            # This would result in option.rect being corrupt immediately after it
                            # was set below!
                            #
                            # creating the object directly and then using initFrom seems to work
                            # though.
                            option = QtGui.QStyleOptionViewItem()
                            option.initFrom(self)

                            option.rect = child_rect

                            if self.selectionModel().isSelected(child_index):
                                option.state |= QtGui.QStyle.State_Selected
                            if child_index == self.currentIndex():
                                option.state |= QtGui.QStyle.State_HasFocus

                            # draw the widget using the item delegate
                            self.itemDelegate().paint(painter, option,
                                                      child_index)

                    # update the y-offset to include the child area:
                    y_offset += item_info.child_area_rect.height(
                    ) + self._group_spacing
                else:
                    y_offset += self._item_spacing.height()

            # hide any group widgets that were not used:
            for w in unused_group_widgets[next_unused_group_widget_idx:]:
                if w.isVisible():
                    w.hide()
        finally:
            painter.end()

        # update geometry for any group widgets that need updating
        # Note, this has to be done after painting has finished otherwise
        # the resize event gets blocked!
        for widget, rect in group_widgets_to_resize:
            widget.setGeometry(rect)

        # call the base implementation:
        QtGui.QAbstractItemView.paintEvent(self, event)