Ejemplo n.º 1
0
    def mousePressEvent(self, e):
        if self.supress:
            return

        self.last_press = self.itemAt(e.scenePos(), QTransform())

        if self.selection:
            if (e.button()==qt.LeftButton and not self.itemAt(e.scenePos(), QTransform())) or e.button()==qt.RightButton:
                old_selection = self.selection
                self.selection = set()
                for it in old_selection:
                    try:
                        it.selected = False
                    except AttributeError:
                        pass
        if not self.itemAt(e.scenePos(), QTransform()):
            if e.button()==qt.LeftButton:
                if e.modifiers()&qt.ShiftModifier:
                    self.selection_path_item = QGraphicsPathItem()
                    self.selection_path = path = QPainterPath()
                    self.selection_path_item.setPen(QPen(Color.selection, 0, qt.DashLine))
                    path.moveTo(e.scenePos())
                    self.selection_path_item.setPath(path)
                    self.addItem(self.selection_path_item)
            if e.button()==qt.LeftButton or (self.use_rightclick and e.button()==qt.RightButton):
                if not e.modifiers()&qt.ShiftModifier:
                    self.place(e.scenePos(), Cell.full if (e.button()==qt.LeftButton)^self.swap_buttons else Cell.empty)
        else:
            QGraphicsScene.mousePressEvent(self, e)
Ejemplo n.º 2
0
    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.fillRect(self.rect(), QtCore.Qt.black)

        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        if self.burst is not None:
            #painter.setPen(QtCore.Qt.green)
            path = QtGui.QPainterPath()
            path.moveTo(0, 0)
            for x in range(len(self._mag_spectrum)):
                y = self._mag_spectrum[x]
                path.lineTo(x, y)
            path.lineTo(len(self._mag_spectrum), 0)

            #if self._drag_x:
            painter.save()
            painter.translate(self.width(), self.height())
            scale_y = float(self.height()) / self._burst_max
            painter.scale(-self.scale_x, -scale_y)
            brush = QtGui.QBrush(QtCore.Qt.red)
            painter.fillPath(path, brush)
            painter.restore()

            painter.save()
            painter.translate(0, self.height())
            scale_y = float(self.height()) / self._burst_max
            painter.scale(self.scale_x, -scale_y)
            brush = QtGui.QBrush(QtCore.Qt.green)
            painter.fillPath(path, brush)
            painter.restore()

        painter.end()
Ejemplo n.º 3
0
   def __path(self, width):
      line = QtCore.QLineF(self.sourcePoint, self.destPoint)
      #print "Source: (%s, %s)" % (self.sourcePoint.x(), self.sourcePoint.y())
      #print "Dest: (%s, %s)" % (self.destPoint.x(), self.destPoint.y())
      #print "Delta: (%s, %s)" % (line.dx(), line.dy())

      if line.length() == 0:
         angle = 0.0
      else:
         angle = math.asin(line.dx() / line.length())

      if line.dy() <= 0:
         angle = 6.28 - angle

      #print "Angle: ", math.degrees(angle)
      cap_angle = math.radians(90) - angle
      #print "Cap angle: ", math.degrees(cap_angle)

      cap_deltax = math.sin(cap_angle) * (width/2)
      cap_deltay = math.cos(cap_angle) * (width/2)

      #print "Length: ", math.pow((math.pow(cap_deltax, 2.0)+math.pow(cap_deltay, 2.0)), 0.5)
      #print "Delta: (%f, %f)" % (cap_deltax, cap_deltay) 

      sourceP1 = QtCore.QPointF(self.sourcePoint.x() - cap_deltax, self.sourcePoint.y() + cap_deltay)
      destP1 = QtCore.QPointF(self.destPoint.x() - cap_deltax, self.destPoint.y() + cap_deltay)
      destP2 = QtCore.QPointF(self.destPoint.x() + cap_deltax, self.destPoint.y() - cap_deltay)
      sourceP2 = QtCore.QPointF(self.sourcePoint.x() + cap_deltax, self.sourcePoint.y() - cap_deltay)

      path = QtGui.QPainterPath()
      path.moveTo(sourceP1)
      path.lineTo(destP1)
      path.lineTo(destP2)
      path.lineTo(sourceP2)
      return path
