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