def paintEvent(self, e): Part.paintEvent(self, e) painter = QPainter(self) color = QColor(self._base) color.setAlpha(200) painter.setPen(color.dark(150)) painter.setBrush(color.dark(115)) painter.setRenderHint(QPainter.Antialiasing) painter.drawEllipse(11, 22, 10, 10) rect = QRectF(25, 17, 7, 20) painter.drawChord(rect, 270 * 16, 180 * 16) rect = QRectF(40, 11, 10, 30) painter.drawChord(rect, 270 * 16, 180 * 16) painter.drawEllipse(63, 14, 5, 5) painter.drawEllipse(63, 35, 5, 5) painter.drawEllipse(81, 14, 5, 5) painter.drawEllipse(81, 35, 5, 5) painter = None if self.data is None: text = None else: text = self.data.name self.drawText(text)
def paintEvent(self, event=None): font = QFont(self.font()) font.setPointSize(font.pointSize() - 1) fm = QFontMetricsF(font) fracWidth = fm.width(FractionSlider.WSTRING) indent = fm.boundingRect("9").width() / 2.0 if not X11: fracWidth *= 1.5 span = self.width() - (FractionSlider.XMARGIN * 2) value = self.__numerator / float(self.__denominator) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.TextAntialiasing) painter.setPen(self.palette().color(QPalette.Mid)) painter.setBrush(self.palette().brush( QPalette.AlternateBase)) painter.drawRect(self.rect()) segColor = QColor(Qt.green).dark(120) segLineColor = segColor.dark() painter.setPen(segLineColor) painter.setBrush(segColor) painter.drawRect(FractionSlider.XMARGIN, FractionSlider.YMARGIN, span, fm.height()) textColor = self.palette().color(QPalette.Text) segWidth = span / self.__denominator segHeight = fm.height() * 2 nRect = fm.boundingRect(FractionSlider.WSTRING) x = FractionSlider.XMARGIN yOffset = segHeight + fm.height() for i in range(self.__denominator + 1): painter.setPen(segLineColor) painter.drawLine(x, FractionSlider.YMARGIN, x, segHeight) painter.setPen(textColor) y = segHeight rect = QRectF(nRect) rect.moveCenter(QPointF(x, y + fm.height() / 2.0)) painter.drawText(rect, Qt.AlignCenter, QString.number(i)) y = yOffset rect.moveCenter(QPointF(x, y + fm.height() / 2.0)) painter.drawText(rect, Qt.AlignCenter, QString.number(self.__denominator)) painter.drawLine(QPointF(rect.left() + indent, y), QPointF(rect.right() - indent, y)) x += segWidth span = int(span) y = FractionSlider.YMARGIN - 0.5 triangle = [QPointF(value * span, y), QPointF((value * span) + (2 * FractionSlider.XMARGIN), y), QPointF((value * span) + FractionSlider.XMARGIN, fm.height())] painter.setPen(Qt.yellow) painter.setBrush(Qt.darkYellow) painter.drawPolygon(QPolygonF(triangle))
def paint(self, painter, rect, widget_size, draw_highlight): GraphXYView.paint(self, painter, rect, widget_size, draw_highlight) self.fm = QFontMetrics(painter.font()) self.updateDimensions(widget_size) option = self.viewOptions() background = option.palette.base() foreground = QPen(option.palette.color(QPalette.Foreground)) painter.save() # Offset to cerrectly center the graph after the resizing due to the step by step increment painter.translate(self.left_margin, self.top_margin) painter.setPen(foreground) if self.validItems > 0: # Draw histograms if self.model().rowCount(self.rootIndex()) != 0 and self.value_max != 0: value_max = self.value_max else: value_max = 10 keyNumber = 0 for row in xrange(self.model().rowCount(self.rootIndex())): index = self.model().index(row, 1, self.rootIndex()) value = self.model().data(index).toDouble()[0] if value >= 0.0: height = self.height_max * value / value_max color = QColor("#a2ca60") if draw_highlight and self.highlight and index.row() == self.highlight.row(): color = color.dark(120) painter.setBrush(QBrush(color)) painter.drawRect((keyNumber+0.5) * self.width, self.height_max - height, COLUMN_SPACE_RATIO * self.width, height) # Legend drawing: painter.translate((keyNumber+0.75) * self.width, self.height_max) painter.rotate(-45.0) text = self.model().data(self.model().index(index.row(), 0, self.rootIndex()), Qt.EditRole).toString() txt_width = self.fm.width(text) painter.drawText(QRect(-txt_width, 0, txt_width, self.width), Qt.AlignRight, text) painter.rotate(45.0) painter.translate(-((keyNumber+0.75) * self.width), -self.height_max) keyNumber += 1 painter.translate(-self.left_margin, -self.top_margin) painter.restore()
def drawMotors(self, qp): # TODO Check if motor update is recent if (self.motors[0]<0): return defaultCol = QColor(0,255,0, 200) #qp = QtGui.QPainter() qp.resetTransform() w = self.width() h = self.height() maxSize = min(w,h)*0.175 minSize = maxSize/10. qp.translate(w- maxSize, h-maxSize) qp.translate(-12,-12) qp.rotate(45) lighter = defaultCol lighter.setAlphaF(0.1) qp.setBrush(lighter.dark()) qp.setPen(lighter) # Draw background circle qp.drawEllipse(QPointF(0,0),maxSize,maxSize) # Draw Thrust Average spread = 2 avg = sum(self.motors)/len(self.motors) /100. * (maxSize-minSize) + minSize lighter.setAlphaF(0.5) qp.setPen(lighter.lighter()) qp.setBrush(QColor(0,0,0,0)) qp.drawEllipse(QPointF(0,0),avg, avg) qp.setBrush(lighter.dark()) lighter.setAlphaF(0.2) qp.setPen(lighter) qp.setPen(QPen(defaultCol)) qp.setBrush(QBrush(defaultCol.dark())) for i in range(4): m = self.motors[i]*2/100. * (maxSize-minSize) + minSize qp.drawPie(QRectF(spread-m/2., spread-m/2., m, m), 0, -90*16) qp.rotate(-90)
class CanvasProps (QGraphicsItem): def __init__(self, parent=None, scene=None): QGraphicsItem.__init__ (self) self.parent = parent self.helper = self.parent.getHelper() #self.setFlags (QGraphicsItem.ItemIsSelectable) self.setAcceptsHoverEvents (True) self.pen_color = QPen (Qt.black, 2) self.color = QColor (Qt.white).dark (150) # init Canvas Animation Tweening self.timeline = QTimeLine (200) self.timeline.setFrameRange (0, 100) self.anim = QGraphicsItemAnimation () self.anim.setItem (self) self.anim.setTimeLine (self.timeline) self.helper.connect (self.timeline, SIGNAL("finished()"), self.moveFurtherUp) self.anim_active = False #self._nodename = QGraphicsTextItem ('text '+str(self.node_id), self) self._nodename = QGraphicsTextItem ('', self) self._nodename.setPos (QPointF (18, 20)) self._nodename.setDefaultTextColor (QColor (Qt.white).light (255)) self._nodename.setFont (QFont ("Helvetica", 11, QFont.Bold, False)) self._nodename.setTextWidth(120) self._nodename.setToolTip (self._nodename.toPlainText ()) #self._nodename.setHtml("<h2 align=\"center\">hello</h2><h2 align=\"center\">world 1234345345</h2>123"); self.props_list = [] self.props_textItem_value_list = [] self.FACTOR = 4.0 self._canvas_height = 0 def boundingRect (self): return QRectF (0, 0, 122, 150) def shape (self): path = QPainterPath () path.addRect (0, 0, 122, 20) return path def paint (self, painter, option, unused_widget): if option.state & QStyle.State_Selected: fillColor = self.color.dark (100) else: fillColor = self.color if option.state & QStyle.State_MouseOver: fillColor = fillColor.light (120) if option.levelOfDetail < 0.2: if option.levelOfDetail < 0.125: painter.fillRect (QRectF (0, 0, 110, 70), fillColor) return painter.setPen (QPen (Qt.black, 0)) painter.setBrush (fillColor) painter.drawRect (0, 0, 120, 20) return oldPen = painter.pen () pen = oldPen width = 0 if option.state & QStyle.State_Selected: width += 2 pen.setWidth (width) if option.state & QStyle.State_Sunken: level = 120 else: level = 100 painter.setBrush (QBrush (fillColor.dark (level))) #painter.drawRoundRect (QRect (0, 0, 80, 34+self.height), 20) painter.drawRect (QRect (0, 20, 120, 30+9*self._canvas_height)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def addProp (self, prop_name, prop_value): i = len (self.props_list) self.props_list.append (QGraphicsTextItem (prop_name + ' : ', self)) self.props_textItem_value_list.append (CustomFloatingText (prop_value, self)) # (1) adding the prop's name. self.props_list[i].setPos (QPointF (7, 35+i*10)) self.props_list[i].setDefaultTextColor (QColor (Qt.white).light (255)) self.props_list[i].setFont (QFont ("Helvetica", 9, QFont.StyleItalic, False)) self.props_list[i].setTextWidth (55) self.props_list[i].setToolTip (self.props_list[i].toPlainText ()) # (2) adding the prop's value. self.props_textItem_value_list[i].setTextInteractionFlags (Qt.TextEditable) self.props_textItem_value_list[i].setPos (QPointF (55, 35+i*10)) self.props_textItem_value_list[i].setDefaultTextColor (QColor (Qt.white).light (255)) self.props_textItem_value_list[i].setFont (QFont ("Helvetica", 9, QFont.StyleNormal, False)) self.props_textItem_value_list[i].setTextWidth (55) receiver = lambda value: self.parent.listenToChangedPropsValues (prop_name, value) self.helper.connect (self.props_textItem_value_list[i], SIGNAL ("textChanged(QString)"), receiver) def getProps (self): tmp_list = [] l = len (self.props_list) for i in range (0, l): tmp_list[i] = [self.props_list[i].toPlainText(), self.props_textItem_value_list[i].toPlainText()] return tmp_list def setTitle (self, title): self._nodename.setPlainText (title) def setCanvasHeightInUnits (self, ch): self._canvas_height = ch # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def moveDown (self, canvas_height_in_units): self.anim_active = True self.upwards_flag = False self.canvas_height = canvas_height_in_units self.timeline.stop () self.anim.setPosAt (0, QPointF(0, 1+(self.canvas_height+1)*self.FACTOR)) self.anim.setPosAt (1, QPointF(0, 1+(self.canvas_height+2)*self.FACTOR)) self.timeline.start () self.update () def moveUp (self, canvas_height_in_units): if self.anim_active == False: self.anim_active = True self.upwards_flag = True self.canvas_height = canvas_height_in_units self.timeline.stop () self.anim.setPosAt (0, QPointF(0, 1+(self.canvas_height+1)*self.FACTOR)) self.anim.setPosAt (1, QPointF(0, 1+(self.canvas_height) *self.FACTOR)) self.timeline.start () self.update () # this method double-checks whether the canvas needs to be further up as a # result of receiving other asynchronous "delete link" SIGNALs while moving up. def moveFurtherUp (self): self.anim_active = False if self.upwards_flag==True: if self.parent.getMaxLen() < self.canvas_height: self.moveUp (self.canvas_height-1) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def hoverEnterEvent (self, e): self._nodename.setToolTip (self._nodename.toPlainText ()) QGraphicsItem.hoverEnterEvent (self, e) '''
def paint(self, painter, rect, widget_size, draw_highlight): GraphXYView.paint(self, painter, rect, widget_size, draw_highlight) smooth = 2.5 # this is invert smoothing, increase it to sharpen graph if not self.model(): return option = self.viewOptions() background = option.palette.base() foreground = QPen(option.palette.color(QPalette.Foreground)) # Handle step by step size increment step_width = floor(widget_size.width() / RESIZE_INCREMENT) * RESIZE_INCREMENT step_height = floor(widget_size.height() / RESIZE_INCREMENT) * RESIZE_INCREMENT if self.model().rowCount(self.rootIndex()) != 0: value_max = self.getValueMaxAll() else: value_max = 0 # If there is no data yet, or the max is at 0, use a max of 10 to dispaly an axe if self.fetcher.fragment.type == 'LoadStream': if value_max < 100: value_max = 100 else: if value_max == 0: value_max = 10 if len(self.data) != 0: xmin = self.getValueMin(0) xmax = self.getValueMax(0) else: return if xmin == xmax: return # avoid division by 0 painter.save() # Offset to cerrectly center the graph after the resizing due to the step by step increment painter.translate((widget_size.width() - step_width) / 2.0, (widget_size.height() - step_height) / 2.0) painter.setPen(foreground) # Draw lines fm = QFontMetrics(painter.font()) margin_size = fm.width("0") if self.fetcher.fragment.type == 'TrafficStream': x_axe_off = fm.width(unicode(value_max * 2000)) else: x_axe_off = fm.width(unicode(value_max * 200)) # Order them by max mean value means = {} for col in xrange(1, len(self.data[0])): means[col] = self.getMeanValue(col) def mean_sorter(x, y): return cmp(y[1], x[1]) means_sorted = means.items() means_sorted.sort(mean_sorter) def slope_by_index(points, index, sens): if index >= len(points): return 0 if index == 0: return points[index].y() + (points[index+1].y()-points[index].y())/smooth if index == (len(points) - 1): return points[index].y() - (points[index].y()-points[index-1].y())/smooth vpoints = [ points[index-1].y(), points[index].y(), points[index+1].y()] if points[index].y() == max(vpoints): return points[index].y() if points[index].y() == min(vpoints): return points[index].y() if sens == 0: return points[index].y() + (points[index+1].y()-points[index].y())/smooth #return points[index].y() + (points[index+1].y() - points[index-1].y()) / (points[index+1].x()-points[index-1].x()) * (points[index+1].x() - points[index].x()) / smooth else: return points[index].y() - (points[index].y()-points[index-1].y())/smooth #return points[index].y() + (points[index+1].y() - points[index-1].y()) / (points[index+1].x()-points[index-1].x()) * (points[index-1].x() - points[index].x()) / smooth colors = odict() for _col in means_sorted: col = _col[0] path = QPainterPath() path.setFillRule(Qt.WindingFill) last_point = None height_max = 0.0 width_max = step_width - 2 * margin_size - x_axe_off height_max = (step_height * (1.0 - TITLE_AREA)) - 2 * margin_size points = [] for row in xrange(len(self.data)): index = self.model().index(row, col, self.rootIndex()) value = index.data().toInt()[0] x_value = int(self.data[row][0]) #print "x=", x_value, "y=", value if value >= 0.0: height = height_max * value/value_max x = (float(x_value - xmin) / float(xmax - xmin)) * width_max point = QPointF(x + margin_size + x_axe_off, height_max - height + margin_size) points.append(point) for index, point in enumerate(points): # draw simple point painter.setBrush(QBrush(QColor(self.colours[col]).dark(200))) painter.drawEllipse(QRectF(point.x() - 2, point.y() - 2, 4, 4)) # init drawing if index == 0: path.moveTo(point) continue px = points[index-1].x() + (points[index].x() - points[index-1].x()) / smooth py = slope_by_index(points, index-1, 0) c1 = QPointF(px, py) px = points[index].x() - ( points[index].x() - points[index-1].x() ) / smooth py = slope_by_index(points, index, 1) c2 = QPointF(px, py) path.cubicTo(c1, c2, point) last_point = points[len(points)-1] if last_point: txt = self.model().headerData(col, Qt.Horizontal).toString() txt_width = fm.width(txt) txt_height = fm.height() colors[txt] = QColor(self.colours[col]) path.lineTo(QPointF(x + margin_size + x_axe_off, height_max + margin_size)) path.lineTo(QPointF(margin_size + x_axe_off, height_max + margin_size)) color = QColor(self.colours[col]) color.setAlpha(200) ## Create the gradient effect grad = QLinearGradient(QPointF(0.0, 0.0), QPointF(0.0, height_max)) grad.setColorAt(1.0, color.dark(150)) grad.setColorAt(0.95, color) grad.setColorAt(0.05, color) grad.setColorAt(0.0, Qt.white) painter.setBrush(QBrush(grad)) painter.drawPath(path) # Graduations nbr_grad = xmax - xmin dgrad = 1 while nbr_grad > 10: nbr_grad = floor(nbr_grad / 10) dgrad = dgrad * 10 if nbr_grad <= 2: dgrad = dgrad / 10 nbr_grad = 10 # Prevent for infinite loops. if dgrad < 1: dgrad = 1 dx = (float(dgrad) / float(xmax-xmin)) * width_max text_dy = fm.height() i = 0 while (i * dgrad) <= xmax - xmin: if self.fetcher.fragment.type != 'TrafficStream' or (self.fetcher.fragment.type == 'TrafficStream' and i % 4 == 0): grad_width = fm.width("0") painter.drawLine(margin_size + x_axe_off + (i*dx), height_max + margin_size, margin_size + x_axe_off + (i*dx), height_max + margin_size + grad_width) text = unicode(int(dgrad * i)) # Legend drawing: painter.translate(margin_size + x_axe_off + (i*dx), height_max + margin_size + 2*grad_width) painter.rotate(-45.0) int_time = (i * dgrad) + xmin text = QString('%ds' % (xmax - int_time)) txt_width = fm.width(text) painter.drawText(QRect(-txt_width, -dx, txt_width, 2*dx), Qt.AlignRight|Qt.AlignVCenter, text) painter.rotate(45.0) painter.translate(-(margin_size + x_axe_off + (i*dx)), -(height_max + margin_size + 2*grad_width)) i = i + 1 interval = 0 height = height_max + margin_size + txt_width * sin(45) + 30 for k, v in colors.iteritems(): painter.setPen(v) legendRect = QRect(interval, height, 10, 10) painter.drawRect(legendRect) painter.fillRect(legendRect, QBrush(v)) painter.setPen(foreground) txt_width = fm.width(k) txt_height = fm.height() painter.drawText(QRect(interval + 10, height, txt_width, txt_height), Qt.AlignRight|Qt.AlignVCenter, k) interval += 20 + txt_width painter.translate((step_width - widget_size.width()) / 2.0, (step_height - widget_size.height()) / 2.0) painter.restore()