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