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