Ejemplo n.º 4
0
	def paintEvent(self, event):
		painter = QtGui.QPainter()
		painter.begin(self)
		painter.fillRect(self.rect(), QtCore.Qt.black)

		painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
		if self.burst is not None:
			#painter.setPen(QtCore.Qt.green)
			path = QtGui.QPainterPath()
			path.moveTo(0, 0)
			for x in xrange(len(self._mag_spectrum)):
				y = self._mag_spectrum[x]
				path.lineTo(x, y)
			path.lineTo(len(self._mag_spectrum), 0)

			#if self._drag_x:
			painter.save()
			painter.translate(self.width(), self.height())
			scale_y = float(self.height()) / self._burst_max
			painter.scale(-self.scale_x, -scale_y)
			brush = QtGui.QBrush(QtCore.Qt.red)
			painter.fillPath(path, brush)
			painter.restore()

			painter.save()
			painter.translate(0, self.height())
			scale_y = float(self.height()) / self._burst_max
			painter.scale(self.scale_x, -scale_y)
			brush = QtGui.QBrush(QtCore.Qt.green)
			painter.fillPath(path, brush)
			painter.restore()

		painter.end()
Ejemplo n.º 5
0
 def __init__(self, *args, **kwargs):
     KineticsDisplayItem.__init__(self, *args, **kwargs)
     points = [
         QtCore.QPointF(ReacItem.defaultWidth / 4, 0),
         QtCore.QPointF(0, ReacItem.defaultHeight / 4),
         QtCore.QPointF(ReacItem.defaultWidth, ReacItem.defaultHeight / 4),
         QtCore.QPointF(3 * ReacItem.defaultWidth / 4,
                        ReacItem.defaultHeight / 2)
     ]
     path = QtGui.QPainterPath()
     path.moveTo(points[0])
     for p in points[1:]:
         path.lineTo(p)
         path.moveTo(p)
     self.gobj = QGraphicsPathItem(path, self)
     self.gobj.setPen(
         QtGui.QPen(QtCore.Qt.black, 2, Qt.SolidLine, Qt.RoundCap,
                    Qt.RoundJoin))
     self.gobj.mobj = self.mobj
     #classname = self.mobj.className
     # classname = 'ReacBase'
     # doc = moose.element('/classes/%s' % (classname)).docs
     # print "docs ",self.gobj.mobj, " ",doc
     # doc = doc.split('Description:')[-1].split('Name:')[0].strip()
     self._Kf = self.gobj.mobj.Kf
     self._Kb = self.gobj.mobj.Kb
     doc = "Kf\t: " + str(self._Kf) + "\nKb\t: " + str(self._Kb)
     self.gobj.setToolTip(doc)
Ejemplo n.º 6
0
    def mousePressEvent(self, e):
        if self.supress:
            return

        self.last_press = self.itemAt(e.scenePos(), QTransform())

        if self.selection:
            if (e.button()==qt.LeftButton and not self.itemAt(e.scenePos(), QTransform())) or e.button()==qt.RightButton:
                old_selection = self.selection
                self.selection = set()
                for it in old_selection:
                    try:
                        it.selected = False
                    except AttributeError:
                        pass
        if not self.itemAt(e.scenePos(), QTransform()):
            if e.button()==qt.LeftButton:
                if e.modifiers()&qt.ShiftModifier:
                    self.selection_path_item = QGraphicsPathItem()
                    self.selection_path = path = QPainterPath()
                    self.selection_path_item.setPen(QPen(Color.selection, 0, qt.DashLine))
                    path.moveTo(e.scenePos())
                    self.selection_path_item.setPath(path)
                    self.addItem(self.selection_path_item)
            if e.button()==qt.LeftButton or (self.use_rightclick and e.button()==qt.RightButton):
                if not e.modifiers()&qt.ShiftModifier:
                    self.place(e.scenePos(), Cell.full if (e.button()==qt.LeftButton)^self.swap_buttons else Cell.empty)
        else:
            QGraphicsScene.mousePressEvent(self, e)
Ejemplo n.º 7
0
 def setPaintPath(self, bool=False):  ## also used by waypts
     path = QPainterPath()
     for pt in self.pathMaker.pts:  ## pts on the screen 
         if not path.elementCount():
             path.moveTo(QPointF(pt))
         path.lineTo(QPointF(pt)) 
     if bool: path.closeSubpath()
     return path
