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