示例#1
0
    def draw_node_rect(painter: QPainter, geom: NodeGeometry,
                       model: NodeDataModel,
                       graphics_object: NodeGraphicsObject,
                       node_style: NodeStyle):
        """
        Draw node rect

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        model : NodeDataModel
        graphics_object : NodeGraphicsObject
        node_style : NodeStyle
        """
        color = (node_style.selected_boundary_color
                 if graphics_object.isSelected() else
                 node_style.normal_boundary_color)
        p = QPen(color, (node_style.hovered_pen_width
                         if geom.hovered else node_style.pen_width))
        painter.setPen(p)

        gradient = QLinearGradient(QPointF(0.0, 0.0),
                                   QPointF(2.0, geom.height))
        for at_, color in node_style.gradient_colors:
            gradient.setColorAt(at_, color)
        painter.setBrush(gradient)

        diam = node_style.connection_point_diameter
        boundary = QRectF(-diam, -diam, 2.0 * diam + geom.width,
                          2.0 * diam + geom.height)
        radius = 3.0
        painter.drawRoundedRect(boundary, radius, radius)
示例#2
0
 def drawRoundFrame(self, painter, rect, palette, lineWidth, frameStyle):
     """
     Draw a round frame
     
     :param QPainter painter: Painter
     :param QRectF rect: Target rectangle
     :param QPalette palette: `QPalette.WindowText` is used for plain borders, `QPalette.Dark` and `QPalette.Light` for raised or sunken borders
     :param int lineWidth: Line width
     :param int frameStyle: bitwise OR´ed value of `QFrame.Shape` and `QFrame.Shadow`
     """
     Plain, Sunken, Raised = list(range(3))
     style = Plain
     if (frameStyle & QFrame.Sunken) == QFrame.Sunken:
         style = Sunken
     elif (frameStyle & QFrame.Raised) == QFrame.Raised:
         style = Raised
     lw2 = 0.5 * lineWidth
     r = rect.adjusted(lw2, lw2, -lw2, -lw2)
     if style != Plain:
         c1 = palette.color(QPalette.Light)
         c2 = palette.color(QPalette.Dark)
         if style == Sunken:
             c1, c2 = c2, c1
         gradient = QLinearGradient(r.topLeft(), r.bottomRight())
         gradient.setColorAt(0.0, c1)
         gradient.setColorAt(1.0, c2)
         brush = QBrush(gradient)
     else:
         brush = palette.brush(QPalette.WindowText)
     painter.save()
     painter.setPen(QPen(brush, lineWidth))
     painter.drawEllipse(r)
     painter.restore()
示例#3
0
    def draw_panel(self, p):
        p.save()
        radius = 100
        lg = QLinearGradient(-radius, -radius, radius, radius)
        lg.setColorAt(0, Qt.white)
        lg.setColorAt(1, Qt.black)
        p.setBrush(lg)
        p.setPen(Qt.NoPen)
        p.drawEllipse(-radius, -radius, radius * 2, radius * 2)

        p.setBrush(self._brush_color)
        p.drawEllipse(-92, -92, 92 * 2, 92 * 2)
        p.restore()
示例#4
0
    def setIntColorScale(self, colors, minVal=None, maxVal=None, labels=None):
        # generate color gradient
        g = QLinearGradient()
        for i in range(len(colors)):
            x = float(i) / len(colors)
            g.setColorAt(x, QColor(colors[i][0], colors[i][1], colors[i][2]))
        self._setGradient(g)

        # add labels
        if labels is None and (minVal is None or maxVal is None):
            self.setLabels(self.labels)
        elif labels is None:
            self.setLabels({1: str(minVal), 0: str(maxVal)})
        else:
            self.setLabels(labels)
示例#5
0
    def barGradient(self, gradient):
        grad = QLinearGradient(0, 0, 0, 0)
        try:
            for stop in gradient:
                pos, r, g, b = stop.split(',')[:4]
                color = QColor(int(r), int(g), int(b))
                grad.setColorAt(float(pos), color)
        except:
            LOG.exception('Invalid gradient.')
            return

        self._gradient_def = gradient
        self.gradient = grad
        self.resizeEvent(None)
        self.update()