Ejemplo n.º 8
0
def GetCubicPath(points: List[Tuple[int, int]]):
    """Returns a drawable path representing a 4 point cubic curve. Points should be a list of 4 tuples representing four (x, y) pixel values."""
    path = QPainterPath()
    x0, y0 = points[0]
    x1, y1 = points[1]
    x2, y2 = points[2]
    x3, y3 = points[3]
    path.moveTo(x0, y0)
    path.cubicTo(x1, y1, x2, y2, x3, y3)
    return path
Ejemplo n.º 9
0
    def _generate_path(self):
        path = QtGui.QPainterPath()

        if self.data is not None:
            sampling_interval = self.data.sampling_interval
            path.moveTo(0, 0)
            for i in range(self.data.sample_count):
                x = i * sampling_interval
                y = self.data.samples[i]
                path.lineTo(x, y)
            path.lineTo(self.data.duration, 0)
        return path
Ejemplo n.º 10
0
    def _generate_path(self):
        path = QtGui.QPainterPath()

        if self.data is not None:
            histogram = numpy.histogram(self.data, bins=self.bin_count)
            path.moveTo(0, histogram[1][0])
            for i in range(len(histogram[1]) - 1):
                x = histogram[0][i]
                y = (histogram[1][i] + histogram[1][i + 1]) / 2.0
                path.lineTo(x, y)
            path.lineTo(0, histogram[1][-1])
        return path
Ejemplo n.º 11
0
	def _generate_path(self):
		path = QtGui.QPainterPath()

		if self.data is not None:
			sampling_interval = self.data.sampling_interval
			path.moveTo(0, 0)
			for i in xrange(self.data.sample_count):
				x = i * sampling_interval
				y = self.data.samples[i]
				path.lineTo(x, y)
			path.lineTo(self.data.duration, 0)
		return path
Ejemplo n.º 12
0
	def _generate_path(self):
		path = QtGui.QPainterPath()

		if self.data is not None:
			histogram = numpy.histogram(self.data, bins=self.bin_count)
			path.moveTo(0, histogram[1][0])
			for i in xrange(len(histogram[1]) - 1):
				x = histogram[0][i]
				y = (histogram[1][i] + histogram[1][i+1]) / 2.0
				path.lineTo(x, y)
			path.lineTo(0, histogram[1][-1])
		return path
Ejemplo n.º 13
0
    def __init__(self, settings, x1, y1, x2, y2):
        QGraphicsPathItem.__init__(self)
        self.__settings = settings

        path = QPainterPath()
        path.moveTo(x1, y1)
        path.lineTo(x2, y2)
        self.setPath(path)

        self.penStyle = None
        self.penColor = None
        self.penWidth = None
Ejemplo n.º 14
0
    def __init__( self, settings, x1, y1, x2, y2 ):
        QGraphicsPathItem.__init__( self )
        self.__settings = settings

        path = QPainterPath()
        path.moveTo( x1, y1 )
        path.lineTo( x2, y2 )
        self.setPath( path )

        self.penStyle = None
        self.penColor = None
        self.penWidth = None
        return
Ejemplo n.º 15
0
    def __init__(self, *args, **kwargs):
        KineticsDisplayItem.__init__(self, *args, **kwargs)

        points = [
            QtCore.QPointF(0, TableItem.defaultWidth / 2),
            QtCore.QPointF(TableItem.defaultHeight / 2 - 2, 0),
            QtCore.QPointF(TableItem.defaultWidth / 2 + 2, 0),
            QtCore.QPointF(TableItem.defaultWidth,
                           TableItem.defaultHeight / 2),
        ]

        path = QtGui.QPainterPath()
        path.moveTo(points[0])
        for p in points[1:]:
            path.lineTo(p)
            path.moveTo(p)
        path.moveTo(0, 0)
        path.lineTo(TableItem.defaultWidth, 0)
        path.moveTo(-(TableItem.defaultWidth / 3), TableItem.defaultHeight / 4)
        path.lineTo((TableItem.defaultWidth + 10), TableItem.defaultHeight / 4)

        self.gobj = QGraphicsPathItem(path, self)
        #self.gobj.setToolTip("Need to see what to show unlike conc/nint for pool")
        tabledoc = (element(self.mobj.path)).outputValue
        self.gobj.setToolTip(str(tabledoc))
        self.gobj.setPen(
            QtGui.QPen(QtCore.Qt.black, 2, Qt.SolidLine, Qt.RoundCap,
                       Qt.RoundJoin))
        self.gobj.mobj = self.mobj
