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