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