コード例 #1
0
def test_InfiniteLine():
    # Test basic InfiniteLine API
    plt = pg.plot()
    plt.setXRange(-10, 10)
    plt.setYRange(-10, 10)
    plt.resize(600, 600)

    # seemingly arbitrary requirements; might need longer wait time for some platforms..
    QtTest.QTest.qWaitForWindowShown(plt)
    QtTest.QTest.qWait(100)

    vline = plt.addLine(x=1)
    assert vline.angle == 90
    br = vline.mapToView(QtGui.QPolygonF(vline.boundingRect()))
    assert br.containsPoint(pg.Point(1, 5), QtCore.Qt.OddEvenFill)
    assert not br.containsPoint(pg.Point(5, 0), QtCore.Qt.OddEvenFill)
    hline = plt.addLine(y=0)
    assert hline.angle == 0
    assert hline.boundingRect().contains(pg.Point(5, 0))
    assert not hline.boundingRect().contains(pg.Point(0, 5))

    vline.setValue(2)
    assert vline.value() == 2
    vline.setPos(pg.Point(4, -5))
    assert vline.value() == 4

    oline = pg.InfiniteLine(angle=30)
    plt.addItem(oline)
    oline.setPos(pg.Point(1, -1))
    assert oline.angle == 30
    assert oline.pos() == pg.Point(1, -1)
    assert oline.value() == [1, -1]

    # test bounding rect for oblique line
    br = oline.mapToScene(oline.boundingRect())
    pos = oline.mapToScene(pg.Point(2, 0))
    assert br.containsPoint(pos, QtCore.Qt.OddEvenFill)
    px = pg.Point(-0.5, -1.0 / 3**0.5)
    assert br.containsPoint(pos + 5 * px, QtCore.Qt.OddEvenFill)
    assert not br.containsPoint(pos + 7 * px, QtCore.Qt.OddEvenFill)
コード例 #2
0
def test_mouseInteraction():
    plt = pg.plot()
    plt.scene().minDragTime = 0  # let us simulate mouse drags very quickly.
    vline = plt.addLine(x=0, movable=True)
    plt.addItem(vline)
    hline = plt.addLine(y=0, movable=True)
    hline2 = plt.addLine(y=-1, movable=False)
    plt.setXRange(-10, 10)
    plt.setYRange(-10, 10)

    # test horizontal drag
    pos = plt.plotItem.vb.mapViewToScene(pg.Point(0, 5)).toPoint()
    pos2 = pos - QtCore.QPoint(200, 200)
    mouseMove(plt, pos)
    assert vline.mouseHovering is True and hline.mouseHovering is False
    mouseDrag(plt, pos, pos2, QtCore.Qt.LeftButton)
    px = vline.pixelLength(pg.Point(1, 0), ortho=True)
    assert abs(vline.value() - plt.plotItem.vb.mapSceneToView(pos2).x()) <= px

    # test missed drag
    pos = plt.plotItem.vb.mapViewToScene(pg.Point(5, 0)).toPoint()
    pos = pos + QtCore.QPoint(0, 6)
    pos2 = pos + QtCore.QPoint(-20, -20)
    mouseMove(plt, pos)
    assert vline.mouseHovering is False and hline.mouseHovering is False
    mouseDrag(plt, pos, pos2, QtCore.Qt.LeftButton)
    assert hline.value() == 0

    # test vertical drag
    pos = plt.plotItem.vb.mapViewToScene(pg.Point(5, 0)).toPoint()
    pos2 = pos - QtCore.QPoint(50, 50)
    mouseMove(plt, pos)
    assert vline.mouseHovering is False and hline.mouseHovering is True
    mouseDrag(plt, pos, pos2, QtCore.Qt.LeftButton)
    px = hline.pixelLength(pg.Point(1, 0), ortho=True)
    assert abs(hline.value() - plt.plotItem.vb.mapSceneToView(pos2).y()) <= px

    # test non-interactive line
    pos = plt.plotItem.vb.mapViewToScene(pg.Point(5, -1)).toPoint()
    pos2 = pos - QtCore.QPoint(50, 50)
    mouseMove(plt, pos)
    assert hline2.mouseHovering == False
    mouseDrag(plt, pos, pos2, QtCore.Qt.LeftButton)
    assert hline2.value() == -1
コード例 #3
0
def test_PolyLineROI():
    rois = [(pg.PolyLineROI([[0, 0], [10, 0], [0, 15]], closed=True,
                            pen=0.3), 'closed'),
            (pg.PolyLineROI([[0, 0], [10, 0], [0, 15]], closed=False,
                            pen=0.3), 'open')]

    #plt = pg.plot()
    plt = pg.GraphicsView()
    plt.show()
    resizeWindow(plt, 200, 200)
    vb = pg.ViewBox()
    plt.scene().addItem(vb)
    vb.resize(200, 200)
    #plt.plotItem = pg.PlotItem()
    #plt.scene().addItem(plt.plotItem)
    #plt.plotItem.resize(200, 200)

    plt.scene().minDragTime = 0  # let us simulate mouse drags very quickly.

    # seemingly arbitrary requirements; might need longer wait time for some platforms..
    QtTest.QTest.qWaitForWindowShown(plt)
    QtTest.QTest.qWait(100)

    for r, name in rois:
        vb.clear()
        vb.addItem(r)
        vb.autoRange()
        app.processEvents()

        assertImageApproved(plt, 'roi/polylineroi/' + name + '_init',
                            'Init %s polyline.' % name)
        initState = r.getState()
        assert len(r.getState()['points']) == 3

        # hover over center
        center = r.mapToScene(pg.Point(3, 3))
        mouseMove(plt, center)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_hover_roi',
                            'Hover mouse over center of ROI.')

        # drag ROI
        mouseDrag(plt, center, center + pg.Point(10, -10),
                  QtCore.Qt.LeftButton)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_drag_roi',
                            'Drag mouse over center of ROI.')

        # hover over handle
        pt = r.mapToScene(pg.Point(r.getState()['points'][2]))
        mouseMove(plt, pt)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_hover_handle',
                            'Hover mouse over handle.')

        # drag handle
        mouseDrag(plt, pt, pt + pg.Point(5, 20), QtCore.Qt.LeftButton)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_drag_handle',
                            'Drag mouse over handle.')

        # hover over segment
        pt = r.mapToScene((pg.Point(r.getState()['points'][2]) +
                           pg.Point(r.getState()['points'][1])) * 0.5)
        mouseMove(plt, pt + pg.Point(0, 2))
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_hover_segment',
                            'Hover mouse over diagonal segment.')

        # click segment
        mouseClick(plt, pt, QtCore.Qt.LeftButton)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_click_segment',
                            'Click mouse over segment.')

        # drag new handle
        mouseMove(
            plt, pt + pg.Point(10, -10)
        )  # pg bug: have to move the mouse off/on again to register hover
        mouseDrag(plt, pt, pt + pg.Point(10, -10), QtCore.Qt.LeftButton)
        assertImageApproved(plt,
                            'roi/polylineroi/' + name + '_drag_new_handle',
                            'Drag mouse over created handle.')

        # clear all points
        r.clearPoints()
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_clear',
                            'All points cleared.')
        assert len(r.getState()['points']) == 0

        # call setPoints
        r.setPoints(initState['points'])
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_setpoints',
                            'Reset points to initial state.')
        assert len(r.getState()['points']) == 3

        # call setState
        r.setState(initState)
        assertImageApproved(plt, 'roi/polylineroi/' + name + '_setstate',
                            'Reset ROI to initial state.')
        assert len(r.getState()['points']) == 3