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