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