]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawcircle2p.cpp
43e3003b6e40774d754d5354cc534cfc94b955dd
[architektonas] / src / actions / actiondrawcircle2p.cpp
1 // 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 "actiondrawcircle2p.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 ActionDrawCircle2P::ActionDrawCircle2P(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw circles",
23                 container, graphicView)
24 {
25         reset();
26 }
27
28 ActionDrawCircle2P::~ActionDrawCircle2P()
29 {
30 }
31
32 void ActionDrawCircle2P::reset()
33 {
34         data.reset();
35         point1 = Vector(false);
36         point2 = Vector(false);
37 }
38
39 void ActionDrawCircle2P::init(int status)
40 {
41         ActionInterface::init(status);
42
43         reset();
44 }
45
46 void ActionDrawCircle2P::trigger()
47 {
48         ActionInterface::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 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 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 ActionDrawCircle2P::mouseReleaseEvent(QMouseEvent * e)
125 {
126         if (e->button() == Qt::LeftButton)
127         {
128                 Vector ce(snapPoint(e));
129                 coordinateEvent(&ce);
130         }
131         else if (e->button() == Qt::RightButton)
132         {
133                 deletePreview();
134                 deleteSnapper();
135                 init(getStatus() - 1);
136         }
137 }
138
139 void ActionDrawCircle2P::coordinateEvent(Vector * e)
140 {
141         if (e == NULL)
142                 return;
143
144         Vector mouse = *e;
145
146         switch (getStatus())
147         {
148         case SetPoint1:
149                 point1 = mouse;
150                 graphicView->moveRelativeZero(mouse);
151                 setStatus(SetPoint2);
152                 break;
153
154         case SetPoint2:
155                 point2 = mouse;
156                 graphicView->moveRelativeZero(mouse);
157                 trigger();
158                 break;
159
160         default:
161                 break;
162         }
163 }
164
165 void ActionDrawCircle2P::commandEvent(RS_CommandEvent * e)
166 {
167         QString c = e->getCommand().toLower();
168
169         if (checkCommand("help", c))
170         {
171                 if (RS_DIALOGFACTORY != NULL)
172                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
173                                 + getAvailableCommands().join(", "));
174                 return;
175         }
176 }
177
178 QStringList ActionDrawCircle2P::getAvailableCommands()
179 {
180         QStringList cmd;
181         return cmd;
182 }
183
184 void ActionDrawCircle2P::updateMouseButtonHints()
185 {
186         if (RS_DIALOGFACTORY != NULL)
187         {
188                 switch (getStatus())
189                 {
190                 case SetPoint1:
191                         RS_DIALOGFACTORY->updateMouseWidget(
192                                 tr("Specify first point"), tr("Cancel"));
193                         break;
194
195                 case SetPoint2:
196                         RS_DIALOGFACTORY->updateMouseWidget(
197                                 tr("Specify second point"), tr("Back"));
198                         break;
199
200                 default:
201                         RS_DIALOGFACTORY->updateMouseWidget("", "");
202                         break;
203                 }
204         }
205 }
206
207 void ActionDrawCircle2P::updateMouseCursor()
208 {
209         graphicView->setMouseCursor(RS2::CadCursor);
210 }
211
212 void ActionDrawCircle2P::updateToolBar()
213 {
214         if (RS_DIALOGFACTORY)
215         {
216                 if (!isFinished())
217                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
218                 else
219                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
220         }
221 }