]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondefault.cpp
Bugfixes related to removing Snapper class.
[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 "enums.h"
20 #include "commandevent.h"
21 #include "debug.h"
22 #include "dialogfactory.h"
23 #include "line.h"
24 #include "modification.h"
25 #include "preview.h"
26 #include "selection.h"
27
28 /**
29  * Constructor.
30  */
31 ActionDefault::ActionDefault(EntityContainer & container,
32         GraphicView & graphicView):
33         ActionInterface("Default", container, graphicView)
34 {
35         DEBUG->print("ActionDefault::ActionDefault");
36         DEBUG->print("ActionDefault::ActionDefault: OK");
37 }
38
39 ActionDefault::~ActionDefault()
40 {
41 }
42
43 /*virtual*/ RS2::ActionType ActionDefault::rtti()
44 {
45         return RS2::ActionDefault;
46 }
47
48 void ActionDefault::init(int status /*= 0*/)
49 {
50         DEBUG->print("ActionDefault::init");
51
52         ActionInterface::init(status);
53         v1 = v2 = Vector(false);
54 //      graphicView->snapper.setSnapMode(RS2::SnapFree);
55 //      graphicView->snapper.setSnapRestriction(RS2::RestrictNothing);
56         graphicView->setDefaultSnapMode(RS2::SnapFree);
57         graphicView->setSnapRestriction(RS2::RestrictNothing);
58         restrBak = RS2::RestrictNothing;
59         DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
60
61         DEBUG->print("ActionDefault::init: OK");
62 }
63
64 void ActionDefault::trigger()
65 {
66         ActionInterface::trigger();
67 }
68
69 void ActionDefault::keyPressEvent(QKeyEvent * e)
70 {
71         if (e->key() == Qt::Key_Shift)
72         {
73 //              restrBak = graphicView->snapper.getSnapRestriction();
74 //              graphicView->snapper.setSnapRestriction(RS2::RestrictOrthogonal);
75                 restrBak = graphicView->getSnapRestriction();
76                 graphicView->setSnapRestriction(RS2::RestrictOrthogonal);
77         }
78 }
79
80 void ActionDefault::keyReleaseEvent(QKeyEvent * e)
81 {
82         if (e->key() == Qt::Key_Shift)
83 //              graphicView->snapper.setSnapRestriction(restrBak);
84                 graphicView->setSnapRestriction(restrBak);
85 }
86
87 void ActionDefault::mouseMoveEvent(QMouseEvent * e)
88 {
89         Vector mouse = graphicView->toGraph(Vector(e->x(), e->y()));
90         Vector relMouse = mouse - graphicView->getRelativeZero();
91         DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse);
92
93         switch (getStatus())
94         {
95         case Dragging:
96                 v2 = mouse;
97
98                 if (graphicView->toGuiDX(v1.distanceTo(v2)) > 10)
99                 {
100                         // look for reference points to drag:
101                         double dist;
102                         Vector ref = container->getNearestSelectedRef(v1, &dist);
103
104                         if (ref.valid && graphicView->toGuiDX(dist) < 8)
105                         {
106                                 DEBUG->print("ActionDefault::mouseMoveEvent: moving reference point");
107                                 setStatus(MovingRef);
108                                 v1 = ref;
109                                 graphicView->moveRelativeZero(v1);
110                         }
111                         else
112                         {
113                                 // test for an entity to drag:
114 //                              Entity * en = graphicView->snapper.catchEntity(v1);
115                                 Entity * en = graphicView->CatchEntity(v1);
116
117                                 if (en && en->isSelected())
118                                 {
119                                         DEBUG->print("ActionDefault::mouseMoveEvent: moving entity");
120                                         setStatus(Moving);
121                                         v1 = en->getNearestRef(v1);
122                                         graphicView->moveRelativeZero(v1);
123                                 }
124                                 // no entity found. start area selection:
125                                 else
126                                         setStatus(SetCorner2);
127                         }
128                 }
129
130                 break;
131
132         case MovingRef:
133                 v2 = snapPoint(e);
134
135 //              clearPreview();
136 //              preview->addSelectionFrom(*container);
137 //              preview->moveRef(v1, v2 - v1);
138 //              drawPreview();
139                 graphicView->preview.clear();
140                 graphicView->preview.addSelectionFrom(*container);
141                 graphicView->preview.moveRef(v1, v2 -v1);
142 //              graphicView->snapper.SetVisible();
143                 graphicView->SetSnapperVisible();
144                 graphicView->preview.SetVisible();
145                 graphicView->redraw();
146                 break;
147
148         case Moving:
149                 v2 = snapPoint(e);
150
151 //              clearPreview();
152 //              preview->addSelectionFrom(*container);
153 //              preview->move(v2 - v1);
154 //              drawPreview();
155                 graphicView->preview.clear();
156                 graphicView->preview.addSelectionFrom(*container);
157                 graphicView->preview.move(v2 -v1);
158 //              graphicView->snapper.SetVisible();
159                 graphicView->SetSnapperVisible();
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 Line(&(graphicView->preview),
172                                 LineData(Vector(v1.x, v1.y), Vector(v2.x, v1.y))));
173                         graphicView->preview.addEntity(new Line(&(graphicView->preview),
174                                 LineData(Vector(v2.x, v1.y), Vector(v2.x, v2.y))));
175                         graphicView->preview.addEntity(new Line(&(graphicView->preview),
176                                 LineData(Vector(v2.x, v2.y), Vector(v1.x, v2.y))));
177                         graphicView->preview.addEntity(new Line(&(graphicView->preview),
178                                 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                         Modification m(*container, graphicView);
207                         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                         DIALOGFACTORY->updateSelectionWidget(container->countSelected());
215                         DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
216                 }
217                         break;
218
219                 case MovingRef:
220                 {
221                         v2 = snapPoint(e);
222 //                      deleteSnapper();
223 //                      deletePreview();
224 //                      clearPreview();
225                         Modification m(*container, graphicView);
226                         MoveRefData data;
227                         data.ref = v1;
228                         data.offset = v2 - v1;
229                         m.moveRef(data);
230                         setStatus(Neutral);
231                         DIALOGFACTORY->updateSelectionWidget(container->countSelected());
232                         DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
233                 }
234                         break;
235
236                 default:
237                         break;
238                 }
239         }
240 }
241
242 void ActionDefault::mouseReleaseEvent(QMouseEvent * e)
243 {
244         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 //                      Entity * en = graphicView->snapper.catchEntity(e);
256                         Entity * en = graphicView->CatchEntity(e);
257
258                         if (en)
259                         {
260 //                              deleteSnapper();
261 //                              deletePreview();
262 //                              clearPreview();
263
264                                 Selection s(*container, graphicView);
265                                 s.selectSingle(en);
266                                 DIALOGFACTORY->updateSelectionWidget(container->countSelected());
267                                 e->accept();
268                                 setStatus(Neutral);
269                         }
270                         else
271                                 setStatus(SetCorner2);
272                 }
273                         break;
274
275                 case SetCorner2:
276                 {
277                         v2 = graphicView->toGraph(e->x(), e->y());
278
279                         // select window:
280 //                      deleteSnapper();
281 //                      deletePreview();
282 //                      clearPreview();
283                         graphicView->preview.SetVisible(false);
284
285                         bool cross = (v2.y > v1.y);
286                         Selection s(*container, graphicView);
287                         s.selectWindow(v1, v2, true, cross);
288
289                         DIALOGFACTORY->updateSelectionWidget(container->countSelected());
290
291                         setStatus(Neutral);
292                         e->accept();
293                 }
294                         break;
295
296                 default:
297                         // Was either moving entity or point, so clear that shiatsu
298 //                      graphicView->snapper.SetVisible(false);
299                         graphicView->SetSnapperVisible(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                         DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
317                         e->accept();
318                         break;
319
320                 default:
321 //                      graphicView->snapper.SetVisible(false);
322                         graphicView->SetSnapperVisible(false);
323                         graphicView->preview.SetVisible(false);
324                         DIALOGFACTORY->requestPreviousMenu();
325                         e->accept();
326                         break;
327                 }
328         }
329 }
330
331 void ActionDefault::commandEvent(CommandEvent * e)
332 {
333         QString c = e->getCommand().toLower();
334 }
335
336 QStringList ActionDefault::getAvailableCommands()
337 {
338         QStringList cmd;
339
340         return cmd;
341 }
342
343 void ActionDefault::updateMouseButtonHints()
344 {
345         switch (getStatus())
346         {
347         case Neutral:
348                 DIALOGFACTORY->updateMouseWidget("", "");
349                 break;
350
351         case SetCorner2:
352                 DIALOGFACTORY->updateMouseWidget(tr("Drag to second point"), tr("Back"));
353                 break;
354
355         default:
356                 DIALOGFACTORY->updateMouseWidget("", "");
357                 break;
358         }
359 }
360
361 void ActionDefault::updateMouseCursor()
362 {
363         switch (getStatus())
364         {
365         case Neutral:
366                 graphicView->setMouseCursor(RS2::ArrowCursor);
367                 break;
368
369         case Moving:
370         case MovingRef:
371                 graphicView->setMouseCursor(RS2::SelectCursor);
372                 break;
373
374         default:
375                 break;
376         }
377 }
378
379 void ActionDefault::updateToolBar()
380 {
381         switch (getStatus())
382         {
383         case Neutral:
384                 break;
385
386         case Moving:
387         case MovingRef:
388                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
389                 break;
390
391         default:
392                 break;
393         }
394 }