]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimradial.cpp
Initial import
[architektonas] / src / actions / rs_actiondimradial.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondimradial.cpp 1134 2004-07-13 23:26:13Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actiondimradial.h"
28
29 #include "rs_creation.h"
30 #include "rs_snapper.h"
31 #include "rs_dialogfactory.h"
32
33
34
35 RS_ActionDimRadial::RS_ActionDimRadial(
36     RS_EntityContainer& container,
37     RS_GraphicView& graphicView)
38         :RS_ActionDimension("Draw Radial Dimensions",
39                     container, graphicView) {
40     reset();
41 }
42
43
44 QAction * RS_ActionDimRadial::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
45 {
46         QAction * action = new QAction(tr("&Radial"), 0);
47 //      QAction * action = new QAction(tr("Radial"), tr("&Radial"),
48 //                                                                      QKeySequence(), NULL);
49         action->setStatusTip(tr("Radial Dimension"));
50
51         return action;
52 }
53
54 void RS_ActionDimRadial::reset()
55 {
56     RS_ActionDimension::reset();
57
58     edata = RS_DimRadialData(Vector(false),
59                              0.0);
60     entity = NULL;
61     pos = Vector(false);
62     RS_DIALOGFACTORY->requestOptions(this, true, true);
63 }
64
65
66
67 void RS_ActionDimRadial::trigger() {
68     RS_ActionDimension::trigger();
69
70     preparePreview();
71     if (entity!=NULL) {
72         RS_DimRadial* newEntity = NULL;
73
74         newEntity = new RS_DimRadial(container,
75                                      data,
76                                      edata);
77
78         newEntity->setLayerToActive();
79         newEntity->setPenToActive();
80         newEntity->update();
81         container->addEntity(newEntity);
82
83         // upd. undo list:
84         if (document!=NULL) {
85             document->startUndoCycle();
86             document->addUndoable(newEntity);
87             document->endUndoCycle();
88         }
89         deleteSnapper();
90         Vector rz = graphicView->getRelativeZero();
91         graphicView->moveRelativeZero(Vector(0.0,0.0));
92         graphicView->drawEntity(newEntity);
93         graphicView->moveRelativeZero(rz);
94         //drawSnapper();
95
96     }
97     else {
98         RS_DEBUG->print("RS_ActionDimRadial::trigger:"
99                         " Entity is NULL\n");
100     }
101 }
102
103
104 void RS_ActionDimRadial::preparePreview() {
105     if (entity!=NULL) {
106         double angle = data.definitionPoint.angleTo(pos);
107         double radius=0.0;
108         if (entity->rtti()==RS2::EntityArc) {
109             radius = ((RS_Arc*)entity)->getRadius();
110         } else if (entity->rtti()==RS2::EntityCircle) {
111             radius = ((RS_Circle*)entity)->getRadius();
112         }
113
114         edata.definitionPoint.setPolar(radius, angle);
115         edata.definitionPoint += data.definitionPoint;
116     }
117 }
118
119
120
121 void RS_ActionDimRadial::mouseMoveEvent(QMouseEvent* e) {
122     RS_DEBUG->print("RS_ActionDimRadial::mouseMoveEvent begin");
123
124     //Vector mouse(graphicView->toGraphX(e->x()),
125     //                graphicView->toGraphY(e->y()));
126
127     switch (getStatus()) {
128     case SetEntity:
129         entity = catchEntity(e, RS2::ResolveAll);
130         break;
131
132     case SetPos:
133         if (entity!=NULL) {
134             pos = snapPoint(e);
135
136             preparePreview();
137
138             RS_DimRadial* d = new RS_DimRadial(preview, data, edata);
139             d->update();
140
141             deletePreview();
142             clearPreview();
143             preview->addEntity(d);
144             drawPreview();
145         }
146         break;
147
148     default:
149         break;
150     }
151
152     RS_DEBUG->print("RS_ActionDimRadial::mouseMoveEvent end");
153 }
154
155
156
157 void RS_ActionDimRadial::mouseReleaseEvent(QMouseEvent* e) {
158
159     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
160         switch (getStatus()) {
161         case SetEntity: {
162                 RS_Entity* en = catchEntity(e, RS2::ResolveAll);
163                 if (en!=NULL) {
164                     if (en->rtti()==RS2::EntityArc ||
165                             en->rtti()==RS2::EntityCircle) {
166                         entity = en;
167                         if (entity->rtti()==RS2::EntityArc) {
168                             data.definitionPoint =
169                                 ((RS_Arc*)entity)->getCenter();
170                         } else if (entity->rtti()==RS2::EntityCircle) {
171                             data.definitionPoint =
172                                 ((RS_Circle*)entity)->getCenter();
173                         }
174                         graphicView->moveRelativeZero(data.definitionPoint);
175                         setStatus(SetPos);
176                     } else {
177                         RS_DIALOGFACTORY->commandMessage(tr("Not a circle "
178                                                             "or arc entity"));
179                     }
180                 }
181             }
182             break;
183
184         case SetPos: {
185                 RS_CoordinateEvent ce(snapPoint(e));
186                 coordinateEvent(&ce);
187             }
188             break;
189
190         default:
191             break;
192         }
193     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
194         deletePreview();
195         deleteSnapper();
196         clearPreview();
197         init(getStatus()-1);
198     }
199 }
200
201
202
203 void RS_ActionDimRadial::coordinateEvent(RS_CoordinateEvent* e) {
204     if (e==NULL) {
205         return;
206     }
207
208     switch (getStatus()) {
209     case SetPos:
210         pos = e->getCoordinate();
211         trigger();
212         reset();
213         setStatus(SetEntity);
214         break;
215
216     default:
217         break;
218     }
219 }
220
221
222
223 void RS_ActionDimRadial::commandEvent(RS_CommandEvent* e) {
224     QString c = e->getCommand().toLower();
225
226     if (checkCommand("help", c)) {
227         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
228                                          + getAvailableCommands().join(", "));
229         return;
230     }
231
232     // setting new text label:
233     if (getStatus()==SetText) {
234         setText(c);
235         RS_DIALOGFACTORY->requestOptions(this, true, true);
236         graphicView->enableCoordinateInput();
237         setStatus(lastStatus);
238         return;
239     }
240
241     // command: text
242     if (checkCommand("text", c)) {
243         lastStatus = (Status)getStatus();
244         graphicView->disableCoordinateInput();
245         setStatus(SetText);
246     }
247
248     // setting angle
249     if (getStatus()==SetPos) {
250         bool ok;
251         double a = RS_Math::eval(c, &ok);
252         if (ok==true) {
253             pos.setPolar(1.0, RS_Math::deg2rad(a));
254             pos += data.definitionPoint;
255             trigger();
256             reset();
257             setStatus(SetEntity);
258         } else {
259             RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
260         }
261         return;
262     }
263 }
264
265
266
267 QStringList RS_ActionDimRadial::getAvailableCommands() {
268     QStringList cmd;
269
270     switch (getStatus()) {
271     case SetEntity:
272     case SetPos:
273         cmd += command("text");
274         break;
275
276     default:
277         break;
278     }
279
280     return cmd;
281 }
282
283
284 void RS_ActionDimRadial::updateMouseButtonHints() {
285     switch (getStatus()) {
286     case SetEntity:
287         RS_DIALOGFACTORY->updateMouseWidget(tr("Select arc or circle entity"),
288                                             tr("Cancel"));
289         break;
290     case SetPos:
291         RS_DIALOGFACTORY->updateMouseWidget(
292             tr("Specify dimension line position or enter angle:"),
293             tr("Cancel"));
294         break;
295     case SetText:
296         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
297         break;
298     default:
299         RS_DIALOGFACTORY->updateMouseWidget("", "");
300         break;
301     }
302 }
303
304
305
306 void RS_ActionDimRadial::showOptions() {
307     RS_ActionInterface::showOptions();
308
309     RS_DIALOGFACTORY->requestOptions(this, true);
310     //RS_DIALOGFACTORY->requestDimRadialOptions(edata, true);
311 }
312
313
314
315 void RS_ActionDimRadial::hideOptions() {
316     RS_ActionInterface::hideOptions();
317
318     //RS_DIALOGFACTORY->requestDimRadialOptions(edata, false);
319     RS_DIALOGFACTORY->requestOptions(this, false);
320 }
321
322
323
324 // EOF