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