| 1 |
""" |
|---|
| 2 |
Commands related to the Diagram (DiaCanvas) |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
import diacanvas |
|---|
| 7 |
import gaphor |
|---|
| 8 |
import gaphor.UML as UML |
|---|
| 9 |
from gaphor.misc.action import Action, CheckAction, RadioAction, register_action |
|---|
| 10 |
|
|---|
| 11 |
from klass import ClassItem |
|---|
| 12 |
from classifier import ClassifierItem |
|---|
| 13 |
|
|---|
| 14 |
class NoFocusItemError(gaphor.GaphorError): |
|---|
| 15 |
pass |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
def get_parent_focus_item(window): |
|---|
| 19 |
"""Get the outer most focus item (the obe that's not a composite).""" |
|---|
| 20 |
view = window.get_current_diagram_view() |
|---|
| 21 |
if view: |
|---|
| 22 |
fi = view.focus_item |
|---|
| 23 |
if fi: |
|---|
| 24 |
item = fi.item |
|---|
| 25 |
while (item.flags & diacanvas.COMPOSITE) != 0: |
|---|
| 26 |
item = item.parent |
|---|
| 27 |
return item |
|---|
| 28 |
raise NoFocusItemError, 'No item has focus.' |
|---|
| 29 |
|
|---|
| 30 |
class ItemRenameAction(Action): |
|---|
| 31 |
id = 'ItemRename' |
|---|
| 32 |
label = '_Rename' |
|---|
| 33 |
tooltip = 'Rename selected item' |
|---|
| 34 |
|
|---|
| 35 |
def init(self, window): |
|---|
| 36 |
self._window = window |
|---|
| 37 |
|
|---|
| 38 |
def update(self): |
|---|
| 39 |
try: |
|---|
| 40 |
item = get_parent_focus_item(self._window) |
|---|
| 41 |
except NoFocusItemError: |
|---|
| 42 |
self.sensitive = False |
|---|
| 43 |
else: |
|---|
| 44 |
if isinstance(item, ClassifierItem): |
|---|
| 45 |
self.sensitive = True |
|---|
| 46 |
|
|---|
| 47 |
def execute(self): |
|---|
| 48 |
item = self._window.get_current_diagram_view().focus_item.item |
|---|
| 49 |
item.rename() |
|---|
| 50 |
|
|---|
| 51 |
register_action(ItemRenameAction, 'ItemFocus') |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
class CreateAttributeAction(Action): |
|---|
| 58 |
id = 'CreateAttribute' |
|---|
| 59 |
label = 'New _Attribute' |
|---|
| 60 |
tooltip = 'Create a new attribute' |
|---|
| 61 |
|
|---|
| 62 |
def init(self, window): |
|---|
| 63 |
self._window = window |
|---|
| 64 |
|
|---|
| 65 |
def update(self): |
|---|
| 66 |
try: |
|---|
| 67 |
item = get_parent_focus_item(self._window) |
|---|
| 68 |
except NoFocusItemError: |
|---|
| 69 |
pass |
|---|
| 70 |
else: |
|---|
| 71 |
if isinstance(item, ClassItem): |
|---|
| 72 |
self.sensitive = item.get_property('show-attributes') |
|---|
| 73 |
|
|---|
| 74 |
def execute(self): |
|---|
| 75 |
subject = get_parent_focus_item(self._window).subject |
|---|
| 76 |
assert isinstance(subject, UML.Class) |
|---|
| 77 |
elemfact = gaphor.resource(UML.ElementFactory) |
|---|
| 78 |
attribute = elemfact.create(UML.Property) |
|---|
| 79 |
attribute.name = 'new' |
|---|
| 80 |
subject.ownedAttribute = attribute |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
register_action(CreateAttributeAction, 'ShowAttributes', 'ItemFocus') |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
class CreateOperationAction(Action): |
|---|
| 87 |
id = 'CreateOperation' |
|---|
| 88 |
label = 'New _Operation' |
|---|
| 89 |
tooltip = 'Create a new operation' |
|---|
| 90 |
|
|---|
| 91 |
def init(self, window): |
|---|
| 92 |
self._window = window |
|---|
| 93 |
|
|---|
| 94 |
def update(self): |
|---|
| 95 |
try: |
|---|
| 96 |
item = get_parent_focus_item(self._window) |
|---|
| 97 |
except NoFocusItemError: |
|---|
| 98 |
pass |
|---|
| 99 |
else: |
|---|
| 100 |
if isinstance(item, ClassItem): |
|---|
| 101 |
self.sensitive = item.get_property('show-operations') |
|---|
| 102 |
|
|---|
| 103 |
def execute(self): |
|---|
| 104 |
subject = get_parent_focus_item(self._window).subject |
|---|
| 105 |
assert isinstance(subject, UML.Classifier) |
|---|
| 106 |
elemfact = gaphor.resource(UML.ElementFactory) |
|---|
| 107 |
operation = elemfact.create(UML.Operation) |
|---|
| 108 |
operation.name = 'new' |
|---|
| 109 |
subject.ownedOperation = operation |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
register_action(CreateOperationAction, 'ShowOperations', 'ItemFocus') |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
class EditItemAction(Action): |
|---|
| 116 |
id = 'EditItem' |
|---|
| 117 |
label = 'Edit' |
|---|
| 118 |
tooltip='Edit' |
|---|
| 119 |
|
|---|
| 120 |
def init(self, window): |
|---|
| 121 |
self._window = window |
|---|
| 122 |
|
|---|
| 123 |
def execute(self): |
|---|
| 124 |
item = self._window.get_current_diagram_view().focus_item.item |
|---|
| 125 |
|
|---|
| 126 |
item.edit() |
|---|
| 127 |
|
|---|
| 128 |
register_action(EditItemAction, 'ItemFocus') |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
class DeleteFeatureAction(Action): |
|---|
| 132 |
|
|---|
| 133 |
def init(self, window): |
|---|
| 134 |
self._window = window |
|---|
| 135 |
|
|---|
| 136 |
def execute(self): |
|---|
| 137 |
|
|---|
| 138 |
item = self._window.get_current_diagram_view().focus_item.item |
|---|
| 139 |
|
|---|
| 140 |
item.subject.unlink() |
|---|
| 141 |
|
|---|
| 142 |
class DeleteAttributeAction(DeleteFeatureAction): |
|---|
| 143 |
id = 'DeleteAttribute' |
|---|
| 144 |
label = 'Delete A_ttribute' |
|---|
| 145 |
tooltip='Delete the selected attribute' |
|---|
| 146 |
|
|---|
| 147 |
register_action(DeleteAttributeAction, 'ShowAttributes', 'CreateAttribute', 'ItemFocus') |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
class DeleteOperationAction(DeleteFeatureAction): |
|---|
| 151 |
id = 'DeleteOperation' |
|---|
| 152 |
label = 'Delete O_peration' |
|---|
| 153 |
tooltip = 'Delete the selected operation' |
|---|
| 154 |
|
|---|
| 155 |
register_action(DeleteOperationAction, 'ShowOperations', 'CreateOperation', 'ItemFocus') |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
class ShowAttributesAction(CheckAction): |
|---|
| 159 |
id = 'ShowAttributes' |
|---|
| 160 |
label = 'Show Attributes' |
|---|
| 161 |
tooltip='show attribute compartment' |
|---|
| 162 |
|
|---|
| 163 |
def init(self, window): |
|---|
| 164 |
self._window = window |
|---|
| 165 |
|
|---|
| 166 |
def update(self): |
|---|
| 167 |
try: |
|---|
| 168 |
item = get_parent_focus_item(self._window) |
|---|
| 169 |
except NoFocusItemError: |
|---|
| 170 |
pass |
|---|
| 171 |
else: |
|---|
| 172 |
if isinstance(item, ClassItem): |
|---|
| 173 |
self.active = item.get_property('show-attributes') |
|---|
| 174 |
|
|---|
| 175 |
def execute(self): |
|---|
| 176 |
item = get_parent_focus_item(self._window) |
|---|
| 177 |
item.set_property('show-attributes', self.active) |
|---|
| 178 |
|
|---|
| 179 |
register_action(ShowAttributesAction, 'ItemFocus') |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
class ShowOperationsAction(CheckAction): |
|---|
| 183 |
id = 'ShowOperations' |
|---|
| 184 |
label = 'Show Operations' |
|---|
| 185 |
tooltip='show attribute compartment' |
|---|
| 186 |
|
|---|
| 187 |
def init(self, window): |
|---|
| 188 |
self._window = window |
|---|
| 189 |
|
|---|
| 190 |
def update(self): |
|---|
| 191 |
try: |
|---|
| 192 |
item = get_parent_focus_item(self._window) |
|---|
| 193 |
except NoFocusItemError: |
|---|
| 194 |
pass |
|---|
| 195 |
else: |
|---|
| 196 |
from klass import ClassItem |
|---|
| 197 |
if isinstance(item, ClassItem): |
|---|
| 198 |
self.active = item.get_property('show-operations') |
|---|
| 199 |
|
|---|
| 200 |
def execute(self): |
|---|
| 201 |
item = get_parent_focus_item(self._window) |
|---|
| 202 |
item.set_property('show-operations', self.active) |
|---|
| 203 |
|
|---|
| 204 |
register_action(ShowOperationsAction, 'ItemFocus') |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
class SegmentAction(Action): |
|---|
| 211 |
"""Base class for add and delete line segment.""" |
|---|
| 212 |
|
|---|
| 213 |
def init(self, window): |
|---|
| 214 |
self._window = window |
|---|
| 215 |
|
|---|
| 216 |
def get_item_and_segment(self): |
|---|
| 217 |
fi = get_parent_focus_item(self._window) |
|---|
| 218 |
view = self._window.get_current_diagram_view() |
|---|
| 219 |
assert isinstance(fi, diacanvas.CanvasLine) |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
wx, wy = view.window_to_world(*view.get_pointer()) |
|---|
| 223 |
x, y = fi.affine_point_w2i(wx, wy) |
|---|
| 224 |
segment = fi.get_closest_segment(x, y) |
|---|
| 225 |
return fi, segment |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
class AddSegmentAction(SegmentAction): |
|---|
| 229 |
id = 'AddSegment' |
|---|
| 230 |
label = 'Add _Segment' |
|---|
| 231 |
tooltip='Add a segment to the line' |
|---|
| 232 |
|
|---|
| 233 |
def execute(self): |
|---|
| 234 |
item, segment = self.get_item_and_segment() |
|---|
| 235 |
if item: |
|---|
| 236 |
item.set_property('add_segment', segment) |
|---|
| 237 |
|
|---|
| 238 |
register_action(AddSegmentAction, 'ItemFocus') |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
class DeleteSegmentAction(SegmentAction): |
|---|
| 242 |
id = 'DeleteSegment' |
|---|
| 243 |
label = 'Delete _Segment' |
|---|
| 244 |
tooltip = 'Delete the segment from the line' |
|---|
| 245 |
|
|---|
| 246 |
def update(self): |
|---|
| 247 |
try: |
|---|
| 248 |
fi = get_parent_focus_item(self._window) |
|---|
| 249 |
if fi and isinstance(fi, diacanvas.CanvasLine): |
|---|
| 250 |
self.sensitive = len(fi.handles) > 2 |
|---|
| 251 |
except NoFocusItemError: |
|---|
| 252 |
pass |
|---|
| 253 |
|
|---|
| 254 |
def execute(self): |
|---|
| 255 |
item, segment = self.get_item_and_segment() |
|---|
| 256 |
if item: |
|---|
| 257 |
item.set_property('del_segment', segment) |
|---|
| 258 |
|
|---|
| 259 |
register_action(DeleteSegmentAction, 'ItemFocus', 'AddSegment') |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
class OrthogonalAction(CheckAction): |
|---|
| 263 |
id = 'Orthogonal' |
|---|
| 264 |
label = 'Orthogonal' |
|---|
| 265 |
tooltip = 'Set the line to orthogonal' |
|---|
| 266 |
|
|---|
| 267 |
def init(self, window): |
|---|
| 268 |
self._window = window |
|---|
| 269 |
|
|---|
| 270 |
def update(self): |
|---|
| 271 |
try: |
|---|
| 272 |
fi = get_parent_focus_item(self._window) |
|---|
| 273 |
if fi and isinstance(fi, diacanvas.CanvasLine): |
|---|
| 274 |
self.active = fi.get_property('orthogonal') |
|---|
| 275 |
except NoFocusItemError: |
|---|
| 276 |
pass |
|---|
| 277 |
|
|---|
| 278 |
def execute(self): |
|---|
| 279 |
fi = get_parent_focus_item(self._window) |
|---|
| 280 |
view = self._window.get_current_diagram_view() |
|---|
| 281 |
assert isinstance(fi, diacanvas.CanvasLine) |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
if self.active and len(fi.handles) < 3: |
|---|
| 285 |
fi.set_property('add_segment', 0) |
|---|
| 286 |
fi.set_property('orthogonal', self.active) |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
register_action(OrthogonalAction, 'ItemFocus', 'AddSegment', 'DeleteSegment') |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
class NavigableAction(CheckAction): |
|---|
| 298 |
end_name='' |
|---|
| 299 |
def init(self, window): |
|---|
| 300 |
self._window = window |
|---|
| 301 |
|
|---|
| 302 |
def get_association_end(self): |
|---|
| 303 |
return get_parent_focus_item(self._window).get_property(self.end_name) |
|---|
| 304 |
|
|---|
| 305 |
def update(self): |
|---|
| 306 |
try: |
|---|
| 307 |
item = get_parent_focus_item(self._window) |
|---|
| 308 |
from association import AssociationItem |
|---|
| 309 |
if isinstance(item, AssociationItem): |
|---|
| 310 |
end = item.get_property(self.end_name) |
|---|
| 311 |
if end.subject: |
|---|
| 312 |
self.active = (end.subject.class_ != None) |
|---|
| 313 |
except NoFocusItemError: |
|---|
| 314 |
pass |
|---|
| 315 |
|
|---|
| 316 |
def execute(self): |
|---|
| 317 |
item = self.get_association_end() |
|---|
| 318 |
assert item.subject |
|---|
| 319 |
assert isinstance(item.subject, UML.Property) |
|---|
| 320 |
item.set_navigable(self.active) |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
class HeadNavigableAction(NavigableAction): |
|---|
| 324 |
id = 'Head_isNavigable' |
|---|
| 325 |
label = 'Navigable' |
|---|
| 326 |
end_name = 'head' |
|---|
| 327 |
|
|---|
| 328 |
register_action(HeadNavigableAction, 'ItemFocus') |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
class TailNavigableAction(NavigableAction): |
|---|
| 332 |
id = 'Tail_isNavigable' |
|---|
| 333 |
label = 'Navigable' |
|---|
| 334 |
end_name = 'tail' |
|---|
| 335 |
|
|---|
| 336 |
register_action(TailNavigableAction, 'ItemFocus') |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
class AggregationAction(RadioAction): |
|---|
| 340 |
|
|---|
| 341 |
def init(self, window): |
|---|
| 342 |
self._window = window |
|---|
| 343 |
|
|---|
| 344 |
def update(self): |
|---|
| 345 |
try: |
|---|
| 346 |
item = get_parent_focus_item(self._window) |
|---|
| 347 |
from association import AssociationItem |
|---|
| 348 |
if isinstance(item, AssociationItem): |
|---|
| 349 |
end = item.get_property(self.end_name) |
|---|
| 350 |
if end.subject: |
|---|
| 351 |
self.active = (end.subject.aggregation == self.aggregation) |
|---|
| 352 |
except NoFocusItemError: |
|---|
| 353 |
pass |
|---|
| 354 |
|
|---|
| 355 |
def execute(self): |
|---|
| 356 |
if self.active: |
|---|
| 357 |
subject = get_parent_focus_item(self._window).get_property(self.end_name).subject |
|---|
| 358 |
assert isinstance(subject, UML.Property) |
|---|
| 359 |
subject.aggregation = self.aggregation |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
class HeadNoneAction(AggregationAction): |
|---|
| 363 |
id = 'Head_AggregationNone' |
|---|
| 364 |
label = 'None' |
|---|
| 365 |
group = 'head_aggregation' |
|---|
| 366 |
end_name = 'head' |
|---|
| 367 |
aggregation = 'none' |
|---|
| 368 |
|
|---|
| 369 |
register_action(HeadNoneAction, 'ItemFocus') |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
class HeadSharedAction(AggregationAction): |
|---|
| 373 |
id = 'Head_AggregationShared' |
|---|
| 374 |
label = 'Shared' |
|---|
| 375 |
group = 'head_aggregation' |
|---|
| 376 |
end_name = 'head' |
|---|
| 377 |
aggregation = 'shared' |
|---|
| 378 |
|
|---|
| 379 |
register_action(HeadSharedAction, 'ItemFocus') |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
class HeadCompositeAction(AggregationAction): |
|---|
| 383 |
id = 'Head_AggregationComposite' |
|---|
| 384 |
label = 'Composite' |
|---|
| 385 |
group = 'head_aggregation' |
|---|
| 386 |
end_name = 'head' |
|---|
| 387 |
aggregation = 'composite' |
|---|
| 388 |
|
|---|
| 389 |
register_action(HeadCompositeAction, 'ItemFocus') |
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
class TailNoneAction(AggregationAction): |
|---|
| 393 |
id = 'Tail_AggregationNone' |
|---|
| 394 |
label = 'None' |
|---|
| 395 |
group = 'tail_aggregation' |
|---|
| 396 |
end_name = 'tail' |
|---|
| 397 |
aggregation = 'none' |
|---|
| 398 |
|
|---|
| 399 |
register_action(TailNoneAction, 'ItemFocus') |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
class TailSharedAction(AggregationAction): |
|---|
| 403 |
id = 'Tail_AggregationShared' |
|---|
| 404 |
label = 'Shared' |
|---|
| 405 |
group = 'tail_aggregation' |
|---|
| 406 |
end_name = 'tail' |
|---|
| 407 |
aggregation = 'shared' |
|---|
| 408 |
|
|---|
| 409 |
register_action(TailSharedAction, 'ItemFocus') |
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
class TailCompositeAction(AggregationAction): |
|---|
| 413 |
id = 'Tail_AggregationComposite' |
|---|
| 414 |
label = 'Composite' |
|---|
| 415 |
group = 'tail_aggregation' |
|---|
| 416 |
end_name = 'tail' |
|---|
| 417 |
aggregation = 'composite' |
|---|
| 418 |
|
|---|
| 419 |
register_action(TailCompositeAction, 'ItemFocus') |
|---|