]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawcircle2p.cpp
Removed a bunch of cruft...
[architektonas] / src / actions / rs_actiondrawcircle2p.cpp
1 // rs_actiondrawcircle2p.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/03/2010  Added this text. :-)
13 //
14
15 #include "rs_actiondrawcircle2p.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 RS_ActionDrawCircle2P::RS_ActionDrawCircle2P(RS_EntityContainer & container, GraphicView & graphicView): RS_PreviewActionInterface("Draw circles",
23                 container, graphicView)
24 {
25         reset();
26 }
27
28 RS_ActionDrawCircle2P::~RS_ActionDrawCircle2P()
29 {
30 }
31
32 void RS_ActionDrawCircle2P::reset()
33 {
34         data.reset();
35         point1 = Vector(false);
36         point2 = Vector(false);
37 }
38
39 void RS_ActionDrawCircle2P::init(int status)
40 {
41         RS_PreviewActionInterface::init(status);
42
43         reset();
44 }
45
46 void RS_ActionDrawCircle2P::trigger()
47 {
48         RS_PreviewActionInterface::trigger();
49
50         preparePreview();
51
52         if (data.isValid())
53         {
54                 RS_Circle * circle = new RS_Circle(container, data);
55                 circle->setLayerToActive();
56                 circle->setPenToActive();
57                 container->addEntity(circle);
58
59                 // upd. undo list:
60                 if (document != NULL)
61                 {
62                         document->startUndoCycle();
63                         document->addUndoable(circle);
64                         document->endUndoCycle();
65                 }
66
67                 deleteSnapper();
68                 Vector rz = graphicView->getRelativeZero();
69                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
70                 graphicView->drawEntity(circle);
71                 graphicView->moveRelativeZero(rz);
72                 drawSnapper();
73
74                 setStatus(SetPoint1);
75                 reset();
76         }
77         else    if (RS_DIALOGFACTORY != NULL)
78                 RS_DIALOGFACTORY->requestWarningDialog(tr("Invalid Circle data."));
79 }
80
81 void RS_ActionDrawCircle2P::preparePreview()
82 {
83         data.reset();
84
85         if (point1.valid && point2.valid)
86         {
87                 RS_Circle circle(NULL, data);
88                 bool suc = circle.createFrom2P(point1, point2);
89
90                 if (suc)
91                         data = circle.getData();
92         }
93 }
94
95 void RS_ActionDrawCircle2P::mouseMoveEvent(QMouseEvent * e)
96 {
97         Vector mouse = snapPoint(e);
98
99         switch (getStatus())
100         {
101         case SetPoint1:
102                 point1 = mouse;
103                 break;
104
105         case SetPoint2:
106                 point2 = mouse;
107                 preparePreview();
108
109                 if (data.isValid())
110                 {
111                         RS_Circle * circle = new RS_Circle(preview, data);
112                         deletePreview();
113                         clearPreview();
114                         preview->addEntity(circle);
115                         drawPreview();
116                 }
117                 break;
118
119         default:
120                 break;
121         }
122 }
123
124 void RS_ActionDrawCircle2P::mouseReleaseEvent(QMouseEvent * e)
125 {
126 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
127         if (e->button() == Qt::LeftButton)
128         {
129                 Vector ce(snapPoint(e));
130                 coordinateEvent(&ce);
131         }
132 //      else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
133         else if (e->button() == Qt::RightButton)
134         {
135                 deletePreview();
136                 deleteSnapper();
137                 init(getStatus() - 1);
138         }
139 }
140
141 void RS_ActionDrawCircle2P::coordinateEvent(Vector * e)
142 {
143         if (e == NULL)
144                 return;
145
146         Vector mouse = *e;
147
148         switch (getStatus())
149         {
150         case SetPoint1:
151                 point1 = mouse;
152                 graphicView->moveRelativeZero(mouse);
153                 setStatus(SetPoint2);
154                 break;
155
156         case SetPoint2:
157                 point2 = mouse;
158                 graphicView->moveRelativeZero(mouse);
159                 trigger();
160                 break;
161
162         default:
163                 break;
164         }
165 }
166
167 void RS_ActionDrawCircle2P::commandEvent(RS_CommandEvent * e)
168 {
169         QString c = e->getCommand().toLower();
170
171         if (checkCommand("help", c))
172         {
173                 if (RS_DIALOGFACTORY != NULL)
174                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
175                                 + getAvailableCommands().join(", "));
176                 return;
177         }
178 }
179
180 QStringList RS_ActionDrawCircle2P::getAvailableCommands()
181 {
182         QStringList cmd;
183         return cmd;
184 }
185
186 void RS_ActionDrawCircle2P::updateMouseButtonHints()
187 {
188         if (RS_DIALOGFACTORY != NULL)
189         {
190                 switch (getStatus())
191                 {
192                 case SetPoint1:
193                         RS_DIALOGFACTORY->updateMouseWidget(
194                                 tr("Specify first point"), tr("Cancel"));
195                         break;
196
197                 case SetPoint2:
198                         RS_DIALOGFACTORY->updateMouseWidget(
199                                 tr("Specify second point"), tr("Back"));
200                         break;
201
202                 default:
203                         RS_DIALOGFACTORY->updateMouseWidget("", "");
204                         break;
205                 }
206         }
207 }
208
209 void RS_ActionDrawCircle2P::updateMouseCursor()
210 {
211         graphicView->setMouseCursor(RS2::CadCursor);
212 }
213
214 void RS_ActionDrawCircle2P::updateToolBar()
215 {
216         if (RS_DIALOGFACTORY != NULL)
217         {
218                 if (!isFinished())
219                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
220                 else
221                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
222         }
223 }
224
225 // EOF
226