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