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