예제 #1
0
def test_merge_first_single(line, canvas, view):
    """Test single line merging starting from 1st segment."""
    line.handles()[1].pos = (20, 0)
    segment = Segment(line, canvas)
    segment.split_segment(0)

    # We start with 3 handles and 2 ports, after merging 2 handles and 1 port
    # are expected
    assert len(line.handles()) == 3
    assert len(line.ports()) == 2
    old_ports = line.ports()[:]

    segment = Segment(line, canvas)
    handles, ports = segment.merge_segment(0)
    # Deleted handles and ports
    assert 1 == len(handles)
    assert 2 == len(ports)
    # Handles and ports left after segment merging
    assert 2 == len(line.handles())
    assert 1 == len(line.ports())

    assert handles[0] not in line.handles()
    assert ports[0] not in line.ports()
    assert ports[1] not in line.ports()

    # Old ports are completely removed as they are replaced by new one port
    assert old_ports == ports

    # Finally, created port shall span between first and last handle
    port = line.ports()[0]
    assert (0, 0) == port.start.pos
    assert (20, 0) == port.end.pos
예제 #2
0
def test_constraints_after_merge(canvas, connections, line, view):
    """Test if constraints are recreated after line merge."""
    line2 = Line(connections)
    canvas.add(line2)
    head = line2.handles()[0]

    canvas.request_update(line)
    canvas.request_update(line2)
    view.update()

    HandleMove(line2, head, view).connect((25, 25))
    cinfo = connections.get_connection(head)
    assert line == cinfo.connected

    segment = Segment(line, canvas)
    segment.split_segment(0)
    assert len(line.handles()) == 3
    orig_constraint = cinfo.constraint

    segment.merge_segment(0)
    assert len(line.handles()) == 2

    h1, h2 = line.handles()
    # Connection shall be reconstrained between 1st and 2nd handle
    cinfo = canvas.connections.get_connection(head)
    assert orig_constraint != cinfo.constraint
예제 #3
0
def test_add_segment_to_line(canvas, connections):
    line = Line(connections)
    canvas.add(line)
    segment = Segment(line, canvas)
    assert 2 == len(line.handles())
    segment.split((5, 5))
    assert 3 == len(line.handles())
예제 #4
0
    def test_orthogonal_horizontal_undo(self):
        """Test orthogonal line constraints bug (#107)
        """
        canvas = Canvas()
        line = Line()
        canvas.add(line)
        assert not line.horizontal
        assert len(canvas.solver._constraints) == 0

        segment = Segment(line, None)
        segment.split_segment(0)

        line.orthogonal = True

        self.assertEqual(2, len(canvas.solver._constraints))
        after_ortho = set(canvas.solver._constraints)

        del undo_list[:]
        line.horizontal = True

        self.assertEqual(2, len(canvas.solver._constraints))

        undo()

        self.assertFalse(line.horizontal)
        self.assertEqual(2, len(canvas.solver._constraints))

        line.horizontal = True

        self.assertTrue(line.horizontal)
        self.assertEqual(2, len(canvas.solver._constraints))
예제 #5
0
    def __init__(self, canvas):
        Gtk.Window.__init__(self, title="Hello World")

        #connect the  button widget
        #connect to its clicked signal
        #add is as child to the top-level window
        #self.button = Gtk.Button(label="Click Here")
        #self.button.connect("clicked", self.on_button_clicked)

        self.label = Gtk.Label()

        self.view = GtkView()  #Gtk widget
        self.view.painter = DefaultPainter()

        self.view.canvas = canvas

        self.win_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        self.add(self.win_box)

        self.win_box.pack_start(self.label, True, True, 0)
        self.win_box.pack_start(self.view, True, True, 0)

        self.b2 = Box(60, 60)
        self.b2.min_width = 40
        self.b2.min_height = 50
        self.b2.matrix.translate(170, 170)
        canvas.add(self.b2)

        # Draw gaphas line
        self.line = Line()
        self.line.matrix.translate(100, 60)
        canvas.add(self.line)
        self.line.handles()[1].pos = (30, 30)
        self.segment = Segment(self.line, canvas)
        self.segment.split_segment(0)
def create_canvas(canvas, title):
    # Setup drawing window
    window = Gtk.Window()
    window.set_title(title)
    window.set_default_size(400, 400)

    view = GtkView()  #Gtk widget
    view.painter = DefaultPainter()
    view.canvas = canvas

    win_box = Gtk.Box(
        orientation=Gtk.Orientation.HORIZONTAL)  #added Gtk box to Gtk window
    window.add(win_box)
    win_box.pack_start(view, True, True, 0)  #added "view" widget to the box

    # Draw gaphas box
    b2 = Box(60, 60)
    b2.min_width = 40
    b2.min_height = 50
    b2.matrix.translate(170, 170)
    canvas.add(b2)

    # Draw gaphas line
    line = Line()
    line.matrix.translate(100, 60)
    canvas.add(line)
    line.handles()[1].pos = (30, 30)
    segment = Segment(line, canvas)
    segment.split_segment(0)

    window.show_all()
    window.connect("destroy", Gtk.main_quit)
예제 #7
0
def test_params_error_exc(canvas, connections):
    """Test parameter error exceptions."""
    line = Line(connections)
    segment = Segment(line, canvas)

    # There is only 1 segment
    with pytest.raises(ValueError):
        segment.split_segment(-1)

    line = Line(connections)
    segment = Segment(line, canvas)
    with pytest.raises(ValueError):
        segment.split_segment(1)

    line = Line(connections)
    # Can't split into one or less segment :)
    segment = Segment(line, canvas)
    with pytest.raises(ValueError):
        segment.split_segment(0, 1)
