]> Shamusworld >> Repos - architektonas/blob - src/actions/actionmodifymoverotate.cpp
0eec89f0dea91038b176ef2acba8b0cbf3987b75
[architektonas] / src / actions / actionmodifymoverotate.cpp
1 // actionmodifymoverotate.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/04/2010  Added this text. :-)
13 //
14
15 #include "actionmodifymoverotate.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "rs_preview.h"
20
21 ActionModifyMoveRotate::ActionModifyMoveRotate(
22         RS_EntityContainer & container, GraphicView & graphicView):
23         ActionInterface("Move and Rotate Entities", container, graphicView)
24 {
25 }
26
27 ActionModifyMoveRotate::~ActionModifyMoveRotate()
28 {
29 }
30
31 /*virtual*/ RS2::ActionType ActionModifyMoveRotate::rtti()
32 {
33         return RS2::ActionModifyMoveRotate;
34 }
35
36 void ActionModifyMoveRotate::init(int status)
37 {
38         ActionInterface::init(status);
39 }
40
41 void ActionModifyMoveRotate::trigger()
42 {
43         RS_DEBUG->print("ActionModifyMoveRotate::trigger()");
44
45         RS_Modification m(*container, graphicView);
46         m.moveRotate(data);
47
48         finish();
49
50         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
51 }
52
53 void ActionModifyMoveRotate::mouseMoveEvent(QMouseEvent * e)
54 {
55         RS_DEBUG->print("ActionModifyMoveRotate::mouseMoveEvent begin");
56
57         if (getStatus() == SetReferencePoint || getStatus() == SetTargetPoint)
58         {
59                 Vector mouse = snapPoint(e);
60
61                 switch (getStatus())
62                 {
63                 case SetReferencePoint:
64                         data.referencePoint = mouse;
65                         break;
66
67                 case SetTargetPoint:
68
69                         if (data.referencePoint.valid)
70                         {
71                                 targetPoint = mouse;
72                                 data.offset = targetPoint - data.referencePoint;
73
74                                 deletePreview();
75                                 clearPreview();
76 /*                              preview->addSelectionFrom(*container);
77                                 preview->rotate(data.referencePoint, data.angle);
78                                 preview->move(data.offset);*/
79                                 drawPreview();
80                         }
81                         break;
82
83                 default:
84                         break;
85                 }
86         }
87
88         RS_DEBUG->print("ActionModifyMoveRotate::mouseMoveEvent end");
89 }
90
91 void ActionModifyMoveRotate::mouseReleaseEvent(QMouseEvent * e)
92 {
93         if (e->button() == Qt::LeftButton)
94         {
95                 Vector ce(snapPoint(e));
96                 coordinateEvent(&ce);
97         }
98         else if (e->button() == Qt::RightButton)
99         {
100                 deletePreview();
101                 deleteSnapper();
102                 init(getStatus() - 1);
103         }
104 }
105
106 void ActionModifyMoveRotate::coordinateEvent(Vector * e)
107 {
108         if (e == NULL)
109                 return;
110
111         Vector pos = *e;
112
113         switch (getStatus())
114         {
115         case SetReferencePoint:
116                 data.referencePoint = pos;
117                 setStatus(SetTargetPoint);
118                 break;
119
120         case SetTargetPoint:
121                 targetPoint = pos;
122
123                 setStatus(ShowDialog);
124                 data.offset = targetPoint - data.referencePoint;
125
126                 if (RS_DIALOGFACTORY->requestMoveRotateDialog(data))
127                         trigger();
128                         //finish();
129                 break;
130
131         default:
132                 break;
133         }
134 }
135
136 void ActionModifyMoveRotate::commandEvent(RS_CommandEvent * e)
137 {
138         QString c = e->getCommand().toLower();
139
140         if (checkCommand("help", c))
141         {
142                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands() + getAvailableCommands().join(", "));
143                 return;
144         }
145
146         switch (getStatus())
147         {
148         case SetReferencePoint:
149         case SetTargetPoint:
150 #warning "Bad comparison (in checkCommand -> rs_actioninterface)... !!! FIX !!!"
151
152                 if (!c.isEmpty() && checkCommand("angle", c))
153 //              if (c == checkCommand("angle", c))
154                 {
155                         deleteSnapper();
156                         deletePreview();
157                         clearPreview();
158                         lastStatus = (Status)getStatus();
159                         setStatus(SetAngle);
160                 }
161                 break;
162
163         case SetAngle:
164         {
165                 bool ok;
166                 double a = RS_Math::eval(c, &ok);
167
168                 if (ok)
169                         data.angle = RS_Math::deg2rad(a);
170                 else
171                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
172
173                 RS_DIALOGFACTORY->requestOptions(this, true, true);
174                 setStatus(lastStatus);
175         }
176         break;
177         }
178 }
179
180 QStringList ActionModifyMoveRotate::getAvailableCommands()
181 {
182         QStringList cmd;
183
184         switch (getStatus())
185         {
186         case SetReferencePoint:
187         case SetTargetPoint:
188                 cmd += command("angle");
189                 break;
190
191         default:
192                 break;
193         }
194
195         return cmd;
196 }
197
198 void ActionModifyMoveRotate::showOptions()
199 {
200         //std::cout << "ActionModifyMoveRotate::showOptions()\n";
201
202         ActionInterface::showOptions();
203
204         RS_DIALOGFACTORY->requestOptions(this, true);
205 }
206
207 void ActionModifyMoveRotate::hideOptions()
208 {
209         //std::cout << "ActionModifyMoveRotate::hideOptions()\n";
210
211         ActionInterface::hideOptions();
212
213         RS_DIALOGFACTORY->requestOptions(this, false);
214 }
215
216 void ActionModifyMoveRotate::updateMouseButtonHints()
217 {
218         switch (getStatus())
219         {
220         case SetReferencePoint:
221                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
222                 break;
223
224         case SetTargetPoint:
225                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"), tr("Back"));
226                 break;
227
228         case SetAngle:
229                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter rotation angle:"), tr("Back"));
230                 break;
231
232         default:
233                 RS_DIALOGFACTORY->updateMouseWidget("", "");
234                 break;
235         }
236 }
237
238 void ActionModifyMoveRotate::updateMouseCursor()
239 {
240         graphicView->setMouseCursor(RS2::CadCursor);
241 }
242
243 void ActionModifyMoveRotate::updateToolBar()
244 {
245         switch (getStatus())
246         {
247         case SetReferencePoint:
248         case SetTargetPoint:
249                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
250                 break;
251
252         default:
253                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarModify);
254                 break;
255         }
256 }
257
258 void ActionModifyMoveRotate::setAngle(double a)
259 {
260         data.angle = a;
261 }
262
263 double ActionModifyMoveRotate::getAngle()
264 {
265         return data.angle;
266 }