]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinebisector.cpp
Major refactoring of actions: Moved implementation from header files
[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_creation.h"
18 #include "rs_dialogfactory.h"
19 #include "rs_graphicview.h"
20 #include "rs_preview.h"
21
22 RS_ActionDrawLineBisector::RS_ActionDrawLineBisector(RS_EntityContainer & container, RS_GraphicView & graphicView):
23         RS_PreviewActionInterface("Draw Bisectors", container, graphicView)
24 {
25         bisector = NULL;
26         length = 10.0;
27         line1 = NULL;
28         line2 = NULL;
29         number = 1;
30         coord1 = Vector(false);
31         coord2 = Vector(false);
32         lastStatus = SetLine1;
33 }
34
35 RS_ActionDrawLineBisector::~RS_ActionDrawLineBisector()
36 {
37 }
38
39 /*virtual*/ RS2::ActionType RS_ActionDrawLineBisector::rtti()
40 {
41         return RS2::ActionDrawLineBisector;
42 }
43
44 void RS_ActionDrawLineBisector::trigger()
45 {
46         RS_PreviewActionInterface::trigger();
47
48         //if (bisector!=NULL) {
49         RS_Creation creation(container, graphicView);
50         creation.createBisector(coord1,
51                 coord2,
52                 length,
53                 number,
54                 line1,
55                 line2);
56         /*RS_Entity* newEntity = NULL;
57
58            newEntity = new RS_Line(container,
59                                 bisector->getData());
60
61            if (newEntity!=NULL) {
62             newEntity->setLayerToActive();
63             newEntity->setPenToActive();
64             container->addEntity(newEntity);
65
66             // upd. undo list:
67             if (document!=NULL) {
68                 document->startUndoCycle();
69                 document->addUndoable(newEntity);
70                 document->endUndoCycle();
71             }
72             graphicView->drawEntity(newEntity);
73             setStatus(SetLine1);
74            }
75            //reset();
76            delete bisector;
77            bisector = NULL;
78          */
79         /*} else {
80             RS_DEBUG->print("RS_ActionDrawLineBisector::trigger:"
81                             " Entity is NULL\n");
82            }*/
83 }
84
85 void RS_ActionDrawLineBisector::mouseMoveEvent(QMouseEvent * e)
86 {
87         RS_DEBUG->print("RS_ActionDrawLineBisector::mouseMoveEvent begin");
88
89         Vector mouse = Vector(graphicView->toGraphX(e->x()),
90                         graphicView->toGraphY(e->y()));
91
92         switch (getStatus())
93         {
94         case SetLine1:
95                 break;
96
97         case SetLine2: {
98                 coord2 = mouse;
99                 RS_Entity * en = catchEntity(e, RS2::ResolveAll);
100
101                 if (en != NULL && en->rtti() == RS2::EntityLine)
102                 {
103                         line2 = (RS_Line *)en;
104
105                         deletePreview();
106                         clearPreview();
107
108                         RS_Creation creation(preview, NULL, false);
109                         creation.createBisector(coord1,
110                                 coord2,
111                                 length,
112                                 number,
113                                 line1,
114                                 line2);
115                         drawPreview();
116                 }
117         }
118         break;
119
120         default:
121                 break;
122         }
123
124         RS_DEBUG->print("RS_ActionDrawLineBisector::mouseMoveEvent end");
125 }
126
127 void RS_ActionDrawLineBisector::mouseReleaseEvent(QMouseEvent * e)
128 {
129         if (RS2::qtToRsButtonState(e->button()) == RS2::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                         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 RS_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 RS_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 RS_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 RS_ActionDrawLineBisector::showOptions()
275 {
276         RS_ActionInterface::showOptions();
277
278         RS_DIALOGFACTORY->requestOptions(this, true);
279 }
280
281 void RS_ActionDrawLineBisector::hideOptions()
282 {
283         RS_ActionInterface::hideOptions();
284
285         RS_DIALOGFACTORY->requestOptions(this, false);
286 }
287
288 void RS_ActionDrawLineBisector::updateMouseCursor()
289 {
290         graphicView->setMouseCursor(RS2::CadCursor);
291 }
292
293 void RS_ActionDrawLineBisector::updateToolBar()
294 {
295         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
296 }
297
298 void RS_ActionDrawLineBisector::setLength(double l)
299 {
300         length = l;
301 }
302
303 double RS_ActionDrawLineBisector::getLength()
304 {
305         return length;
306 }
307
308 void RS_ActionDrawLineBisector::setNumber(int n)
309 {
310         number = n;
311 }
312
313 int RS_ActionDrawLineBisector::getNumber()
314 {
315         return number;
316 }
317