| 1 |
|
|---|
| 2 |
"""Basic functionality for line-like objects on a diagram. |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
import diacanvas |
|---|
| 6 |
from gaphor.diagram import initialize_item |
|---|
| 7 |
|
|---|
| 8 |
class DiagramLine(diacanvas.CanvasLine): |
|---|
| 9 |
"""Gaphor wrapper for lines.""" |
|---|
| 10 |
|
|---|
| 11 |
popup_menu = ( |
|---|
| 12 |
'AddSegment', |
|---|
| 13 |
'DeleteSegment', |
|---|
| 14 |
'Orthogonal', |
|---|
| 15 |
'separator', |
|---|
| 16 |
'EditDelete' |
|---|
| 17 |
) |
|---|
| 18 |
|
|---|
| 19 |
def __init__(self): |
|---|
| 20 |
|
|---|
| 21 |
self.__gobject_init__() |
|---|
| 22 |
|
|---|
| 23 |
def save (self, save_func): |
|---|
| 24 |
for prop in ('affine', 'line_width', 'color', \ |
|---|
| 25 |
'cap', 'join', 'orthogonal', 'horizontal'): |
|---|
| 26 |
save_func(prop, self.get_property(prop)) |
|---|
| 27 |
points = [ ] |
|---|
| 28 |
for h in self.handles: |
|---|
| 29 |
pos = h.get_pos_i () |
|---|
| 30 |
points.append (pos) |
|---|
| 31 |
save_func('points', points) |
|---|
| 32 |
c = self.handles[0].connected_to |
|---|
| 33 |
if c: |
|---|
| 34 |
save_func('head_connection', c, reference=True) |
|---|
| 35 |
c = self.handles[-1].connected_to |
|---|
| 36 |
if c: |
|---|
| 37 |
save_func ('tail_connection', c, reference=True) |
|---|
| 38 |
|
|---|
| 39 |
def load (self, name, value): |
|---|
| 40 |
if name == 'points': |
|---|
| 41 |
points = eval(value) |
|---|
| 42 |
self.set_property('head_pos', points[0]) |
|---|
| 43 |
self.set_property('tail_pos', points[1]) |
|---|
| 44 |
for p in points[2:]: |
|---|
| 45 |
self.set_property ('add_point', p) |
|---|
| 46 |
elif name == 'head_connection': |
|---|
| 47 |
self._load_head_connection = value |
|---|
| 48 |
elif name == 'tail_connection': |
|---|
| 49 |
self._load_tail_connection = value |
|---|
| 50 |
else: |
|---|
| 51 |
try: |
|---|
| 52 |
self.set_property(name, eval(value)) |
|---|
| 53 |
except: |
|---|
| 54 |
log.warning('%s has no property named %s (value %s)' % (self, name, value)) |
|---|
| 55 |
|
|---|
| 56 |
def postload(self): |
|---|
| 57 |
if hasattr(self, '_load_head_connection'): |
|---|
| 58 |
self._load_head_connection.connect_handle(self.handles[0]) |
|---|
| 59 |
del self._load_head_connection |
|---|
| 60 |
if hasattr(self, '_load_tail_connection'): |
|---|
| 61 |
self._load_tail_connection.connect_handle(self.handles[-1]) |
|---|
| 62 |
del self._load_tail_connection |
|---|
| 63 |
|
|---|
| 64 |
def has_capability(self, capability): |
|---|
| 65 |
|
|---|
| 66 |
if capability == 'orthogonal': |
|---|
| 67 |
return self.get_property('orthogonal') |
|---|
| 68 |
elif capability == 'del_segment': |
|---|
| 69 |
if self.get_property('orthogonal'): |
|---|
| 70 |
return len(self.handles) > 3 |
|---|
| 71 |
else: |
|---|
| 72 |
return len(self.handles) > 2 |
|---|
| 73 |
return False |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
def find_relationship(self, head_subject, tail_subject): |
|---|
| 84 |
"""Find an already existing relationship between head_subject and |
|---|
| 85 |
tail_subject. The following things should be taken into account: |
|---|
| 86 |
- The returned relationship object will be used for this item. |
|---|
| 87 |
- The relationship should not already exist in the canvas. |
|---|
| 88 |
""" |
|---|
| 89 |
return None |
|---|
| 90 |
|
|---|
| 91 |
def allow_connect_handle(self, handle, connecting_to): |
|---|
| 92 |
"""This method is called by a canvas item if the user tries to |
|---|
| 93 |
connect this object's handle. allow_connect_handle() checks if |
|---|
| 94 |
the line is allowed to be connected. In this case that means |
|---|
| 95 |
that one end of the line should be connected to a Relationship. |
|---|
| 96 |
Returns: True if connection is allowed, False otherwise. |
|---|
| 97 |
""" |
|---|
| 98 |
return False |
|---|
| 99 |
|
|---|
| 100 |
def confirm_connect_handle (self, handle): |
|---|
| 101 |
"""This method is called after a connection is established. |
|---|
| 102 |
This method sets the internal state of the line and updates |
|---|
| 103 |
the data model. Returns nothing |
|---|
| 104 |
""" |
|---|
| 105 |
pass |
|---|
| 106 |
|
|---|
| 107 |
def allow_disconnect_handle (self, handle): |
|---|
| 108 |
""" If a handle wants to disconnect, this method is called first. |
|---|
| 109 |
This method is here mainly for the sake of completeness, since it |
|---|
| 110 |
is quite unlikely that a handle is not allowed to disconnect. |
|---|
| 111 |
""" |
|---|
| 112 |
return 1 |
|---|
| 113 |
|
|---|
| 114 |
def confirm_disconnect_handle (self, handle, was_connected_to): |
|---|
| 115 |
"""This method is called to do some cleanup after 'self' has been |
|---|
| 116 |
disconnected from 'was_connected_to'. |
|---|
| 117 |
""" |
|---|
| 118 |
pass |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
initialize_item(DiagramLine) |
|---|