]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawarctangential.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawarctangential.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawarctangential.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_actiondrawarctangential.h"
28
29 #include "commands.h"
30 #include "rs_snapper.h"
31 #include "rs_dialogfactory.h"
32
33
34
35 RS_ActionDrawArcTangential::RS_ActionDrawArcTangential(RS_EntityContainer& container,
36         RS_GraphicView& graphicView)
37         :RS_PreviewActionInterface("Draw arcs tangential",
38                            container, graphicView) {
39     reset();
40 }
41
42
43
44 RS_ActionDrawArcTangential::~RS_ActionDrawArcTangential() {}
45
46
47 QAction* RS_ActionDrawArcTangential::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
48 {
49         QAction * action = new QAction(tr("&Tangential"), 0);
50 //      QAction* action = new QAction(tr("Arc: Tangential"),
51 //                                                                      tr("&Tangential"),
52 //                                                                      QKeySequence(), NULL);
53         action->setStatusTip(tr("Draw arcs tangential to base entity"));
54         return action;
55 }
56
57
58 void RS_ActionDrawArcTangential::reset() {
59     baseEntity = NULL;
60     isStartPoint = false;
61     point = Vector(false);
62 }
63
64
65
66 void RS_ActionDrawArcTangential::init(int status) {
67     RS_PreviewActionInterface::init(status);
68
69     //reset();
70 }
71
72
73
74 void RS_ActionDrawArcTangential::trigger() {
75     RS_PreviewActionInterface::trigger();
76
77     if (point.valid==false || baseEntity==NULL) {
78         RS_DEBUG->print("RS_ActionDrawArcTangential::trigger: "
79                         "conditions not met");
80         return;
81     }
82
83     preparePreview();
84     RS_Arc* arc = new RS_Arc(container, data);
85     arc->setLayerToActive();
86     arc->setPenToActive();
87     container->addEntity(arc);
88
89     // upd. undo list:
90     if (document!=NULL) {
91         document->startUndoCycle();
92         document->addUndoable(arc);
93         document->endUndoCycle();
94     }
95
96     deleteSnapper();
97     graphicView->moveRelativeZero(Vector(0.0,0.0));
98     graphicView->drawEntity(arc);
99     graphicView->moveRelativeZero(arc->getCenter());
100     drawSnapper();
101
102     setStatus(SetBaseEntity);
103     reset();
104 }
105
106
107
108 void RS_ActionDrawArcTangential::preparePreview() {
109     if (baseEntity!=NULL && point.valid) {
110         Vector startPoint;
111         double direction;
112         if (isStartPoint) {
113             startPoint = baseEntity->getStartpoint();
114             direction = RS_Math::correctAngle(baseEntity->getDirection1()+M_PI);
115         } else {
116             startPoint = baseEntity->getEndpoint();
117             direction = RS_Math::correctAngle(baseEntity->getDirection2()+M_PI);
118         }
119
120         RS_Arc arc(NULL, RS_ArcData());
121         bool suc = arc.createFrom2PDirectionRadius(startPoint, point, direction, data.radius);
122         if (suc) {
123             data = arc.getData();
124         }
125     }
126 }
127
128
129 void RS_ActionDrawArcTangential::mouseMoveEvent(QMouseEvent* e) {
130     switch (getStatus()) {
131     case SetBaseEntity:
132         break;
133
134     case SetEndAngle: {
135             point = snapPoint(e);
136             preparePreview();
137             if (data.isValid()) {
138                 RS_Arc* arc = new RS_Arc(preview, data);
139                 deletePreview();
140                 clearPreview();
141                 preview->addEntity(arc);
142                 drawPreview();
143             }
144         }
145         break;
146
147     default:
148         break;
149     }
150 }
151
152
153
154 void RS_ActionDrawArcTangential::mouseReleaseEvent(QMouseEvent* e) {
155     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
156         switch (getStatus()) {
157
158             // set base entity:
159         case SetBaseEntity: {
160                 Vector coord = graphicView->toGraph(e->x(), e->y());
161                 RS_Entity* entity = catchEntity(coord, RS2::ResolveAll);
162                 if (entity!=NULL) {
163                     if (entity->isAtomic()) {
164                         baseEntity = (RS_AtomicEntity*)entity;
165                         if (baseEntity->getStartpoint().distanceTo(coord) <
166                                 baseEntity->getEndpoint().distanceTo(coord)) {
167                             isStartPoint = true;
168                         } else {
169                             isStartPoint = false;
170                         }
171                         setStatus(SetEndAngle);
172                         updateMouseButtonHints();
173                     } else {
174                         // TODO: warning
175                     }
176                 }
177                 else {
178                     deleteSnapper();
179                 }
180             }
181             break;
182
183             // set angle (point that defines the angle)
184         case SetEndAngle: {
185                 RS_CoordinateEvent ce(snapPoint(e));
186                 coordinateEvent(&ce);
187             }
188             break;
189         }
190     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
191         deletePreview();
192         deleteSnapper();
193         init(getStatus()-1);
194     }
195 }
196
197
198
199 void RS_ActionDrawArcTangential::coordinateEvent(RS_CoordinateEvent* e) {
200     if (e==NULL) {
201         return;
202     }
203     Vector mouse = e->getCoordinate();
204
205     switch (getStatus()) {
206     case SetBaseEntity:
207         break;
208
209     case SetEndAngle:
210         point = mouse;
211         trigger();
212         break;
213
214     default:
215         break;
216     }
217 }
218
219
220
221 void RS_ActionDrawArcTangential::commandEvent(RS_CommandEvent* e) {
222     QString c = e->getCommand().toLower();
223
224     if (checkCommand("help", c)) {
225         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
226                                          + getAvailableCommands().join(", "));
227         return;
228     }
229 }
230
231
232
233 QStringList RS_ActionDrawArcTangential::getAvailableCommands() {
234     QStringList cmd;
235     return cmd;
236 }
237
238
239 void RS_ActionDrawArcTangential::showOptions() {
240     RS_ActionInterface::showOptions();
241
242     if (RS_DIALOGFACTORY!=NULL) {
243         RS_DIALOGFACTORY->requestOptions(this, true);
244     }
245     updateMouseButtonHints();
246 }
247
248
249
250 void RS_ActionDrawArcTangential::hideOptions() {
251     RS_ActionInterface::hideOptions();
252
253     if (RS_DIALOGFACTORY!=NULL) {
254         RS_DIALOGFACTORY->requestOptions(this, false);
255     }
256 }
257
258
259
260 void RS_ActionDrawArcTangential::updateMouseButtonHints() {
261     switch (getStatus()) {
262     case SetBaseEntity:
263         RS_DIALOGFACTORY->updateMouseWidget(
264             tr("Specify base entity"),
265             tr("Cancel"));
266         break;
267     case SetEndAngle:
268         RS_DIALOGFACTORY->updateMouseWidget(
269             tr("Specify end angle"), tr("Back"));
270         break;
271     default:
272         RS_DIALOGFACTORY->updateMouseWidget("", "");
273         break;
274     }
275 }
276
277
278
279 void RS_ActionDrawArcTangential::updateMouseCursor() {
280     graphicView->setMouseCursor(RS2::CadCursor);
281 }
282
283
284
285 void RS_ActionDrawArcTangential::updateToolBar() {
286     if (!isFinished()) {
287         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
288     } else {
289         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarArcs);
290     }
291 }
292
293
294 // EOF
295