]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinebisector.cpp
Last checkin before major refactor...
[architektonas] / src / actions / rs_actiondrawlinebisector.cpp
1 // rs_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 "rs_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 RS_ActionDrawLineBisector::RS_ActionDrawLineBisector(RS_EntityContainer & container, GraphicView & graphicView):
24         RS_PreviewActionInterface("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 RS_ActionDrawLineBisector::~RS_ActionDrawLineBisector()
37 {
38 }
39
40 /*virtual*/ RS2::ActionType RS_ActionDrawLineBisector::rtti()
41 {
42         return RS2::ActionDrawLineBisector;
43 }
44
45 void RS_ActionDrawLineBisector::trigger()
46 {
47         RS_PreviewActionInterface::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("RS_ActionDrawLineBisector::trigger:"
82                             " Entity is NULL\n");
83            }*/
84 }
85
86 void RS_ActionDrawLineBisector::mouseMoveEvent(QMouseEvent * e)
87 {
88         RS_DEBUG->print("RS_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                 coord2 = mouse;
100                 RS_Entity * en = catchEntity(e, RS2::ResolveAll);
101
102                 if (en != NULL && en->rtti() == RS2::EntityLine)
103                 {
104                         line2 = (RS_Line *)en;
105
106                         deletePreview();
107                         clearPreview();
108
109                         RS_Creation creation(preview, NULL, false);
110                         creation.createBisector(coord1,
111                                 coord2,
112                                 length,
113                                 number,
114                                 line1,
115                                 line2);
116                         drawPreview();
117                 }
118         }
119         break;
120
121         default:
122                 break;
123         }
124
125         RS_DEBUG->print("RS_ActionDrawLineBisector::mouseMoveEvent end");
126 }
127
128 void RS_ActionDrawLineBisector::mouseReleaseEvent(QMouseEvent * e)
129 {
130         if (e->button() == Qt::RightButton)
131         {
132                 deletePreview();
133                 clearPreview();
134                 init(getStatus() - 1);
135         }
136         else
137         {
138                 Vector mouse = Vector(graphicView->toGraphX(e->x()),
139                                 graphicView->toGraphY(e->y()));
140
141                 switch (getStatus())
142                 {
143                 case SetLine1: {
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 RS_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 RS_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 RS_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 RS_ActionDrawLineBisector::showOptions()
276 {
277         RS_ActionInterface::showOptions();
278
279         RS_DIALOGFACTORY->requestOptions(this, true);
280 }
281
282 void RS_ActionDrawLineBisector::hideOptions()
283 {
284         RS_ActionInterface::hideOptions();
285
286         RS_DIALOGFACTORY->requestOptions(this, false);
287 }
288
289 void RS_ActionDrawLineBisector::updateMouseCursor()
290 {
291         graphicView->setMouseCursor(RS2::CadCursor);
292 }
293
294 void RS_ActionDrawLineBisector::updateToolBar()
295 {
296         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
297 }
298
299 void RS_ActionDrawLineBisector::setLength(double l)
300 {
301         length = l;
302 }
303
304 double RS_ActionDrawLineBisector::getLength()
305 {
306         return length;
307 }
308
309 void RS_ActionDrawLineBisector::setNumber(int n)
310 {
311         number = n;
312 }
313
314 int RS_ActionDrawLineBisector::getNumber()
315 {
316         return number;
317 }
318