Python PyQt5.QtCore.QLineF() Examples

The following are 15 code examples of PyQt5.QtCore.QLineF(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PyQt5.QtCore , or try the search function .
Example #1
Source File: base.py    From eddy with GNU General Public License v3.0 6 votes vote down vote up
def anchorMove(self, node, mousePos):
        """
        Move the selected anchor point.
        :type node: AbstractNode
        :type mousePos: QtCore.QPointF
        """
        nodePos = node.pos()
        snapToGrid = self.session.action('toggle_grid').isChecked()
        mousePos = snap(mousePos, self.diagram.GridSize, snapToGrid)
        path = self.mapFromItem(node, node.painterPath())
        if path.contains(mousePos):
            # Mouse is inside the shape => use this position as anchor point.
            pos = nodePos if distance(mousePos, nodePos) < 10.0 else mousePos
        else:
            # Mouse is outside the shape => use the intersection point as anchor point.
            pos = node.intersection(QtCore.QLineF(mousePos, nodePos))
            for pair in set(permutations([-1, -1, 0, 0, 1, 1], 2)):
                p = pos + QtCore.QPointF(*pair)
                if path.contains(p):
                    pos = p
                    break

        node.setAnchor(self, pos) 
Example #2
Source File: base.py    From eddy with GNU General Public License v3.0 6 votes vote down vote up
def createPath(self, source, target, points):
        """
        Returns a list of QtCore.QLineF instance representing all the visible edge pieces.
        Subpaths which are obscured by the source or target shape are excluded by this method.
        :type source: AbstractNode
        :type target: AbstractNode
        :type points: list
        :rtype: list
        """
        # Get the source node painter path (the source node is always available).
        A = self.mapFromItem(source, source.painterPath())
        B = self.mapFromItem(target, target.painterPath()) if target else None
        # Exclude all the "subpaths" which are not visible (obscured by the shapes).
        return [x for x in (QtCore.QLineF(points[i], points[i + 1]) for i in range(len(points) - 1)) \
                    if (not A.contains(x.p1()) or not A.contains(x.p2())) and \
                        (not B or (not B.contains(x.p1()) or not B.contains(x.p2())))] 
Example #3
Source File: charts.py    From Quantdom with Apache License 2.0 6 votes vote down vote up
def _generate(self, p):
        hl = np.array(
            [QtCore.QLineF(q.id, q.low, q.id, q.high) for q in Quotes]
        )
        op = np.array(
            [QtCore.QLineF(q.id - self.w, q.open, q.id, q.open) for q in Quotes]
        )
        cl = np.array(
            [
                QtCore.QLineF(q.id + self.w, q.close, q.id, q.close)
                for q in Quotes
            ]
        )
        lines = np.concatenate([hl, op, cl])
        long_bars = np.resize(Quotes.close > Quotes.open, len(lines))
        short_bars = np.resize(Quotes.close < Quotes.open, len(lines))

        p.setPen(self.bull_brush)
        p.drawLines(*lines[long_bars])

        p.setPen(self.bear_brush)
        p.drawLines(*lines[short_bars]) 
Example #4
Source File: charts.py    From Quantdom with Apache License 2.0 6 votes vote down vote up
def _generate(self, p):
        rects = np.array(
            [
                QtCore.QRectF(q.id - self.w, q.open, self.w2, q.close - q.open)
                for q in Quotes
            ]
        )

        p.setPen(self.line_pen)
        p.drawLines([QtCore.QLineF(q.id, q.low, q.id, q.high) for q in Quotes])

        p.setBrush(self.bull_brush)
        p.drawRects(*rects[Quotes.close > Quotes.open])

        p.setBrush(self.bear_brush)
        p.drawRects(*rects[Quotes.close < Quotes.open]) 
Example #5
Source File: quickplotter.py    From phidl with MIT License 5 votes vote down vote up
def add_port(self, port, is_subport = False):
        if (port.width is None) or (port.width == 0):
            x,y = port.midpoint
            cs = 1 # cross size
            pn = QPointF(x, y+cs)
            ps = QPointF(x, y-cs)
            pe = QPointF(x+cs, y)
            pw = QPointF(x-cs, y)
            qline1 = self.scene.addLine(QLineF(pn, ps))
            qline2 = self.scene.addLine(QLineF(pw, pe))
            port_shapes = [qline1,qline2]
        else:
            point1, point2 = port.endpoints
            point1 = QPointF(point1[0], point1[1])
            point2 = QPointF(point2[0], point2[1])
            qline = self.scene.addLine(QLineF(point1, point2))
            arrow_points = np.array([[0,0],[10,0],[6,4],[6,2],[0,2]])/(40)*port.width
            arrow_qpoly = QPolygonF( [QPointF(p[0], p[1]) for p in arrow_points] )
            port_scene_poly = self.scene.addPolygon(arrow_qpoly)
            port_scene_poly.setRotation(port.orientation)
            port_scene_poly.moveBy(port.midpoint[0], port.midpoint[1])
            port_shapes = [qline,port_scene_poly]
        qtext = self.scene.addText(str(port.name), self.portfont)
        port_items = port_shapes + [qtext]
        rad = port.orientation*np.pi/180
        x,y = port.endpoints[0]*1/4 +  port.endpoints[1]*3/4 + np.array([np.cos(rad), np.sin(rad)])*port.width/8
#        x,y = port.midpoint[0], port.midpoint[1]
#        x,y  = x - qtext.boundingRect().width()/2, y - qtext.boundingRect().height()/2
        qtext.setPos(QPointF(x,y))
        qtext.setFlag(QGraphicsItem.ItemIgnoresTransformations)

        if not is_subport:
            [shape.setPen(self.portpen) for shape in port_shapes]
            qtext.setDefaultTextColor(self.portfontcolor)
            self.portitems += port_items
        else:
            [shape.setPen(self.subportpen) for shape in port_shapes]
            qtext.setDefaultTextColor(self.subportfontcolor)
            self.subportitems += port_items
#        self.portlabels.append(qtext) 
Example #6
Source File: multiplot.py    From kite with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent):
        QtWidgets.QGraphicsItem.__init__(self, parent=parent)

        self.p1 = QtCore.QPointF()
        self.p2 = QtCore.QPointF()
        self.line = QtCore.QLineF(self.p1, self.p2)

        self.setOrientation(0., 0.)
        self.setZValue(10000) 
