def view(diagram): view = GtkView(model=diagram, selection=Selection()) view._qtree.resize((-100, -100, 400, 400)) item_painter = ItemPainter(view.selection) view.painter = item_painter view.bounding_box_painter = BoundingBoxPainter(item_painter) return view
def test_get_item_at_point(self): """ Hover tool only reacts on motion-notify events """ canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() box.width = 50 box.height = 50 # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() assert len(view._qtree._ids) == 1 assert not view._qtree._bucket.bounds == ( 0, 0, 0, 0), view._qtree._bucket.bounds assert view.get_item_at_point((10, 10)) is box assert view.get_item_at_point((60, 10)) is None window.destroy()
def test_item_removal(self): canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() assert len(canvas.get_all_items()) == len(view._qtree) view.focused_item = box canvas.remove(box) assert len(canvas.get_all_items()) == 0 assert len(view._qtree) == 0 window.destroy()
def test_get_item_at_point(self): """ Hover tool only reacts on motion-notify events """ canvas = Canvas() view = GtkView(canvas) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() box.width = 50 box.height = 50 # Process pending (expose) events, which cause the canvas to be drawn. while gtk.events_pending(): gtk.main_iteration() assert len(view._qtree._ids) == 1 assert not view._qtree._bucket.bounds == (0, 0, 0, 0), view._qtree._bucket.bounds assert view.get_item_at_point((10, 10)) is box assert view.get_item_at_point((60, 10)) is None window.destroy()
def test_item_removal(self): canvas = Canvas() view = GtkView(canvas) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() # Process pending (expose) events, which cause the canvas to be drawn. while gtk.events_pending(): gtk.main_iteration() assert len(canvas.get_all_items()) == len(view._qtree) view.focused_item = box canvas.remove(box) assert len(canvas.get_all_items()) == 0 assert len(view._qtree) == 0 window.destroy()
def test_custom_selection_setter(): custom_selection = CustomSelection() view = GtkView() view.selection = custom_selection assert view.selection is custom_selection
def item_at_point( view: GtkView, pos: Pos, distance: float = 0.5, exclude: Sequence[Item] = (), ) -> Optional[Item]: """Return the topmost item located at ``pos`` (x, y). Parameters: - view: a view - pos: Position, a tuple ``(x, y)`` in view coordinates - selected: if False returns first non-selected item """ item: Item vx, vy = pos rect = (vx - distance, vy - distance, distance * 2, distance * 2) for item in reversed(list(view.get_items_in_rectangle(rect))): if item in exclude: continue v2i = view.get_matrix_v2i(item) ix, iy = v2i.transform_point(vx, vy) item_distance = item.point(ix, iy) if item_distance is None: log.warning("Item distance is None for %s", item) continue if item_distance < distance: return item return None
def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
def __init__(self, graphical_editor_v, state_machine_m, *args): GtkView.__init__(self, *args) Observer.__init__(self) self._selection = state_machine_m.selection self.value_cache = ValueCache() self.observe_model(self._selection) self.observe_model(state_machine_m.root_state) self._bounding_box_painter = BoundingBoxPainter(self) self._graphical_editor = ref(graphical_editor_v)
def test_scroll_adjustments_signal(self): def handler(self, hadj, vadj): self.handled = True sc = gtk.ScrolledWindow() view = GtkView(Canvas()) view.connect('set-scroll-adjustments', handler) sc.add(view) assert view.handled
def test_pickle_with_gtk_view(canvas_fixture): pickled = pickle.dumps(canvas_fixture.canvas) c2 = pickle.loads(pickled) win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
def test_pickle_with_gtk_view_with_connection(self): canvas = create_canvas() box = canvas._tree.nodes[0] assert isinstance(box, Box) line = canvas._tree.nodes[2] assert isinstance(line, Line) view = GtkView(canvas=canvas) # from gaphas.tool import ConnectHandleTool # handle_tool = ConnectHandleTool() # handle_tool.connect(view, line, line.handles()[0], (40, 0)) # assert line.handles()[0].connected_to is box, line.handles()[0].connected_to # assert line.handles()[0].connection_data # assert line.handles()[0].disconnect # assert isinstance(line.handles()[0].disconnect, object), line.handles()[0].disconnect import io f = io.BytesIO() pickler = MyPickler(f) pickler.dump(canvas) pickled = f.getvalue() c2 = pickle.loads(pickled) from gi.repository import Gtk win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
def test_pickle_demo(): canvas = demo.create_canvas() pickled = pickle.dumps(canvas) c2 = pickle.loads(pickled) win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
def test_view_registration_2(self): """ Test view registration and destroy when view is destroyed. """ canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) assert hasattr(box, '_matrix_i2v') assert hasattr(box, '_matrix_v2i') assert box._matrix_i2v[view] assert box._matrix_v2i[view] assert len(canvas._registered_views) == 1 assert view in canvas._registered_views window.destroy() assert len(canvas._registered_views) == 0 assert view not in box._matrix_i2v assert view not in box._matrix_v2i
def test_pickle_with_gtk_view(self): canvas = create_canvas() pickled = pickle.dumps(canvas) c2 = pickle.loads(pickled) from gi.repository import Gtk win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
def test_scroll_adjustments(self): sc = gtk.ScrolledWindow() view = GtkView(Canvas()) sc.add(view) print sc.get_hadjustment(), view.hadjustment assert sc.get_hadjustment() is view.hadjustment assert sc.get_vadjustment() is view.vadjustment
def do_configure_event(self, event): if hasattr(self, "_back_buffer"): GtkView.do_configure_event(self, event) # Keep position of state machine fixed within the window, also when size of left sidebar changes window = self.get_toplevel() if window: new_widget_pos = self.translate_coordinates(window, 0, 0) if self._widget_pos: delta_pos = new_widget_pos[0] - self._widget_pos[ 0], new_widget_pos[1] - self._widget_pos[1] self._matrix.translate(-delta_pos[0] / self._matrix[0], -delta_pos[1] / self._matrix[3]) # Make sure everything's updated self.request_update((), self._canvas.get_all_items()) self._widget_pos = new_widget_pos
def test_get_handle_at_point(self): canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() box.min_width = 20 box.min_height = 30 box.matrix.translate(20, 20) box.matrix.rotate(old_div(math.pi, 1.5)) canvas.add(box) i, h = view.get_handle_at_point((20, 20)) assert i is box assert h is box.handles()[0]
def test_get_handle_at_point(self): canvas = Canvas() view = GtkView(canvas) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() box = Box() box.min_width = 20 box.min_height = 30 box.matrix.translate(20, 20) box.matrix.rotate(math.pi/1.5) canvas.add(box) i, h = view.get_handle_at_point((20, 20)) assert i is box assert h is box.handles()[0]
def test_pickle_demo(self): import demo canvas = demo.create_canvas() pickled = pickle.dumps(canvas) c2 = pickle.loads(pickled) import gtk win = gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
def test_get_handle_at_point_at_pi_div_2(self): canvas = Canvas() view = GtkView(canvas) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() box = Box() box.min_width = 20 box.min_height = 30 box.matrix.translate(20, 20) box.matrix.rotate(math.pi / 2) canvas.add(box) p = canvas.get_matrix_i2c(box).transform_point(0, 20) p = canvas.get_matrix_c2i(box).transform_point(20, 20) i, h = view.get_handle_at_point((20, 20)) assert i is box assert h is box.handles()[0]
def __init__(self): self.canvas = Canvas() self.view = GtkView(self.canvas) self.window = Gtk.Window() self.window.add(self.view) self.window.show_all() self.line = Line() self.canvas.add(self.line) self.e1 = Element() self.e2 = Element() self.e3 = Element()
def test_pickle_with_gtk_view_with_connection(canvas_fixture): box = canvas_fixture.canvas._tree.nodes[0] assert isinstance(box, Box) line = canvas_fixture.canvas._tree.nodes[2] assert isinstance(line, Line) f = io.BytesIO() pickler = MyPickler(f) pickler.dump(canvas_fixture.canvas) pickled = f.getvalue() c2 = pickle.loads(pickled) win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
class SimpleCanvas(object): """Creates a test canvas object. Adds a view, canvas, and handle connection tool to a test case. Two boxes and a line are added to the canvas as well. """ def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
def test_view_registration(): canvas = Canvas() # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 if Gtk.get_major_version() == 3: window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() else: window = Gtk.Window.new() window.set_child(view) view.model = None assert len(canvas._registered_views) == 0 view.model = canvas assert len(canvas._registered_views) == 1
def test_pickle_with_gtk_view_with_connection(self): canvas = create_canvas() box = canvas._tree.nodes[0] assert isinstance(box, Box) line = canvas._tree.nodes[2] assert isinstance(line, Line) view = GtkView(canvas=canvas) # from gaphas.tool import ConnectHandleTool # handle_tool = ConnectHandleTool() # handle_tool.connect(view, line, line.handles()[0], (40, 0)) # assert line.handles()[0].connected_to is box, line.handles()[0].connected_to # assert line.handles()[0].connection_data # assert line.handles()[0].disconnect # assert isinstance(line.handles()[0].disconnect, object), line.handles()[0].disconnect import StringIO f = StringIO.StringIO() pickler = MyPickler(f) pickler.dump(canvas) pickled = f.getvalue() c2 = pickle.loads(pickled) from gi.repository import Gtk win = Gtk.Window() view = GtkView(canvas=c2) win.add(view) view.show() win.show() view.update()
class SimpleCanvas: """Creates a test canvas object. Adds a view, canvas, and handle connection tool to a test case. Two boxes and a line are added to the canvas as well. """ def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
def test_bounding_box_calculations(view_fixture): """A view created before and after the canvas is populated should contain the same data. """ view_fixture.view.realize() view_fixture.box.matrix = (1.0, 0.0, 0.0, 1, 10, 10) line = Line() line.fuzziness = 1 line.handles()[1].pos = (30, 30) line.matrix.translate(30, 60) view_fixture.canvas.add(line) window2 = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) view2 = GtkView(canvas=view_fixture.canvas) window2.add(view2) window2.show_all() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() try: assert view2.get_item_bounding_box(view_fixture.box) assert view_fixture.view.get_item_bounding_box(view_fixture.box) assert view_fixture.view.get_item_bounding_box( view_fixture.box ) == view2.get_item_bounding_box( view_fixture.box ), f"{view_fixture.view.get_item_bounding_box(view_fixture.box)} != {view2.get_item_bounding_box(view_fixture.box)}" assert view_fixture.view.get_item_bounding_box( line ) == view2.get_item_bounding_box( line ), f"{view_fixture.view.get_item_bounding_box(line)} != {view2.get_item_bounding_box(line)}" finally: view_fixture.window.destroy() window2.destroy()
def __init__(self): self.session = Session(services=self.services) self.element_factory = self.session.get_service("element_factory") self.modeling_language = self.session.get_service("modeling_language") assert len(list(self.element_factory.select())) == 0, list( self.element_factory.select()) self.diagram = self.element_factory.create(UML.Diagram) # We need to hook up a view for now, so updates are done instantly self.view = GtkView(self.diagram, selection=Selection()) self.view.painter = ItemPainter(self.view.selection) self.view.bounding_box_painter = BoundingBoxPainter(self.view.painter) assert len(list(self.element_factory.select())) == 1, list( self.element_factory.select())
def test_bounding_box_calculations(self): """ A view created before and after the canvas is populated should contain the same data. """ canvas = Canvas() window1 = gtk.Window(gtk.WINDOW_TOPLEVEL) view1 = GtkView(canvas=canvas) window1.add(view1) view1.realize() window1.show_all() box = Box() box.matrix = (1.0, 0.0, 0.0, 1, 10,10) canvas.add(box) line = Line() line.fyzzyness = 1 line.handles()[1].pos = (30, 30) #line.split_segment(0, 3) line.matrix.translate(30, 60) canvas.add(line) window2 = gtk.Window(gtk.WINDOW_TOPLEVEL) view2 = GtkView(canvas=canvas) window2.add(view2) window2.show_all() # Process pending (expose) events, which cause the canvas to be drawn. while gtk.events_pending(): gtk.main_iteration() try: assert view2.get_item_bounding_box(box) assert view1.get_item_bounding_box(box) assert view1.get_item_bounding_box(box) == view2.get_item_bounding_box(box), '%s != %s' % (view1.get_item_bounding_box(box), view2.get_item_bounding_box(box)) assert view1.get_item_bounding_box(line) == view2.get_item_bounding_box(line), '%s != %s' % (view1.get_item_bounding_box(line), view2.get_item_bounding_box(line)) finally: window1.destroy() window2.destroy()
def __init__(self): self.canvas = Canvas() self.view = GtkView(self.canvas) self.window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) self.window.add(self.view) self.window.show_all() self.box = Box() self.canvas.add(self.box) # No gtk main loop, so updates occur instantly assert not self.canvas.require_update() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration()
def handle_at_point( view: GtkView, pos: Pos, distance: int = 6) -> Union[Tuple[Item, Handle], Tuple[None, None]]: """Look for a handle at ``pos`` and return the tuple (item, handle).""" def find(item): """Find item's handle at pos.""" v2i = view.get_matrix_v2i(item) d = distance_point_point_fast(v2i.transform_distance(0, distance)) x, y = v2i.transform_point(*pos) for h in order_handles(item.handles()): if not h.movable: continue hx, hy = h.pos if -d < (hx - x) < d and -d < (hy - y) < d: return h selection = view.selection # The focused item is the preferred item for handle grabbing if selection.focused_item: h = find(selection.focused_item) if h: return selection.focused_item, h # then try hovered item if selection.hovered_item: h = find(selection.hovered_item) if h: return selection.hovered_item, h # Last try all items, checking the bounding box first x, y = pos items = reversed( list( view.get_items_in_rectangle( (x - distance, y - distance, distance * 2, distance * 2)))) for item in items: h = find(item) if h: return item, h return None, None
def test_scroll_adjustments_signal(self): sc = Gtk.ScrolledWindow() view = GtkView(Canvas()) sc.add(view) assert view.hadjustment assert view.vadjustment assert view.hadjustment.get_value() == 0.0 assert view.hadjustment.get_lower() == 0.0 assert view.hadjustment.get_upper() == 1.0 assert view.hadjustment.get_step_increment() == 0.0 assert view.hadjustment.get_page_increment() == 1.0 assert view.hadjustment.get_page_size() == 1.0 assert view.vadjustment.get_value() == 0.0 assert view.vadjustment.get_lower() == 0.0 assert view.vadjustment.get_upper() == 1.0 assert view.vadjustment.get_step_increment() == 0.0 assert view.vadjustment.get_page_increment() == 1.0 assert view.vadjustment.get_page_size() == 1.0
def test_view_registration(view_fixture): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert view not in box._matrix_i2v assert view not in box._matrix_v2i # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert view not in box._matrix_i2v assert view not in box._matrix_v2i window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert view in box._matrix_i2v assert view in box._matrix_v2i view.canvas = None assert len(canvas._registered_views) == 0 assert view not in box._matrix_i2v assert view not in box._matrix_v2i view.canvas = canvas assert len(canvas._registered_views) == 1 assert view in box._matrix_i2v assert view in box._matrix_v2i
def test_view_registration(self): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view) view.canvas = None assert len(canvas._registered_views) == 0 assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) view.canvas = canvas assert len(canvas._registered_views) == 1 assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view)
def construct(self): """Create the widget. Returns: the newly created widget. """ assert self.diagram view = GtkView(model=self.diagram, selection=Selection()) view.drag_dest_set( Gtk.DestDefaults.ALL, DiagramPage.VIEW_DND_TARGETS, Gdk.DragAction.MOVE | Gdk.DragAction.COPY | Gdk.DragAction.LINK, ) self.diagram_css = Gtk.CssProvider.new() view.get_style_context().add_provider(self.diagram_css, Gtk.STYLE_PROVIDER_PRIORITY_USER) scrolled_window = Gtk.ScrolledWindow() scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) scrolled_window.set_shadow_type(Gtk.ShadowType.IN) scrolled_window.add(view) scrolled_window.show_all() self.widget = scrolled_window view.selection.add_handler(self._on_view_selection_changed) view.connect("drag-data-received", self._on_drag_data_received) self.view = view self.widget.action_group = create_action_group(self, "diagram") self.select_tool("toolbox-pointer") self.set_drawing_style() return self.widget
def construct(self): """ Create the widget. Returns: the newly created widget. """ assert self.diagram view = GtkView(canvas=self.diagram.canvas) try: view.set_css_name("diagramview") except AttributeError: pass # Gtk.Widget.set_css_name() is added in 3.20 view.drag_dest_set( Gtk.DestDefaults.ALL, DiagramPage.VIEW_DND_TARGETS, Gdk.DragAction.MOVE | Gdk.DragAction.COPY | Gdk.DragAction.LINK, ) scrolled_window = Gtk.ScrolledWindow() scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) scrolled_window.set_shadow_type(Gtk.ShadowType.IN) scrolled_window.add(view) scrolled_window.show_all() self.widget = scrolled_window view.connect("focus-changed", self._on_view_selection_changed) view.connect("selection-changed", self._on_view_selection_changed) view.connect_after("key-press-event", self._on_key_press_event) # view.connect("drag-drop", self._on_drag_drop) view.connect("drag-data-received", self._on_drag_data_received) self.view = view self.toolbox = DiagramToolbox(self.diagram, view) return self.widget
def construct(self): """ Create the widget. Returns: the newly created widget, a DockItem. """ assert self.diagram view = GtkView(canvas=self.diagram.canvas) view.drag_dest_set(gtk.DEST_DEFAULT_MOTION, DiagramTab.VIEW_DND_TARGETS, gtk.gdk.ACTION_MOVE | gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_LINK) scrolled_window = gtk.ScrolledWindow() scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scrolled_window.set_shadow_type(gtk.SHADOW_IN) scrolled_window.add(view) scrolled_window.show_all() view.connect('focus-changed', self._on_view_selection_changed) view.connect('selection-changed', self._on_view_selection_changed) view.connect_after('key-press-event', self._on_key_press_event) view.connect('drag-drop', self._on_drag_drop) view.connect('drag-data-received', self._on_drag_data_received) self.view = view self.toolbox = DiagramToolbox(self.diagram, view) item = DockItem(title=self.title, stock_id='gaphor-diagram') item.add(scrolled_window) self.widget = item return item