Ejemplo n.º 16
0
    def __update(self) -> None:
        color = port_colors[self.__target_type or self.current_type()]

        if self.__highlighted:
            self.setOpacity(1.0)

            self.__tooltip.setVisible(self.__highlighted)
            ttpos = self.scenePos()
            ttpos += QtCore.QPointF(
                0, -self.__tooltip.boundingRect().height() / 2)
            if self.__desc.direction == node_db.PortDescription.OUTPUT:
                ttpos += QtCore.QPointF(20, 0)
            else:
                ttpos -= QtCore.QPointF(
                    20 + self.__tooltip.boundingRect().width(), 0)
            self.__tooltip.setPos(ttpos)

        else:
            self.setOpacity(0.7)
            self.__tooltip.setVisible(False)

        if self.__highlighted or self.__target_type is not None:
            pen = QtGui.QPen()
            pen.setColor(Qt.red)
            pen.setWidth(2)
            self.setPen(pen)
            self.setBrush(color)
            rect = QtCore.QRectF(-15, -12, 30, 24)

        else:
            pen = QtGui.QPen()
            pen.setColor(QtGui.QColor(80, 80, 200))
            pen.setWidth(1)
            self.setPen(pen)
            self.setBrush(color)
            rect = QtCore.QRectF(-10, -8, 20, 16)

        path = QtGui.QPainterPath()
        if self.__desc.direction == node_db.PortDescription.INPUT:
            path.moveTo(0, rect.top())
            path.arcTo(rect, 90, 180)

        else:
            path.moveTo(0, rect.top())
            path.arcTo(rect, 90, -180)

        self.setPath(path)
Ejemplo n.º 17
0
 def refresh( self,scale):
     defaultWidth = ReacItem.defaultWidth*scale
     defaultHeight = ReacItem.defaultHeight*scale
     points = [QtCore.QPointF(defaultWidth/4, 0),
                       QtCore.QPointF(0,defaultHeight/4),
                       QtCore.QPointF(defaultWidth, defaultHeight/4),
                       QtCore.QPointF(3*defaultWidth/4,defaultHeight/2)]
     path = QtGui.QPainterPath()
     path.moveTo(points[0])
     for p in points[1:]:
         path.lineTo(p)
         path.moveTo(p)
             
     self.gobj.setPath(path)
     ReacPen = self.gobj.pen()
     defaultpenwidth = ReacItem.defaultPenWidth
     reacWidth =  defaultpenwidth*scale
     ReacPen.setWidth(reacWidth)
     self.gobj.setPen(ReacPen)
Ejemplo n.º 18
0
    def __update(self) -> None:
        color = port_colors[self.__connection.type]

        if self.__highlighted:
            pen = QtGui.QPen()
            pen.setColor(color)
            pen.setWidth(4)
            self.setPen(pen)
        else:
            pen = QtGui.QPen()
            pen.setColor(color)
            pen.setWidth(2)
            self.setPen(pen)

        pos1 = self.__src_node.portHandleScenePos(
            self.__connection.source_port)
        pos2 = self.__dest_node.portHandleScenePos(self.__connection.dest_port)
        cpos = QtCore.QPointF(min(100, abs(pos2.x() - pos1.x()) / 2), 0)

        path = QtGui.QPainterPath()
        path.moveTo(pos1)
        path.cubicTo(pos1 + cpos, pos2 - cpos, pos2)
        self.setPath(path)
