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