]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondimradial.cpp
In the middle of major refactoring...
[architektonas] / src / actions / actiondimradial.cpp
1 // actiondimradial.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 "actiondimradial.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 ActionDimRadial::ActionDimRadial(RS_EntityContainer & container, GraphicView & graphicView):
23         ActionDimension("Draw Radial Dimensions", container, graphicView)
24 {
25         reset();
26 }
27
28 ActionDimRadial::~ActionDimRadial()
29 {
30 }
31
32 /*virtual*/ RS2::ActionType ActionDimRadial::rtti()
33 {
34         return RS2::ActionDimRadial;
35 }
36
37 void ActionDimRadial::reset()
38 {
39         ActionDimension::reset();
40
41         edata = RS_DimRadialData(Vector(false), 0.0);
42         entity = NULL;
43         pos = Vector(false);
44         RS_DIALOGFACTORY->requestOptions(this, true, true);
45 }
46
47 void ActionDimRadial::trigger()
48 {
49         ActionDimension::trigger();
50
51         preparePreview();
52
53         if (entity)
54         {
55                 RS_DimRadial * newEntity = new RS_DimRadial(container, data, edata);
56                 newEntity->setLayerToActive();
57                 newEntity->setPenToActive();
58                 newEntity->update();
59                 container->addEntity(newEntity);
60
61                 // upd. undo list:
62                 if (document)
63                 {
64                         document->startUndoCycle();
65                         document->addUndoable(newEntity);
66                         document->endUndoCycle();
67                 }
68
69                 deleteSnapper();
70                 Vector rz = graphicView->getRelativeZero();
71                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
72                 graphicView->drawEntity(newEntity);
73                 graphicView->moveRelativeZero(rz);
74                 //drawSnapper();
75         }
76         else
77                 RS_DEBUG->print("ActionDimRadial::trigger: Entity is NULL\n");
78 }
79
80 void ActionDimRadial::preparePreview()
81 {
82         if (entity != NULL)
83         {
84                 double angle = data.definitionPoint.angleTo(pos);
85                 double radius = 0.0;
86
87                 if (entity->rtti() == RS2::EntityArc)
88                         radius = ((RS_Arc *)entity)->getRadius();
89                 else if (entity->rtti() == RS2::EntityCircle)
90                         radius = ((RS_Circle *)entity)->getRadius();
91
92                 edata.definitionPoint.setPolar(radius, angle);
93                 edata.definitionPoint += data.definitionPoint;
94         }
95 }
96
97 void ActionDimRadial::mouseMoveEvent(QMouseEvent * e)
98 {
99         RS_DEBUG->print("ActionDimRadial::mouseMoveEvent begin");
100
101         switch (getStatus())
102         {
103         case SetEntity:
104                 entity = catchEntity(e, RS2::ResolveAll);
105                 break;
106
107         case SetPos:
108
109                 if (entity)
110                 {
111                         pos = snapPoint(e);
112
113                         preparePreview();
114
115 //                      RS_DimRadial * d = new RS_DimRadial(preview, data, edata);
116 //                      d->update();
117 //
118 //                      deletePreview();
119 //                      clearPreview();
120 //                      preview->addEntity(d);
121 //                      drawPreview();
122                 }
123                 break;
124
125         default:
126                 break;
127         }
128
129         RS_DEBUG->print("ActionDimRadial::mouseMoveEvent end");
130 }
131
132 void ActionDimRadial::mouseReleaseEvent(QMouseEvent * e)
133 {
134         if (e->button() == Qt::LeftButton)
135         {
136                 switch (getStatus())
137                 {
138                 case SetEntity: {
139                         RS_Entity * en = catchEntity(e, RS2::ResolveAll);
140
141                         if (en != NULL)
142                         {
143                                 if (en->rtti() == RS2::EntityArc
144                                     || en->rtti() == RS2::EntityCircle)
145                                 {
146                                         entity = en;
147
148                                         if (entity->rtti() == RS2::EntityArc)
149                                                 data.definitionPoint =
150                                                         ((RS_Arc *)entity)->getCenter();
151                                         else if (entity->rtti() == RS2::EntityCircle)
152                                                 data.definitionPoint =
153                                                         ((RS_Circle *)entity)->getCenter();
154                                         graphicView->moveRelativeZero(data.definitionPoint);
155                                         setStatus(SetPos);
156                                 }
157                                 else
158                                         RS_DIALOGFACTORY->commandMessage(tr("Not a circle "
159                                                         "or arc entity"));
160                         }
161                 }
162                 break;
163
164                 case SetPos: {
165                         Vector ce(snapPoint(e));
166                         coordinateEvent(&ce);
167                 }
168                 break;
169
170                 default:
171                         break;
172                 }
173         }
174         else if (e->button() == Qt::RightButton)
175         {
176                 deletePreview();
177                 deleteSnapper();
178                 clearPreview();
179                 init(getStatus() - 1);
180         }
181 }
182
183 void ActionDimRadial::coordinateEvent(Vector * e)
184 {
185         if (e == NULL)
186                 return;
187
188         switch (getStatus())
189         {
190         case SetPos:
191                 pos = *e;
192                 trigger();
193                 reset();
194                 setStatus(SetEntity);
195                 break;
196
197         default:
198                 break;
199         }
200 }
201
202 void ActionDimRadial::commandEvent(RS_CommandEvent * e)
203 {
204         QString c = e->getCommand().toLower();
205
206         if (checkCommand("help", c))
207         {
208                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
209                         + getAvailableCommands().join(", "));
210                 return;
211         }
212
213         // setting new text label:
214         if (getStatus() == SetText)
215         {
216                 setText(c);
217                 RS_DIALOGFACTORY->requestOptions(this, true, true);
218                 graphicView->enableCoordinateInput();
219                 setStatus(lastStatus);
220                 return;
221         }
222
223         // command: text
224         if (checkCommand("text", c))
225         {
226                 lastStatus = (Status)getStatus();
227                 graphicView->disableCoordinateInput();
228                 setStatus(SetText);
229         }
230
231         // setting angle
232         if (getStatus() == SetPos)
233         {
234                 bool ok;
235                 double a = RS_Math::eval(c, &ok);
236
237                 if (ok == true)
238                 {
239                         pos.setPolar(1.0, RS_Math::deg2rad(a));
240                         pos += data.definitionPoint;
241                         trigger();
242                         reset();
243                         setStatus(SetEntity);
244                 }
245                 else
246                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
247
248                 return;
249         }
250 }
251
252 QStringList ActionDimRadial::getAvailableCommands()
253 {
254         QStringList cmd;
255
256         switch (getStatus())
257         {
258         case SetEntity:
259         case SetPos:
260                 cmd += command("text");
261                 break;
262
263         default:
264                 break;
265         }
266
267         return cmd;
268 }
269
270 void ActionDimRadial::updateMouseButtonHints()
271 {
272         switch (getStatus())
273         {
274         case SetEntity:
275                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select arc or circle entity"),
276                         tr("Cancel"));
277                 break;
278
279         case SetPos:
280                 RS_DIALOGFACTORY->updateMouseWidget(
281                         tr("Specify dimension line position or enter angle:"),
282                         tr("Cancel"));
283                 break;
284
285         case SetText:
286                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
287                 break;
288
289         default:
290                 RS_DIALOGFACTORY->updateMouseWidget("", "");
291                 break;
292         }
293 }
294
295 void ActionDimRadial::showOptions()
296 {
297         ActionInterface::showOptions();
298
299         RS_DIALOGFACTORY->requestOptions(this, true);
300         //RS_DIALOGFACTORY->requestDimRadialOptions(edata, true);
301 }
302
303 void ActionDimRadial::hideOptions()
304 {
305         ActionInterface::hideOptions();
306
307         //RS_DIALOGFACTORY->requestDimRadialOptions(edata, false);
308         RS_DIALOGFACTORY->requestOptions(this, false);
309 }
310
311