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