示例#6
0
    def _draw_rect(self, rect, painter):
        """
        Draw the background rectangle using the current style primitive color.

        :param rect: The fold zone rect to draw

        :param painter: The widget's painter.
        """
        c = self.editor.sideareas_color
        grad = QLinearGradient(rect.topLeft(), rect.topRight())
        if sys.platform == 'darwin':
            grad.setColorAt(0, c.lighter(100))
            grad.setColorAt(1, c.lighter(110))
            outline = c.darker(110)
        else:
            grad.setColorAt(0, c.lighter(110))
            grad.setColorAt(1, c.lighter(130))
            outline = c.darker(100)
        painter.fillRect(rect, grad)
        painter.setPen(QPen(outline))
        painter.drawLine(rect.topLeft() + QPointF(1, 0),
                         rect.topRight() - QPointF(1, 0))
        painter.drawLine(rect.bottomLeft() + QPointF(1, 0),
                         rect.bottomRight() - QPointF(1, 0))
        painter.drawLine(rect.topRight() + QPointF(0, 1),
                         rect.bottomRight() - QPointF(0, 1))
        painter.drawLine(rect.topLeft() + QPointF(0, 1),
                         rect.bottomLeft() - QPointF(0, 1))
示例#7
0
    def _draw_rect(self, rect, painter):
        """
        Draw the background rectangle using the current style primitive color
        or foldIndicatorBackground if nativeFoldingIndicator is true.

        :param rect: The fold zone rect to draw

        :param painter: The widget's painter.
        """
        c = self._custom_color
        if self._native:
            c = self.get_system_bck_color()
        grad = QLinearGradient(rect.topLeft(), rect.topRight())
        if sys.platform == 'darwin':
            grad.setColorAt(0, c.lighter(100))
            grad.setColorAt(1, c.lighter(110))
            outline = c.darker(110)
        else:
            grad.setColorAt(0, c.lighter(110))
            grad.setColorAt(1, c.lighter(130))
            outline = c.darker(100)
        painter.fillRect(rect, grad)
        painter.setPen(QPen(outline))
        painter.drawLine(rect.topLeft() + QPointF(1, 0),
                         rect.topRight() - QPointF(1, 0))
        painter.drawLine(rect.bottomLeft() + QPointF(1, 0),
                         rect.bottomRight() - QPointF(1, 0))
        painter.drawLine(rect.topRight() + QPointF(0, 1),
                         rect.bottomRight() - QPointF(0, 1))
        painter.drawLine(rect.topLeft() + QPointF(0, 1),
                         rect.bottomLeft() - QPointF(0, 1))
示例#8
0
    def _draw_rect(self, rect, painter):
        """
        Draw the background rectangle using the current style primitive color.

        :param rect: The fold zone rect to draw

        :param painter: The widget's painter.
        """
        c = self.editor.sideareas_color
        grad = QLinearGradient(rect.topLeft(),
                                     rect.topRight())
        if sys.platform == 'darwin':
            grad.setColorAt(0, c.lighter(100))
            grad.setColorAt(1, c.lighter(110))
            outline = c.darker(110)
        else:
            grad.setColorAt(0, c.lighter(110))
            grad.setColorAt(1, c.lighter(130))
            outline = c.darker(100)
        painter.fillRect(rect, grad)
        painter.setPen(QPen(outline))
        painter.drawLine(rect.topLeft() +
                         QPointF(1, 0),
                         rect.topRight() -
                         QPointF(1, 0))
        painter.drawLine(rect.bottomLeft() +
                         QPointF(1, 0),
                         rect.bottomRight() -
                         QPointF(1, 0))
        painter.drawLine(rect.topRight() +
                         QPointF(0, 1),
                         rect.bottomRight() -
                         QPointF(0, 1))
        painter.drawLine(rect.topLeft() +
                         QPointF(0, 1),
                         rect.bottomLeft() -
                         QPointF(0, 1))
示例#9
0
    def __init__(self,
                 size=None,
                 colors=None,
                 labels=None,
                 minVal=None,
                 maxVal=None):
        GraphicsWidget.__init__(self)
        self.setAcceptedMouseButtons(Qt.NoButton)
        self.size = size

        # if min and max values are None, assume normalized data
        self.minVal = 0 if minVal is None else minVal
        self.maxVal = 1 if maxVal is None else maxVal
        self.labels = {1: 'min', 0: 'max'} if labels is None else labels
        self.label_format = '{: .3f}'
        self.gradient = QLinearGradient()
        if colors is None:
            self.colors = [[0, 0, 0], [255, 0, 0]]
        else:
            self.colors = colors
        self.setIntColorScale(colors=self.colors,
                              minVal=self.minVal,
                              maxVal=self.maxVal,
                              labels=self.labels)
