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