예제 #8
0
def test_orthogonal_line_split(canvas, line):
    """Test orthogonal line splitting."""
    # Start with no orthogonal constraints
    assert len(line._orthogonal_constraints) == 0

    segment = Segment(line, canvas)
    segment.split_segment(0)

    line.orthogonal = True

    # Check orthogonal constraints
    assert 2 == len(line._orthogonal_constraints)
    assert 3 == len(line.handles())

    Segment(line, canvas).split_segment(0)

    # 3 handles and 2 ports are expected
    # 2 constraints keep the self.line orthogonal
    assert 3 == len(line._orthogonal_constraints)
    assert 4 == len(line.handles())
    assert 3 == len(line.ports())
예제 #9
0
def create_canvas(c=None):
    if not c:
        c = Canvas()
    b = MyBox(c.connections)
    b.min_width = 20
    b.min_height = 30
    b.matrix.translate(20, 20)
    b.width = b.height = 40
    c.add(b)

    bb = Box(c.connections)
    bb.matrix.translate(10, 10)
    c.add(bb, parent=b)

    bb = Box(c.connections)
    bb.matrix.rotate(math.pi / 1.567)
    c.add(bb, parent=b)

    circle = Circle()
    h1, h2 = circle.handles()
    circle.radius = 20
    circle.matrix.translate(50, 160)
    c.add(circle)

    pb = Box(c.connections, 60, 60)
    pb.min_width = 40
    pb.min_height = 50
    pb.matrix.translate(100, 20)
    c.add(pb)

    ut = UnderlineText()
    ut.matrix.translate(100, 130)
    c.add(ut)

    t = MyText("Single line")
    t.matrix.translate(100, 170)
    c.add(t)

    line = MyLine(c.connections)
    c.add(line)
    line.handles()[1].pos = (30, 30)
    segment = Segment(line, c)
    segment.split_segment(0, 3)
    line.matrix.translate(30, 80)
    line.orthogonal = True

    return c
예제 #10
0
def test_constraints_after_split(canvas, connections, line, view):
    """Test if constraints are recreated after line split."""
    # Connect line2 to self.line
    line2 = Line(connections)
    canvas.add(line2)
    head = line2.handles()[0]
    HandleMove(line2, head, view).connect((25, 25))
    cinfo = canvas.connections.get_connection(head)
    assert line == cinfo.connected
    orig_constraint = cinfo.constraint

    Segment(line, canvas).split_segment(0)
    assert len(line.handles()) == 3
    h1, h2, h3 = line.handles()

    cinfo = canvas.connections.get_connection(head)
    assert cinfo.constraint != orig_constraint
예제 #11
0
class ConnectorTool(BlockConnectTool):
    """
	This is a port-connecting handle tool that additionally initiates the
	creation of connector lines when the user is withing gluing distance of an 
	as-yet-unconnected Port.
	"""
    def __init__(self):
        self._handle_index_glued = 0
        self._handle_index_dragged = 1
        self.grabbed_handle = None
        self.grabbed_item = None
        self._new_item = None
        self.motion_handle = None

    def on_button_press(self, event):

        glueitem, glueport, gluepos = self.view.get_port_at_point(
            (event.x, event.y), distance=10, exclude=[])
        line, handle = self.view.get_handle_at_point((event.x, event.y))
        try:
            if glueport and hasattr(glueport, "point"):
                self.toggle_highlight_ports(glueport.portinstance)
                self.line = self._create_line((event.x, event.y))
                self._new_item = self.line
                h_glue = self.line.handles()[self._handle_index_glued]
                conn = Connector(self.line, h_glue)
                sink = ConnectionSink(glueitem, glueport)
                conn.connect_port(sink)
                h_drag = self.line.handles()[self._handle_index_dragged]
                self.grab_handle(self.line, h_drag)
                return True
        except Exception as e:
            print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e

    def _create_line(self, (x, y)):

        canvas = self.view.canvas
        line = BlockLine()
        segment = Segment(line, view=self.view)
        canvas.add(line)
        x, y = self.view.get_matrix_v2i(line).transform_point(x, y)
        line.matrix.translate(x, y)
        line.fuzziness = 1.0

        return line
예제 #12
0
def test_ports_after_split(canvas, line):
    """Test ports removal after split."""
    line.handles()[1].pos = (20, 16)

    segment = Segment(line, canvas)

    segment.split_segment(0)
    handles = line.handles()
    old_ports = line.ports()[:]

    # Start with 3 handles and 2 ports
    assert len(handles) == 3
    assert len(old_ports) == 2

    # Split 1st segment again: 1st port should be deleted, but 2nd one should
    # remain untouched
    segment.split_segment(0)
    assert old_ports[0] not in line.ports()
    assert old_ports[1] == line.ports()[2]
예제 #13
0
    def test_orthogonal_line_undo(self):
        """Test orthogonal line undo
        """
        canvas = Canvas()
        line = Line()
        canvas.add(line)

        segment = Segment(line, None)
        segment.split_segment(0)

        # start with no orthogonal constraints
        assert len(canvas.solver._constraints) == 0

        line.orthogonal = True

        # check orthogonal constraints
        self.assertEqual(2, len(canvas.solver._constraints))
        self.assertEqual(3, len(line.handles()))

        undo()

        self.assertFalse(line.orthogonal)
        self.assertEqual(0, len(canvas.solver._constraints))
        self.assertEqual(2, len(line.handles()))
예제 #14
0
 def on_split_line_clicked(button):
     selection = view.selection
     if isinstance(selection.focused_item, Line):
         segment = Segment(selection.focused_item, canvas)
         segment.split_segment(0)