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