]> Shamusworld >> Repos - architektonas/blob - src/base/actioninterface.cpp
1b4d5d8171303c46f9658943b82853a27675bf4a
[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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/22/2010  Added this text. :-)
15 // JLH  08/09/2010  Preparation for removal of GraphicView object from this
16 //                  class
17 //
18
19 #include "actioninterface.h"
20
21 #include "commands.h"
22 #include "debug.h"
23 #include "dialogfactory.h"
24 #include "entitycontainer.h"
25 #include "graphicview.h"
26 #include "grid.h"
27
28 /*
29 I think what's needed here is for the constructor to save the state of the snapper
30 and to restore it in the destructor. This, of course, assumes that the actions are
31 created and used in a certain order, perhaps that needs enforcement? Dunno, but worth
32 a try as suspend() and resume() seem to fuck it up badly.
33 #define _ASSUAN_DEPRECATED  __attribute__ ((__deprecated__))
34 on MS it's: __declspec(deprecated)
35 */
36
37 /**
38  * Constructor.
39  *
40  * Sets the entity container on which the action class inherited from this
41  * interface operates.
42  *
43  * @param name Action name. This can be used internally for debugging mainly.
44  * @param container Entity container this action operates on.
45  * @param graphicView Graphic view instance this action operates on. Please
46  *                    note that an action belongs to this view.
47  */
48 ActionInterface::ActionInterface(const char * name, EntityContainer & ec,
49         GraphicView & gv): graphicView(&gv), container(&ec),
50 //      snapperVisibility(false), previewVisibility(false), suspendCount(0)
51 //hm.
52         snapperVisibility(true), previewVisibility(true), suspendCount(0)
53 {
54         DEBUG->print("ActionInterface::ActionInterface: Setting up action: \"%s\"", name);
55
56 //This doesn't work properly; not sure why that is...
57 //Actually, it's working perfectly. Now we just need to propagate the fixes everywhere. :-/
58         // We'll use snapperVisibility for the save/restore functionality...
59         snapperVisibility = graphicView->SnapperVisible();
60
61         this->name = name;
62         status = 0;
63         finished = false;
64
65         // Graphic provides a pointer to the graphic if the entity container is a
66         // graphic (i.e. can also hold layers).
67         graphic = ec.getGraphic();
68
69         // Document pointer will be used for undo / redo
70         document = ec.getDocument();
71
72         // \o/ \o/ \o/ BY GRABTHAR'S HAMMER, IT HAS BEEN EXPUNGED!!! \o/ \o/ \o/
73
74         // This is here until I can figure out a better way to contain all of this
75         // circular referential nonsense that exists in this codebase. It will be
76         // expunged, by Grabthar's Hammer!
77 //      graphicView->snapper.SetContainer(container);
78 //      graphicView->snapper.SetGraphicView(graphicView);       // <-- THIS is what I mean! INSANE!
79         // Not all actions use these. Perhaps we need to pass params to the contructor
80         // in order to set these? Setting the default to true for both?
81 //      graphicView->snapper.SetVisible();
82         graphicView->SetSnapperVisible();
83         graphicView->preview.SetVisible();
84
85         DEBUG->print("ActionInterface::ActionInterface: Setting up action: \"%s\": OK", name);
86 //printf("ActionInterface::ActionInterface() [%08X]\n", this);
87 }
88
89 /**
90  * Destructor.
91  */
92 /*virtual*/ ActionInterface::~ActionInterface()
93 {
94         // would be pure virtual now:
95         // hideOptions();
96 //JLH: Only it isn't pure virtual...
97 //printf("ActionInterface::~ActionInterface() [%08X]\n", this);
98
99         // We'll use snapperVisibility for the save/restore functionality...
100         graphicView->SetSnapperVisible(snapperVisibility);
101 }
102
103 /**
104  * Must be implemented to return the ID of this action.
105 *
106 * @todo no default implementation
107  */
108 RS2::ActionType ActionInterface::rtti()
109 {
110         return RS2::ActionNone;
111 }
112
113 /**
114  * @return name of this action
115  */
116 QString ActionInterface::getName()
117 {
118         return name;
119 }
120
121 /**
122  * Called to initiate an action. This function is often
123  * overwritten by the implementing action.
124  *
125  * @param status The status on which to initiate this action.
126  * default is 0 to begin the action.
127  */
128 void ActionInterface::init(int status/*= 0*/)
129 {
130 //      Snapper::init();
131         setStatus(status);
132
133         if (status >= 0)
134         {
135                 //graphicView->setMouseCursor(cursor);
136                 updateMouseButtonHints();
137                 updateMouseCursor();
138                 updateToolBar();
139         }
140         else    // status < 0, e.g. this action is finished
141         {
142 //              graphicView->snapper.SetVisible(false);
143                 graphicView->SetSnapperVisible(false);
144                 graphicView->preview.SetVisible(false);
145                 graphicView->preview.clear();
146                 graphicView->redraw();  //hm.
147         }
148 }
149
150 /**
151  * Called when the mouse moves and this is the current action.
152  * This function can be overwritten by the implementing action.
153  * The default implementation keeps track of the mouse position.
154  */
155 void ActionInterface::mouseMoveEvent(QMouseEvent *)
156 {
157 }
158
159 /**
160  * Called when the left mouse button is pressed and this is the
161  * current action.
162  * This function can be overwritten by the implementing action.
163  * The default implementation does nothing.
164  */
165 void ActionInterface::mousePressEvent(QMouseEvent *)
166 {
167 }
168
169 /**
170  * Called when the left mouse button is released and this is
171  * the current action.
172  * This function can be overwritten by the implementing action.
173  * The default implementation does nothing.
174  */
175 void ActionInterface::mouseReleaseEvent(QMouseEvent *)
176 {
177 }
178
179 /**
180  * Called when a key is pressed and this is the current action.
181  * This function can be overwritten by the implementing action.
182  * The default implementation does nothing.
183  */
184 void ActionInterface::keyPressEvent(QKeyEvent * e)
185 {
186     e->ignore();
187 }
188
189 /**
190  * Called when a key is released and this is the current action.
191  * This function can be overwritten by the implementing action.
192  * The default implementation does nothing.
193  */
194 void ActionInterface::keyReleaseEvent(QKeyEvent * e)
195 {
196     e->ignore();
197 }
198
199 /**
200  * Coordinate event. Triggered usually from a command line.
201  * This function can be overwritten by the implementing action.
202  * The default implementation does nothing.
203  */
204 void ActionInterface::coordinateEvent(Vector *)
205 {
206 }
207
208 /**
209  * Called when a command from the command line is launched.
210  * and this is the current action.
211  * This function can be overwritten by the implementing action.
212  * The default implementation does nothing.
213  */
214 void ActionInterface::commandEvent(CommandEvent *)
215 {
216 }
217
218 /**
219  * Must be implemented to return the currently available commands
220  *  for the command line.
221  */
222 QStringList ActionInterface::getAvailableCommands()
223 {
224         QStringList l;
225         return l;
226 }
227
228 /**
229  * Sets the current status (progress) of this action.
230  * The default implementation sets the class variable 'status' to the
231  * given value and finishes the action if 'status' is negative.
232  *
233  * @param status Status number. It's up to the action implementor
234  *               what the action uses the status for. However, a
235  *               negative status number finishes the action. Usually
236  *               the status of an action increases for every step
237  *               of progress and decreases when the user goes one
238  *               step back (i.e. presses the right mouse button).
239  */
240 void ActionInterface::setStatus(int value)
241 {
242         status = value;
243
244         if (status < 0)
245         {
246                 finish();
247                 status = 0;
248         }
249
250         updateMouseButtonHints();
251         updateToolBar();
252         updateMouseCursor();
253 }
254
255 /**
256  * @return Current status of this action.
257  */
258 int ActionInterface::getStatus()
259 {
260         return status;
261 }
262
263 /**
264  * Triggers this action. This should be called after all
265  * data needed for this action was collected / set.
266  * The default implementation does nothing.
267  */
268 void ActionInterface::trigger()
269 {
270 }
271
272 /**
273  * Should be overwritten to update the mouse button hints
274  * wherever they might needed.
275  */
276 void ActionInterface::updateMouseButtonHints()
277 {
278 }
279
280 /**
281  * Should be overwritten to set the mouse cursor for this action.
282  */
283 void ActionInterface::updateMouseCursor()
284 {
285 }
286
287 /**
288  * Should be overwritten to set the toolbar for this action.
289  */
290 void ActionInterface::updateToolBar()
291 {
292 }
293
294 /**
295  * @return true, if the action is finished and can be deleted.
296  */
297 bool ActionInterface::isFinished()
298 {
299         return finished;
300 }
301
302 /**
303  * Forces a termination of the action without any cleanup.
304  */
305 void ActionInterface::setFinished()
306 {
307         status = -1;
308 }
309
310 /**
311  * Finishes this action.
312  */
313 void ActionInterface::finish()
314 {
315         DEBUG->print("ActionInterface::finish");
316         status = -1;
317 //      graphicView->setMouseCursor(RS2::ArrowCursor);
318         //graphicView->requestToolBar(RS2::ToolBarMain);
319         updateToolBar();
320 //Maybe change this to SnapperOff()?
321 //jlh:  deleteSnapper();
322         hideOptions();
323         finished = true;
324 //      Snapper::finish();              // Sets Snapper::finished = true
325         // I think this is where we want to update the screen...
326 //      graphicView->redraw();
327         // hm.
328 //      graphicView->snapper.SetVisible(false);
329         graphicView->SetSnapperVisible(false);
330 //Short circuit the destructor fuxoring with this:
331 //snapperVisibility = false;
332 //Only it causes other stuff to be fuxorred... Grr... Not sure how to fix this...
333         graphicView->preview.SetVisible(false);
334 //      graphicView->preview.clear();
335         graphicView->redraw();  //hm.
336         DEBUG->print("ActionInterface::finish: OK");
337 }
338
339 /**
340  * Called by the event handler to give this action a chance to
341  * communicate with its predecessor.
342  */
343 void ActionInterface::setPredecessor(ActionInterface * p)
344 {
345         predecessor = p;
346 }
347
348 #if 0
349 Here is a problem. suspend() and resume() don't do what they should:
350 The problem is that Actions are modifying a shared resource though it's acting
351 as if it were not. Case in point below: ActionZoomPan sets the snapper/preview
352 visibility to FALSE and then EventHandler calls suspend() here, which queries
353 the graphicView to see what its state is. We need to fix this...!
354
355 This ties into the problem where we have GraphicView pointers scattered all
356 over the place. We need to fix that too!
357 #endif
358 /**
359  * Suspends this action while another action takes place.
360  */
361 void ActionInterface::suspend()
362 {
363 #if 0
364 printf("ActionInterface::suspend(%i): [%08X] ", ++suspendCount, this);
365         // Maybe this is where we need to save the state of the snapper
366         // & preview objects???
367 //      graphicView->setMouseCursor(RS2::ArrowCursor);
368 //      Snapper::suspend();
369         snapperVisibility = graphicView->snapper.Visible();
370         previewVisibility = graphicView->preview.Visible();
371 printf("snapperVisibility = %s, previewVisibility = %s...\n", (snapperVisibility ? "true" : "FALSE"), (previewVisibility ? "true" : "FALSE"));
372 #endif
373 }
374
375 /**
376  * Resumes an action after it was suspended.
377  */
378 void ActionInterface::resume()
379 {
380 #if 0
381 if (suspendCount == 0)
382         printf("!!! RESUME BEFORE SUSPEND !!!\n");
383 printf("ActionInterface::resume(%i): [%08X] ", suspendCount, this);
384         updateMouseCursor();
385         updateToolBar();
386 //      Snapper::resume();
387         graphicView->snapper.SetVisible(snapperVisibility);
388         graphicView->preview.SetVisible(previewVisibility);
389 printf("snapperVisibility = %s, previewVisibility = %s...\n", (snapperVisibility ? "true" : "FALSE"), (previewVisibility ? "true" : "FALSE"));
390 #endif
391 }
392
393 /**
394  * Hides the tool options. Default implementation does nothing.
395  */
396 void ActionInterface::hideOptions()
397 {
398 //      Snapper::hideOptions();
399 }
400
401 /**
402  * Shows the tool options. Default implementation does nothing.
403  */
404 void ActionInterface::showOptions()
405 {
406 //      Snapper::showOptions();
407 }
408
409 /**
410  * Calls checkCommand() from the COMMANDS module.
411  */
412 bool ActionInterface::checkCommand(const QString & cmd, const QString & str,
413         RS2::ActionType action)
414 {
415         return COMMANDS->checkCommand(cmd, str, action);
416 }
417
418 /**
419  * Calls command() from the COMMANDS module.
420  */
421 QString ActionInterface::command(const QString & cmd)
422 {
423         return COMMANDS->command(cmd);
424 }
425
426 /**
427  * Calls msgAvailableCommands() from the COMMANDS module.
428  */
429 QString ActionInterface::msgAvailableCommands()
430 {
431         return COMMANDS->msgAvailableCommands();
432 }
433
434 // This is here to save some typing in all the action* classes derived from
435 // this one. May go away in the future.
436 Vector ActionInterface::snapPoint(QMouseEvent * e)
437 {
438 //      return graphicView->snapper.snapPoint(e);
439         return graphicView->SnapPoint(e);
440 }
441
442 Entity * ActionInterface::catchEntity(QMouseEvent * e, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
443 {
444         return graphicView->CatchEntity(e, level);
445 }
446
447 Entity * ActionInterface::catchEntity(Vector v, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
448 {
449         return graphicView->CatchEntity(v, level);
450 }
451
452 #warning "!!! Dummy functions need to be deleted once all actions no longer use these !!!"
453 //dummy functions, will delete later...
454 void ActionInterface::drawSnapper(void)
455 {
456 }
457
458 void ActionInterface::deleteSnapper(void)
459 {
460 }
461
462 void ActionInterface::drawPreview(void)
463 {
464 }
465
466 void ActionInterface::clearPreview(void)
467 {
468 }
469
470 void ActionInterface::deletePreview(void)
471 {
472 }