]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinebisector.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondrawlinebisector.cpp
1 // actiondrawlinebisector.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 "actiondrawlinebisector.h"
18
19 #include "commandevent.h"
20 #include "creation.h"
21 #include "debug.h"
22 #include "dialogfactory.h"
23 #include "graphicview.h"
24 #include "preview.h"
25
26 ActionDrawLineBisector::ActionDrawLineBisector(EntityContainer & container,
27         GraphicView & graphicView):
28         ActionInterface("Draw Bisectors", container, graphicView),
29         bisector(NULL), line1(NULL), line2(NULL), length(10.0), number(1),
30         coord1(Vector(false)), coord2(Vector(false)), lastStatus(SetLine1)
31 {
32         graphicView.SetSnapperVisible(false);
33 }
34
35 ActionDrawLineBisector::~ActionDrawLineBisector()
36 {
37 }
38
39 /*virtual*/ RS2::ActionType ActionDrawLineBisector::rtti()
40 {
41         return RS2::ActionDrawLineBisector;
42 }
43
44 void ActionDrawLineBisector::trigger()
45 {
46         ActionInterface::trigger();
47         Creation creation(container, graphicView);
48         creation.createBisector(coord1, coord2, length, number, line1, line2);
49         graphicView->redraw();
50 }
51
52 void ActionDrawLineBisector::mouseMoveEvent(QMouseEvent * e)
53 {
54         DEBUG->print("ActionDrawLineBisector::mouseMoveEvent begin");
55
56         Vector mouse = Vector(graphicView->toGraphX(e->x()),
57                 graphicView->toGraphY(e->y()));
58
59         switch (getStatus())
60         {
61         case SetLine1:
62                 break;
63
64         case SetLine2:
65         {
66                 coord2 = mouse;
67                 Entity * en = catchEntity(e, RS2::ResolveAll);
68
69                 if (en && en->rtti() == RS2::EntityLine)
70                 {
71                         line2 = (Line *)en;
72                         graphicView->preview.clear();
73                         Creation creation(&(graphicView->preview), NULL, false);
74                         creation.createBisector(coord1, coord2, length, number, line1, line2);
75                 }
76         }
77                 break;
78
79         default:
80                 break;
81         }
82
83         graphicView->redraw();
84         DEBUG->print("ActionDrawLineBisector::mouseMoveEvent end");
85 }
86
87 void ActionDrawLineBisector::mouseReleaseEvent(QMouseEvent * e)
88 {
89         if (e->button() == Qt::RightButton)
90         {
91 //              deletePreview();
92 //              clearPreview();
93                 init(getStatus() - 1);
94         }
95         else
96         {
97                 Vector mouse = Vector(graphicView->toGraphX(e->x()),
98                         graphicView->toGraphY(e->y()));
99
100                 switch (getStatus())
101                 {
102                 case SetLine1:
103                 {
104                         coord1 = mouse;
105                         Entity * en = catchEntity(e, RS2::ResolveAll);
106
107                         if (en && en->rtti() == RS2::EntityLine)
108                         {
109                                 line1 = (Line *)en;
110                                 setStatus(SetLine2);
111                         }
112                 }
113                         break;
114
115                 case SetLine2:
116                         coord2 = mouse;
117                         trigger();
118                         setStatus(SetLine1);
119                         break;
120                 }
121         }
122 }
123
124 void ActionDrawLineBisector::commandEvent(CommandEvent * e)
125 {
126         QString c = e->getCommand().toLower();
127
128         if (checkCommand("help", c))
129         {
130                 DIALOGFACTORY->commandMessage(msgAvailableCommands()
131                         + getAvailableCommands().join(", "));
132                 return;
133         }
134
135         switch (getStatus())
136         {
137         case SetLine1:
138         case SetLine2:
139                 lastStatus = (Status)getStatus();
140
141                 if (checkCommand("length", c))
142                 {
143 //                      deleteSnapper();
144 //                      deletePreview();
145 //                      clearPreview();
146                         setStatus(SetLength);
147                 }
148                 else if (checkCommand("number", c))
149                 {
150 //                      deleteSnapper();
151 //                      deletePreview();
152 //                      clearPreview();
153                         setStatus(SetNumber);
154                 }
155
156                 break;
157
158         case SetLength:
159         {
160                 bool ok;
161                 double l = Math::eval(c, &ok);
162
163                 if (ok)
164                         length = l;
165                 else
166                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
167
168                 DIALOGFACTORY->requestOptions(this, true, true);
169                 setStatus(lastStatus);
170         }
171                 break;
172
173         case SetNumber:
174         {
175                 bool ok;
176                 int n = (int)Math::eval(c, &ok);
177
178                 if (ok)
179                         number = n;
180                 else
181                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
182
183                 DIALOGFACTORY->requestOptions(this, true, true);
184                 setStatus(lastStatus);
185         }
186                 break;
187
188         default:
189                 break;
190         }
191 }
192
193 QStringList ActionDrawLineBisector::getAvailableCommands()
194 {
195         QStringList cmd;
196
197         switch (getStatus())
198         {
199         case SetLine1:
200         case SetLine2:
201                 cmd += command("length");
202                 cmd += command("number");
203                 break;
204
205         default:
206                 break;
207         }
208
209         return cmd;
210 }
211
212 void ActionDrawLineBisector::updateMouseButtonHints()
213 {
214         switch (getStatus())
215         {
216         case SetLine1:
217                 DIALOGFACTORY->updateMouseWidget(tr("Select first line"), tr("Cancel"));
218                 break;
219
220         case SetLine2:
221                 DIALOGFACTORY->updateMouseWidget(tr("Select second line"), tr("Back"));
222                 break;
223
224         case SetLength:
225                 DIALOGFACTORY->updateMouseWidget(tr("Enter bisector length:"), tr("Back"));
226                 break;
227
228         case SetNumber:
229                 DIALOGFACTORY->updateMouseWidget(tr("Enter number of bisectors:"), tr("Back"));
230                 break;
231
232         default:
233                 DIALOGFACTORY->updateMouseWidget("", "");
234                 break;
235         }
236 }
237
238 void ActionDrawLineBisector::showOptions()
239 {
240         ActionInterface::showOptions();
241         DIALOGFACTORY->requestOptions(this, true);
242 }
243
244 void ActionDrawLineBisector::hideOptions()
245 {
246         ActionInterface::hideOptions();
247         DIALOGFACTORY->requestOptions(this, false);
248 }
249
250 void ActionDrawLineBisector::updateMouseCursor()
251 {
252         graphicView->setMouseCursor(RS2::CadCursor);
253 }
254
255 void ActionDrawLineBisector::updateToolBar()
256 {
257         DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
258 }
259
260 void ActionDrawLineBisector::setLength(double l)
261 {
262         length = l;
263 }
264
265 double ActionDrawLineBisector::getLength()
266 {
267         return length;
268 }
269
270 void ActionDrawLineBisector::setNumber(int n)
271 {
272         number = n;
273 }
274
275 int ActionDrawLineBisector::getNumber()
276 {
277         return number;
278 }