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