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