예제 #1
0
    def redraw(self):
        """
        Re-draws the buttons. May be called as initialization method too.
        """
        # Pixmap that contains the painter and the DR
        canvas = QPixmap("assets/" + self.digirule.get_img_name())
        self.setPixmap(canvas)
        painter = QPainter(self.pixmap(
        ))  # Create a painter in which buttons and LEDs are drawn

        translate_matrix = QMatrix()
        offset_scale = self.digirule.get_scale_offset()
        translate_matrix.scale(offset_scale[0], offset_scale[1])
        painter.setMatrix(translate_matrix)

        # Drawing
        pen = QPen()
        pen.setWidth(self.digirule.get_buttons_width())
        pen.setColor(QColor(0, 0, 0, 0))  # Buttons color
        painter.setPen(pen)

        # Buttons
        for btn in self.digirule.get_buttons_positions_dic():
            painter.drawPoint(QPoint(btn[0], btn[1]))

        pen.setWidth(2)
        painter.setPen(pen)

        for btn in self.digirule.get_special_buttons_rects_dic():
            # Draw rectangle given the top-left and bottom-right corners coordinates
            painter.drawRect(self._build_rect(btn))
예제 #2
0
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.HighQualityAntialiasing)

        # determine max width/height of all labels
        if self._maxLabelWidth is None or self._maxLabelHeight is None:
            self._maxLabelWidth = 0
            self._maxLabelHeight = 0
            for k in self.labels.values():
                b = painter.boundingRect(
                    QRectF(0, 0, 0, 0),
                    int(Qt.AlignLeft) | int(Qt.AlignVCenter), str(k))
                self._maxLabelWidth = max(self._maxLabelWidth, b.width())
                self._maxLabelHeight = max(self._maxLabelHeight, b.height())

        barRect = self.rect()

        if self._orientation == 'Vertical':
            # translate Y axis
            matrix = QMatrix()
            matrix.translate(0, self.height() - self.margin)
            matrix.scale(1, -1)
            painter.setMatrix(matrix)

            self.gradient.setFinalStop(0, self.height() - self.margin)
            barRect.setWidth(self._barThickness)
            barRect.setHeight(barRect.height() - 2 * self.margin)
            painter.fillRect(barRect, self.gradient)

            # restore normal coordinates
            painter.scale(1, -1)

            self.setMinimumWidth(self._barThickness + self._labelMargin +
                                 self._maxLabelWidth)

        elif self._orientation == 'Horizontal':
            self.margin = self._maxLabelWidth / 2 + self._labelMargin
            barRect.setHeight(self._barThickness)
            barRect.setLeft(
                self.margin
            )  # reduces width by margin (as opposed to shifting)

            # Reduce the width by another margin to pull off the right hand side
            barRect.setWidth(barRect.width() - self.margin)

            painter.fillRect(barRect, self.gradient)

            line_step = barRect.width() / 20
            pos = barRect.left()
            while pos <= barRect.right():
                painter.drawLine(pos,
                                 2 * (barRect.bottom() - barRect.top()) / 3,
                                 pos, barRect.bottom())
                pos += line_step

            self.setMinimumHeight(self._maxLabelHeight + self._barThickness +
                                  self._labelMargin)

        for pos, label in self.labels.items():
            # Figure coordinate position. 1=height-margin for vertical, or width-margin for horizontal
            if self._orientation == 'Vertical':
                lpos = -1 * ((self.height() - (2 * self.margin)) * pos)
                painter.drawText(self._barThickness + self._labelMargin,
                                 (.5 * self._maxLabelHeight) + lpos, label)
            elif self._orientation == 'Horizontal':
                text_rect = painter.boundingRect(QRectF(0, 0, 0, 0),
                                                 int(Qt.AlignLeft), str(label))
                lpos = ((self.width() - (2 * self.margin)) * pos
                        )  # Center position
                lleft = lpos - text_rect.width() / 2
                painter.drawText(lleft + self.margin, self.height() - 1, label)