| 1 |
|
|---|
| 2 |
''' |
|---|
| 3 |
Feature diagram item. Feature is a super class of both Attribute, Operations and |
|---|
| 4 |
Methods. |
|---|
| 5 |
''' |
|---|
| 6 |
|
|---|
| 7 |
import gobject |
|---|
| 8 |
import pango |
|---|
| 9 |
import diacanvas |
|---|
| 10 |
import diacanvas.shape |
|---|
| 11 |
from diacanvas import CanvasItem, CanvasEditable |
|---|
| 12 |
from diacanvas.geometry import distance_rectangle_point |
|---|
| 13 |
from diagramitem import DiagramItem |
|---|
| 14 |
|
|---|
| 15 |
from gaphor.diagram import initialize_item |
|---|
| 16 |
|
|---|
| 17 |
class FeatureItem(CanvasItem, CanvasEditable, DiagramItem): |
|---|
| 18 |
"""FeatureItems are model elements who recide inside a ClassItem, such as |
|---|
| 19 |
methods and attributes. Those items can have comments attached, but only on |
|---|
| 20 |
the left and right side. |
|---|
| 21 |
Note that features can also be used inside objects. |
|---|
| 22 |
""" |
|---|
| 23 |
|
|---|
| 24 |
__gproperties__ = { |
|---|
| 25 |
'expression': (gobject.TYPE_STRING, 'expression', |
|---|
| 26 |
'The expression represented by this Feature', |
|---|
| 27 |
'', gobject.PARAM_READWRITE) |
|---|
| 28 |
} |
|---|
| 29 |
__gproperties__.update(DiagramItem.__gproperties__) |
|---|
| 30 |
|
|---|
| 31 |
__gsignals__ = DiagramItem.__gsignals__ |
|---|
| 32 |
|
|---|
| 33 |
FONT='sans 10' |
|---|
| 34 |
|
|---|
| 35 |
def __init__(self, id=None): |
|---|
| 36 |
self.__gobject_init__() |
|---|
| 37 |
DiagramItem.__init__(self, id) |
|---|
| 38 |
self._expression = diacanvas.shape.Text() |
|---|
| 39 |
self._expression.set_font_description(pango.FontDescription(FeatureItem.FONT)) |
|---|
| 40 |
self._expression.set_wrap_mode(diacanvas.shape.WRAP_NONE) |
|---|
| 41 |
self._expression.set_markup(False) |
|---|
| 42 |
self.set_flags(diacanvas.COMPOSITE) |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
connect = DiagramItem.connect |
|---|
| 46 |
disconnect = DiagramItem.disconnect |
|---|
| 47 |
notify = DiagramItem.notify |
|---|
| 48 |
|
|---|
| 49 |
def save(self, save_func): |
|---|
| 50 |
for prop in ('affine',): |
|---|
| 51 |
self.save_property(save_func, prop) |
|---|
| 52 |
DiagramItem.save(self, save_func) |
|---|
| 53 |
|
|---|
| 54 |
def postload(self): |
|---|
| 55 |
if self.subject: |
|---|
| 56 |
self._expression.set_text(self.subject.render()) |
|---|
| 57 |
|
|---|
| 58 |
def do_set_property(self, pspec, value): |
|---|
| 59 |
if pspec.name == 'expression': |
|---|
| 60 |
if self.subject: |
|---|
| 61 |
self.preserve_property('expression') |
|---|
| 62 |
self.subject.parse(value) |
|---|
| 63 |
self._expression.set_text(self.subject.render()) |
|---|
| 64 |
self.request_update() |
|---|
| 65 |
else: |
|---|
| 66 |
DiagramItem.do_set_property(self, pspec, value) |
|---|
| 67 |
|
|---|
| 68 |
def do_get_property(self, pspec): |
|---|
| 69 |
if pspec.name == 'expression': |
|---|
| 70 |
return self.subject and self.subject.render() or '' |
|---|
| 71 |
else: |
|---|
| 72 |
return DiagramItem.do_get_property(self, pspec) |
|---|
| 73 |
|
|---|
| 74 |
def get_size(self, update=False): |
|---|
| 75 |
"""Return the size of the feature. If update == True the item is |
|---|
| 76 |
directly updated. |
|---|
| 77 |
""" |
|---|
| 78 |
w, h = self._expression.to_pango_layout(True).get_pixel_size() |
|---|
| 79 |
if update: |
|---|
| 80 |
self.set_bounds((0, 0, max(10, w), h)) |
|---|
| 81 |
return w, h |
|---|
| 82 |
|
|---|
| 83 |
def set_pos(self, x, y): |
|---|
| 84 |
a = self.get_property('affine') |
|---|
| 85 |
a = (a[0], a[1], a[2], a[3], x, y) |
|---|
| 86 |
self.set(affine=a) |
|---|
| 87 |
|
|---|
| 88 |
def edit(self): |
|---|
| 89 |
self.start_editing(self._expression) |
|---|
| 90 |
|
|---|
| 91 |
def on_subject_notify(self, pspec, notifiers=()): |
|---|
| 92 |
DiagramItem.on_subject_notify(self, pspec, notifiers) |
|---|
| 93 |
self._expression.set_text(self.subject and self.subject.render() or '') |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
def on_update(self, affine): |
|---|
| 98 |
CanvasItem.on_update(self, affine) |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
def on_event(self, event): |
|---|
| 102 |
if event.type == diacanvas.EVENT_BUTTON_PRESS: |
|---|
| 103 |
|
|---|
| 104 |
return True |
|---|
| 105 |
if event.type == diacanvas.EVENT_2BUTTON_PRESS: |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
self.edit() |
|---|
| 109 |
return True |
|---|
| 110 |
else: |
|---|
| 111 |
return CanvasItem.on_event(self, event) |
|---|
| 112 |
|
|---|
| 113 |
def on_point(self, x, y): |
|---|
| 114 |
return distance_rectangle_point(self.get_bounds(), (x, y)) |
|---|
| 115 |
|
|---|
| 116 |
def on_shape_iter(self): |
|---|
| 117 |
return iter([self._expression]) |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
def on_editable_start_editing(self, shape): |
|---|
| 122 |
|
|---|
| 123 |
pass |
|---|
| 124 |
|
|---|
| 125 |
def on_editable_editing_done(self, shape, new_text): |
|---|
| 126 |
self.set_property('expression', new_text) |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
initialize_item(FeatureItem) |
|---|
| 132 |
|
|---|