]> Shamusworld >> Repos - architektonas/blob - src/base/actioninterface.cpp
033fbe1c428e66f3b943ce7a88f3d40b4cf9aa0d
[architektonas] / src / base / actioninterface.cpp
1 // 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 "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 ActionInterface::ActionInterface(const char * name, RS_EntityContainer & c,
39         GraphicView & v): graphicView(&v), container(&c)
40 {
41         RS_DEBUG->print("ActionInterface::ActionInterface: Setting up action: \"%s\"", name);
42
43         this->name = name;
44         status = 0;
45         finished = false;
46
47         // Graphic provides a pointer to the graphic if the entity container is a
48         //graphic (i.e. can also hold layers).
49         graphic = c.getGraphic();
50
51         // Document pointer will be used for undo / redo
52         document = c.getDocument();
53
54         // This is here until I can figure out a better way to contain all of this
55         // circular referential nonsense that exists in this codebase. It will be
56         // expunged, by Grabthar's Hammer!
57         graphicView->snapper.SetContainer(container);
58         graphicView->snapper.SetGraphicView(graphicView);       // <-- THIS is what I mean! INSANE!
59
60         RS_DEBUG->print("ActionInterface::ActionInterface: Setting up action: \"%s\": OK", name);
61 }
62
63 /**
64  * Destructor.
65  */
66 ActionInterface::~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 ActionInterface::rtti()
79 {
80         return RS2::ActionNone;
81 }
82
83 /**
84  * @return name of this action
85  */
86 QString ActionInterface::getName()
87 {
88         return name;
89 }
90
91 /**
92  * Called to initiate an action. This function 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 ActionInterface::init(int status/*= 0*/)
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 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 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 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 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 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 ActionInterface::coordinateEvent(Vector *)
167 {
168 }
169
170 /**
171  * Called when a command from the command line is launched.
172  * and this is the current action.
173  * This function can be overwritten by the implementing action.
174  * The default implementation does nothing.
175  */
176 void ActionInterface::commandEvent(RS_CommandEvent *)
177 {
178 }
179
180 /**
181  * Must be implemented to return the currently available commands
182  *  for the command line.
183  */
184 QStringList ActionInterface::getAvailableCommands()
185 {
186         QStringList l;
187         return l;
188 }
189
190 /**
191  * Sets the current status (progress) of this action.
192  * The default implementation sets the class variable 'status' to the
193  * given value and finishes the action if 'status' is negative.
194  *
195  * @param status Status number. It's up to the action implementor
196  *               what the action uses the status for. However, a
197  *               negative status number finishes the action. Usually
198  *               the status of an action increases for every step
199  *               of progress and decreases when the user goes one
200  *               step back (i.e. presses the right mouse button).
201  */
202 void ActionInterface::setStatus(int status)
203 {
204         this->status = status;
205
206         if (status < 0)
207         {
208                 finish();
209                 status = 0;
210         }
211
212         updateMouseButtonHints();
213         updateToolBar();
214         updateMouseCursor();
215 }
216
217 /**
218  * @return Current status of this action.
219  */
220 int ActionInterface::getStatus()
221 {
222         return status;
223 }
224
225 /**
226  * Triggers this action. This should be called after all
227  * data needed for this action was collected / set.
228  * The default implementation does nothing.
229  */
230 void ActionInterface::trigger()
231 {
232 }
233
234 /**
235  * Should be overwritten to update the mouse button hints
236  * wherever they might needed.
237  */
238 void ActionInterface::updateMouseButtonHints()
239 {
240 }
241
242 /**
243  * Should be overwritten to set the mouse cursor for this action.
244  */
245 void ActionInterface::updateMouseCursor()
246 {
247 }
248
249 /**
250  * Should be overwritten to set the toolbar for this action.
251  */
252 void ActionInterface::updateToolBar()
253 {
254 }
255
256 /**
257  * @return true, if the action is finished and can be deleted.
258  */
259 bool ActionInterface::isFinished()
260 {
261         return finished;
262 }
263
264 /**
265  * Forces a termination of the action without any cleanup.
266  */
267 void ActionInterface::setFinished()
268 {
269         status = -1;
270 }
271
272 /**
273  * Finishes this action.
274  */
275 void ActionInterface::finish()
276 {
277         RS_DEBUG->print("ActionInterface::finish");
278         status = -1;
279 //      graphicView->setMouseCursor(RS2::ArrowCursor);
280         //graphicView->requestToolBar(RS2::ToolBarMain);
281         updateToolBar();
282 //Maybe change this to SnapperOff()?
283 //jlh:  deleteSnapper();
284         hideOptions();
285         finished = true;
286 //      RS_Snapper::finish();           // Sets RS_Snapper::finished = true
287         // I think this is where we want to update the screen...
288 //      graphicView->redraw();
289         RS_DEBUG->print("ActionInterface::finish: OK");
290 }
291
292 /**
293  * Called by the event handler to give this action a chance to
294  * communicate with its predecessor.
295  */
296 void ActionInterface::setPredecessor(ActionInterface * p)
297 {
298         predecessor = p;
299 }
300
301 /**
302  * Suspends this action while another action takes place.
303  */
304 void ActionInterface::suspend()
305 {
306 //      graphicView->setMouseCursor(RS2::ArrowCursor);
307 //      RS_Snapper::suspend();
308 }
309
310 /**
311  * Resumes an action after it was suspended.
312  */
313 void ActionInterface::resume()
314 {
315         updateMouseCursor();
316         updateToolBar();
317 //      RS_Snapper::resume();
318 }
319
320 /**
321  * Hides the tool options. Default implementation does nothing.
322  */
323 void ActionInterface::hideOptions()
324 {
325 //      RS_Snapper::hideOptions();
326 }
327
328 /**
329  * Shows the tool options. Default implementation does nothing.
330  */
331 void ActionInterface::showOptions()
332 {
333 //      RS_Snapper::showOptions();
334 }
335
336 /**
337  * Calls checkCommand() from the RS_COMMANDS module.
338  */
339 bool ActionInterface::checkCommand(const QString & cmd, const QString & str,
340         RS2::ActionType action)
341 {
342         return RS_COMMANDS->checkCommand(cmd, str, action);
343 }
344
345 /**
346  * Calls command() from the RS_COMMANDS module.
347  */
348 QString ActionInterface::command(const QString & cmd)
349 {
350         return RS_COMMANDS->command(cmd);
351 }
352
353 /**
354  * Calls msgAvailableCommands() from the RS_COMMANDS module.
355  */
356 QString ActionInterface::msgAvailableCommands()
357 {
358         return RS_COMMANDS->msgAvailableCommands();
359 }
360
361 // This is here to save some typing in all the action* classes derived from
362 // this one. May go away in the future.
363 Vector ActionInterface::snapPoint(QMouseEvent * e)
364 {
365         return graphicView->snapper.snapPoint(e);
366 }
367
368 RS_Entity * ActionInterface::catchEntity(QMouseEvent * e, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
369 {
370         return graphicView->snapper.catchEntity(e, level);
371 }
372
373 RS_Entity * ActionInterface::catchEntity(Vector v, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
374 {
375         return graphicView->snapper.catchEntity(v, level);
376 }
377
378 //dummy functions, will delete later...
379 void ActionInterface::drawSnapper(void)
380 {
381 }
382
383 void ActionInterface::deleteSnapper(void)
384 {
385 }
386
387 void ActionInterface::drawPreview(void)
388 {
389 }
390
391 void ActionInterface::clearPreview(void)
392 {
393 }
394
395 void ActionInterface::deletePreview(void)
396 {
397 }