def _draw_indicator(self, event, qp): cx, cy, w, r = self._get_geometry(event) pen = qp.pen() pen.setWidth(3) pen.setColor(Qt.black) qp.setPen(pen) qp.setBrush(Qt.black) ro = r - 10 cx, cy = r + 5, r + 5 qp.translate(cx, cy) qp.rotate(self.indicator) path = QPainterPath() path.moveTo(-2, 0) path.lineTo(0, -ro) path.lineTo(2, 0) qp.drawPath(path) cr = 10 cr2 = cr / 2.0 qp.drawEllipse(-cr2, -cr2, cr, cr)
class SelectedMineral: def __init__(self, parent, code, persistent = False): self.parent = parent self.persistent = persistent self.isActive = True # maybe to delete self.code = code self.name = self.parent.qtColorsNames[self.code][0] self.color = self.parent.qtColorsNames[self.code][1] self.painterPath = QPainterPath() self.verts, self.edges = parent.fetchMineral(self.code) if not self.verts: self.isActive = False # TODO: try to make it static or abstract in the future def project(self, projector, alef=19, bet=30): self.points = {} projector.setupProjectionMatrix(alef, bet) for k, v in self.verts.iteritems(): self.points[k] = projector.project(v).tolist() for edge in self.edges.values(): p1 = self.points[edge[0]] p2 = self.points[edge[1]] self.painterPath.moveTo(p1[0], p1[1]) self.painterPath.lineTo(p2[0], p2[1])
def _draw_horizontal_ruler(self, painter, text, y_value, color): y = y_value * self.y_factor pen = QPen() pen.setCapStyle(Qt.SquareCap) pen.setColor(color) qp = QPainterPath() r = QRect(1,1,1000,1000) bb = painter.boundingRect(r,Qt.AlignLeft,text) bb = QRect(self.x_base,self.y_base - y - bb.height(), bb.width() + 2, bb.height()) # print("{} {} {} {}".format(bb.x(),bb.y(),bb.width(),bb.height())) fill_color = QColor(0,0,0) fill_color.setAlpha(128) brush = QBrush(fill_color) painter.fillRect(bb,brush) qp.moveTo(self.x_base,self.y_base - y) qp.lineTo(self.total_width + self.x_base, self.y_base - y) painter.setPen(pen) painter.drawPath(qp) text_pen = QPen() text_pen.setCapStyle(Qt.RoundCap) text_pen.setColor(color) # alpha=255=fully opaque text_pen.setWidth(1) painter.setPen(text_pen) painter.drawText(self.x_base,self.y_base - y - 5,text)
def _draw_serie(self, painter, ndx_serie, color): serie = self.data[ndx_serie] if not serie: return qp = QPainterPath() # qp.addRect(2,2,total_width-4,total_height-4) x = float(0) * self.x_factor y = serie[0] * self.y_factor qp.moveTo(self.x_centers[0],self.y_base - y) # print y_base for i in range(1,len(serie)): x = float(i) * self.x_factor y = float(serie[i]) * self.y_factor qp.lineTo(self.x_centers[i], self.y_base-y) pen = QPen() pen.setColor(color) if ndx_serie == self.ndx_best_serie: pen.setWidth(6) else: pen.setWidth(2) painter.setPen(pen) painter.drawPath(qp)
def __init__(self, parent, scene, view): super(QNodesEditor, self).__init__(parent) self.scene = scene self.scene.installEventFilter(self) gridSize = 25 gridMap = QPixmap(gridSize,gridSize) gridPainter = QPainter(gridMap) gridPainter.fillRect(0,0,gridSize,gridSize, QApplication.palette().window().color().darker(103)) gridPainter.fillRect(1,1,gridSize-2,gridSize-2, QApplication.palette().window()) gridPainter.end() self.scene.setBackgroundBrush( QBrush(gridMap) ) originSize = 50 originItem = QGraphicsPathItem() path = QPainterPath() path.moveTo(0,-originSize) path.lineTo(0,originSize) path.moveTo(-originSize,0) path.lineTo(originSize,0) originItem.setPath(path) originItem.setPen(QPen(QApplication.palette().window().color().darker(110),2)) originItem.setZValue(-2) self.scene.addItem(originItem) self.view = view self.view.setDragMode(QGraphicsView.RubberBandDrag) self.view.setRenderHint(QPainter.Antialiasing) self.connection = None
def draw_ref(self, painter, option, ref, repo, x): if repo.head_ref and posixpath.basename(ref) == 'HEAD': return x ref_text, ref_type = git_api.parse_ref(ref) if ref_type == git_api.REF_BRANCH: ref_color = self.ref_palette[ref_type, ref_text == repo.head_ref] else: ref_color = self.ref_palette.get(ref_type, self.ref_color_default) lane_size = option.rect.height() painter.setPen(QPen(Qt.black, self.ref_frame_thickness)) painter.setBrush(QBrush(ref_color)) painter.setFont(option.font) text_rect = painter.boundingRect(0, 0, 0, 0, Qt.AlignLeft | Qt.AlignTop, ref_text) text_rect.translate(-text_rect.x(), -text_rect.y()) text_rect.setWidth(text_rect.width() + self.ref_padding_x) text_rect.setHeight(text_rect.height() + self.ref_padding_y) text_rect.translate( x + text_rect.height() * self.ref_arrow_ratio, (lane_size - text_rect.height()) / 2) path = QPainterPath() path.moveTo(x, lane_size / 2) path.lineTo(text_rect.left(), text_rect.top()) path.lineTo(text_rect.right(), text_rect.top()) path.lineTo(text_rect.right(), text_rect.bottom()) path.lineTo(text_rect.left(), text_rect.bottom()) path.lineTo(x, lane_size / 2) painter.drawPath(path) painter.drawText(text_rect, Qt.AlignLeft | Qt.AlignVCenter, ref_text) return text_rect.right() + self.ref_spacing
def _draw_selected_items_in_series(self, painter): fm = painter.fontMetrics() pen = QPen() pen.setWidth(1) pen.setColor(Qt.GlobalColor.white) painter.setPen(pen) # We assume all series have the very same number of values # and all values on the same index are drawn on the same X # coordinate ndx_serie = self.best_serie_intra_ndx aesthetic_shift = 5 if ndx_serie is not None: to_draw = [] for i in range(len(self.data)): x, y = self._item_coordinates_lines(i, ndx_serie) #mainlog.debug("{} {}".format(ndx_serie,i)) v = self.data[i][ndx_serie] text = str(int(v)) to_draw.append( (self.y_base - y, text) ) last_y = 100000 for y, text in sorted( to_draw, key=lambda z : z[0], reverse=True): r = QRect(x + aesthetic_shift,0,1000,1000) bb = painter.boundingRect(r,Qt.AlignLeft,text) if bb.right() > self.width(): x = x - bb.width() - 2*aesthetic_shift# left align if y + bb.height() > last_y: y = last_y - bb.height() fill_color = QColor(16, 16, 48) fill_color.setAlpha(196) brush = QBrush(fill_color) margin = 2 r = QRect(x + aesthetic_shift - margin, y - aesthetic_shift - bb.height() - margin, bb.width() + 2*margin, bb.height() + 2*margin) painter.fillRect(r, brush) painter.drawText(x + aesthetic_shift, y - aesthetic_shift, text) last_y = y x, y = self._item_coordinates_lines(0, ndx_serie) qp = QPainterPath() qp.moveTo(x, self.y_base) qp.lineTo(x, self.y_base - self.total_height) painter.drawPath(qp)
def objectSavePath(self): """ TOWRITE :rtype: `QPainterPath`_ """ path = QPainterPath() path.lineTo(self.objectDeltaX(), self.objectDeltaY()) return path
def set_shape(self, width, height): ''' Compute the polygon to fit in width, height ''' path = QPainterPath() path.addRoundedRect(0, 0, width, height, height / 2, height / 2) path.moveTo(min(width / 2, height / 2), 0) path.lineTo(min(width / 2, height / 2), height) path.moveTo(max(width / 2, width - height / 2), 0) path.lineTo(max(width / 2, width - height / 2), height) self.setPath(path) super(Start, self).set_shape(width, height)
def set_shape(self, width, height): ''' Compute the polygon to fit in width, height ''' path = QPainterPath() path.lineTo(width - 11, 0) path.lineTo(width, height / 2) path.lineTo(width - 11, height) path.lineTo(0, height) path.lineTo(0, 0) self.setPath(path) super(Output, self).set_shape(width, height)
def set_shape(self, width, height): ''' Compute the polygon to fit in width, height ''' path = QPainterPath() path.addRect(0, 0, width, height) path.moveTo(7, 0) path.lineTo(7, height) path.moveTo(width - 7, 0) path.lineTo(width - 7, height) self.setPath(path) super(ProcedureCall, self).set_shape(width, height)
def set_shape(self, width, height): ''' Compute the polygon to fit in width, height ''' self.setPen(QPen(Qt.blue)) self.textbox_alignment = Qt.AlignLeft | Qt.AlignTop path = QPainterPath() path.moveTo(0, 0) path.lineTo(0, height) #path.moveTo(0, height / 2) #path.lineTo(width, height / 2) self.setPath(path) super(Input, self).set_shape(width, height)
def setLineFromLine(self, li): """ TOWRITE :param `li`: TOWRITE :type `li`: QPointF """ p = QPainterPath() p.moveTo(li.p1()) p.lineTo(li.p2()) self.setPath(p) self.objLine = li
def subPathList(self): """ TOWRITE :rtype: QList<QPainterPath> """ s = self.scale() # qreal trans = QTransform() trans.rotate(self.rotation()) trans.scale(s, s) ## QList<QPainterPath> pathList; pathList = [] path = self.objTextPath # QPainterPath element = QPainterPath.Element pathMoves = [] # QList<int> numMoves = 0 # int for i in range(0, path.elementCount()): # for(int i = 0; i < path.elementCount(); i++) element = path.elementAt(i) if element.isMoveTo(): pathMoves.append(i) # pathMoves << i; numMoves += 1 # numMoves++; pathMoves.append(path.elementCount()) # pathMoves << path.elementCount(); for p in range(0, len(pathMoves) - 1): # for(int p = 0; p < pathMoves.size()-1 && p < numMoves; p++) if not (p < numMoves): break subPath = QPainterPath() for i in range(pathMoves[p], pathMoves[p + 1]): # for(int i = pathMoves.value(p); i < pathMoves.value(p+1); i++) element = path.elementAt(i) if element.isMoveTo(): subPath.moveTo(element.x, element.y) elif element.isLineTo(): subPath.lineTo(element.x, element.y) elif element.isCurveTo(): subPath.cubicTo(path.elementAt(i).x, path.elementAt(i).y, # control point 1 path.elementAt(i + 1).x, path.elementAt(i + 1).y, # control point 2 path.elementAt(i + 2).x, path.elementAt(i + 2).y) # end point pathList.append(trans.map(subPath)) return pathList
def realRender(self, painter, renderPath): # TODO/PORT: Still needs work. """ TOWRITE :param `painter`: TOWRITE :type `painter`: `QPainter`_ :param `renderPath`: TOWRITE :type `renderPath`: `QPainterPath`_ """ color1 = self.objectColor() #QColor # lighter color color2 = color1.darker(150) #QColor # darker color # If we have a dark color, lighten it darkness = color1.lightness() #int threshold = 32 #int #TODO: This number may need adjusted or maybe just add it to settings. if darkness < threshold: color2 = color1 if not darkness: color1 = QColor(threshold, threshold, threshold) # lighter() does not affect pure black else : color1 = color2.lighter(100 + threshold) count = renderPath.elementCount() # int for i in range(0, count - 1): # for(int i = 0; i < count-1; ++i); elem = renderPath.elementAt(i) # QPainterPath::Element next = renderPath.elementAt(i + 1) # QPainterPath::Element if next.isMoveTo(): continue elemPath = QPainterPath() elemPath.moveTo(elem.x, elem.y) elemPath.lineTo(next.x, next.y) renderPen = QPen(QColor(0, 0, 0, 0)) renderPen.setWidthF(0) painter.setPen(renderPen) stroker = QPainterPathStroker() stroker.setWidth(0.35) stroker.setCapStyle(Qt.RoundCap) stroker.setJoinStyle(Qt.RoundJoin) realPath = stroker.createStroke(elemPath) # QPainterPath painter.drawPath(realPath) grad = QLinearGradient(elemPath.pointAtPercent(0.5), elemPath.pointAtPercent(0.0)) grad.setColorAt(0, color1) grad.setColorAt(1, color2) grad.setSpread(QGradient.ReflectSpread) painter.fillPath(realPath, QBrush(grad))
def subPathList(self): """ TOWRITE :rtype: QList<QPainterPath> """ s = self.scale() # qreal trans = QTransform() trans.rotate(self.rotation()) trans.scale(s, s) ## QList<QPainterPath> pathList; pathList = [] path = objTextPath # QPainterPath element = QPainterPath.Element pathMoves = [] # QList<int> numMoves = 0 # int for i in range(0, path.elementCount()): # for(int i = 0; i < path.elementCount(); i++) element = path.elementAt(i) if element.isMoveTo(): pathMoves.append(i) # pathMoves << i; numMoves += 1 # numMoves++; pathMoves.append(path.elementCount()) # pathMoves << path.elementCount(); for p in range(0, pathMoves.size() - 1 and numMoves): # for(int p = 0; p < pathMoves.size()-1 && p < numMoves; p++) subPath = QPainterPath() for i in range(pathMoves.value(p), pathMoves.value(p + 1)): # for(int i = pathMoves.value(p); i < pathMoves.value(p+1); i++) element = path.elementAt(i) if element.isMoveTo(): subPath.moveTo(element.x, element.y) elif element.isLineTo(): subPath.lineTo(element.x, element.y) elif element.isCurveTo(): subPath.cubicTo(path.elementAt(i).x, path.elementAt(i).y, # control point 1 path.elementAt(i + 1).x, path.elementAt(i + 1).y, # control point 2 path.elementAt(i + 2).x, path.elementAt(i + 2).y) # end point pathList.append(trans.map(subPath)) return pathList
def set_shape(self, width, height): ''' Define the symbol shape ''' circ = min(width, height) path = QPainterPath() path.addEllipse(0, 0, circ, circ) point1 = path.pointAtPercent(0.625) point2 = path.pointAtPercent(0.125) point3 = path.pointAtPercent(0.875) point4 = path.pointAtPercent(0.375) path.moveTo(point1) path.lineTo(point2) path.moveTo(point3) path.lineTo(point4) self.setPath(path) # call Join superclass, otherwise symbol will take Join shape super(Join, self).set_shape(circ, circ)
def setupPaint(self): """Draw the wire segments and handle.""" if not self.data['startIO'] or not self.data['endIO']: self.setPen(QPen(QBrush(QColor(QColor('black'))), 2)) elif self.data['startIO'].value: self.setPen(QPen(QBrush(QColor(QColor('green'))), 2)) else: self.setPen(QPen(QBrush(QColor(QColor('red'))), 2)) path = QPainterPath() path.moveTo(self.data['points'][0]) for p in self.data['points'][1:]: path.lineTo(p) if not self.complete: # An incomplete wire needs a handle path.addEllipse(self.data['points'][-1], self.radius, self.radius) self.setPath(path) self.update()
def reshape(self): ''' Update the connection or arrow shape ''' shape = QPainterPath() shape.moveTo(self.start_point) for point in self.middle_points: shape.lineTo(point) shape.lineTo(self.end_point) # If required draw an arrow head (e.g. in SDL NEXTSTATE and JOIN) if self.child.arrow_head: self.draw_arrow_head(shape, origin='head', kind=self.child.arrow_head) if self.child.arrow_tail: shape.moveTo(shape.pointAtPercent(0)) self.draw_arrow_head(shape, origin='tail', kind=self.child.arrow_head) self.setPath(shape)
def setLineFromXXYY(self, x1, y1, x2, y2): """ TOWRITE :param `x1`: TOWRITE :type `x1`: qreal :param `y1`: TOWRITE :type `y1`: qreal :param `x2`: TOWRITE :type `x2`: qreal :param `y2`: TOWRITE :type `y2`: qreal """ p = QPainterPath() p.moveTo(x1, y1) p.lineTo(x2, y2) self.setPath(p) self.objLine.setLine(x1, y1, x2, y2)
def set_shape(self, width, height): ''' Define the shape ''' path = QPainterPath() path.moveTo(15, 0) path.lineTo(0, height / 2.0) path.lineTo(15, height) path.moveTo(width - 15, 0) path.lineTo(width, height / 2.0) path.lineTo(width - 15, height) self.setPath(path) super(ContinuousSignal, self).set_shape(width, height)
def reshape(self): ''' Update the shape of the edge (redefined function) ''' path = QPainterPath() # If there is a starting point, draw a line to the first curve point if self.start_point: path.moveTo(self.source_connection.center) path.lineTo(self.bezier[0]) else: path.moveTo(self.source_connection.center) # Loop over the curve points: for group in self.bezier[1:]: path.cubicTo(*[point.center for point in group]) # If there is an ending point, draw a line to it if self.end_point: path.lineTo(self.end_connection.center) end_point = path.currentPosition() arrowhead = self.angle_arrow(path) path.lineTo(arrowhead[0]) path.moveTo(end_point) path.lineTo(arrowhead[1]) path.moveTo(end_point) try: # Add the transition label, if any (none for the START edge) font = QFont('arial', pointSize=8) metrics = QFontMetrics(font) label = self.edge.get('label', '') or '' lines = label.split('\n') width = metrics.width(max(lines)) # longest line height = metrics.height() * len(lines) # lp is the position of the center of the text pos = self.mapFromScene(*self.edge['lp']) if not self.text_label: self.text_label = QGraphicsTextItem(self.edge.get('label', ''), parent=self) self.text_label.setX(pos.x() - width / 2) self.text_label.setY(pos.y() - height / 2) self.text_label.setFont(font) # Make horizontal center alignment, as dot does self.text_label.setTextWidth( self.text_label.boundingRect().width()) fmt = QTextBlockFormat() fmt.setAlignment(Qt.AlignHCenter) cursor = self.text_label.textCursor() cursor.select(QTextCursor.Document) cursor.mergeBlockFormat(fmt) cursor.clearSelection() self.text_label.setTextCursor(cursor) self.text_label.show() except KeyError: # no label pass self.setPath(path)
def reshape(self): ''' Update the shape of the edge (redefined function) ''' path = QPainterPath() # If there is a starting point, draw a line to the first curve point if self.start_point: path.moveTo(self.source_connection.center) path.lineTo(self.bezier[0]) else: path.moveTo(self.source_connection.center) # Loop over the curve points: for group in self.bezier[1:]: path.cubicTo(*[point.center for point in group]) # If there is an ending point, draw a line to it if self.end_point: path.lineTo(self.end_connection.center) end_point = path.currentPosition() arrowhead = self.angle_arrow(path) path.lineTo(arrowhead[0]) path.moveTo(end_point) path.lineTo(arrowhead[1]) path.moveTo(end_point) try: # Add the transition label, if any (none for the START edge) font = QFont('arial', pointSize=8) metrics = QFontMetrics(font) label = self.edge.get('label', '') or '' lines = label.split('\n') width = metrics.width(max(lines)) # longest line height = metrics.height() * len(lines) # lp is the position of the center of the text pos = self.mapFromScene(*self.edge['lp']) if not self.text_label: self.text_label = QGraphicsTextItem( self.edge.get('label', ''), parent=self) self.text_label.setX(pos.x() - width / 2) self.text_label.setY(pos.y() - height / 2) self.text_label.setFont(font) # Make horizontal center alignment, as dot does self.text_label.setTextWidth( self.text_label.boundingRect().width()) fmt = QTextBlockFormat() fmt.setAlignment(Qt.AlignHCenter) cursor = self.text_label.textCursor() cursor.select(QTextCursor.Document) cursor.mergeBlockFormat(fmt) cursor.clearSelection() self.text_label.setTextCursor(cursor) self.text_label.show() except KeyError: # no label pass self.setPath(path)
def _setupPainterPath(self): painter_path = QPainterPath() painter_path.moveTo(0, 15) #left painter_path.lineTo(0, 0) #up painter_path.lineTo(self.width() - 1, 0) # right painter_path.lineTo(self.width() - 1, 15) # down painter_path.arcTo(QRect(self.width() - 6, 15, 5, 4), 0, -90) # control point1, cp2, destPoint painter_path.lineTo(5, 19) # left painter_path.arcTo(QRect(1, 15, 5, 4), 270, -90) #arc left up painter_path.closeSubpath() self._painter_path = painter_path
def paint(self, painter, option, widget): w = self.w h = self.h key = QPainterPath() key.setFillRule(Qt.WindingFill) painter.save() painter.rotate(self.angle) key.moveTo(0.0, 0.0) key.lineTo(w, 0.0) key.lineTo(w, h) key.lineTo(0.0, h) painter.fillPath(key, self.color) painter.setPen(QPen(Qt.white, 1)) key.moveTo(0, 0) painter.drawLine(0, 0, w, 0) painter.drawLine(w, 0, w, h) painter.drawLine(w, h, 0, h) painter.drawLine(0, h, 0, 0) painter.setPen(Qt.white) metric = painter.fontMetrics() margin = 5 textBound = metric.boundingRect(self.text) painter.drawText(margin, h - margin, self.text) painter.restore()
def _draw_grid(self,painter, y_base, width, height, steps): m = int(self.maxi / self.rounded_step_size) if self.timer.isActive(): self.gride_lines_number = self.timer_ticks if self.gride_lines_number >= m: self.timer.stop() else: self.gride_lines_number = m for i in range( self.gride_lines_number + 2): qp = QPainterPath() #mainlog.debug("self.rounded_step_size = {}, self.maxi={}".format(self.rounded_step_size, self.maxi)) y = y_base - self.rounded_step_size * i * self.y_factor qp.moveTo(0,y) # A bit of shuffle on y for visual effect qp.lineTo(width,y + (i % 3) - 1) pen = QPen() pen.setCapStyle(Qt.RoundCap) pen.setColor(QColor(0, 0, 25)) # alpha=255=fully opaque pen.setWidth(5) painter.setPen(pen) painter.drawPath(qp) pen = QPen() pen.setCapStyle(Qt.RoundCap) pen.setColor(QColor(30, 30, 30)) # alpha=255=fully opaque pen.setWidth(1) painter.setPen(pen) painter.drawPath(qp)
def set_shape(self, width, height): ''' Define the shape of the LABEL symbol ''' path = QPainterPath() path.addEllipse(0, height / 2, width / 4, height / 2) path.moveTo(width / 4, height * 3 / 4) path.lineTo(width / 2, height * 3 / 4) # Add arrow head path.moveTo(width / 2 - 5, height * 3 / 4 - 5) path.lineTo(width / 2, height * 3 / 4) path.lineTo(width / 2 - 5, height * 3 / 4 + 5) # Add vertical line in the middle of the symbol path.moveTo(width / 2, 0) path.lineTo(width / 2, height) # Make sure the bounding rect is withing specifications path.moveTo(width, height) self.setPath(path) super(Label, self).set_shape(width, height)
def set_shape(self, width, height): ''' Define polygon points to draw the symbol ''' path = QPainterPath() path.moveTo(width / 2, 0) path.lineTo(width, height / 2) path.lineTo(width / 2, height) path.lineTo(0, height / 2) path.lineTo(width / 2, 0) self.setPath(path) super(Decision, self).set_shape(width, height)
def set_shape(self, width, height): ''' Define the shape of the LABEL symbol ''' #print traceback.print_stack() path = QPainterPath() path.addEllipse(0, height / 2, width / 4, height / 2) path.moveTo(width / 4, height * 3 / 4) path.lineTo(width / 2, height * 3 / 4) # Add arrow head path.moveTo(width / 2 - 5, height * 3 / 4 - 5) path.lineTo(width / 2, height * 3 / 4) path.lineTo(width / 2 - 5, height * 3 / 4 + 5) # Add vertical line in the middle of the symbol path.moveTo(width / 2, 0) path.lineTo(width / 2, height) # Make sure the bounding rect is withing specifications path.moveTo(width, height) self.setPath(path) super(Label, self).set_shape(width, height)
def _draw_serie(self, painter, ndx_serie, color): last_item = len(self.data) - 1 serie = self.data[ndx_serie] serie_below = [0] * len(self.data[last_item]) if ndx_serie < len(self.data) - 1: serie_below = self.data[ ndx_serie + 1 ] qlp = QPainterPath() # The top line path qfp = QPainterPath() # the filled region path x = float(0) * self.x_factor y = serie[0] * self.y_factor qlp.moveTo(self.x_base + x,self.y_base - y) qfp.moveTo(self.x_base + x,self.y_base - y) for i in range(1,len(serie)): x = float(i) * self.x_factor y = float(serie[i]) * self.y_factor #print i,y qlp.lineTo(self.x_base + x, self.y_base-y) qfp.lineTo(self.x_base + x, self.y_base-y) for i in reversed(range(0,len(serie_below))): x = float(i) * self.x_factor y = float(serie_below[i]) * self.y_factor qfp.lineTo(self.x_base + x, self.y_base-y) qfp.closeSubpath() fill_color = QColor(color) fill_color.setAlpha(64) brush = QBrush(fill_color) painter.fillPath(qfp,brush) pen = QPen() pen.setColor(color) if ndx_serie == self.ndx_best_serie: pen.setWidth(6) else: pen.setWidth(2) painter.setPen(pen) painter.drawPath(qlp)
def objectSavePath(self): """ TOWRITE :rtype: `QPainterPath`_ """ path = QPainterPath() r = self.rect() # QRectF path.moveTo(r.bottomLeft()) path.lineTo(r.bottomRight()) path.lineTo(r.topRight()) path.lineTo(r.topLeft()) path.lineTo(r.bottomLeft()) s = self.scale() # qreal trans = QTransform() trans.rotate(self.rotation()) trans.scale(s, s) return trans.map(path)
def reshape(self): ''' Update the shape of the edge (redefined function) ''' path = QPainterPath() # If there is a starting point, draw a line to the first curve point if self.start_point: path.moveTo(self.source_connection.center) path.lineTo(self.bezier[0]) else: path.moveTo(self.source_connection.center) # Loop over the curve points: for group in self.bezier[1:]: path.cubicTo(*[point.center for point in group]) # If there is an ending point, draw a line to it if self.end_point: path.lineTo(self.end_connection.center) end_point = path.currentPosition() arrowhead = self.angle_arrow(path) path.lineTo(arrowhead[0]) path.moveTo(end_point) path.lineTo(arrowhead[1]) path.moveTo(end_point) try: # Add the transition label, if any (none for the START edge) font = QFont('arial', pointSize=8) width = QFontMetrics(font).width( self.edge.get('label', 0)) pos = self.mapFromScene(*self.edge['lp']) #path.addText(pos.x() - width/2, pos.y(), # font, self.edge['label']) if not self.text_label: self.text_label = QGraphicsTextItem( self.edge.get('label', ''), parent=self) self.text_label.setX(pos.x() - width / 2) self.text_label.setY(pos.y()) self.text_label.setFont(font) self.text_label.show() except KeyError: # no label pass self.setPath(path)
def __init__(self, scene): ''' Initialize the game ''' self.scene = scene scene.scene_left.connect(self.quit_scene) self.rocket = Rocket() self.width = self.scene.sceneRect().width() self.height = self.scene.sceneRect().height() self.screen_bottom = self.height - self.rocket.boundingRect().height() scene.addItem(self.rocket) # Compute random land points random.seed() p1 = QPointF(0.0, random.uniform(0.0, self.height)) p2 = QPointF(random.uniform(0.0, self.width / 4.0), random.uniform(0.0, self.height)) p3 = QPointF(random.uniform(p2.x(), 2 * self.width / 3.0), self.height) p4 = QPointF(p3.x() + 40.0, self.height) p5 = QPointF(self.width, random.uniform(0.0, self.height)) path = QPainterPath(p1) slope = (p2.y() - p1.y()) / (p2.x() - p1.x()) sign = 3 for point in range(int((p2.x() - p1.x()) / 5)): sign = -sign x = p1.x() + point * 5 path.lineTo(x, slope * x + sign) path.lineTo(p2) path.lineTo(p3) path.lineTo(p4) path.lineTo(p5) scene.addPath(path) # Initialize the music try: self.music = phonon.Phonon.createPlayer( phonon.Phonon.MusicCategory, phonon.Phonon.MediaSource(':/lander.mp3')) except NameError: LOG.warning('Could not initialise phonon') # Initialize the animation for the translation of the rocket self.animation = QtCore.QPropertyAnimation(self.rocket, "position") self.rocket_position = None # Initialize the animation for the rotation of the rocket self.rotation = QtCore.QPropertyAnimation(self.rocket, "angle") # Catch the key events to add user interaction: self.scene.keyPressEvent = lambda x: self.key(x) # updateCurrentTime is called by Qt when time changes self.animation.updateCurrentTime = lambda x: self.time_progress(x) # Connect signal sent at end of animation self.animation.finished.connect(self.animation_finished)
def reshape(self): ''' Update the shape of the edge (redefined function) ''' path = QPainterPath() # If there is a starting point, draw a line to the first curve point if self.start_point: path.moveTo(self.source_connection.center) path.lineTo(self.bezier[0]) else: path.moveTo(self.source_connection.center) # Loop over the curve points: for group in self.bezier[1:]: path.cubicTo(*[point.center for point in group]) # If there is an ending point, draw a line to it if self.end_point: path.lineTo(self.end_connection.center) end_point = path.currentPosition() arrowhead = self.angle_arrow(path) path.lineTo(arrowhead[0]) path.moveTo(end_point) path.lineTo(arrowhead[1]) path.moveTo(end_point) try: # Add the transition label, if any (none for the START edge) font = QFont('arial', pointSize=8) width = QFontMetrics(font).width(self.edge.get('label', 0)) pos = self.mapFromScene(*self.edge['lp']) #path.addText(pos.x() - width/2, pos.y(), # font, self.edge['label']) if not self.text_label: self.text_label = QGraphicsTextItem(self.edge.get('label', ''), parent=self) self.text_label.setX(pos.x() - width / 2) self.text_label.setY(pos.y()) self.text_label.setFont(font) self.text_label.show() except KeyError: # no label pass self.setPath(path)
def setObjectText(self, strng): """ TOWRITE :param `strng`: TOWRITE :type `strng`: QString """ self.objText = strng textPath = QPainterPath() font = QFont() font.setFamily(self.objTextFont) font.setPointSizeF(self.objTextSize) font.setBold(self.objTextBold) font.setItalic(self.objTextItalic) font.setUnderline(self.objTextUnderline) font.setStrikeOut(self.objTextStrikeOut) font.setOverline(self.objTextOverline) textPath.addText(0., 0., font, strng) # Translate the path based on the justification. jRect = textPath.boundingRect() # QRectF if self.objTextJustify == "Left": textPath.translate(-jRect.left(), 0) elif self.objTextJustify == "Center": textPath.translate(-jRect.center().x(), 0) elif self.objTextJustify == "Right": textPath.translate(-jRect.right(), 0) elif self.objTextJustify == "Aligned": pass # TODO: TextSingleObject Aligned Justification elif self.objTextJustify == "Middle": textPath.translate(-jRect.center()) elif self.objTextJustify == "Fit": pass # TODO: TextSingleObject Fit Justification elif self.objTextJustify == "Top Left": textPath.translate(-jRect.topLeft()) elif self.objTextJustify == "Top Center": textPath.translate(-jRect.center().x(), -jRect.top()) elif self.objTextJustify == "Top Right": textPath.translate(-jRect.topRight()) elif self.objTextJustify == "Middle Left": textPath.translate(-jRect.left(), -jRect.top()/2.0) elif self.objTextJustify == "Middle Center": textPath.translate(-jRect.center().x(), -jRect.top()/2.0) elif self.objTextJustify == "Middle Right": textPath.translate(-jRect.right(), -jRect.top()/2.0) elif self.objTextJustify == "Bottom Left": textPath.translate(-jRect.bottomLeft()) elif self.objTextJustify == "Bottom Center": textPath.translate(-jRect.center().x(), -jRect.bottom()) elif self.objTextJustify == "Bottom Right": textPath.translate(-jRect.bottomRight()) # Backward or Upside Down. if self.objTextBackward or self.objTextUpsideDown: horiz = 1.0 # qreal vert = 1.0 # qreal if self.objTextBackward: horiz = -1.0 if self.objTextUpsideDown: vert = -1.0 flippedPath = QPainterPath() element = QPainterPath.Element P2 = QPainterPath.Element P3 = QPainterPath.Element P4 = QPainterPath.Element for i in range(0, textPath.elementCount()): # for(int i = 0; i < textPath.elementCount(); ++i) element = textPath.elementAt(i) if element.isMoveTo(): flippedPath.moveTo(horiz * element.x, vert * element.y) elif element.isLineTo(): flippedPath.lineTo(horiz * element.x, vert * element.y) elif element.isCurveTo(): # start point P1 is not needed P2 = textPath.elementAt(i) # control point P3 = textPath.elementAt(i + 1) # control point P4 = textPath.elementAt(i + 2) # end point flippedPath.cubicTo(horiz * P2.x, vert * P2.y, horiz * P3.x, vert * P3.y, horiz * P4.x, vert * P4.y) objTextPath = flippedPath else: objTextPath = textPath # Add the grip point to the shape path. gripPath = objTextPath # QPainterPath gripPath.connectPath(objTextPath) gripPath.addRect(-0.00000001, -0.00000001, 0.00000002, 0.00000002) self.setObjectPath(gripPath)
def updatePath(self): """ TOWRITE """ path = QPainterPath() r = self.rect() # QRectF path.moveTo(r.bottomLeft()) path.lineTo(r.bottomRight()) path.lineTo(r.topRight()) path.lineTo(r.topLeft()) path.lineTo(r.bottomLeft()) # NOTE: Reverse the path so that the inside area isn't considered part of the rectangle path.lineTo(r.topLeft()) path.lineTo(r.topRight()) path.lineTo(r.bottomRight()) path.moveTo(r.bottomLeft()) self.setObjectPath(path)
def set_shape(self, width, height): ''' Define the polygon of the text symbol ''' path = QPainterPath() path.moveTo(width - 10, 0) path.lineTo(0, 0) path.lineTo(0, height) path.lineTo(width, height) path.lineTo(width, 10) path.lineTo(width - 10, 10) path.lineTo(width - 10, 0) path.lineTo(width, 10) self.setPath(path) super(TextSymbol, self).set_shape(width, height)
def set_shape(self, width, height): ''' Compute the polygon to fit in width, height ''' path = QPainterPath() path.moveTo(7, 0) path.lineTo(0, 7) path.lineTo(0, height - 7) path.lineTo(7, height) path.lineTo(width - 7, height) path.lineTo(width, height - 7) path.lineTo(width, 7) path.lineTo(width - 7, 0) path.lineTo(7, 0) self.setPath(path) super(Process, self).set_shape(width, height)
def updateLeader(self): """ TOWRITE """ arrowStyle = Closed # int # TODO: Make this customizable arrowStyleAngle = 15.0 # qreal # TODO: Make this customizable arrowStyleLength = 1.0 # qreal # TODO: Make this customizable self.lineStyleAngle = 45.0 # qreal # TODO: Make this customizable self.lineStyleLength = 1.0 # qreal # TODO: Make this customizable lyne = self.line() # QLineF angle = lyne.angle() # qreal ap0 = lyne.p1() # QPointF lp0 = lyne.p2() # QPointF # Arrow lynePerp = QLineF(lyne.pointAt(arrowStyleLength / lyne.length()), lp0) lynePerp.setAngle(angle + 90) lyne1 = QLineF(ap0, lp0) lyne2 = QLineF(ap0, lp0) lyne1.setAngle(angle + arrowStyleAngle) lyne2.setAngle(angle - arrowStyleAngle) # ap1 = QPointF() # ap2 = QPointF() _, ap1 = lynePerp.intersect(lyne1) _, ap2 = lynePerp.intersect(lyne2) # Math Diagram # .(ap1) .(lp1) # /| /| # / | / | # / | / | # / | / | # / | / | # / | / | # / | / | # / | / | # /+(aSA) | /+(lSA) | # (ap0)./__(aSL)__|__________(lp0)./__(lSL)__| # \ -(aSA) | \ -(lSA) | # \ | \ | # \ | \ | # \ | \ | # \ | \ | # \ | \ | # \ | \ | # \ | \ | # \ | \ | # \| \| # .(ap2) .(lp2) if arrowStyle == Open: arrowStylePath = QPainterPath() arrowStylePath.moveTo(ap1) arrowStylePath.lineTo(ap0) arrowStylePath.lineTo(ap2) arrowStylePath.lineTo(ap0) arrowStylePath.lineTo(ap1) elif arrowStyle == Closed: arrowStylePath = QPainterPath() arrowStylePath.moveTo(ap1) arrowStylePath.lineTo(ap0) arrowStylePath.lineTo(ap2) arrowStylePath.lineTo(ap1) elif arrowStyle == Dot: arrowStylePath = QPainterPath() arrowStylePath.addEllipse(ap0, arrowStyleLength, arrowStyleLength) elif arrowStyle == Box: arrowStylePath = QPainterPath() side = QLineF(ap1, ap2).length() # qreal ar0 = QRectF(0, 0, side, side) ar0.moveCenter(ap0) arrowStylePath.addRect(ar0) elif arrowStyle == Tick: pass #TODO/PORT# Is this supposed to be empty? lineStylePath = QPainterPath() lineStylePath.moveTo(ap0) lineStylePath.lineTo(lp0)
def updateRubber(self, painter=None): """ TOWRITE :param `painter`: TOWRITE :type `painter`: `QPainter`_ """ rubberMode = self.objectRubberMode() # int if rubberMode == OBJ_RUBBER_POLYGON: self.setObjectPos(self.objectRubberPoint("POLYGON_POINT_0")) ok = False # bool numStr = self.objectRubberText("POLYGON_NUM_POINTS") # QString if not numStr: return try: num = int(numStr) except ValueError: return appendStr = '' # QString rubberPath = QPainterPath() rubberPath.moveTo( self.mapFromScene(self.objectRubberPoint("POLYGON_POINT_0"))) for i in range(1, num): # for(int i = 1; i <= num; i++) appendStr = "POLYGON_POINT_" + '%s' % i # QString().setNum(i) appendPoint = self.mapFromScene( self.objectRubberPoint(appendStr)) # QPointF rubberPath.lineTo(appendPoint) ## rubberPath.lineTo(0,0) self.updatePath(rubberPath) # Ensure the path isn't updated until the number of points is changed again. self.setObjectRubberText("POLYGON_NUM_POINTS", '') elif rubberMode == OBJ_RUBBER_POLYGON_INSCRIBE: self.setObjectPos(self.objectRubberPoint("POLYGON_CENTER")) numSides = self.objectRubberPoint( "POLYGON_NUM_SIDES").x() # quint16 inscribePoint = self.mapFromScene( self.objectRubberPoint("POLYGON_INSCRIBE_POINT")) # QPointF inscribeLine = QLineF(QPointF(0, 0), inscribePoint) # QLineF inscribeAngle = inscribeLine.angle() # qreal inscribeInc = 360.0 / numSides # qreal if painter: self.drawRubberLine(inscribeLine, painter, "VIEW_COLOR_CROSSHAIR") inscribePath = QPainterPath() # First Point. inscribePath.moveTo(inscribePoint) # Remaining Points. for i in range(1, numSides): # for(int i = 1; i < numSides; i++) inscribeLine.setAngle(inscribeAngle + inscribeInc * i) inscribePath.lineTo(inscribeLine.p2()) self.updatePath(inscribePath) elif rubberMode == OBJ_RUBBER_POLYGON_CIRCUMSCRIBE: self.setObjectPos(self.objectRubberPoint("POLYGON_CENTER")) numSides = self.objectRubberPoint( "POLYGON_NUM_SIDES").x() # quint16 circumscribePoint = self.mapFromScene( self.objectRubberPoint( "POLYGON_CIRCUMSCRIBE_POINT")) # QPointF circumscribeLine = QLineF(QPointF(0, 0), circumscribePoint) # QLineF circumscribeAngle = circumscribeLine.angle() # qreal circumscribeInc = 360.0 / numSides # qreal if painter: self.drawRubberLine(circumscribeLine, painter, "VIEW_COLOR_CROSSHAIR") circumscribePath = QPainterPath() # First Point. prev = QLineF(circumscribeLine.p2(), QPointF(0, 0)) prev = prev.normalVector() circumscribeLine.setAngle(circumscribeAngle + circumscribeInc) perp = QLineF(circumscribeLine.p2(), QPointF(0, 0)) perp = perp.normalVector() # iPoint = QPointF() _, iPoint = perp.intersect(prev) circumscribePath.moveTo(iPoint) # Remaining Points. for i in range(2, numSides): # for(int i = 2; i <= numSides; i++) prev = perp circumscribeLine.setAngle(circumscribeAngle + circumscribeInc * i) perp = QLineF(circumscribeLine.p2(), QPointF(0, 0)) perp = perp.normalVector() perp.intersect(prev, iPoint) circumscribePath.lineTo(iPoint) self.updatePath(circumscribePath) elif rubberMode == OBJ_RUBBER_GRIP: if painter: elemCount = self.normalPath.elementCount() # int gripPoint = self.objectRubberPoint("GRIP_POINT") # QPointF if self.gripIndex == -1: self.gripIndex = self.findIndex(gripPoint) if self.gripIndex == -1: return m = 0 # int n = 0 # int if not self.gripIndex: m = elemCount - 1 n = 1 elif self.gripIndex == elemCount - 1: m = elemCount - 2 n = 0 else: m = self.gripIndex - 1 n = self.gripIndex + 1 em = self.normalPath.elementAt(m) # QPainterPath::Element en = self.normalPath.elementAt(n) # QPainterPath::Element emPoint = QPointF(em.x, em.y) # QPointF enPoint = QPointF(en.x, en.y) # QPointF painter.drawLine(emPoint, self.mapFromScene(self.objectRubberPoint(""))) painter.drawLine(enPoint, self.mapFromScene(self.objectRubberPoint(""))) rubLine = QLineF(self.mapFromScene(gripPoint), self.mapFromScene(self.objectRubberPoint(""))) self.drawRubberLine(rubLine, painter, "VIEW_COLOR_CROSSHAIR")
def updateRubber(self, painter=None): """ TOWRITE :param `painter`: TOWRITE :type `painter`: `QPainter`_ """ rubberMode = self.objectRubberMode() # int if rubberMode == OBJ_RUBBER_POLYLINE: self.setObjectPos(self.objectRubberPoint("POLYLINE_POINT_0")) rubberLine = QLineF(self.normalPath.currentPosition(), self.mapFromScene(self.objectRubberPoint(""))) if painter: self.drawRubberLine(rubberLine, painter, "VIEW_COLOR_CROSSHAIR") ok = False # bool numStr = self.objectRubberText("POLYLINE_NUM_POINTS") # QString if not numStr: return try: num = int(numStr) except ValueError: return appendStr = '' # QString rubberPath = QPainterPath() for i in range(1, num): # for(int i = 1; i <= num; i++) appendStr = "POLYLINE_POINT_" + "%s" % i # QString().setNum(i); appendPoint = self.mapFromScene(self.objectRubberPoint(appendStr)) # QPointF rubberPath.lineTo(appendPoint) self.updatePath(rubberPath) # Ensure the path isn't updated until the number of points is changed again. self.setObjectRubberText("POLYLINE_NUM_POINTS", "") elif rubberMode == OBJ_RUBBER_GRIP: if painter: elemCount = self.normalPath.elementCount() # int gripPoint = self.objectRubberPoint("GRIP_POINT") # QPointF if self.gripIndex == -1: self.gripIndex = self.findIndex(gripPoint) if self.gripIndex == -1: return if not self.gripIndex: # First. ef = self.normalPath.elementAt(1) # QPainterPath::Element efPoint = QPointF(ef.x, ef.y) # QPointF painter.drawLine(efPoint, self.mapFromScene(self.objectRubberPoint(""))) elif self.gripIndex == elemCount - 1: # Last. el = self.normalPath.elementAt(self.gripIndex - 1) # QPainterPath::Element elPoint = QPointF(el.x, el.y) # QPointF painter.drawLine(elPoint, self.mapFromScene(self.objectRubberPoint(""))) else: # Middle. em = self.normalPath.elementAt(self.gripIndex - 1) # QPainterPath::Element en = self.normalPath.elementAt(self.gripIndex + 1) # QPainterPath::Element emPoint = QPointF(em.x, em.y) # QPointF enPoint = QPointF(en.x, en.y) # QPointF painter.drawLine(emPoint, self.mapFromScene(self.objectRubberPoint(""))) painter.drawLine(enPoint, self.mapFromScene(self.objectRubberPoint(""))) rubLine = QLineF(self.mapFromScene(gripPoint), self.mapFromScene(self.objectRubberPoint(""))) self.drawRubberLine(rubLine, painter, "VIEW_COLOR_CROSSHAIR")