| 1 |
""" |
|---|
| 2 |
This module contains the actions used in the Toolbox (lower left section |
|---|
| 3 |
of the main window. |
|---|
| 4 |
|
|---|
| 5 |
The Toolbox is bound to a diagram. When a diagram page (tab) is switched, |
|---|
| 6 |
the actions bound to the toolbuttons should change as well. |
|---|
| 7 |
""" |
|---|
| 8 |
|
|---|
| 9 |
from gaphor import UML |
|---|
| 10 |
from gaphor.diagram import items |
|---|
| 11 |
from gaphor.core import _, inject, radio_action, build_action_group |
|---|
| 12 |
from diagramtools import PlacementTool, DefaultTool |
|---|
| 13 |
from groupingtools import GroupPlacementTool |
|---|
| 14 |
from gaphas.item import SE |
|---|
| 15 |
|
|---|
| 16 |
__all__ = [ 'DiagramToolbox', 'TOOLBOX_ACTIONS' ] |
|---|
| 17 |
|
|---|
| 18 |
TOOLBOX_ACTIONS = ( |
|---|
| 19 |
('', ( |
|---|
| 20 |
('toolbox-pointer', _('Pointer'), 'gaphor-pointer'), |
|---|
| 21 |
('toolbox-line', _('Line'), 'gaphor-line'), |
|---|
| 22 |
('toolbox-box', _('Box'), 'gaphor-box'), |
|---|
| 23 |
('toolbox-ellipse', _('Ellipse'), 'gaphor-ellipse'), |
|---|
| 24 |
('toolbox-comment', _('Comment'), 'gaphor-comment'), |
|---|
| 25 |
('toolbox-comment-line', _('Comment line'), 'gaphor-comment-line'), |
|---|
| 26 |
)), (_('Classes'), ( |
|---|
| 27 |
('toolbox-class', _('Class'), 'gaphor-class'), |
|---|
| 28 |
('toolbox-interface', _('Interface'), 'gaphor-interface'), |
|---|
| 29 |
('toolbox-package', _('Package'), 'gaphor-package'), |
|---|
| 30 |
('toolbox-association', _('Association'), 'gaphor-association'), |
|---|
| 31 |
('toolbox-dependency', _('Dependency'), 'gaphor-dependency'), |
|---|
| 32 |
('toolbox-generalization', _('Generalization'), 'gaphor-generalization'), |
|---|
| 33 |
('toolbox-implementation', _('Implementation'), 'gaphor-implementation'), |
|---|
| 34 |
)), (_('Components'), ( |
|---|
| 35 |
('toolbox-component', _('Component'), 'gaphor-component'), |
|---|
| 36 |
|
|---|
| 37 |
('toolbox-node', _('Node'), 'gaphor-node'), |
|---|
| 38 |
('toolbox-artifact', _('Artifact'), 'gaphor-artifact'), |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
)), (_('Actions'), ( |
|---|
| 42 |
('toolbox-action', _('Action'), 'gaphor-action'), |
|---|
| 43 |
('toolbox-initial-node', _('Initial node'), 'gaphor-initial-node'), |
|---|
| 44 |
('toolbox-activity-final-node', _('Activity final node'), 'gaphor-activity-final-node'), |
|---|
| 45 |
('toolbox-flow-final-node', _('Flow final node'), 'gaphor-flow-final-node'), |
|---|
| 46 |
('toolbox-decision-node', _('Decision/merge node'), 'gaphor-decision-node'), |
|---|
| 47 |
('toolbox-fork-node', _('Fork/join node'), 'gaphor-fork-node'), |
|---|
| 48 |
('toolbox-object-node', _('Object node'), 'gaphor-object-node'), |
|---|
| 49 |
('toolbox-flow', _('Control/object flow'), 'gaphor-control-flow'), |
|---|
| 50 |
)), (_('Interactions'), ( |
|---|
| 51 |
('toolbox-lifeline', _('Lifeline'), 'gaphor-lifeline'), |
|---|
| 52 |
('toolbox-message', _('Message'), 'gaphor-message'), |
|---|
| 53 |
('toolbox-interaction', _('Interaction'), 'gaphor-interaction'), |
|---|
| 54 |
)), (_('Use Cases'), ( |
|---|
| 55 |
('toolbox-usecase', _('Use case'), 'gaphor-usecase'), |
|---|
| 56 |
('toolbox-actor', _('Actor'), 'gaphor-actor'), |
|---|
| 57 |
('toolbox-usecase-association', _('Association'), 'gaphor-association'), |
|---|
| 58 |
('toolbox-include', _('Include'), 'gaphor-include'), |
|---|
| 59 |
('toolbox-extend', _('Extend'), 'gaphor-extend'), |
|---|
| 60 |
)), (_('Profiles'), ( |
|---|
| 61 |
('toolbox-profile', _('Profile'), 'gaphor-profile'), |
|---|
| 62 |
('toolbox-metaclass', _('Metaclass'), 'gaphor-class'), |
|---|
| 63 |
('toolbox-stereotype', _('Stereotype'), 'gaphor-stereotype'), |
|---|
| 64 |
('toolbox-extension', _('Extension'), 'gaphor-extension'), |
|---|
| 65 |
)), |
|---|
| 66 |
) |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
def itemiter(toolbox_actions): |
|---|
| 70 |
""" |
|---|
| 71 |
Iterate toolbox items, irregardless section headers |
|---|
| 72 |
""" |
|---|
| 73 |
for name, section in toolbox_actions: |
|---|
| 74 |
for e in section: |
|---|
| 75 |
yield e |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
class DiagramToolbox(object): |
|---|
| 79 |
""" |
|---|
| 80 |
Composite class for DiagramTab (diagramtab.py). |
|---|
| 81 |
""" |
|---|
| 82 |
|
|---|
| 83 |
element_factory = inject('element_factory') |
|---|
| 84 |
properties = inject('properties') |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
def __init__(self, view=None): |
|---|
| 88 |
self.view = view |
|---|
| 89 |
self.action_group = build_action_group(self) |
|---|
| 90 |
|
|---|
| 91 |
diagram = property(lambda s: s.view.diagram) |
|---|
| 92 |
namespace = property(lambda s: s.view.diagram.namespace) |
|---|
| 93 |
|
|---|
| 94 |
@radio_action(names=zip(*list(itemiter(TOOLBOX_ACTIONS)))[0], |
|---|
| 95 |
labels=zip(*list(itemiter(TOOLBOX_ACTIONS)))[1], |
|---|
| 96 |
stock_ids=zip(*list(itemiter(TOOLBOX_ACTIONS)))[2]) |
|---|
| 97 |
def _set_toolbox_action(self, id): |
|---|
| 98 |
""" |
|---|
| 99 |
Activate a tool based on its index in the TOOLBOX_ACTIONS list. |
|---|
| 100 |
""" |
|---|
| 101 |
tool = list(itemiter(TOOLBOX_ACTIONS))[id][0] |
|---|
| 102 |
getattr(self, tool.replace('-', '_'))() |
|---|
| 103 |
|
|---|
| 104 |
def _item_factory(self, item_class, subject_class=None): |
|---|
| 105 |
def factory_method(parent=None): |
|---|
| 106 |
if subject_class: |
|---|
| 107 |
subject = self.element_factory.create(subject_class) |
|---|
| 108 |
else: |
|---|
| 109 |
subject = None |
|---|
| 110 |
return self.diagram.create(item_class, subject=subject, |
|---|
| 111 |
parent=parent) |
|---|
| 112 |
return factory_method |
|---|
| 113 |
|
|---|
| 114 |
def _namespace_item_factory(self, item_class, subject_class): |
|---|
| 115 |
""" |
|---|
| 116 |
Returns a factory method for Namespace classes. |
|---|
| 117 |
To be used by the PlacementTool. |
|---|
| 118 |
""" |
|---|
| 119 |
def factory_method(parent=None): |
|---|
| 120 |
subject = self.element_factory.create(subject_class) |
|---|
| 121 |
subject.package = self.namespace |
|---|
| 122 |
subject.name = 'New%s' % subject_class.__name__ |
|---|
| 123 |
return self.diagram.create(item_class, subject=subject, parent=parent) |
|---|
| 124 |
factory_method.item_class = item_class |
|---|
| 125 |
return factory_method |
|---|
| 126 |
|
|---|
| 127 |
def _after_handler(self): |
|---|
| 128 |
if self.properties('reset-tool-after-create', False): |
|---|
| 129 |
self.action_group.get_action('toolbox-pointer').activate() |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
def toolbox_pointer(self): |
|---|
| 138 |
if self.view: |
|---|
| 139 |
self.view.tool = DefaultTool() |
|---|
| 140 |
|
|---|
| 141 |
def toolbox_line(self): |
|---|
| 142 |
self.view.tool = PlacementTool( |
|---|
| 143 |
item_factory=self._item_factory(items.Line), |
|---|
| 144 |
after_handler=self._after_handler) |
|---|
| 145 |
|
|---|
| 146 |
def toolbox_box(self): |
|---|
| 147 |
self.view.tool = PlacementTool( |
|---|
| 148 |
item_factory=self._item_factory(items.Box), |
|---|
| 149 |
handle_index=SE, |
|---|
| 150 |
after_handler=self._after_handler) |
|---|
| 151 |
|
|---|
| 152 |
def toolbox_ellipse(self): |
|---|
| 153 |
self.view.tool = PlacementTool( |
|---|
| 154 |
item_factory=self._item_factory(items.Ellipse), |
|---|
| 155 |
handle_index=SE, |
|---|
| 156 |
after_handler=self._after_handler) |
|---|
| 157 |
|
|---|
| 158 |
def toolbox_comment(self): |
|---|
| 159 |
self.view.tool = PlacementTool( |
|---|
| 160 |
item_factory=self._item_factory(items.CommentItem, UML.Comment), |
|---|
| 161 |
handle_index=SE, |
|---|
| 162 |
after_handler=self._after_handler) |
|---|
| 163 |
|
|---|
| 164 |
def toolbox_comment_line(self): |
|---|
| 165 |
self.view.tool = PlacementTool( |
|---|
| 166 |
item_factory=self._item_factory(items.CommentLineItem), |
|---|
| 167 |
after_handler=self._after_handler) |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
def toolbox_class(self): |
|---|
| 172 |
self.view.tool = PlacementTool( |
|---|
| 173 |
item_factory=self._namespace_item_factory(items.ClassItem, |
|---|
| 174 |
UML.Class), |
|---|
| 175 |
handle_index=SE, |
|---|
| 176 |
after_handler=self._after_handler) |
|---|
| 177 |
|
|---|
| 178 |
def toolbox_interface(self): |
|---|
| 179 |
self.view.tool = PlacementTool( |
|---|
| 180 |
item_factory=self._namespace_item_factory(items.InterfaceItem, |
|---|
| 181 |
UML.Interface), |
|---|
| 182 |
handle_index=SE, |
|---|
| 183 |
after_handler=self._after_handler) |
|---|
| 184 |
|
|---|
| 185 |
def toolbox_package(self): |
|---|
| 186 |
self.view.tool = PlacementTool( |
|---|
| 187 |
item_factory=self._namespace_item_factory(items.PackageItem, |
|---|
| 188 |
UML.Package), |
|---|
| 189 |
handle_index=SE, |
|---|
| 190 |
after_handler=self._after_handler) |
|---|
| 191 |
|
|---|
| 192 |
def toolbox_association(self): |
|---|
| 193 |
self.view.tool = PlacementTool( |
|---|
| 194 |
item_factory=self._item_factory(items.AssociationItem), |
|---|
| 195 |
after_handler=self._after_handler) |
|---|
| 196 |
|
|---|
| 197 |
def toolbox_dependency(self): |
|---|
| 198 |
self.view.tool = PlacementTool( |
|---|
| 199 |
item_factory=self._item_factory(items.DependencyItem), |
|---|
| 200 |
after_handler=self._after_handler) |
|---|
| 201 |
|
|---|
| 202 |
def toolbox_generalization(self): |
|---|
| 203 |
self.view.tool = PlacementTool( |
|---|
| 204 |
item_factory=self._item_factory(items.GeneralizationItem), |
|---|
| 205 |
after_handler=self._after_handler) |
|---|
| 206 |
|
|---|
| 207 |
def toolbox_implementation(self): |
|---|
| 208 |
self.view.tool = PlacementTool( |
|---|
| 209 |
item_factory=self._item_factory(items.ImplementationItem), |
|---|
| 210 |
after_handler=self._after_handler) |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
def toolbox_component(self): |
|---|
| 215 |
self.view.tool = PlacementTool( |
|---|
| 216 |
item_factory=self._namespace_item_factory(items.ComponentItem, |
|---|
| 217 |
UML.Component), |
|---|
| 218 |
handle_index=SE, |
|---|
| 219 |
after_handler=self._after_handler) |
|---|
| 220 |
|
|---|
| 221 |
def toolbox_node(self): |
|---|
| 222 |
self.view.tool = PlacementTool( |
|---|
| 223 |
item_factory=self._namespace_item_factory(items.NodeItem, |
|---|
| 224 |
UML.Node), |
|---|
| 225 |
handle_index=SE, |
|---|
| 226 |
after_handler=self._after_handler) |
|---|
| 227 |
|
|---|
| 228 |
def toolbox_artifact(self): |
|---|
| 229 |
self.view.tool = PlacementTool( |
|---|
| 230 |
item_factory=self._namespace_item_factory(items.ArtifactItem, |
|---|
| 231 |
UML.Artifact), |
|---|
| 232 |
handle_index=SE, |
|---|
| 233 |
after_handler=self._after_handler) |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
def toolbox_action(self): |
|---|
| 238 |
self.view.tool = PlacementTool( |
|---|
| 239 |
item_factory=self._namespace_item_factory(items.ActionItem, |
|---|
| 240 |
UML.Action), |
|---|
| 241 |
handle_index=SE, |
|---|
| 242 |
after_handler=self._after_handler) |
|---|
| 243 |
|
|---|
| 244 |
def toolbox_initial_node(self): |
|---|
| 245 |
self.view.tool = PlacementTool( |
|---|
| 246 |
item_factory=self._item_factory(items.InitialNodeItem, |
|---|
| 247 |
UML.InitialNode), |
|---|
| 248 |
handle_index=SE, |
|---|
| 249 |
after_handler=self._after_handler) |
|---|
| 250 |
|
|---|
| 251 |
def toolbox_activity_final_node(self): |
|---|
| 252 |
self.view.tool = PlacementTool( |
|---|
| 253 |
item_factory=self._item_factory(items.ActivityFinalNodeItem, |
|---|
| 254 |
UML.ActivityFinalNode), |
|---|
| 255 |
handle_index=SE, |
|---|
| 256 |
after_handler=self._after_handler) |
|---|
| 257 |
|
|---|
| 258 |
def toolbox_flow_final_node(self): |
|---|
| 259 |
self.view.tool = PlacementTool( |
|---|
| 260 |
item_factory=self._item_factory(items.FlowFinalNodeItem, |
|---|
| 261 |
UML.FlowFinalNode), |
|---|
| 262 |
handle_index=SE, |
|---|
| 263 |
after_handler=self._after_handler) |
|---|
| 264 |
|
|---|
| 265 |
def toolbox_decision_node(self): |
|---|
| 266 |
self.view.tool = PlacementTool( |
|---|
| 267 |
item_factory=self._item_factory(items.DecisionNodeItem, |
|---|
| 268 |
UML.DecisionNode), |
|---|
| 269 |
handle_index=SE, |
|---|
| 270 |
after_handler=self._after_handler) |
|---|
| 271 |
|
|---|
| 272 |
def toolbox_fork_node(self): |
|---|
| 273 |
self.view.tool = PlacementTool( |
|---|
| 274 |
item_factory=self._item_factory(items.ForkNodeItem, |
|---|
| 275 |
UML.JoinNode), |
|---|
| 276 |
handle_index=1, |
|---|
| 277 |
after_handler=self._after_handler) |
|---|
| 278 |
|
|---|
| 279 |
def toolbox_object_node(self): |
|---|
| 280 |
self.view.tool = PlacementTool( |
|---|
| 281 |
item_factory=self._namespace_item_factory(items.ObjectNodeItem, |
|---|
| 282 |
UML.ObjectNode), |
|---|
| 283 |
handle_index=SE, |
|---|
| 284 |
after_handler=self._after_handler) |
|---|
| 285 |
|
|---|
| 286 |
def toolbox_flow(self): |
|---|
| 287 |
self.view.tool = PlacementTool( |
|---|
| 288 |
item_factory=self._item_factory(items.FlowItem), |
|---|
| 289 |
after_handler=self._after_handler) |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
def toolbox_interaction(self): |
|---|
| 293 |
self.view.tool = PlacementTool( |
|---|
| 294 |
item_factory=self._namespace_item_factory(items.InteractionItem, |
|---|
| 295 |
UML.Interaction), |
|---|
| 296 |
handle_index=SE, |
|---|
| 297 |
after_handler=self._after_handler) |
|---|
| 298 |
|
|---|
| 299 |
def toolbox_lifeline(self): |
|---|
| 300 |
self.view.tool = GroupPlacementTool( |
|---|
| 301 |
item_factory=self._namespace_item_factory(items.LifelineItem, |
|---|
| 302 |
UML.Lifeline), |
|---|
| 303 |
handle_index=SE, |
|---|
| 304 |
after_handler=self._after_handler) |
|---|
| 305 |
|
|---|
| 306 |
def toolbox_message(self): |
|---|
| 307 |
self.view.tool = PlacementTool( |
|---|
| 308 |
item_factory=self._item_factory(items.MessageItem), |
|---|
| 309 |
after_handler=self._after_handler) |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
def toolbox_usecase(self): |
|---|
| 314 |
self.view.tool = PlacementTool( |
|---|
| 315 |
item_factory=self._namespace_item_factory(items.UseCaseItem, |
|---|
| 316 |
UML.UseCase), |
|---|
| 317 |
handle_index=SE, |
|---|
| 318 |
after_handler=self._after_handler) |
|---|
| 319 |
|
|---|
| 320 |
def toolbox_actor(self): |
|---|
| 321 |
self.view.tool = PlacementTool( |
|---|
| 322 |
item_factory=self._namespace_item_factory(items.ActorItem, |
|---|
| 323 |
UML.Actor), |
|---|
| 324 |
handle_index=SE, |
|---|
| 325 |
after_handler=self._after_handler) |
|---|
| 326 |
|
|---|
| 327 |
def toolbox_usecase_association(self): |
|---|
| 328 |
self.view.tool = PlacementTool( |
|---|
| 329 |
item_factory=self._item_factory(items.AssociationItem), |
|---|
| 330 |
after_handler=self._after_handler) |
|---|
| 331 |
|
|---|
| 332 |
def toolbox_include(self): |
|---|
| 333 |
self.view.tool = PlacementTool( |
|---|
| 334 |
item_factory=self._item_factory(items.IncludeItem), |
|---|
| 335 |
after_handler=self._after_handler) |
|---|
| 336 |
|
|---|
| 337 |
def toolbox_extend(self): |
|---|
| 338 |
self.view.tool = PlacementTool( |
|---|
| 339 |
item_factory=self._item_factory(items.ExtendItem), |
|---|
| 340 |
after_handler=self._after_handler) |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
def toolbox_profile(self): |
|---|
| 345 |
self.view.tool = PlacementTool( |
|---|
| 346 |
item_factory=self._namespace_item_factory(items.PackageItem, |
|---|
| 347 |
UML.Profile), |
|---|
| 348 |
handle_index=SE, |
|---|
| 349 |
after_handler=self._after_handler) |
|---|
| 350 |
|
|---|
| 351 |
def toolbox_metaclass(self): |
|---|
| 352 |
self.view.tool = PlacementTool( |
|---|
| 353 |
item_factory=self._namespace_item_factory(items.ClassItem, |
|---|
| 354 |
UML.Class), |
|---|
| 355 |
handle_index=SE, |
|---|
| 356 |
after_handler=self._after_handler) |
|---|
| 357 |
|
|---|
| 358 |
def toolbox_stereotype(self): |
|---|
| 359 |
self.view.tool = PlacementTool( |
|---|
| 360 |
item_factory=self._namespace_item_factory(items.ClassItem, |
|---|
| 361 |
UML.Stereotype), |
|---|
| 362 |
handle_index=SE, |
|---|
| 363 |
after_handler=self._after_handler) |
|---|
| 364 |
|
|---|
| 365 |
def toolbox_extension(self): |
|---|
| 366 |
self.view.tool = PlacementTool( |
|---|
| 367 |
item_factory=self._item_factory(items.ExtensionItem), |
|---|
| 368 |
after_handler=self._after_handler) |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|