Ejemplo n.º 19
0
Archivo: gui2.py Proyecto: LCAS/ToNE
    def __init__(self, diagramType, contextMenu, parent=None, scene=None):
        super(DiagramItem, self).__init__(parent, scene)

        self.arrows = []

        self.diagramType = diagramType
        self.contextMenu = contextMenu

        path = QtGui.QPainterPath()
        if self.diagramType == self.StartEnd:
            path.moveTo(200, 50)
            path.arcTo(150, 0, 50, 50, 0, 90)
            path.arcTo(50, 0, 50, 50, 90, 90)
            path.arcTo(50, 50, 50, 50, 180, 90)
            path.arcTo(150, 50, 50, 50, 270, 90)
            path.lineTo(200, 25)
            self.myPolygon = path.toFillPolygon()
        elif self.diagramType == self.Conditional:
            self.myPolygon = QtGui.QPolygonF([
                    QtCore.QPointF(-25, 0), QtCore.QPointF(0, 25),
                    QtCore.QPointF(25, 0), QtCore.QPointF(0, -25),
                    QtCore.QPointF(-25, 0)])
        elif self.diagramType == self.Step:
            self.myPolygon = QtGui.QPolygonF([
                    QtCore.QPointF(-25, -25), QtCore.QPointF(25, -25),
                    QtCore.QPointF(25, 25), QtCore.QPointF(-25, 25),
                    QtCore.QPointF(-25, -25)])
        else:
            self.myPolygon = QtGui.QPolygonF([
                    QtCore.QPointF(-30, -20), QtCore.QPointF(-15, 20),
                    QtCore.QPointF(30, 20), QtCore.QPointF(15, -20),
                    QtCore.QPointF(-30, -20)])


        self.setPolygon(self.myPolygon)
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
Ejemplo n.º 20
0
def polygonToCurvedPath(polygon, radius):

    path = QPainterPath()
    for i, pt in enumerate(polygon):

        #TODO: if two points are too close to draw the desired radius, either remove those points or draw at smaller radius
        px, py = polygon[i - 1] if i > 0 else polygon[-1]
        nx, ny = polygon[i + 1] if i < len(polygon) - 1 else polygon[0]
        x, y = pt

        if px == x:
            dy = y - py
            r = radius if dy < 0 else -radius
            p1 = QPointF(x, y + r)
        else:
            dx = x - px
            r = radius if dx < 0 else -radius
            p1 = QPointF(x + r, y)

        if x == nx:
            dy = y - ny
            r = radius if dy < 0 else -radius
            p2 = QPointF(x, y + r)
        else:
            dx = x - nx
            r = radius if dx < 0 else -radius
            p2 = QPointF(x + r, y)

        if i == 0:
            path.moveTo(p1)
        else:
            path.lineTo(p1)
        path.cubicTo(pt, pt, p2)

    path.closeSubpath()
    return path
Ejemplo n.º 21
0
def drawOutlineContour(contour, color):
    u"""
    Draws vector points using a CocoaPen.
    """
    if len(contour) == 0:
        return

    path = BezierPath()
    print 'new path'
    curves = []
    curve = [contour[0]]

    for i, point in enumerate(contour[1:]):

        if point.type == 0:
            offcurve = True
        else:
            offcurve = False

        if offcurve is True:
            curve.append(point)
        else:
            curve.append(point)
            curves.append(curve)
            curve = [point]

    point0 = curves[0][0]
    p0 = (point0.x, point0.y)
    path.moveTo(p0)

    for curve in curves:
        drawCurve(curve, path)

    path.closePath()
    fill(0, 0, 0)
    drawPath(path)
Ejemplo n.º 22
0
def polygonToCurvedPath(polygon, radius):
    
    path = QPainterPath()
    for i, pt in enumerate(polygon):
        
        #TODO: if two points are too close to draw the desired radius, either remove those points or draw at smaller radius
        px, py = polygon[i - 1] if i > 0 else polygon[-1]
        nx, ny = polygon[i + 1] if i < len(polygon) - 1 else polygon[0]
        x, y = pt
        
        if px == x:
            dy = y - py
            r = radius if dy < 0 else -radius
            p1 = QPointF(x, y + r)
        else:
            dx = x - px
            r = radius if dx < 0 else -radius
            p1 = QPointF(x + r, y)
        
        if x == nx:
            dy = y - ny
            r = radius if dy < 0 else -radius
            p2 = QPointF(x, y + r)
        else:
            dx = x - nx
            r = radius if dx < 0 else -radius
            p2 = QPointF(x + r, y)
        
        if i == 0:
            path.moveTo(p1)
        else:
            path.lineTo(p1)
        path.cubicTo(pt, pt, p2)

    path.closeSubpath()
    return path
