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