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