]> Shamusworld >> Repos - architektonas/blob - src/base/rs_actioninterface.cpp
7a885f8f85f770b40868d69497ce4737a2fcc35d
[architektonas] / src / base / rs_actioninterface.cpp
1 // rs_actioninterface.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/22/2010  Added this text. :-)
13 //
14
15 #include "rs_actioninterface.h"
16
17 #include "commands.h"
18 #include "rs_debug.h"
19 #include "rs_entitycontainer.h"
20 #include "graphicview.h"
21
22 /**
23  * Constructor.
24  *
25  * Sets the entity container on which the action class inherited
26  * from this interface operates.
27  *
28  * @param name Action name. This can be used internally for
29  *             debugging mainly.
30  * @param container Entity container this action operates on.
31  * @param graphicView Graphic view instance this action operates on.
32  *                    Please note that an action belongs to this
33  *                    view.
34  * @param cursor Default mouse cursor for this action. If the action
35  *               is suspended and resumed again the cursor will always
36  *               be reset to the one given here.
37  */
38 RS_ActionInterface::RS_ActionInterface(const char * name,
39         RS_EntityContainer & container, GraphicView & graphicView):
40         RS_Snapper(container, graphicView)
41 {
42         RS_DEBUG->print("RS_ActionInterface::RS_ActionInterface: Setting up action: \"%s\"", name);
43
44         this->name = name;
45         status = 0;
46         finished = false;
47         //triggerOnResume = false;
48
49         // graphic provides a pointer to the graphic if the
50         // entity container is a graphic (i.e. can also hold
51         // layers).
52         graphic = container.getGraphic();
53
54         // document pointer will be used for undo / redo
55         document = container.getDocument();
56
57         //this->cursor = cursor;
58         //setSnapMode(graphicView.getDefaultSnapMode());
59
60         RS_DEBUG->print("RS_ActionInterface::RS_ActionInterface: Setting up action: \"%s\": OK", name);
61 }
62
63 /**
64  * Destructor.
65  */
66 RS_ActionInterface::~RS_ActionInterface()
67 {
68         // would be pure virtual now:
69         // hideOptions();
70 //JLH: Only it isn't pure virtual...
71 }
72
73 /**
74  * Must be implemented to return the ID of this action.
75 *
76 * @todo no default implementation
77  */
78 RS2::ActionType RS_ActionInterface::rtti()
79 {
80         return RS2::ActionNone;
81 }
82
83 /**
84  * @return name of this action
85  */
86 QString RS_ActionInterface::getName()
87 {
88         return name;
89 }
90
91 /**
92  * Called to initiate an action. This funtcion is often
93  * overwritten by the implementing action.
94  *
95  * @param status The status on which to initiate this action.
96  * default is 0 to begin the action.
97  */
98 void RS_ActionInterface::init(int status)
99 {
100         RS_Snapper::init();
101         setStatus(status);
102
103         if (status >= 0)
104         {
105                 //graphicView->setMouseCursor(cursor);
106                 updateMouseButtonHints();
107                 updateMouseCursor();
108                 updateToolBar();
109         }
110 }
111
112 /**
113  * Called when the mouse moves and this is the current action.
114  * This function can be overwritten by the implementing action.
115  * The default implementation keeps track of the mouse position.
116  */
117 void RS_ActionInterface::mouseMoveEvent(QMouseEvent *)
118 {
119 }
120
121 /**
122  * Called when the left mouse button is pressed and this is the
123  * current action.
124  * This function can be overwritten by the implementing action.
125  * The default implementation does nothing.
126  */
127 void RS_ActionInterface::mousePressEvent(QMouseEvent *)
128 {
129 }
130
131 /**
132  * Called when the left mouse button is released and this is
133  * the current action.
134  * This function can be overwritten by the implementing action.
135  * The default implementation does nothing.
136  */
137 void RS_ActionInterface::mouseReleaseEvent(QMouseEvent *)
138 {
139 }
140
141 /**
142  * Called when a key is pressed and this is the current action.
143  * This function can be overwritten by the implementing action.
144  * The default implementation does nothing.
145  */
146 void RS_ActionInterface::keyPressEvent(QKeyEvent * e)
147 {
148     e->ignore();
149 }
150
151 /**
152  * Called when a key is released and this is the current action.
153  * This function can be overwritten by the implementing action.
154  * The default implementation does nothing.
155  */
156 void RS_ActionInterface::keyReleaseEvent(QKeyEvent * e)
157 {
158     e->ignore();
159 }
160
161 /**
162  * Coordinate event. Triggered usually from a command line.
163  * This function can be overwritten by the implementing action.
164  * The default implementation does nothing.
165  */
166 //void RS_ActionInterface::coordinateEvent(RS_CoordinateEvent *)
167 void RS_ActionInterface::coordinateEvent(Vector *)
168 {
169 }
170
171 /**
172  * Called when a command from the command line is launched.
173  * and this is the current action.
174  * This function can be overwritten by the implementing action.
175  * The default implementation does nothing.
176  */
177 void RS_ActionInterface::commandEvent(RS_CommandEvent *)
178 {
179 }
180
181 /**
182  * Must be implemented to return the currently available commands
183  *  for the command line.
184  */
185 QStringList RS_ActionInterface::getAvailableCommands()
186 {
187         QStringList l;
188         return l;
189 }
190
191 /**
192  * Sets the current status (progress) of this action.
193  * The default implementation sets the class variable 'status' to the
194  * given value and finishes the action if 'status' is negative.
195  *
196  * @param status Status number. It's up to the action implementor
197  *               what the action uses the status for. However, a
198  *               negative status number finishes the action. Usually
199  *               the status of an action increases for every step
200  *               of progress and decreases when the user goes one
201  *               step back (i.e. presses the right mouse button).
202  */
203 void RS_ActionInterface::setStatus(int status)
204 {
205         this->status = status;
206
207         if (status < 0)
208         {
209                 finish();
210                 status = 0;
211         }
212
213         updateMouseButtonHints();
214         updateToolBar();
215         updateMouseCursor();
216 }
217
218 /**
219  * @return Current status of this action.
220  */
221 int RS_ActionInterface::getStatus()
222 {
223         return status;
224 }
225
226 /**
227  * Triggers this action. This should be called after all
228  * data needed for this action was collected / set.
229  * The default implementation does nothing.
230  */
231 void RS_ActionInterface::trigger()
232 {
233 }
234
235 /**
236  * Should be overwritten to update the mouse button hints
237  * wherever they might needed.
238  */
239 void RS_ActionInterface::updateMouseButtonHints()
240 {
241 }
242
243 /**
244  * Should be overwritten to set the mouse cursor for this action.
245  */
246 void RS_ActionInterface::updateMouseCursor()
247 {
248 }
249
250 /**
251  * Should be overwritten to set the toolbar for this action.
252  */
253 void RS_ActionInterface::updateToolBar()
254 {
255 }
256
257 /**
258  * @return true, if the action is finished and can be deleted.
259  */
260 bool RS_ActionInterface::isFinished()
261 {
262         return finished;
263 }
264
265 /**
266  * Forces a termination of the action without any cleanup.
267  */
268 void RS_ActionInterface::setFinished()
269 {
270         status = -1;
271 }
272
273 /**
274  * Finishes this action.
275  */
276 void RS_ActionInterface::finish()
277 {
278         RS_DEBUG->print("RS_ActionInterface::finish");
279         status = -1;
280         graphicView->setMouseCursor(RS2::ArrowCursor);
281         //graphicView->requestToolBar(RS2::ToolBarMain);
282         updateToolBar();
283 //jlh:  deleteSnapper();
284         hideOptions();
285         finished = true;
286         RS_Snapper::finish();
287         RS_DEBUG->print("RS_ActionInterface::finish: OK");
288 }
289
290 /**
291  * Called by the event handler to give this action a chance to
292  * communicate with its predecessor.
293  */
294 void RS_ActionInterface::setPredecessor(RS_ActionInterface * pre)
295 {
296         predecessor = pre;
297 }
298
299 /**
300  * Suspends this action while another action takes place.
301  */
302 void RS_ActionInterface::suspend()
303 {
304         graphicView->setMouseCursor(RS2::ArrowCursor);
305         RS_Snapper::suspend();
306 }
307
308 /**
309  * Resumes an action after it was suspended.
310  */
311 void RS_ActionInterface::resume()
312 {
313         updateMouseCursor();
314         updateToolBar();
315         RS_Snapper::resume();
316 }
317
318 /**
319  * Hides the tool options. Default implementation does nothing.
320  */
321 void RS_ActionInterface::hideOptions()
322 {
323         RS_Snapper::hideOptions();
324 }
325
326 /**
327  * Shows the tool options. Default implementation does nothing.
328  */
329 void RS_ActionInterface::showOptions()
330 {
331         RS_Snapper::showOptions();
332 }
333
334 /**
335  * Calls checkCommand() from the RS_COMMANDS module.
336  */
337 bool RS_ActionInterface::checkCommand(const QString & cmd, const QString & str,
338         RS2::ActionType action)
339 {
340         return RS_COMMANDS->checkCommand(cmd, str, action);
341 }
342
343 /**
344  * Calls command() from the RS_COMMANDS module.
345  */
346 QString RS_ActionInterface::command(const QString & cmd)
347 {
348         return RS_COMMANDS->command(cmd);
349 }
350
351 /**
352  * Calls msgAvailableCommands() from the RS_COMMANDS module.
353  */
354 QString RS_ActionInterface::msgAvailableCommands()
355 {
356         return RS_COMMANDS->msgAvailableCommands();
357 }