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