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