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