| 1 |
|
|---|
| 2 |
''' |
|---|
| 3 |
Feature diagram item. Feature is a super class of both Attribute, Operations and |
|---|
| 4 |
Methods. |
|---|
| 5 |
''' |
|---|
| 6 |
|
|---|
| 7 |
from gaphas.item import Item |
|---|
| 8 |
from diagramitem import DiagramItem |
|---|
| 9 |
from gaphor.diagram import DiagramItemMeta |
|---|
| 10 |
from gaphas.util import text_extents, text_set_font, text_align |
|---|
| 11 |
import font |
|---|
| 12 |
|
|---|
| 13 |
class FeatureItem(DiagramItem): |
|---|
| 14 |
""" |
|---|
| 15 |
FeatureItems are model elements who recide inside a ClassifierItem, such |
|---|
| 16 |
as methods and attributes. Those items can have comments attached, but only |
|---|
| 17 |
on the left and right side. |
|---|
| 18 |
Note that features can also be used inside objects. |
|---|
| 19 |
""" |
|---|
| 20 |
|
|---|
| 21 |
def __init__(self, id=None): |
|---|
| 22 |
DiagramItem.__init__(self, id) |
|---|
| 23 |
self.width = 0 |
|---|
| 24 |
self.height = 0 |
|---|
| 25 |
self.text = '' |
|---|
| 26 |
|
|---|
| 27 |
self.canvas = None |
|---|
| 28 |
self.need_sync = False |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
connect = DiagramItem.connect |
|---|
| 33 |
disconnect = DiagramItem.disconnect |
|---|
| 34 |
notify = DiagramItem.notify |
|---|
| 35 |
|
|---|
| 36 |
def save(self, save_func): |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
DiagramItem.save(self, save_func) |
|---|
| 40 |
|
|---|
| 41 |
def postload(self): |
|---|
| 42 |
if self.subject: |
|---|
| 43 |
self._expression.set_text(self.subject.render()) |
|---|
| 44 |
|
|---|
| 45 |
def get_size(self, update=False): |
|---|
| 46 |
""" |
|---|
| 47 |
Return the size of the feature. If update == True the item is |
|---|
| 48 |
directly updated. |
|---|
| 49 |
""" |
|---|
| 50 |
return self.width, self.height |
|---|
| 51 |
|
|---|
| 52 |
def get_text(self): |
|---|
| 53 |
return '' |
|---|
| 54 |
|
|---|
| 55 |
def update_size(self, text, context): |
|---|
| 56 |
cr = context.cairo |
|---|
| 57 |
self.width, self.height = text_extents(cr, text) |
|---|
| 58 |
|
|---|
| 59 |
def on_subject_notify(self, pspec, notifiers=()): |
|---|
| 60 |
DiagramItem.on_subject_notify(self, pspec, notifiers) |
|---|
| 61 |
|
|---|
| 62 |
self.text = self.subject and self.subject.render() or '' |
|---|
| 63 |
|
|---|
| 64 |
def point(self, x, y): |
|---|
| 65 |
""" |
|---|
| 66 |
""" |
|---|
| 67 |
return distance_rectangle_point((0, 0, self.width, self.height), (x, y)) |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
class AttributeItem(FeatureItem): |
|---|
| 71 |
|
|---|
| 72 |
def __init__(self, id=None): |
|---|
| 73 |
FeatureItem.__init__(self, id) |
|---|
| 74 |
self.need_sync = False |
|---|
| 75 |
|
|---|
| 76 |
def on_subject_notify(self, pspec, notifiers=()): |
|---|
| 77 |
FeatureItem.on_subject_notify(self, pspec, ('name', |
|---|
| 78 |
'isDerived', |
|---|
| 79 |
'visibility', |
|---|
| 80 |
'lowerValue.value', |
|---|
| 81 |
'upperValue.value', |
|---|
| 82 |
'defaultValue.value', |
|---|
| 83 |
'typeValue.value', |
|---|
| 84 |
'taggedValue', |
|---|
| 85 |
'association') |
|---|
| 86 |
+ notifiers) |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
def on_subject_notify__name(self, subject, pspec): |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
self.request_update() |
|---|
| 94 |
|
|---|
| 95 |
on_subject_notify__isDerived = on_subject_notify__name |
|---|
| 96 |
on_subject_notify__visibility = on_subject_notify__name |
|---|
| 97 |
on_subject_notify__lowerValue_value = on_subject_notify__name |
|---|
| 98 |
on_subject_notify__upperValue_value = on_subject_notify__name |
|---|
| 99 |
on_subject_notify__defaultValue_value = on_subject_notify__name |
|---|
| 100 |
on_subject_notify__typeValue_value = on_subject_notify__name |
|---|
| 101 |
on_subject_notify__taggedValue = on_subject_notify__name |
|---|
| 102 |
|
|---|
| 103 |
def on_subject_notify__association(self, subject, pspec): |
|---|
| 104 |
""" |
|---|
| 105 |
Make sure we update the attribute compartment (in case |
|---|
| 106 |
the class_ property was defined before it is connected to |
|---|
| 107 |
an association. |
|---|
| 108 |
""" |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
self.need_sync = True |
|---|
| 112 |
self.request_update() |
|---|
| 113 |
|
|---|
| 114 |
def pre_update(self, context): |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
self.need_sync = False |
|---|
| 118 |
self.update_size(self.subject.render(), context) |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
def draw(self, context): |
|---|
| 122 |
cr = context.cairo |
|---|
| 123 |
text_set_font(cr, font.FONT) |
|---|
| 124 |
text_align(cr, 0, 0, self.subject.render() or '', align_x=1, align_y=1) |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
class OperationItem(FeatureItem): |
|---|
| 132 |
|
|---|
| 133 |
def __init__(self, id=None): |
|---|
| 134 |
FeatureItem.__init__(self, id) |
|---|
| 135 |
self.need_sync = False |
|---|
| 136 |
|
|---|
| 137 |
def on_subject_notify(self, pspec, notifiers=()): |
|---|
| 138 |
FeatureItem.on_subject_notify(self, pspec, |
|---|
| 139 |
('name', 'visibility', 'isAbstract') |
|---|
| 140 |
+ notifiers) |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
def postload(self): |
|---|
| 145 |
FeatureItem.postload(self) |
|---|
| 146 |
self.need_sync = False |
|---|
| 147 |
|
|---|
| 148 |
def on_subject_notify__name(self, subject, pspec): |
|---|
| 149 |
|
|---|
| 150 |
self.request_update() |
|---|
| 151 |
|
|---|
| 152 |
on_subject_notify__isAbstract = on_subject_notify__name |
|---|
| 153 |
on_subject_notify__visibility = on_subject_notify__name |
|---|
| 154 |
on_subject_notify__taggedValue = on_subject_notify__name |
|---|
| 155 |
|
|---|
| 156 |
def pre_update(self, context): |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
self.need_sync = False |
|---|
| 160 |
self.update_size(self.subject.render(), context) |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
def draw(self, context): |
|---|
| 164 |
cr = context.cairo |
|---|
| 165 |
text_set_font(cr, font.FONT) |
|---|
| 166 |
text_align(cr, 0, 0, self.subject.render() or '', align_x=1, align_y=1) |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|