Ejemplo n.º 23
0
 def refresh( self,scale):
     defaultWidth = TableItem.defaultWidth*scale
     defaultHeight = TableItem.defaultHeight*scale
     points = [QtCore.QPointF(0,defaultWidth/2),
               QtCore.QPointF(defaultHeight/2-2,0),
               QtCore.QPointF(defaultWidth/2+2,0),
               QtCore.QPointF(defaultWidth,defaultHeight/2)
               ]
     path = QtGui.QPainterPath()
     path.moveTo(points[0])
     for p in points[1:]:
         path.lineTo(p)
         path.moveTo(p)
     path.moveTo(0,0)
     path.lineTo(defaultWidth,0)
     path.moveTo(-(defaultWidth/3),defaultHeight/4)
     path.lineTo((defaultWidth+10),defaultHeight/4)
     self.gobj.setPath(path)
     TablePen = self.gobj.pen()
     defaultpenwidth = TableItem.defaultPenWidth
     tableWidth =  TableItem.defaultPenWidth*scale
     TablePen.setWidth(tableWidth)
     self.gobj.setPen(TablePen)
Ejemplo n.º 24
0
def contourToPath(contour):
    path = BezierPath()
    p0prev = 0
    p1prev = 0
    cp2 = None

    for segment in contour:
        points = segment[1]
        
        if segment[0] == 'M':
            p0, p1 = points
            p0prev = p0 = float(p0)
            p1prev = p1 = float(p1)
            point = (p0, p1)
            path.moveTo(point)
        elif segment[0] == 'L':
            p0, p1 = points
            p0 = float(p0)
            p1 = float(p1)
            p0prev = p0
            p1prev = p1
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'l':
            p0, p1 = points
            p0 = float(p0) + p0prev
            p1 = float(p1) + p1prev
            p0prev = p0
            p1prev = p1
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'h':
            p0 = points[0]
            p0 = float(p0) + p0prev
            p1 = p1prev
            p0prev = p0
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'H':
            p0 = points[0]
            p0 = float(p0)
            p1 = p1prev
            p0prev = p0
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'v':
            p1 = points[0]
            p1 = float(p1) + p1prev
            p0 = p0prev
            p1prev = p1
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'V':
            p1 = points[0]
            p1 = float(p1)
            p0 = p0prev
            p1prev = p1
            point = (p0, p1)
            path.lineTo(point)
        elif segment[0] == 'C':
            p0 = float(points.pop(0))
            p1 = float(points.pop(0))
            p2 = float(points.pop(0))
            p3 = float(points.pop(0))
            p4 = float(points.pop(0))
            p5 = float(points.pop(0))
            p0prev = p4
            p1prev = p5
            cp2 = reflect(p2, p3, p4, p5) # TODO: move to S, s
            path.curveTo((p0, p1), (p2, p3), (p4, p5))
                
        elif segment[0] == 'c':
            p0 = float(points.pop(0)) + p0prev
            p1 = float(points.pop(0)) + p1prev
            p2 = float(points.pop(0)) + p0prev
            p3 = float(points.pop(0)) + p1prev
            p4 = float(points.pop(0)) + p0prev
            p5 = float(points.pop(0)) + p1prev
            p0prev = p4
            p1prev = p5
            path.curveTo((p0, p1), (p2, p3), (p4, p5))
            cp2 = reflect(p2, p3, p4, p5) # TODO: move to S, s
        elif segment[0] == 's':
            p0 = float(points.pop(0)) + p0prev
            p1 = float(points.pop(0)) + p1prev
            p2 = float(points.pop(0)) + p0prev
            p3 = float(points.pop(0)) + p1prev
            p0prev = p2
            p1prev = p3
            path.curveTo(cp2, (p0, p1), (p2, p3))
        elif segment[0] == 'S':
            p0 = float(points.pop(0))
            p1 = float(points.pop(0))
            p2 = float(points.pop(0))
            p3 = float(points.pop(0))
            p0prev = p2
            p1prev = p3
            path.curveTo(cp2, (p0, p1), (p2, p3))
            #path.curveTo()
    path.closePath()
    return path
