]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondefault.cpp
Sanity check stage II: rename classes...
[architektonas] / src / actions / actiondefault.cpp
1 // actiondefault.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  06/03/2010  Added this text. :-)
15 //
16
17 #include "actiondefault.h"
18
19 #include "rs.h"
20 #include "rs_commandevent.h"
21 #include "rs_debug.h"
22 #include "rs_dialogfactory.h"
23 #include "rs_line.h"
24 #include "rs_modification.h"
25 #include "rs_preview.h"
26 #include "rs_snapper.h"
27 #include "rs_selection.h"
28
29 /**
30  * Constructor.
31  */
32 ActionDefault::ActionDefault(RS_EntityContainer & container,
33         GraphicView & graphicView): ActionInterface("Default",
34         container, graphicView)
35 {
36         RS_DEBUG->print("ActionDefault::ActionDefault");
37         //hm.
38         graphicView.snapper.SetVisible();
39         graphicView.preview.SetVisible();
40         RS_DEBUG->print("ActionDefault::ActionDefault: OK");
41 }
42
43 ActionDefault::~ActionDefault()
44 {
45 }
46
47 /*virtual*/ RS2::ActionType ActionDefault::rtti()
48 {
49         return RS2::ActionDefault;
50 }
51
52 void ActionDefault::init(int status /*= 0*/)
53 {
54         RS_DEBUG->print("ActionDefault::init");
55
56         ActionInterface::init(status);
57         v1 = v2 = Vector(false);
58         graphicView->snapper.setSnapMode(RS2::SnapFree);
59         graphicView->snapper.setSnapRestriction(RS2::RestrictNothing);
60         restrBak = RS2::RestrictNothing;
61         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
62 //hm. Nope.
63 //      graphicView->snapper.SetVisible();
64 //      graphicView->preview.SetVisible();
65
66         RS_DEBUG->print("ActionDefault::init: OK");
67 }
68
69 void ActionDefault::trigger()
70 {
71         ActionInterface::trigger();
72 }
73
74 void ActionDefault::keyPressEvent(QKeyEvent * e)
75 {
76         if (e->key() == Qt::Key_Shift)
77         {
78                 restrBak = graphicView->snapper.getSnapRestriction();
79                 graphicView->snapper.setSnapRestriction(RS2::RestrictOrthogonal);
80         }
81 }
82
83 void ActionDefault::keyReleaseEvent(QKeyEvent * e)
84 {
85         if (e->key() == Qt::Key_Shift)
86                 graphicView->snapper.setSnapRestriction(restrBak);
87 }
88
89 void ActionDefault::mouseMoveEvent(QMouseEvent * e)
90 {
91         Vector mouse = graphicView->toGraph(Vector(e->x(), e->y()));
92         Vector relMouse = mouse - graphicView->getRelativeZero();
93
94         RS_DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse);
95
96         switch (getStatus())
97         {
98         case Dragging:
99                 v2 = mouse;
100
101                 if (graphicView->toGuiDX(v1.distanceTo(v2)) > 10)
102                 {
103                         // look for reference points to drag:
104                         double dist;
105                         Vector ref = container->getNearestSelectedRef(v1, &dist);
106
107                         if (ref.valid && graphicView->toGuiDX(dist) < 8)
108                         {
109                                 RS_DEBUG->print("ActionDefault::mouseMoveEvent: moving reference point");
110                                 setStatus(MovingRef);
111                                 v1 = ref;
112                                 graphicView->moveRelativeZero(v1);
113                         }
114                         else
115                         {
116                                 // test for an entity to drag:
117                                 RS_Entity * en = graphicView->snapper.catchEntity(v1);
118
119                                 if (en && en->isSelected())
120                                 {
121                                         RS_DEBUG->print("ActionDefault::mouseMoveEvent: moving entity");
122                                         setStatus(Moving);
123                                         v1 = en->getNearestRef(v1);
124                                         graphicView->moveRelativeZero(v1);
125                                 }
126                                 // no entity found. start area selection:
127                                 else
128                                         setStatus(SetCorner2);
129                         }
130                 }
131
132                 break;
133
134         case MovingRef:
135                 v2 = snapPoint(e);
136
137 //              clearPreview();
138 //              preview->addSelectionFrom(*container);
139 //              preview->moveRef(v1, v2 - v1);
140 //              drawPreview();
141                 graphicView->preview.clear();
142                 graphicView->preview.addSelectionFrom(*container);
143                 graphicView->preview.moveRef(v1, v2 -v1);
144                 graphicView->snapper.SetVisible();
145                 graphicView->preview.SetVisible();
146                 graphicView->redraw();
147                 break;
148
149         case Moving:
150                 v2 = snapPoint(e);
151
152 //              clearPreview();
153 //              preview->addSelectionFrom(*container);
154 //              preview->move(v2 - v1);
155 //              drawPreview();
156                 graphicView->preview.clear();
157                 graphicView->preview.addSelectionFrom(*container);
158                 graphicView->preview.move(v2 -v1);
159                 graphicView->snapper.SetVisible();
160                 graphicView->preview.SetVisible();
161                 graphicView->redraw();
162                 break;
163
164         case SetCorner2:
165                 if (v1.valid)
166                 {
167                         v2 = mouse;
168
169                         graphicView->preview.clear();
170
171                         graphicView->preview.addEntity(new RS_Line(&(graphicView->preview),
172                                 RS_LineData(Vector(v1.x, v1.y), Vector(v2.x, v1.y))));
173                         graphicView->preview.addEntity(new RS_Line(&(graphicView->preview),
174                                 RS_LineData(Vector(v2.x, v1.y), Vector(v2.x, v2.y))));
175                         graphicView->preview.addEntity(new RS_Line(&(graphicView->preview),
176                                 RS_LineData(Vector(v2.x, v2.y), Vector(v1.x, v2.y))));
177                         graphicView->preview.addEntity(new RS_Line(&(graphicView->preview),
178                                 RS_LineData(Vector(v1.x, v2.y), Vector(v1.x, v1.y))));
179
180                         graphicView->preview.SetVisible();
181                         graphicView->redraw();
182                 }
183
184         default:
185                 break;
186         }
187 }
188
189 void ActionDefault::mousePressEvent(QMouseEvent * e)
190 {
191         if (e->button() == Qt::LeftButton)
192         {
193                 switch (getStatus())
194                 {
195                 case Neutral:
196                         v1 = graphicView->toGraph(e->x(), e->y());
197                         setStatus(Dragging);
198                         break;
199
200                 case Moving:
201                 {
202                         v2 = snapPoint(e);
203 //                      deleteSnapper();
204 //                      deletePreview();
205 //                      clearPreview();
206                         RS_Modification m(*container, graphicView);
207                         RS_MoveData data;
208                         data.number = 0;
209                         data.useCurrentLayer = false;
210                         data.useCurrentAttributes = false;
211                         data.offset = v2 - v1;
212                         m.move(data);
213                         setStatus(Neutral);
214                         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
215                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
216                 }
217                 break;
218
219                 case MovingRef:
220                 {
221                         v2 = snapPoint(e);
222 //                      deleteSnapper();
223 //                      deletePreview();
224 //                      clearPreview();
225                         RS_Modification m(*container, graphicView);
226                         RS_MoveRefData data;
227                         data.ref = v1;
228                         data.offset = v2 - v1;
229                         m.moveRef(data);
230                         setStatus(Neutral);
231                         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
232                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
233                 }
234                 break;
235
236                 default:
237                         break;
238                 }
239         }
240 }
241
242 void ActionDefault::mouseReleaseEvent(QMouseEvent * e)
243 {
244         RS_DEBUG->print("ActionDefault::mouseReleaseEvent()");
245
246         if (e->button() == Qt::LeftButton)
247         {
248                 v2 = graphicView->toGraph(e->x(), e->y());
249
250                 switch (getStatus())
251                 {
252                 case Dragging:
253                 {
254                         // select single entity:
255                         RS_Entity * en = graphicView->snapper.catchEntity(e);
256
257                         if (en)
258                         {
259 //                              deleteSnapper();
260 //                              deletePreview();
261 //                              clearPreview();
262
263                                 RS_Selection s(*container, graphicView);
264                                 s.selectSingle(en);
265
266                                 RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
267
268                                 e->accept();
269                                 setStatus(Neutral);
270                         }
271                         else
272                                 setStatus(SetCorner2);
273                 }
274                         break;
275
276                 case SetCorner2:
277                 {
278                         v2 = graphicView->toGraph(e->x(), e->y());
279
280                         // select window:
281 //                      deleteSnapper();
282 //                      deletePreview();
283 //                      clearPreview();
284                         graphicView->preview.SetVisible(false);
285
286                         bool cross = (v2.y > v1.y);
287                         RS_Selection s(*container, graphicView);
288                         s.selectWindow(v1, v2, true, cross);
289
290                         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
291
292                         setStatus(Neutral);
293                         e->accept();
294                 }
295                         break;
296
297                 default:
298                         // Was either moving entity or point, so clear that shiatsu
299                         graphicView->snapper.SetVisible(false);
300                         graphicView->preview.SetVisible(false);
301                         graphicView->redraw();
302                         break;
303                 }
304         }
305         else if (e->button() == Qt::RightButton)
306         {
307                 switch (getStatus())
308                 {
309                 case SetCorner2:
310                 case Moving:
311                 case MovingRef:
312 //                      deletePreview();
313 //                      clearPreview();
314 //                      deleteSnapper();
315                         setStatus(Neutral);
316                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
317                         e->accept();
318                         break;
319
320                 default:
321                         graphicView->snapper.SetVisible(false);
322                         graphicView->preview.SetVisible(false);
323                         RS_DIALOGFACTORY->requestPreviousMenu();
324                         e->accept();
325                         break;
326                 }
327         }
328 }
329
330 void ActionDefault::commandEvent(RS_CommandEvent * e)
331 {
332         QString c = e->getCommand().toLower();
333 }
334
335 QStringList ActionDefault::getAvailableCommands()
336 {
337         QStringList cmd;
338
339         return cmd;
340 }
341
342 void ActionDefault::updateMouseButtonHints()
343 {
344         switch (getStatus())
345         {
346         case Neutral:
347                 RS_DIALOGFACTORY->updateMouseWidget("", "");
348                 break;
349
350         case SetCorner2:
351                 RS_DIALOGFACTORY->updateMouseWidget(tr("Drag to second point"), tr("Back"));
352                 break;
353
354         default:
355                 RS_DIALOGFACTORY->updateMouseWidget("", "");
356                 break;
357         }
358 }
359
360 void ActionDefault::updateMouseCursor()
361 {
362         switch (getStatus())
363         {
364         case Neutral:
365                 graphicView->setMouseCursor(RS2::ArrowCursor);
366                 break;
367
368         case Moving:
369         case MovingRef:
370                 graphicView->setMouseCursor(RS2::SelectCursor);
371                 break;
372
373         default:
374                 break;
375         }
376 }
377
378 void ActionDefault::updateToolBar()
379 {
380         switch (getStatus())
381         {
382         case Neutral:
383                 break;
384
385         case Moving:
386         case MovingRef:
387                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
388                 break;
389
390         default:
391                 break;
392         }
393 }