def __init__(self, parent=None, associatedWidget=None, orientation=Qt.Horizontal): """ Constructor """ logging.debug("%s: Constructor" % self.__class__.__name__) self._cursorEntered = False self._menuEntryTextFields = [] self._menuEntrySlots = [] self._hoverEntry = None self._spacer = TextField() VispaWidget.__init__(self, parent) self.hide() self._associatedWidget = associatedWidget self.setMouseTracking(True) self.setDragable(False) #self._hoverBrush = QBrush(self.HOVER_COLOR1) self._hoverGradient = QRadialGradient() self._hoverGradient.setColorAt(0, self.HOVER_COLOR1) self._hoverGradient.setColorAt(1, self.HOVER_COLOR2) #self._hoverBrush = QBrush(self.HOVER_COLOR1) self._spacer.setFontSizeRange(self.TEXTFIELD_FONTSIZE_MIN, self.TEXTFIELD_FONTSIZE_MAX) #self._textField.setDefaultWidth(self.getDistance('textFieldWidth', 1, True)) #entry.setDefaultHeight(self.getDistance('textFieldHeight', 1, True)) self._spacer.setDefaultFontSize(self.TEXTFIELD_FONTSIZE) self._spacer.setAutosizeFont(self.TEXTFIELD_AUTOSIZE_FONT_FLAG) self._spacer.setAutotruncate(self.TEXTFIELD_AUTOTRUNCATE_TEXT_FLAG) self._spacer.setAutoscale(True, False) self._spacer.setPenColor(self.TITLE_COLOR) self._spacer.setFont(self.font()) self._spacer.setText(" | ") self._spacer.calculateDimensions()
def gradient_darker(grad, factor): """Return a copy of the QGradient darkened by factor. .. note:: Only QLinearGradeint and QRadialGradient are supported. """ if type(grad) is QGradient: if grad.type() == QGradient.LinearGradient: grad = sip.cast(grad, QLinearGradient) elif grad.type() == QGradient.RadialGradient: grad = sip.cast(grad, QRadialGradient) if isinstance(grad, QLinearGradient): new_grad = QLinearGradient(grad.start(), grad.finalStop()) elif isinstance(grad, QRadialGradient): new_grad = QRadialGradient(grad.center(), grad.radius(), grad.focalPoint()) else: raise TypeError new_grad.setCoordinateMode(grad.coordinateMode()) for pos, color in grad.stops(): new_grad.setColorAt(pos, color.darker(factor)) return new_grad
def paintEvent(self, event): realSize = min(self.width(), self.height()) painter = QPainter() painter.begin(self) pen = QPen(Qt.black) pen.setWidth(1) painter.setPen(Qt.black) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) painter.scale( float(realSize) / self.scaledSize, float(realSize) / self.scaledSize) gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(pen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), 500, 500) gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(pen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), 450, 450) painter.setPen(pen) if (self.isChecked()): gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500)) gradient.setColorAt(0, self._onColor1) gradient.setColorAt(1, self._onColor2) else: gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500)) gradient.setColorAt(0, self._offColor1) gradient.setColorAt(1, self._offColor2) painter.setBrush(gradient) painter.drawEllipse(QPointF(0, 0), 400, 400) painter.end()
def paint(self, painter, option, widget): gradient = QRadialGradient(-3, -3, 10) if option.state & QStyle.State_Sunken: gradient.setCenter(3, 3) gradient.setFocalPoint(3, 3) gradient.setColorAt(0, QColor(Qt.yellow).light(120)) else: gradient.setColorAt(0, QColor(Qt.yellow).light(120)) painter.setBrush(gradient) painter.setPen(QPen(Qt.black, 0)) painter.drawRoundedRect(self.boundingRect(), 3, 3)
def radial_gradient(color, color_light=50): """ radial_gradient(QColor, QColor) radial_gradient(QColor, int) Return a radial gradient. `color_light` can be a QColor or an int. In the later case the light color is derived from `color` using `saturated(color, color_light)`. """ if not isinstance(color_light, QColor): color_light = saturated(color, color_light) gradient = QRadialGradient(0.5, 0.5, 0.5) gradient.setColorAt(0.0, color_light) gradient.setColorAt(0.5, color_light) gradient.setColorAt(1.0, color) gradient.setCoordinateMode(QRadialGradient.ObjectBoundingMode) return gradient
def drawData(self, painter, size, angle, dataname, color): """ Draw data @param painter: @type painter: @param size: @type size: @param angle: @type angle: @param dataname: @type dataname: @param color: @type color: """ col1 = QColor(color) col2 = QColor(color) col3 = QColor(color) col1 = col1.lighter(105) col2 = col1.darker(140) col3 = col3.darker() gradient = QRadialGradient( QPointF(size.width() / 2, size.height() / 2), size.width() / 2 - 20) gradient.setColorAt(0.0, col1) gradient.setColorAt(0.6, col2) gradient.setColorAt(1.0, col3) painter.setPen(Qt.NoPen) painter.setBrush(gradient) painter.drawPie(QRect(20, 20, size.width() - 40, size.height() - 40), self.__startAngle, angle) self.__startAngle = self.__startAngle + angle
def paintEvent(self, event): w = self.width() h = self.height() s = min(w, h) key = self._generateKey() pixmap = QPixmapCache.find(key) if not pixmap: pixmap = QPixmap(w, h) pixmap.fill(self, QPoint(0, 0)) # Fill pixmap with widget background pixPainter = QPainter(pixmap) pixPainter.setRenderHint(QPainter.Antialiasing) # Offsets for centering ox = int(0.5 * (w - s)) oy = int(0.5 * (h - s)) insideColor = self._color if not self._value: insideColor = insideColor.darker(250) gradient = QRadialGradient(ox + 0.5 * s, oy + 0.5 * s, 0.5 * s) gradient.setColorAt(0.27, insideColor) gradient.setColorAt(0.6, insideColor.darker(120)) gradient.setColorAt(1.0, insideColor.darker(160)) pixPainter.setBrush(gradient) pixPainter.setPen(QPen(Qt.black, 2)) pixPainter.drawEllipse(1 + ox, 1 + oy, s - 2, s - 2) pixPainter.end() QPixmapCache.insert(key, pixmap) p = QPainter() p.begin(self) p.drawPixmap(QPoint(0, 0), pixmap) p.end()
def paint(self, painter, option, widget): """ Draws this item with the inputed painter. :param painter | <QPainter> rect | <QRect> """ if self._dirty: self.rebuild() scene = self.scene() if not scene: return grid = scene.gridRect() typ = self.chartType() # draw the line chart if typ == XChartScene.Type.Line: painter.setRenderHint(painter.Antialiasing) # draw the path area area = self._buildData.get('path_area') if area and self.isShaded(): clr = QColor(self.color()) clr.setAlpha(120) painter.setPen(Qt.NoPen) painter.setBrush(clr) painter.drawPath(area) # draw the line data pen = QPen(self.color()) pen.setWidth(2) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawPath(self.path()) if (self.showPointsInLine()): palette = QApplication.palette() pen = QPen(palette.color(palette.Base)) pen.setWidth(2) painter.setBrush(self.color()) painter.setPen(pen) for point in self._ellipses: painter.drawEllipse(point, self.pointRadius(), self.pointRadius()) # draw a bar chart elif typ == XChartScene.Type.Bar: painter.setRenderHint(painter.Antialiasing) pen = QPen(self.color()) pen.setWidth(1) painter.setPen(pen) for key, value, sub_path in self._subpaths: gradient = QLinearGradient() clr = QColor(self.color()) if (sub_path != self._hoveredPath): clr.setAlpha(130) gradient.setColorAt(0.0, clr.lighter(140)) gradient.setColorAt(0.1, clr.lighter(120)) gradient.setColorAt(0.25, clr.lighter(110)) gradient.setColorAt(1.0, clr.lighter(105)) if (self.orientation() == Qt.Horizontal): gradient.setStart(0, sub_path.boundingRect().top()) gradient.setFinalStop(0, sub_path.boundingRect().bottom()) else: gradient.setStart(sub_path.boundingRect().left(), 0) gradient.setFinalStop(sub_path.boundingRect().right(), 0) painter.setBrush(gradient) painter.drawPath(sub_path) # draw a simple pie chart (calculated by scene) elif typ == XChartScene.Type.Pie: painter.setRenderHint(painter.Antialiasing) center = self.pieCenter() radius = self.radius() for key, value, sub_path in self._subpaths: clr = self.keyColor(key) gradient = QRadialGradient(QPointF(0, 0), radius) a = QColor(clr.lighter(140)) b = QColor(clr.lighter(110)) a.setAlpha(40) b.setAlpha(80) # look for mouse over if (sub_path == self._hoveredPath): a.setAlpha(100) b.setAlpha(200) gradient.setColorAt(0, a) gradient.setColorAt(1, b) pen = QPen(clr) pen.setWidth(1) painter.setBrush(gradient) painter.setPen(pen) painter.drawPath(sub_path)
def drawCorner(self, painter, position, cornerType, maxRadius=None): #logging.debug(self.__class__.__name__ +": drawCorner() "+ self.cornerTypeString(cornerType)) thickness = self.CONNECTION_THICKNESS * self.zoomFactor() halfthick = thickness / 2 cornerRoundness = halfthick**0.5 cornerOffset = halfthick * (cornerRoundness) innerCorner = halfthick * (cornerRoundness - 1) outerCorner = halfthick * (cornerRoundness + 1) innerWidth = halfthick * (cornerRoundness - 1) radius = halfthick * (cornerRoundness + 1) if maxRadius: maxRadius = max(maxRadius, thickness) radius = min(radius, maxRadius) if cornerType == self.CornerType.TOP_RIGHT: startAngle = 0 outerCorner = QPointF(position.x() + halfthick - 2 * radius, position.y() - halfthick) innerCorner = QPointF(outerCorner.x(), outerCorner.y() + (thickness)) center = QPointF(outerCorner.x() + radius, outerCorner.y() + radius) outerRect = QRectF(outerCorner, QSizeF(2 * radius, 2 * radius)) innerRect = QRectF( innerCorner, QSizeF((2 * radius - thickness), (2 * radius - thickness))) outerStart = QPointF(outerCorner.x() + 2 * radius, outerCorner.y() + (radius + halfthick)) innerStart = QPointF(outerCorner.x() + (radius - halfthick), outerCorner.y()) elif cornerType == self.CornerType.TOP_LEFT: startAngle = 90 outerCorner = QPointF(position.x() - halfthick, position.y() - halfthick) innerCorner = QPointF(outerCorner.x() + (thickness), outerCorner.y() + (thickness)) center = QPointF(outerCorner.x() + radius, outerCorner.y() + radius) outerRect = QRectF(outerCorner, QSizeF(2 * radius, 2 * radius)) innerRect = QRectF( innerCorner, QSizeF((2 * radius - thickness), (2 * radius - thickness))) outerStart = QPointF(outerCorner.x() + (radius + halfthick), outerCorner.y()) innerStart = QPointF(outerCorner.x(), outerCorner.y() + (radius + halfthick)) elif cornerType == self.CornerType.BOTTOM_LEFT: startAngle = 180 outerCorner = QPointF(position.x() - halfthick, position.y() + halfthick - 2 * radius) innerCorner = QPointF(outerCorner.x() + (thickness), outerCorner.y()) center = QPointF(outerCorner.x() + radius, outerCorner.y() + radius) outerRect = QRectF(outerCorner, QSizeF(2 * radius, 2 * radius)) innerRect = QRectF( innerCorner, QSizeF((2 * radius - thickness), (2 * radius - thickness))) outerStart = QPointF(outerCorner.x(), outerCorner.y() + (radius - halfthick)) innerStart = QPointF(outerCorner.x() + (radius + halfthick), outerCorner.y() + (2 * radius)) elif cornerType == self.CornerType.BOTTOM_RIGHT: startAngle = 270 outerCorner = QPointF(position.x() + halfthick - 2 * radius, position.y() + halfthick - 2 * radius) innerCorner = QPointF(outerCorner.x(), outerCorner.y()) center = QPointF(outerCorner.x() + radius, outerCorner.y() + radius) outerRect = QRectF(outerCorner, QSizeF(2 * radius, 2 * radius)) innerRect = QRectF( innerCorner, QSizeF((2 * radius - thickness), (2 * radius - thickness))) outerStart = QPointF(outerCorner.x() + (radius - halfthick), outerCorner.y() + 2 * radius) innerStart = QPointF(outerCorner.x() + 2 * radius, outerCorner.y() + (radius - halfthick)) else: # No defined corner, so nothing to draw. #print "PointToPointConnection.drawCorner() - No valid corner, aborting..." return if painter.redirected(painter.device()): # e.q. QPixmap.grabWidget() painter.setBrush(self.FILL_COLOR1) else: brush = QRadialGradient(center, radius) if radius >= thickness: brush.setColorAt((radius - thickness) / radius, self.FILL_COLOR1) # inner border brush.setColorAt((radius - halfthick + 1) / radius, self.FILL_COLOR2) # center of line else: # If zoom is too small use single color brush.setColorAt(0, self.FILL_COLOR1) brush.setColorAt(1, self.FILL_COLOR1) # outer border painter.setBrush(brush) path = QPainterPath() path.moveTo(outerStart) path.arcTo(outerRect, startAngle, 90) path.lineTo(innerStart) path.arcTo(innerRect, startAngle + 90, -90) path.closeSubpath() #painter.setPen(Qt.NoPen) painter.drawPath(path)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) width = self.width() height = self.height() if self.dynamic_resize: knob_radius = self.dynamic_knob_radius else: knob_radius = self.knob_radius # ensure that the center point is in the middle of a pixel to ensure # that exact vertial and horizantal ticks are drawn exactly 1px wide x = math.floor(width / 2.0) + 0.5 y = math.floor(height / 2.0) + 0.5 if DEBUG: painter.fillRect(0, 0, width, height, Qt.yellow) painter.translate(x, y) if self.knob_style == KnobWidget.STYLE_NEEDLE: r = min(x, y) - 1 painter.setPen(Qt.white) painter.setBrush(Qt.white) painter.drawEllipse(QPoint(0, 0), r, r) angle = self.value_factor * self.total_angle - (self.total_angle / 2.0) # draw base knob or needle spike if self.knob_style == KnobWidget.STYLE_ROUND: painter.setPen(self.border_color) if self.pressed: gradient = QRadialGradient(0, 0, knob_radius) gradient.setColorAt(0, self.base_color_pressed) gradient.setColorAt(0.85, self.base_color) gradient.setColorAt(1, self.base_color) painter.setBrush(gradient) else: painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), knob_radius, knob_radius) elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.save() painter.rotate(angle) painter.setPen(self.needle_color) painter.setBrush(self.needle_color) needle = QPolygonF() needle.append(QPointF(self.needle_base_radius * 0.6, 0)) needle.append(QPointF(0, -knob_radius)) needle.append(QPointF(-self.needle_base_radius * 0.6, 0)) painter.drawPolygon(needle) painter.restore() # draw knob mark or needle base if self.knob_style == KnobWidget.STYLE_ROUND: painter.save() painter.rotate(angle) painter.setPen(QPen(self.mark_color, 2)) painter.drawLine(0, -knob_radius * 0.4, 0, -knob_radius * 0.8) painter.restore() elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.setPen(self.border_color) painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), self.needle_base_radius, self.needle_base_radius) if self.scale_visible: painter.setPen(Qt.black) # draw scale arc if self.scale_arc_visible: painter.drawArc(-knob_radius - self.knob_to_scale, -knob_radius - self.knob_to_scale, knob_radius * 2 + self.knob_to_scale * 2, knob_radius * 2 + self.knob_to_scale * 2, (90 + self.total_angle / 2) * 16, -self.total_angle * 16) # draw scale ticks def value_to_angle(value): return (float(value - self.minimum_value) / self.value_range) * self.total_angle - (self.total_angle / 2.0) value = self.minimum_value while value <= self.maximum_value: angle = value_to_angle(value) painter.save() painter.rotate(value_to_angle(value)) painter.drawLine(0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_large) if self.scale_text_visible: p = painter.worldTransform().map(QPoint(0, -knob_radius - \ self.knob_to_scale - \ self.tick_size_large - \ self.tick_to_text - \ self.text_radius)) painter.restore() if self.scale_text_visible: if DEBUG: painter.save() painter.setPen(QColor(255, 0, 0, 50)) painter.setBrush(QColor(255, 0, 0, 50)) painter.drawEllipse(QPoint(p.x() - x, p.y() - y), self.text_radius, self.text_radius) painter.restore() painter.drawText(p.x() - x - 30, p.y() - y - 30, 60, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, str(value)) for i in range(1, self.scale_step_divisions): sub_value = value + (float(self.scale_step_size) * i) / self.scale_step_divisions if sub_value > self.maximum_value: break painter.save() painter.rotate(value_to_angle(sub_value)) painter.drawLine(0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_small) painter.restore() value += self.scale_step_size if self.title_text != None: painter.drawText(-knob_radius, knob_radius - 30, knob_radius * 2, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, self.title_text)