Ejemplo n.º 25
0
def contourToPath(contour):
    u"""Converts SVG contour to a path in DrawBot."""
    path = BezierPath()
    pPrev = [0.0, 0.0]
    pPrev2 = None
    previousCommand = None

    for segment in contour:
        command = segment[0]
        points = segment[1]

        relative = False

        if command.islower():
            relative = True

        command = command.lower()

        if command == 'm':
            if relative:
                points = getRelative(points, pPrev)

            path.moveTo(points[0])
        elif command in ('l', 'h', 'v'):
            if command == 'l':
                if relative:
                    points = getRelative(points, pPrev)
            elif command == 'h':
                if relative:
                    points[0][0] += pPrev[0]

                points[0].append(pPrev[1])
            elif command == 'v':
                points[0].insert(0, pPrev[0])

                if relative:
                    points[0][1] += pPrev[1]

            path.lineTo(points[0])
        elif command == 'c':
            if relative:
                points = getRelative(points, pPrev)

            path.curveTo(*points)
        elif command == 's':
            if relative:
                points = getRelative(points, pPrev)

            # TODO: From the spec:
            #
            # If there is no previous command or if the previous command was
            # not an C, c, S or s, assume the first control point is coincident
            # with the current point
            if previousCommand in ('c', 's'):
                cp = reflect(pPrev2, pPrev)
            else:
                print('TODO: implement, test')
                #copyPoint(cp, points[0])
            path.curveTo(cp, *points)

        elif command == 'z':
            continue

        copyPoint(pPrev, points[-1])

        if command in ('c', 's'):
            pPrev2 = [0.0, 0.0]
            copyPoint(pPrev2, points[-2])
        else:
            pPrev2 = None

        previousCommand = command

    path.closePath()
    return path
Ejemplo n.º 26
0
    def paintEvent(self, event):
        ## Draw Background Grid
        minorGridSpacing = 25
        lineLength = 10000
        lineCount = 100
        majorGridSpacing = 8
        minorGridColor = [100, 100, 100, 100]
        majorGridColor = [0, 0, 0, 255]

        ## Draw grid background
        painter = QPainter(self)
        painter.setPen(
            QPen(
                QColor(minorGridColor[0], minorGridColor[1], minorGridColor[2],
                       minorGridColor[3]), 1, Qt.SolidLine))
        ##TODO: This is hapazard with the line distances. Probably should be fixed
        for i in range(0, lineCount):
            painter.drawLine(minorGridSpacing * i, 0, minorGridSpacing * i,
                             lineLength)
            painter.drawLine(0, minorGridSpacing * i, lineLength,
                             minorGridSpacing * i)

        painter.setPen(
            QPen(
                QColor(majorGridColor[0], majorGridColor[1], majorGridColor[2],
                       majorGridColor[3]), 2, Qt.SolidLine))

        for i in range(0, lineCount):
            painter.drawLine(majorGridSpacing * minorGridSpacing * i, 0,
                             majorGridSpacing * minorGridSpacing * i,
                             lineLength)
            painter.drawLine(0, majorGridSpacing * minorGridSpacing * i,
                             lineLength,
                             majorGridSpacing * minorGridSpacing * i)

        painter.setPen(QPen(QColor(255, 255, 255), 2, Qt.SolidLine))
        painter.setRenderHint(QPainter.Antialiasing)
        cubicCurveFactor = 0.5
        if (self.mouseIsHot):
            mousePos = self.mapFromGlobal(QtGui.QCursor.pos())
            hotPortLoc = self.hotPort.parent.pos() + self.hotPort.pos(
            ) + QPoint(self.hotPort.width / 2, self.hotPort.height / 2)
            path = QPainterPath()
            path.moveTo(hotPortLoc)
            ##TODO CUBIC PATH
            path.cubicTo(
                hotPortLoc + QPoint(
                    (mousePos.x() - hotPortLoc.x()) * cubicCurveFactor, 0),
                mousePos - QPoint(
                    (mousePos.x() - hotPortLoc.x()) * cubicCurveFactor, 0),
                mousePos)

            painter.drawPath(path)
            # painter.drawLine(hotPortLoc.x(), hotPortLoc.y(), mousePos.x(), mousePos.y())

        for input in self.connectionManager.inputPorts:
            for connection in input.connections:
                painter.setPen(QPen(QColor(255, 255, 255), 2, Qt.SolidLine))
                painter.setRenderHint(QPainter.Antialiasing)
                port1Pos = input.parent.mapToParent(input.pos()) + QPoint(
                    input.size().width(),
                    input.size().height()) / 2 - 0 * QPoint(
                        self.size().width(), 0)
                port2Pos = connection.parent.mapToParent(connection.pos(
                )) + QPoint(connection.size().width(),
                            connection.size().height()) / 2 - 0 * QPoint(
                                self.size().width(), 0)
                path = QPainterPath()
                path.moveTo(port1Pos)
                ##TODO CUBIC PATH
                path.cubicTo(
                    port1Pos - QPoint(
                        (port1Pos.x() - port2Pos.x()) * cubicCurveFactor, 0),
                    port2Pos + QPoint(
                        (port1Pos.x() - port2Pos.x()) * cubicCurveFactor, 0),
                    port2Pos)
                painter.drawPath(path)

        if self.shouldDrawSelectionBox:
            painter.setPen(QPen(QColor(255, 255, 255), 1, Qt.DashLine))
            selectionRect = QRect(
                np.min(
                    [self.mousePressLocation.x(),
                     self.mouseDragLocation.x()]),
                np.min(
                    [self.mousePressLocation.y(),
                     self.mouseDragLocation.y()]),
                np.abs(self.mousePressLocation.x() -
                       self.mouseDragLocation.x()),
                np.abs(self.mousePressLocation.y() -
                       self.mouseDragLocation.y()))
            painter.drawRect(selectionRect)

            for brick in self.bricks:
                P1 = brick.rect().topLeft()
                brickRect = QRect(
                    brick.mapTo(self, P1),
                    QSize(brick.rect().size().width(),
                          brick.rect().size().height()))
                if selectionRect.intersects(brickRect):
                    self.addToSelected(brick)
                    if brick not in self.boxedRects:
                        self.boxedRects.append(brick)
                else:
                    if brick in self.boxedRects:
                        self.deselect(brick)
                        self.boxedRects.remove(brick)
