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