]> Shamusworld >> Repos - architektonas/blob - src/base/actioninterface.cpp
9f38848e2df778ae588dc33ab3dfb9dee8e2435d
[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         graphicView->snapper.SetVisible();
62         graphicView->preview.SetVisible();
63
64         RS_DEBUG->print("ActionInterface::ActionInterface: Setting up action: \"%s\": OK", name);
65 }
66
67 /**
68  * Destructor.
69  */
70 ActionInterface::~ActionInterface()
71 {
72         // would be pure virtual now:
73         // hideOptions();
74 //JLH: Only it isn't pure virtual...
75 }
76
77 /**
78  * Must be implemented to return the ID of this action.
79 *
80 * @todo no default implementation
81  */
82 RS2::ActionType ActionInterface::rtti()
83 {
84         return RS2::ActionNone;
85 }
86
87 /**
88  * @return name of this action
89  */
90 QString ActionInterface::getName()
91 {
92         return name;
93 }
94
95 /**
96  * Called to initiate an action. This function is often
97  * overwritten by the implementing action.
98  *
99  * @param status The status on which to initiate this action.
100  * default is 0 to begin the action.
101  */
102 void ActionInterface::init(int status/*= 0*/)
103 {
104 //      RS_Snapper::init();
105         setStatus(status);
106
107         if (status >= 0)
108         {
109                 //graphicView->setMouseCursor(cursor);
110                 updateMouseButtonHints();
111                 updateMouseCursor();
112                 updateToolBar();
113         }
114         else    // status < 0, e.g. this action is finished
115         {
116                 graphicView->snapper.SetVisible(false);
117                 graphicView->preview.SetVisible(false);
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 //      graphicView->setMouseCursor(RS2::ArrowCursor);
316 //      RS_Snapper::suspend();
317 }
318
319 /**
320  * Resumes an action after it was suspended.
321  */
322 void ActionInterface::resume()
323 {
324         updateMouseCursor();
325         updateToolBar();
326 //      RS_Snapper::resume();
327 }
328
329 /**
330  * Hides the tool options. Default implementation does nothing.
331  */
332 void ActionInterface::hideOptions()
333 {
334 //      RS_Snapper::hideOptions();
335 }
336
337 /**
338  * Shows the tool options. Default implementation does nothing.
339  */
340 void ActionInterface::showOptions()
341 {
342 //      RS_Snapper::showOptions();
343 }
344
345 /**
346  * Calls checkCommand() from the RS_COMMANDS module.
347  */
348 bool ActionInterface::checkCommand(const QString & cmd, const QString & str,
349         RS2::ActionType action)
350 {
351         return RS_COMMANDS->checkCommand(cmd, str, action);
352 }
353
354 /**
355  * Calls command() from the RS_COMMANDS module.
356  */
357 QString ActionInterface::command(const QString & cmd)
358 {
359         return RS_COMMANDS->command(cmd);
360 }
361
362 /**
363  * Calls msgAvailableCommands() from the RS_COMMANDS module.
364  */
365 QString ActionInterface::msgAvailableCommands()
366 {
367         return RS_COMMANDS->msgAvailableCommands();
368 }
369
370 // This is here to save some typing in all the action* classes derived from
371 // this one. May go away in the future.
372 Vector ActionInterface::snapPoint(QMouseEvent * e)
373 {
374         return graphicView->snapper.snapPoint(e);
375 }
376
377 RS_Entity * ActionInterface::catchEntity(QMouseEvent * e, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
378 {
379         return graphicView->snapper.catchEntity(e, level);
380 }
381
382 RS_Entity * ActionInterface::catchEntity(Vector v, RS2::ResolveLevel level/*= RS2::ResolveNone*/)
383 {
384         return graphicView->snapper.catchEntity(v, level);
385 }
386
387 //dummy functions, will delete later...
388 void ActionInterface::drawSnapper(void)
389 {
390 }
391
392 void ActionInterface::deleteSnapper(void)
393 {
394 }
395
396 void ActionInterface::drawPreview(void)
397 {
398 }
399
400 void ActionInterface::clearPreview(void)
401 {
402 }
403
404 void ActionInterface::deletePreview(void)
405 {
406 }