Example #7
Source File: items.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def paint(self, painter, option, widget):
        """
        Paints the path lines
        """
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setClipRect(option.exposedRect)

        color = globals.theme.color('path_connector')
        painter.setBrush(QtGui.QBrush(color))
        painter.setPen(QtGui.QPen(color, 3 * globals.TileWidth / 24, join=Qt.RoundJoin, cap=Qt.RoundCap))

        lines = []

        snl = self.nodelist
        mult = globals.TileWidth / 16
        for j, node in enumerate(snl):
            if ((j + 1) < len(snl)):
                a = QtCore.QPointF(float((snl[j]['x'] + 8) * mult) - self.x(), float((snl[j]['y'] + 8) * mult) - self.y())
                b = QtCore.QPointF(float((snl[j + 1]['x'] + 8) * mult) - self.x(), float((snl[j + 1]['y'] + 8) * mult) - self.y())
                lines.append(QtCore.QLineF(a, b))
            elif self.loops and (j + 1) == len(snl):
                a = QtCore.QPointF(float((snl[j]['x'] + 8) * mult) - self.x(), float((snl[j]['y'] + 8) * mult) - self.y())
                b = QtCore.QPointF(float((snl[0]['x'] + 8) * mult) - self.x(), float((snl[0]['y'] + 8) * mult) - self.y())
                lines.append(QtCore.QLineF(a, b))

        painter.drawLines(lines) 
Example #8
Source File: items.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def paint(self, painter, option, widget):
        """
        Paints the path lines
        """
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setClipRect(option.exposedRect)

        color = globals.theme.color('nabbit_path_connector')
        painter.setBrush(QtGui.QBrush(color))
        painter.setPen(QtGui.QPen(color, 3 * globals.TileWidth / 24, join=Qt.RoundJoin, cap=Qt.RoundCap))

        lines = []

        snl = self.nodelist
        mult = globals.TileWidth / 16
        for j, node in enumerate(snl):
            if ((j + 1) < len(snl)):
                a = QtCore.QPointF(float(snl[j]['x'] * mult) - self.x(), float(snl[j]['y'] * mult) - self.y())
                b = QtCore.QPointF(float(snl[j + 1]['x'] * mult) - self.x(), float(snl[j + 1]['y'] * mult) - self.y())
                lines.append(QtCore.QLineF(a, b))
            elif self.loops and (j + 1) == len(snl):
                a = QtCore.QPointF(float(snl[j]['x'] * mult) - self.x(), float(snl[j]['y'] * mult) - self.y())
                b = QtCore.QPointF(float(snl[0]['x'] * mult) - self.x(), float(snl[0]['y'] * mult) - self.y())
                lines.append(QtCore.QLineF(a, b))

        painter.drawLines(lines) 
