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