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