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