root/gaphor/tags/gaphor-0.3.0/gaphor/diagram/actions.py

Revision 271, 5.5 kB (checked in by arjanmol, 5 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim:sw=4
2 """Menu actions that create diagram items.
3
4 This module is initialized from gaphor.ui.diagramwindow
5 """
6 import gaphor
7 import gaphor.UML as UML
8 import gaphor.diagram as diagram
9 from gaphor.misc.action import Action, CheckAction, RadioAction
10 from gaphor.misc.action import register_action as _register_action
11 from gaphor.misc.action import action_dependencies as _action_dependencies
12
13 def register_action(action, *args):
14     _register_action(action, *args)
15     _action_dependencies(action, 'ToolChange', 'TabChange', 'OpenModelElement')
16
17 def tool_changed(action, window):
18     view = window.get_current_diagram_view()
19     if not view:
20         action.sensitive = False
21         return
22     else:
23         action.sensitive = True
24     tool = view.get_property('tool')
25     if tool:
26         id = tool.action_id
27     else:
28         id = 'Pointer'
29     if id == action.id:
30         action.active = True
31
32
33 class PointerAction(RadioAction):
34     id = 'Pointer'
35     label = '_Pointer'
36     stock_id = 'gaphor-pointer'
37     group = 'placementtools'
38     accel = 'C-p'
39     tooltip = 'Pointer'
40
41     def init(self, window):
42         self._window = window
43         #self._window.get_current_diagram_view().connect('notify::tool', tool_changed, window)
44
45     def update(self):
46         tool_changed(self, self._window)
47
48     def execute(self):
49         self._window.get_current_diagram_view().set_tool(None)
50         self._window.set_message('')
51
52 register_action(PointerAction)
53
54
55 class PlacementAction(RadioAction):
56     """Abstract base command for commands that place an object on the canvas.
57     """
58     name = ''
59     type = None
60     subject_type = None
61     group = 'placementtools'
62
63     def init(self, window):
64         self._window = window
65
66     def update(self):
67         tool_changed(self, self._window)
68
69     def item_factory(self):
70         """Create a new instance of the item and return it."""
71         item = self._window.get_current_diagram().create(self.type)
72         if self.subject_type:
73             subject = gaphor.resource('ElementFactory').create(self.subject_type)
74             try:
75                 #print 'set subject'
76                 #item.set_property('subject', subject)
77                 item.subject = subject
78                 #print 'set subject done'
79             except Exception, e:
80                 print 'ERROR:', e
81         return item
82
83     def execute(self):
84         assert self.type != None
85         tool = diagram.PlacementTool(self.item_factory, self.id)
86         self._window.get_current_diagram_view().set_tool(tool)
87         self._window.set_message('Create new %s' % self.name)
88
89
90 class NamespacePlacementAction(PlacementAction):
91
92     def item_factory(self):
93         """Create a new instance of the item and return it."""
94         item = PlacementAction.item_factory(self)
95         item.subject.package = self._window.get_current_diagram().namespace
96         return item
97
98
99 class ActorPlacementAction(NamespacePlacementAction):
100     id = 'InsertActor'
101     label = '_Actor'
102     stock_id = 'gaphor-actor'
103     tooltip = 'Create a new Actor item'
104     name = 'Actor'
105     type = diagram.ActorItem
106     subject_type = UML.Actor
107
108 register_action(ActorPlacementAction)
109
110
111 class UseCasePlacementAction(NamespacePlacementAction):
112     id = 'InsertUseCase'
113     label = '_UseCase'
114     tooltip = 'Create a new Use case item'
115     stock_id = 'gaphor-usecase'
116     name = 'UseCase'
117     type = diagram.UseCaseItem
118     subject_type = UML.UseCase
119
120 register_action(UseCasePlacementAction)
121
122
123 class ClassPlacementAction(NamespacePlacementAction):
124     id = 'InsertClass'
125     label = '_Class'
126     tooltip = 'Create a new Class item'
127     stock_id = 'gaphor-class'
128     name = 'Class'
129     accel = 'C-c'
130     type = diagram.ClassItem
131     subject_type = UML.Class
132
133 register_action(ClassPlacementAction)
134
135
136 class PackagePlacementAction(NamespacePlacementAction):
137     id = 'InsertPackage'
138     label = '_Package'
139     tooltip = 'Create a new Package item'
140     stock_id = 'gaphor-package'
141     name = 'Package'
142     type = diagram.PackageItem
143     subject_type = UML.Package
144
145 register_action(PackagePlacementAction)
146
147
148 #class StatePlacementAction(PlacementAction):
149 #    id = 'InsertState'
150 #    label = 'State'
151 #    tooltip = 'Create a new State node'
152 #    stock_id = 'gaphor-comment-line'
153 #    name = 'State'
154 #    type = diagram.StateItem
155 #
156 #register_action(StatePlacementAction)
157
158
159 class CommentPlacementAction(PlacementAction):
160     id = 'InsertComment'
161     label = 'C_omment'
162     tooltip = 'Create a new Comment item'
163     stock_id = 'gaphor-comment'
164     name = 'Comment'
165     type = diagram.CommentItem
166     subject_type = UML.Comment
167
168 register_action(CommentPlacementAction)
169
170
171 class CommentLinePlacementAction(PlacementAction):
172     id = 'InsertCommentLine'
173     label = 'Comment _line'
174     tooltip = 'Create a new Comment line'
175     stock_id = 'gaphor-comment-line'
176     name = 'Comment line'
177     type = diagram.CommentLineItem
178
179 register_action(CommentLinePlacementAction)
180
181
182 class AssociationPlacementAction(PlacementAction):
183     id = 'InsertAssociation'
184     label = '_Association'
185     tooltip = 'Create a new Association line'
186     stock_id = 'gaphor-association'
187     name = 'Association'
188     type = diagram.AssociationItem
189
190     def __init__(self):
191         PlacementAction.__init__(self)
192
193 register_action(AssociationPlacementAction)
194
195
196 class DependencyPlacementAction(PlacementAction):
197     id = 'InsertDependency'
198     label = '_Dependency'
199     tooltip = 'Create a new Dependency'
200     stock_id = 'gaphor-dependency'
201     name = 'Dependency'
202     type = diagram.DependencyItem
203
204 register_action(DependencyPlacementAction)
205
206
207 class GeneralizationPlacementAction(PlacementAction):
208     id = 'InsertGeneralization'
209     label = '_Generalization'
210     tooltip = 'Create a new Generalization'
211     stock_id = 'gaphor-generalization'
212     name = 'Generalization'
213     type = diagram.GeneralizationItem
214
215 register_action(GeneralizationPlacementAction)
216
Note: See TracBrowser for help on using the browser.