Ejemplo n.º 27
0
def GetLinePath(points: List[Tuple[int, int]]):
    """Returns a drawable line path representing. Points should be a list of tuples representing (x, y) pixel values."""
    path = QPainterPath()
    path.moveTo(*points[0])
    [path.lineTo(x, y) for x, y in points]
    return path
Ejemplo n.º 28
0
def contourToPath(contour):
    u"""Converts SVG contour to a path in DrawBot."""
    path = BezierPath()
    pPrev = [0.0, 0.0]
    pPrev2 = None
    previousCommand = None

    for segment in contour:
        command = segment[0]
        points = segment[1]

        relative = False

        if command.islower():
            relative = True

        command = command.lower()

        if command == 'm':
            if relative:
                points = getRelative(points, pPrev)

            path.moveTo(points[0])
        elif command in ('l', 'h', 'v'):
            if command == 'l':
                if relative:
                    points = getRelative(points, pPrev)
            elif command == 'h':
                if relative:
                    points[0][0] += pPrev[0]

                points[0].append(pPrev[1])
            elif command == 'v':
                points[0].insert(0, pPrev[0])

                if relative:
                    points[0][1] += pPrev[1]

            path.lineTo(points[0])
        elif command == 'c':
            if relative:
                points = getRelative(points, pPrev)

            path.curveTo(*points)
        elif command == 's':
            if relative:
                points = getRelative(points, pPrev)

            # TODO: From the spec:
            #
            # If there is no previous command or if the previous command was
            # not an C, c, S or s, assume the first control point is coincident
            # with the current point
            if previousCommand in ('c', 's'):
                cp = reflect(pPrev2, pPrev)
            else:
                print 'TODO: implement, test'
                #copyPoint(cp, points[0])
            path.curveTo(cp, *points)

        elif command == 'z':
            continue

        copyPoint(pPrev, points[-1])

        if command in ('c', 's'):
            pPrev2 = [0.0, 0.0]
            copyPoint(pPrev2, points[-2])
        else:
            pPrev2 = None

        previousCommand = command

    path.closePath()
    return path