示例#10
0
def create1DGradient(
        width,
        height,
        direction=Qt.Horizontal,
        color1=(0, 0, 0, 1),
        color2=(1, 1, 1, 1),
):
    """
    Creates 1D Linear gradient to be displayed to the user.

    Args:
        width (int)
        height (int)
        direction (Qt.Direction): The direction the gradient should go
        color1 (QColorF): The first color in the gradient, the default value is black.
        color2 (QColorF): The second color in the gradient, the default value is white.

    Returns (QBrush)
    """
    # create QColor Floats
    colorA = QColor()
    colorA.setRgbF(*color1)
    colorB = QColor()
    colorB.setRgbF(*color2)

    # set direction
    if direction == Qt.Horizontal:
        gradient = QLinearGradient(0, 0, width, 0)
    elif direction == Qt.Vertical:
        gradient = QLinearGradient(0, 0, 0, height)

    # create brush
    gradient.setSpread(QGradient.RepeatSpread)
    gradient.setColorAt(0, colorA)
    gradient.setColorAt(1, colorB)

    return gradient
示例#11
0
    def resizeEvent(self, event):
        if self.width() <= 0:
            return
        self.m_window_matrix = QMatrix4x4()
        self.m_window_matrix.translate(self.width() / 2.0, self.height() / 2.0)
        self.m_window_matrix.scale(self.width() / 2.0, -self.height() / 2.0)

        self.m_projection.setToIdentity()
        self.m_projection.perspective(45.0,
                                      self.width() * 1.0 / self.height(), 0.1,
                                      100.0)

        gradient = QLinearGradient(QPointF(-1, -1), QPointF(1, 1))
        gradient.setColorAt(0, Qt.red)
        gradient.setColorAt(1, Qt.green)

        self.m_brush = QBrush(gradient)
示例#12
0
 def drawRoundedFrame(self, painter, rect, xRadius, yRadius, palette,
                      lineWidth, frameStyle):
     """
     Draw a rectangular frame with rounded borders
     
     :param QPainter painter: Painter
     :param QRectF rect: Frame rectangle
     :param float xRadius: x-radius of the ellipses defining the corners
     :param float yRadius: y-radius of the ellipses defining the corners
     :param QPalette palette: `QPalette.WindowText` is used for plain borders, `QPalette.Dark` and `QPalette.Light` for raised or sunken borders
     :param int lineWidth: Line width
     :param int frameStyle: bitwise OR´ed value of `QFrame.Shape` and `QFrame.Shadow`
     """
     painter.save()
     painter.setRenderHint(QPainter.Antialiasing, True)
     painter.setBrush(Qt.NoBrush)
     lw2 = lineWidth * 0.5
     r = rect.adjusted(lw2, lw2, -lw2, -lw2)
     path = QPainterPath()
     path.addRoundedRect(r, xRadius, yRadius)
     Plain, Sunken, Raised = list(range(3))
     style = Plain
     if (frameStyle & QFrame.Sunken) == QFrame.Sunken:
         style = Sunken
     if (frameStyle & QFrame.Raised) == QFrame.Raised:
         style = Raised
     if style != Plain and path.elementCount() == 17:
         pathList = [QPainterPath() for _i in range(8)]
         for i in range(4):
             j = i * 4 + 1
             pathList[2 * i].moveTo(
                 path.elementAt(j - 1).x,
                 path.elementAt(j - 1).y)
             pathList[2 * i].cubicTo(
                 path.elementAt(j + 0).x,
                 path.elementAt(j + 0).y,
                 path.elementAt(j + 1).x,
                 path.elementAt(j + 1).y,
                 path.elementAt(j + 2).x,
                 path.elementAt(j + 2).y,
             )
             pathList[2 * i + 1].moveTo(
                 path.elementAt(j + 2).x,
                 path.elementAt(j + 2).y)
             pathList[2 * i + 1].lineTo(
                 path.elementAt(j + 3).x,
                 path.elementAt(j + 3).y)
         c1 = QColor(palette.color(QPalette.Dark))
         c2 = QColor(palette.color(QPalette.Light))
         if style == Raised:
             c1, c2 = c2, c1
         for i in range(5):
             r = pathList[2 * i].controlPointRect()
             arcPen = QPen()
             arcPen.setCapStyle(Qt.FlatCap)
             arcPen.setWidth(lineWidth)
             linePen = QPen()
             linePen.setCapStyle(Qt.FlatCap)
             linePen.setWidth(lineWidth)
             if i == 0:
                 arcPen.setColor(c1)
                 linePen.setColor(c1)
             elif i == 1:
                 gradient = QLinearGradient()
                 gradient.setStart(r.topLeft())
                 gradient.setFinalStop(r.bottomRight())
                 gradient.setColorAt(0.0, c1)
                 gradient.setColorAt(1.0, c2)
                 arcPen.setBrush(gradient)
                 linePen.setColor(c2)
             elif i == 2:
                 arcPen.setColor(c2)
                 linePen.setColor(c2)
             elif i == 3:
                 gradient = QLinearGradient()
                 gradient.setStart(r.bottomRight())
                 gradient.setFinalStop(r.topLeft())
                 gradient.setColorAt(0.0, c2)
                 gradient.setColorAt(1.0, c1)
                 arcPen.setBrush(gradient)
                 linePen.setColor(c1)
             painter.setPen(arcPen)
             painter.drawPath(pathList[2 * i])
             painter.setPen(linePen)
             painter.drawPath(pathList[2 * i + 1])
     else:
         pen = QPen(palette.color(QPalette.WindowText), lineWidth)
         painter.setPen(pen)
         painter.drawPath(path)
     painter.restore()
