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