]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawcircle.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawcircle.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawcircle.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_actiondrawcircle.h"
28
29 #include "rs_snapper.h"
30
31 RS_ActionDrawCircle::RS_ActionDrawCircle(RS_EntityContainer & container,
32         RS_GraphicView & graphicView):
33         RS_PreviewActionInterface("Draw circles", container, graphicView)
34 {
35     reset();
36 }
37
38 RS_ActionDrawCircle::~RS_ActionDrawCircle() {}
39
40 QAction * RS_ActionDrawCircle::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
41 {
42         QAction * action = new QAction(tr("Center, &Point"), 0);
43 //      QAction* action = new QAction(tr("Circle: Center, Point"),
44 //                                                                      tr("Center, &Point"),
45 //                                                                      QKeySequence(), NULL);
46         action->setStatusTip(tr("Draw circles with center and point"));
47         return action;
48 }
49
50 void RS_ActionDrawCircle::reset()
51 {
52     data = RS_CircleData(Vector(false), 0.0);
53 }
54
55
56
57 void RS_ActionDrawCircle::init(int status) {
58     RS_PreviewActionInterface::init(status);
59
60     reset();
61 }
62
63
64
65 void RS_ActionDrawCircle::trigger() {
66     RS_PreviewActionInterface::trigger();
67
68     RS_Circle* circle = new RS_Circle(container,
69                                       data);
70     circle->setLayerToActive();
71     circle->setPenToActive();
72     container->addEntity(circle);
73
74     // upd. undo list:
75     if (document!=NULL) {
76         document->startUndoCycle();
77         document->addUndoable(circle);
78         document->endUndoCycle();
79     }
80     deleteSnapper();
81     Vector rz = graphicView->getRelativeZero();
82     graphicView->moveRelativeZero(Vector(0.0,0.0));
83     graphicView->drawEntity(circle);
84     graphicView->moveRelativeZero(circle->getCenter());
85     drawSnapper();
86
87     setStatus(SetCenter);
88     reset();
89
90     RS_DEBUG->print("RS_ActionDrawCircle::trigger(): circle added: %d",
91                     circle->getId());
92 }
93
94
95
96 void RS_ActionDrawCircle::mouseMoveEvent(QMouseEvent* e) {
97     RS_DEBUG->print("RS_ActionDrawCircle::mouseMoveEvent begin");
98
99     Vector mouse = snapPoint(e);
100     switch (getStatus()) {
101     case SetCenter:
102         data.center = mouse;
103         break;
104
105     case SetRadius:
106         if (data.center.valid) {
107             data.radius = data.center.distanceTo(mouse);
108             deletePreview();
109             clearPreview();
110             preview->addEntity(new RS_Circle(preview,
111                                              data));
112             drawPreview();
113         }
114         break;
115     }
116
117     RS_DEBUG->print("RS_ActionDrawCircle::mouseMoveEvent end");
118 }
119
120
121
122 void RS_ActionDrawCircle::mouseReleaseEvent(QMouseEvent* e) {
123     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
124         RS_CoordinateEvent ce(snapPoint(e));
125         coordinateEvent(&ce);
126     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
127         deletePreview();
128         deleteSnapper();
129         init(getStatus()-1);
130     }
131 }
132
133
134
135 void RS_ActionDrawCircle::coordinateEvent(RS_CoordinateEvent* e) {
136     if (e==NULL) {
137         return;
138     }
139
140     Vector mouse = e->getCoordinate();
141
142     switch (getStatus()) {
143     case SetCenter:
144         data.center = mouse;
145         graphicView->moveRelativeZero(mouse);
146         setStatus(SetRadius);
147         break;
148
149     case SetRadius:
150         if (data.center.valid) {
151             graphicView->moveRelativeZero(mouse);
152             data.radius = data.center.distanceTo(mouse);
153             trigger();
154         }
155         //setStatus(SetCenter);
156         break;
157
158     default:
159         break;
160     }
161 }
162
163
164
165 void RS_ActionDrawCircle::commandEvent(RS_CommandEvent* e) {
166     QString c = e->getCommand().toLower();
167
168     if (checkCommand("help", c)) {
169         if (RS_DIALOGFACTORY!=NULL) {
170             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
171                                              + getAvailableCommands().join(", "));
172         }
173         return;
174     }
175
176     switch (getStatus()) {
177
178     case SetRadius: {
179             bool ok;
180             double r = RS_Math::eval(c, &ok);
181             if (ok==true) {
182                 data.radius = r;
183             } else {
184                 if (RS_DIALOGFACTORY!=NULL) {
185                     RS_DIALOGFACTORY->commandMessage(
186                         tr("Not a valid expression"));
187                 }
188             }
189             trigger();
190             //setStatus(SetCenter);
191         }
192         break;
193
194     default:
195         break;
196     }
197 }
198
199
200
201 QStringList RS_ActionDrawCircle::getAvailableCommands() {
202     QStringList cmd;
203     return cmd;
204 }
205
206
207 void RS_ActionDrawCircle::updateMouseButtonHints() {
208     switch (getStatus()) {
209     case SetCenter:
210         if (RS_DIALOGFACTORY!=NULL) {
211             RS_DIALOGFACTORY->updateMouseWidget(
212                 tr("Specify center"), tr("Cancel"));
213         }
214         break;
215     case SetRadius:
216         if (RS_DIALOGFACTORY!=NULL) {
217             RS_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
218         }
219         break;
220     default:
221         if (RS_DIALOGFACTORY!=NULL) {
222             RS_DIALOGFACTORY->updateMouseWidget("", "");
223         }
224         break;
225     }
226 }
227
228
229
230 void RS_ActionDrawCircle::showOptions() {
231     RS_ActionInterface::showOptions();
232 }
233
234
235
236 void RS_ActionDrawCircle::hideOptions() {
237     RS_ActionInterface::hideOptions();
238 }
239
240
241
242 void RS_ActionDrawCircle::updateMouseCursor() {
243     graphicView->setMouseCursor(RS2::CadCursor);
244 }
245
246
247
248 void RS_ActionDrawCircle::updateToolBar() {
249     if (!isFinished()) {
250         if (RS_DIALOGFACTORY!=NULL) {
251             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
252         }
253     } else {
254         if (RS_DIALOGFACTORY!=NULL) {
255             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
256         }
257     }
258 }
259
260
261 // EOF
262