def __init__(self, id=None): Item.__init__(self) DiagramItem.__init__(self, id) h1, h2 = Handle(), Handle() self._handles.append(h1) self._handles.append(h2) self._ports.append(LinePort(h1.pos, h2.pos)) self._combined = None self._join_spec = self.add_text('joinSpec', pattern='{ joinSpec = %s }', style=self.STYLE_TOP, visible=self.is_join_spec_visible) self._name = self.add_text('name', style={ 'text-align': self.style.name_align, 'text-padding': self.style.name_padding, 'text-outside': self.style.name_outside, 'text-align-str': self.style.name_align_str, 'text-align-group': 'stereotype', }, editable=True) self.watch('subject<NamedElement>.name', self.on_named_element_name)\ .watch('subject<JoinNode>.joinSpec', self.on_join_node_join_spec)
def __init__(self, id=None, model=None): Item.__init__(self) DiagramItem.__init__(self, id, model) h1, h2 = Handle(), Handle() self._handles.append(h1) self._handles.append(h2) self._ports.append(LinePort(h1.pos, h2.pos)) self._combined = None self._join_spec = self.add_text( "joinSpec", pattern="{ joinSpec = %s }", style=self.STYLE_TOP, visible=self.is_join_spec_visible, ) self._name = self.add_text( "name", style={ "text-align": self.style.name_align, "text-padding": self.style.name_padding, "text-outside": self.style.name_outside, "text-align-str": self.style.name_align_str, "text-align-group": "stereotype", }, editable=True, ) self.watch("subject<NamedElement>.name", self.on_named_element_name).watch( "subject<JoinNode>.joinSpec", self.on_join_node_join_spec)
def __init__(self, id=None): Item.__init__(self) DiagramItem.__init__(self, id) h1, h2 = Handle(), Handle() self._handles.append(h1) self._handles.append(h2) self._ports.append(LinePort(h1.pos, h2.pos)) self._combined = None self._join_spec = self.add_text( "joinSpec.value", pattern="{ joinSpec = %s }", style=self.STYLE_TOP, visible=self.is_join_spec_visible ) self._name = self.add_text( "name", style={ "text-align": self.style.name_align, "text-padding": self.style.name_padding, "text-outside": self.style.name_outside, "text-align-str": self.style.name_align_str, "text-align-group": "stereotype", }, editable=True, ) self.watch("subject<NamedElement>.name", self.on_named_element_name).watch( "subject<JoinNode>.joinSpec<LiteralSpecification>.value", self.on_join_node_join_spec )
def setup_canvas(self): Item.setup_canvas(self) h1, h2 = self._handles cadd = self.canvas.solver.add_constraint c1 = EqualsConstraint(a=h1.pos.x, b=h2.pos.x) c2 = LessThanConstraint(smaller=h1.pos.y, bigger=h2.pos.y, delta=30) self._constraints.extend((cadd(c1), cadd(c2)))
def test_left_of_constraint(self): """ Test "less than" constraint (horizontal) creation. """ item = Item() p1 = Variable(1), Variable(2) p2 = Variable(3), Variable(4) item.constraint(left_of=(p1, p2)) self.assertEquals(1, len(item._constraints)) c = item._constraints[0] self.assertTrue(isinstance(c, LessThanConstraint)) self.assertEquals(1, c.smaller) self.assertEquals(3, c.bigger)
def test_above_constraint(self): """ Test "less than" constraint (vertical) creation. """ item = Item() p1 = Variable(1), Variable(2) p2 = Variable(3), Variable(4) item.constraint(above=(p1, p2)) self.assertEquals(1, len(item._constraints)) c = item._constraints[0] self.assertTrue(isinstance(c, LessThanConstraint)) self.assertEquals(2, c.smaller) self.assertEquals(4, c.bigger)
def test_line_constraint(self): """ Test line creation constraint. """ item = Item() pos = Variable(1), Variable(2) line = (Variable(3), Variable(4)), (Variable(5), Variable(6)) item.constraint(line=(pos, line)) self.assertEquals(1, len(item._constraints)) c = item._constraints[0] self.assertTrue(isinstance(c, LineConstraint)) self.assertEquals((1, 2), c._point) self.assertEquals(((3, 4), (5, 6)), c._line)
def test_vertical_constraint(self): """ Test vertical constraint creation. """ item = Item() p1 = Variable(1), Variable(2) p2 = Variable(3), Variable(4) item.constraint(vertical=(p1, p2)) self.assertEquals(1, len(item._constraints)) c = item._constraints[0] self.assertTrue(isinstance(c, EqualsConstraint)) # expect constraint on x-axis self.assertEquals(1, c.a) self.assertEquals(3, c.b)
def test_horizontal_constraint(self): """ Test horizontal constraint creation. """ item = Item() p1 = Variable(1), Variable(2) p2 = Variable(3), Variable(4) item.constraint(horizontal=(p1, p2)) self.assertEqual(1, len(item._constraints)) c = item._constraints[0] self.assertTrue(isinstance(c, EqualsConstraint)) # expect constraint on y-axis self.assertEqual(2, c.a) self.assertEqual(4, c.b)
def paint_item(self, item: Item, cairo: CairoContext) -> None: cairo.save() try: cairo.transform(item.matrix_i2c.to_cairo()) selection = self.selection item.draw( DrawContext( cairo=cairo, selected=(item in selection.selected_items), focused=(item is selection.focused_item), hovered=(item is selection.hovered_item), )) finally: cairo.restore()
def draw(self, context): """ Draw vertical line - symbol of fork and join nodes. Join specification is also drawn above the item. """ Item.draw(self, context) DiagramItem.draw(self, context) cr = context.cairo cr.set_line_width(6) h1, h2 = self._handles cr.move_to(h1.pos.x, h1.pos.y) cr.line_to(h2.pos.x, h2.pos.y) cr.stroke()
def test_segment_fails_for_item(self): """ Test if Segment aspect can be applied to Item """ item = Item() try: s = Segment(item, self.view) print item, 'segment aspect:', s except TypeError, e: print 'TypeError', e
def test_segment_fails_for_item(self): """ Test if Segment aspect can be applied to Item """ item = Item() try: s = Segment(item, self.view) except TypeError as e: pass else: assert False, 'Should not be reached'
def _draw_handles( self, item: Item, cairo: CairoContext, opacity: Optional[float] = None, ) -> None: """Draw handles for an item. The handles are drawn in non-antialiased mode for clarity. """ view = self.view model = view.model assert model cairo.save() if not opacity: opacity = (item is view.selection.focused_item) and 0.7 or 0.4 cairo.set_antialias(ANTIALIAS_NONE) cairo.set_line_width(1) get_connection = model.connections.get_connection for h in item.handles(): if not h.visible: continue # connected and not being moved, see HandleTool.on_button_press if get_connection(h): r, g, b = 1.0, 0.0, 0.0 # connected but being moved, see HandleTool.on_button_press elif get_connection(h): r, g, b = 1, 0.6, 0 elif h.movable: r, g, b = 0, 1, 0 else: r, g, b = 0, 0, 1 vx, vy = cairo.user_to_device(*item.matrix_i2c.transform_point( *h.pos)) cairo.save() cairo.identity_matrix() cairo.translate(vx, vy) cairo.rectangle(-4, -4, 8, 8) cairo.set_source_rgba(r, g, b, opacity) cairo.fill_preserve() if h.connectable: cairo.move_to(-2, -2) cairo.line_to(2, 3) cairo.move_to(2, -2) cairo.line_to(-2, 3) cairo.set_source_rgba(r / 4.0, g / 4.0, b / 4.0, opacity * 1.3) cairo.stroke() cairo.restore() cairo.restore()
def test_selection_move(self): """ Test the Selection role methods """ view = self.view item = Item() self.canvas.add(item) inmotion = InMotion(item, view) self.assertEqual((1, 0, 0, 1, 0, 0), tuple(item.matrix)) inmotion.start_move((0, 0)) inmotion.move((12, 26)) self.assertEqual((1, 0, 0, 1, 12, 26), tuple(item.matrix))
def test_selection_select(self): """ Test the Selection role methods """ view = self.view item = Item() self.canvas.add(item) selection = Selection(item, view) assert item not in view.selected_items selection.select() assert item in view.selected_items assert item is view.focused_item selection.unselect() assert item not in view.selected_items assert None is view.focused_item
def __init__(self): self.item = Item() self.pos1 = Variable(1), Variable(2) self.pos2 = Variable(3), Variable(4)
def post_update(self, context): Item.post_update(self, context) DiagramItem.post_update(self, context)
def pre_update(self, context): self.update_stereotype() Item.pre_update(self, context) DiagramItem.pre_update(self, context)
def __init__(self): self.canvas = Canvas() self.line = Line() self.canvas.add(self.line) self.view = View(self.canvas) self.item = Item()