Example #9
Source File: view.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def viewportEvent(self, viewportEvent):
        """
        Perform pinch to zoom feature to scale the viewport.
        :type viewportEvent: QTouchEvent
        """
        if viewportEvent.type() in {QtCore.QEvent.TouchBegin, QtCore.QEvent.TouchUpdate, QtCore.QEvent.TouchEnd}:

            if viewportEvent.type() in {QtCore.QEvent.TouchBegin, QtCore.QEvent.TouchEnd}:
                self.pinchFactor = 1.0

            pts = viewportEvent.touchPoints()
            if len(pts) == 2:
                p0 = pts[0]
                p1 = pts[1]
                p2 = midpoint(p0.pos(), p1.pos())
                pinchFactor = QtCore.QLineF(p0.pos(), p1.pos()).length() / QtCore.QLineF(p0.startPos(), p1.startPos()).length()
                pinchFactor = snapF(pinchFactor, DiagramView.PinchSize)
                if pinchFactor < DiagramView.PinchGuard[0] or pinchFactor > DiagramView.PinchGuard[1]:
                    if pinchFactor != self.pinchFactor:
                        zoom = self.zoom
                        zoom += +DiagramView.ZoomStep if pinchFactor > self.pinchFactor else -DiagramView.ZoomStep
                        zoom = clamp(zoom, DiagramView.ZoomMin, DiagramView.ZoomMax)
                        self.pinchFactor = pinchFactor
                        if zoom != self.zoom:
                            self.scaleViewOnPoint(zoom, p2.toPoint())

        return super().viewportEvent(viewportEvent)

    #############################################
    #   INTERFACE
    ################################# 
Example #10
Source File: base.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def breakPointAdd(self, mousePos):
        """
        Create a new breakpoint from the given mouse position returning its index.
        :type mousePos: QtCore.QPointF
        :rtype: int
        """
        index = 0
        point = None
        between = None
        shortest = 999

        source = self.source.anchor(self)
        target = self.target.anchor(self)
        points = [source] + self.breakpoints + [target]

        # Estimate between which breakpoints the new one is being added.
        for subpath in (QtCore.QLineF(points[i], points[i + 1]) for i in range(len(points) - 1)):
            dis, pos = projection(subpath, mousePos)
            if dis < shortest:
                point = pos
                shortest = dis
                between = subpath.p1(), subpath.p2()

        # If there is no breakpoint the new one will be appended.
        for i, breakpoint in enumerate(self.breakpoints):

            if breakpoint == between[1]:
                # In case the new breakpoint is being added between
                # the source node of this edge and the last breakpoint.
                index = i
                break

            if breakpoint == between[0]:
                # In case the new breakpoint is being added between
                # the last breakpoint and the target node of this edge.
                index = i + 1
                break

        self.session.undostack.push(CommandEdgeBreakpointAdd(self.diagram, self, index, point))
        return index 
Example #11
Source File: base.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def intersection(self, line):
        """
        Returns the intersection of the shape with the given line (in scene coordinates).
        :type line: QtCore.QLineF
        :rtype: QPointF
        """
        intersection = QtCore.QPointF()
        path = self.painterPath()
        polygon = self.mapToScene(path.toFillPolygon(self.transform()))
        for i in range(0, polygon.size() - 1):
            polyline = QtCore.QLineF(polygon[i], polygon[i + 1])
            if polyline.intersect(line, intersection) == QtCore.QLineF.BoundedIntersection:
                return intersection
        return None 
