]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawcircle2p.cpp
f0719f30fcc8c2bc295e333fdc1c696363510687
[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 //
16
17 #include "actiondrawcircle2p.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
23
24 ActionDrawCircle2P::ActionDrawCircle2P(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw circles",
25                 container, graphicView)
26 {
27         reset();
28 }
29
30 ActionDrawCircle2P::~ActionDrawCircle2P()
31 {
32 }
33
34 void ActionDrawCircle2P::reset()
35 {
36         data.reset();
37         point1 = Vector(false);
38         point2 = Vector(false);
39 }
40
41 void ActionDrawCircle2P::init(int status)
42 {
43         ActionInterface::init(status);
44
45         reset();
46 }
47
48 void ActionDrawCircle2P::trigger()
49 {
50         ActionInterface::trigger();
51
52         preparePreview();
53
54         if (data.isValid())
55         {
56                 RS_Circle * circle = new RS_Circle(container, data);
57                 circle->setLayerToActive();
58                 circle->setPenToActive();
59                 container->addEntity(circle);
60
61                 // upd. undo list:
62                 if (document != NULL)
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
76                 setStatus(SetPoint1);
77                 reset();
78         }
79         else    if (RS_DIALOGFACTORY != NULL)
80                 RS_DIALOGFACTORY->requestWarningDialog(tr("Invalid Circle data."));
81 }
82
83 void ActionDrawCircle2P::preparePreview()
84 {
85         data.reset();
86
87         if (point1.valid && point2.valid)
88         {
89                 RS_Circle circle(NULL, data);
90                 bool suc = circle.createFrom2P(point1, point2);
91
92                 if (suc)
93                         data = circle.getData();
94         }
95 }
96
97 void ActionDrawCircle2P::mouseMoveEvent(QMouseEvent * e)
98 {
99         Vector mouse = snapPoint(e);
100
101         switch (getStatus())
102         {
103         case SetPoint1:
104                 point1 = mouse;
105                 break;
106
107         case SetPoint2:
108                 point2 = mouse;
109                 preparePreview();
110
111                 if (data.isValid())
112                 {
113 //                      RS_Circle * circle = new RS_Circle(preview, data);
114 //                      deletePreview();
115 //                      clearPreview();
116 //                      preview->addEntity(circle);
117 //                      drawPreview();
118                 }
119                 break;
120
121         default:
122                 break;
123         }
124 }
125
126 void ActionDrawCircle2P::mouseReleaseEvent(QMouseEvent * e)
127 {
128         if (e->button() == Qt::LeftButton)
129         {
130                 Vector ce(snapPoint(e));
131                 coordinateEvent(&ce);
132         }
133         else if (e->button() == Qt::RightButton)
134         {
135                 deletePreview();
136                 deleteSnapper();
137                 init(getStatus() - 1);
138         }
139 }
140
141 void 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 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 ActionDrawCircle2P::getAvailableCommands()
181 {
182         QStringList cmd;
183         return cmd;
184 }
185
186 void 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 ActionDrawCircle2P::updateMouseCursor()
210 {
211         graphicView->setMouseCursor(RS2::CadCursor);
212 }
213
214 void ActionDrawCircle2P::updateToolBar()
215 {
216         if (RS_DIALOGFACTORY)
217         {
218                 if (!isFinished())
219                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
220                 else
221                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
222         }
223 }