示例#13
0
    def paintEvent(self, paint_event):
        QFrame.paintEvent(self, paint_event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

        rect = self.contentsRect()
        """@type: QRect"""

        painter.fillRect(rect, self.background_color)

        x = rect.x()
        y = rect.y()
        height = rect.height()
        width = floor(rect.width() * self.__progress)

        painter.fillRect(x, y, width, height, self.color)

        if self.__shiny:
            #Shiny overlay!
            gradient = QLinearGradient(rect.width() / 2, 0, rect.width() / 2, rect.height())
            gradient.setColorAt(0, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.2, QColor(255, 255, 255, 200))
            gradient.setColorAt(0.4, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.85, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.85, QColor(0, 0, 0, 0))
            gradient.setColorAt(1, QColor(0, 0, 0, 127))
            painter.fillRect(rect, gradient)
示例#14
0
class _GradientLegend(GraphicsWidget):
    """Class to make GradientLegend a GraphicsWidget.

    Draws a color gradient rectangle along with text labels denoting the value
    at specific points along the gradient.

    Parameters
    ----------
    size : list of int, optional
        Size of the widget within the GraphicsWidget rectangle.
    colors : a list of RGB codes, optional
        The lookup table to be used to design the gradient.
    labels : dict of keys = strings and values = int
        The labels that will appear along the colorbar.
        The values determine the sequence of labels in descending order.
    minVal : int, optional
        If labels is None, so only minVal and maxVal are displayed in the
        beggining and in the end of the colorbar.
    maxVal : int, optional
        If labels is None, so only minVal and maxVal are displayed in the
        beggining and in the end of the colorbar.
    """
    def __init__(self,
                 size=None,
                 colors=None,
                 labels=None,
                 minVal=None,
                 maxVal=None):
        GraphicsWidget.__init__(self)
        self.setAcceptedMouseButtons(Qt.NoButton)
        self.size = size

        # if min and max values are None, assume normalized data
        self.minVal = 0 if minVal is None else minVal
        self.maxVal = 1 if maxVal is None else maxVal
        self.labels = {1: 'min', 0: 'max'} if labels is None else labels
        self.label_format = '{: .3f}'
        self.gradient = QLinearGradient()
        if colors is None:
            self.colors = [[0, 0, 0], [255, 0, 0]]
        else:
            self.colors = colors
        self.setIntColorScale(colors=self.colors,
                              minVal=self.minVal,
                              maxVal=self.maxVal,
                              labels=self.labels)

    def setIntColorScale(self, colors, minVal=None, maxVal=None, labels=None):
        # generate color gradient
        g = QLinearGradient()
        for i in range(len(colors)):
            x = float(i) / len(colors)
            g.setColorAt(x, QColor(colors[i][0], colors[i][1], colors[i][2]))
        self._setGradient(g)

        # add labels
        if labels is None and (minVal is None or maxVal is None):
            self.setLabels(self.labels)
        elif labels is None:
            self.setLabels({1: str(minVal), 0: str(maxVal)})
        else:
            self.setLabels(labels)

    def _setGradient(self, g):
        self.gradient = g
        self.update()

    def setLimits(self, data):
        minVal = data[0]
        maxVal = data[1]
        if minVal == self.minVal and maxVal == self.maxVal:
            return
        self.minVal = minVal
        self.maxVal = maxVal
        for k in self.labels.keys():
            newv = minVal + (1 - k) * (maxVal - minVal)
            newv = self.label_format.format(newv)
            self.labels[k] = newv
        self.setLabels(self.labels)

    def setLabels(self, l):
        """
        Define labels to appear next to the color scale.

        Accepts a dict of {text: value} pairs.
        """
        self.labels = l
        self.update()

    def paint(self, p, opt, widget):
        GraphicsWidget.paint(self, p, opt, widget)
        rect = self.boundingRect()  # Boundaries of visible area in scene coord
        size = self.size if self.size is not None \
            else [rect.right()-rect.left(), rect.bottom() - rect.top()]

        # determine max width of all labels
        textPadding = 2  # in px
        labelWidth = 0
        labelHeight = 0
        for val in self.labels.values():
            b = p.boundingRect(QRectF(0, 0, 0, 0),
                               Qt.AlignLeft | Qt.AlignVCenter, str(val))
            labelWidth = max(labelWidth, b.width())
            labelHeight = max(labelHeight, b.height())

        x1_back = (rect.right() - rect.left() - size[0]) / 2
        x2_back = (rect.right() - rect.left() + size[0]) / 2
        y1_back = (rect.bottom() - rect.top() - size[1]) / 2
        y2_back = (rect.bottom() - rect.top() + size[1]) / 2

        x1_grad = x1_back + textPadding * 2
        x2_grad = x2_back - (labelWidth + textPadding * 2)
        y1_grad = y1_back + (labelHeight / 2 + textPadding)
        y2_grad = y2_back - (labelHeight / 2 + textPadding)

        # Draw background
        p.setPen(QPen(QColor(0, 0, 0, 0)))
        p.setBrush(QBrush(QColor(255, 255, 255, 0)))
        rect = QRectF(QPointF(x1_back, y1_back), QPointF(x2_back, y2_back))
        p.drawRect(rect)

        # Draw color bar
        self.gradient.setStart(0, y2_grad)
        self.gradient.setFinalStop(0, y1_grad)
        p.setPen(QPen(QColor(0, 0, 0)))
        p.setBrush(self.gradient)
        rect = QRectF(QPointF(x1_grad, y1_grad), QPointF(x2_grad, y2_grad))
        p.drawRect(rect)

        # draw labels
        tx = x2_grad + textPadding * 2
        lw = labelWidth
        lh = labelHeight
        for key, val in self.labels.items():
            y = y1_grad + key * (y2_grad - y1_grad)
            p.drawText(QRectF(tx, y - lh / 2.0, lw, lh),
                       Qt.AlignLeft | Qt.AlignVCenter, str(val))

        self.setMinimumWidth(labelWidth + 2 * textPadding + 20)
示例#15
0
 def as_qgradient(self):
     qgradient = QLinearGradient(0, 0, 100, 0)
     for pos, color in zip(self.positions, self.colors):
         qgradient.setColorAt(pos, QColor.fromHsvF(*color))
     return qgradient
示例#16
0
    def paintEvent(self, paint_event):
        QFrame.paintEvent(self, paint_event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

        rect = self.contentsRect()
        """@type: QRect"""

        painter.fillRect(rect, self.__color)

        x = rect.x()
        y = rect.y()
        width = rect.width()
        height = rect.height()

        if not self.__indeterminate:
            count = len(self.__state_order)
            for index in range(count):
                state = self.__state_order[index]
                state_width = floor(width * (state.progress / 100.0))

                if index == count - 1:
                    state_width = width - x + 1

                painter.fillRect(x, y, state_width, height, state.color)

                x += state_width
        else:
            painter.fillRect(rect, self.__indeterminate_color)

            p = self.__indeterminate_state
            s = self.__indeterminate_step_size

            gradient = QLinearGradient(0,
                                       rect.height() / 2, rect.width(),
                                       rect.height() / 2)
            gradient.setColorAt(p - s, QColor(255, 255, 255, 0))
            gradient.setColorAt(p, QColor(255, 255, 255, 200))
            gradient.setColorAt(p + s, QColor(255, 255, 255, 0))
            painter.fillRect(rect, gradient)

            self.__indeterminate_state += s

            if self.__indeterminate_state + s >= 1.0 or self.__indeterminate_state + s <= 0.0:
                self.__indeterminate_step_size *= -1
                self.__indeterminate_state = round(
                    self.__indeterminate_state
                ) + self.__indeterminate_step_size

        if self.__shiny:
            #Shiny overlay!
            gradient = QLinearGradient(rect.width() / 2, 0,
                                       rect.width() / 2, rect.height())
            gradient.setColorAt(0, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.2, QColor(255, 255, 255, 200))
            gradient.setColorAt(0.4, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.85, QColor(255, 255, 255, 0))
            gradient.setColorAt(0.85, QColor(0, 0, 0, 0))
            gradient.setColorAt(1, QColor(0, 0, 0, 127))
            painter.fillRect(rect, gradient)