Example #12
Source File: test_functions.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def test_intersection(self):
        self.assertEqual(QtCore.QPointF(0, 0), intersection(QtCore.QLineF(QtCore.QPointF(-1, 0), QtCore.QPointF(1, 0)), QtCore.QLineF(QtCore.QPointF(0, -1), QtCore.QPointF(0, 1))))
        self.assertEqual(QtCore.QPointF(-4, 0), intersection(QtCore.QLineF(QtCore.QPointF(-10, 0), QtCore.QPointF(10, 0)), QtCore.QLineF(QtCore.QPointF(-4, -12), QtCore.QPointF(-4, 14))))
        self.assertIsNone(intersection(QtCore.QLineF(QtCore.QPointF(-1, 0), QtCore.QPointF(1, 0)), QtCore.QLineF(QtCore.QPointF(-1, 2), QtCore.QPointF(1, 2)))) 
Example #13
Source File: GraphicsItem.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def paint_drop_indicator(self, painter):
        brush = QBrush(QColor(Qt.darkRed))
        pen = QPen(brush, 2, Qt.SolidLine)
        painter.setPen(pen)
        rect = self.boundingRect()

        if self.drop_indicator_position == QAbstractItemView.AboveItem:
            painter.drawLine(QLineF(rect.topLeft(), rect.topRight()))
        else:
            painter.drawLine(QLineF(rect.bottomLeft(), rect.bottomRight())) 
Example #14
Source File: RuleItem.py    From urh with GNU General Public License v3.0 5 votes vote down vote up
def paint_drop_indicator(self, painter):
        painter.setPen(QPen(Qt.darkRed, 2, Qt.SolidLine))
        painter.setBrush(Qt.NoBrush)
        rect = self.boundingRect()

        if self.drop_indicator_position == QAbstractItemView.AboveItem:
            painter.drawLine(QLineF(rect.topLeft(), rect.topRight()))
        elif self.drop_indicator_position == QAbstractItemView.OnItem:
            painter.drawRect(rect)
        else:
            painter.drawLine(QLineF(rect.bottomLeft(), rect.bottomRight())) 
Example #15
Source File: GridScene.py    From urh with GNU General Public License v3.0 4 votes vote down vote up
def drawBackground(self, painter: QPainter, rect: QRectF):
        # freqs = np.fft.fftfreq(len(w), 1 / self.sample_rate)
        if self.draw_grid and len(self.frequencies) > 0:
            painter.setPen(QPen(painter.pen().color(), 0))
            parent_width = self.parent().width() if hasattr(self.parent(), "width") else 750
            view_rect = self.parent().view_rect() if hasattr(self.parent(), "view_rect") else rect

            font_width = self.font_metrics.width(Formatter.big_value_with_suffix(self.center_freq) + "   ")
            x_grid_size = int(view_rect.width() / parent_width * font_width)
            # x_grid_size = int(0.1 * view_rect.width()) if 0.1 * view_rect.width() > 1 else 1
            y_grid_size = 1
            x_mid = np.where(self.frequencies == 0)[0]
            x_mid = int(x_mid[0]) if len(x_mid) > 0 else 0

            left = int(rect.left()) - (int(rect.left()) % x_grid_size)
            left = left if left > 0 else 0

            top = rect.top() - (rect.top() % y_grid_size)
            bottom = rect.bottom() - (rect.bottom() % y_grid_size)
            right_border = int(rect.right()) if rect.right() < len(self.frequencies) else len(self.frequencies)

            x_range = list(range(x_mid, left, -x_grid_size)) + list(range(x_mid, right_border, x_grid_size))
            lines = [QLineF(x, rect.top(), x, bottom) for x in x_range] \
                    + [QLineF(rect.left(), y, rect.right(), y) for y in np.arange(top, bottom, y_grid_size)]

            painter.drawLines(lines)
            scale_x, scale_y = util.calc_x_y_scale(rect, self.parent())

            painter.scale(scale_x, scale_y)
            counter = -1  # Counter for Label for every second line

            for x in x_range:
                freq = self.frequencies[x]
                counter += 1

                if freq != 0 and (counter % 2 != 0): # Label for every second line
                    continue

                if freq != 0:
                    prefix = "+" if freq > 0 else ""
                    value = prefix+Formatter.big_value_with_suffix(freq, 2)
                else:
                    counter = 0
                    value = Formatter.big_value_with_suffix(self.center_freq)
                font_width = self.font_metrics.width(value)
                painter.drawText(QPointF(x / scale_x - font_width / 2, bottom / scale_y), value)