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)
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()
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))
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))
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()
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 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()
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)
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))
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
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()
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)
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
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)