]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlineangle.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawlineangle.cpp
1 // rs_actiondrawlineangle.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  05/22/2010  Added this text. :-)
13 //
14
15 #include "rs_actiondrawlineangle.h"
16 #include "rs_snapper.h"
17
18 RS_ActionDrawLineAngle::RS_ActionDrawLineAngle(RS_EntityContainer& container,
19         RS_GraphicView& graphicView,
20         double angle,
21         bool fixedAngle)
22         :RS_PreviewActionInterface("Draw lines with given angle",
23                            container, graphicView)
24 {
25     this->angle = angle;
26     length = 1.0;
27     snpPoint = 0;
28     this->fixedAngle = fixedAngle;
29     pos = Vector(false);
30     reset();
31 }
32
33 RS_ActionDrawLineAngle::~RS_ActionDrawLineAngle()
34 {
35 }
36
37 QAction * RS_ActionDrawLineAngle::createGUIAction(RS2::ActionType type, QObject * /*parent*/)
38 {
39         QAction * action = NULL;
40
41         if (type == RS2::ActionDrawLineAngle)
42         {
43                 action = new QAction(tr("&Angle"), 0);
44 //              action = new QAction(tr("Line: Angle"), tr("&Angle"),
45 //                                                                      QKeySequence(), NULL);
46                 action->setStatusTip(tr("Draw lines with a given angle"));
47         }
48         else if (type == RS2::ActionDrawLineHorizontal)
49         {
50                 action = new QAction(tr("&Horizontal"), 0);
51 //              action = new QAction(tr("Line: Horizontal"), tr("&Horizontal"),
52 //                                                              QKeySequence(), NULL);
53                 action->setStatusTip(tr("Draw horizontal lines"));
54         }
55         else if (type == RS2::ActionDrawLineVertical)
56         {
57                 action = new QAction(tr("H&orizontal / Vertical"), 0);
58 //              action = new QAction(tr("hor./vert. line"), tr("H&orizontal / Vertical"),
59 //                                                              QKeySequence(), NULL);
60                 action->setStatusTip(tr("Draw horizontal/vertical lines"));
61         }
62
63         return action;
64 }
65
66 void RS_ActionDrawLineAngle::reset()
67 {
68         data = RS_LineData(Vector(false), Vector(false));
69 }
70
71
72
73 void RS_ActionDrawLineAngle::init(int status) {
74     RS_PreviewActionInterface::init(status);
75
76     reset();
77 }
78
79
80
81 void RS_ActionDrawLineAngle::trigger() {
82     RS_PreviewActionInterface::trigger();
83
84     preparePreview();
85     RS_Line* line = new RS_Line(container,
86                                 data);
87     line->setLayerToActive();
88     line->setPenToActive();
89     container->addEntity(line);
90
91     // upd. undo list:
92     if (document!=NULL) {
93         document->startUndoCycle();
94         document->addUndoable(line);
95         document->endUndoCycle();
96     }
97     deleteSnapper();
98     graphicView->moveRelativeZero(Vector(0.0,0.0));
99     graphicView->drawEntity(line);
100     graphicView->moveRelativeZero(data.startpoint);
101     RS_DEBUG->print("RS_ActionDrawLineAngle::trigger(): line added: %d",
102                     line->getId());
103 }
104
105
106
107 void RS_ActionDrawLineAngle::mouseMoveEvent(QMouseEvent* e) {
108     RS_DEBUG->print("RS_ActionDrawLineAngle::mouseMoveEvent begin");
109
110     if (getStatus()==SetPos) {
111         pos = snapPoint(e);
112         deletePreview();
113         clearPreview();
114         preparePreview();
115         preview->addEntity(new RS_Line(preview,
116                                        data));
117         drawPreview();
118     }
119
120     RS_DEBUG->print("RS_ActionDrawLineAngle::mouseMoveEvent end");
121 }
122
123
124
125 void RS_ActionDrawLineAngle::mouseReleaseEvent(QMouseEvent* e) {
126     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
127         if (getStatus()==SetPos) {
128             RS_CoordinateEvent ce(snapPoint(e));
129             coordinateEvent(&ce);
130         }
131     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
132         deletePreview();
133         deleteSnapper();
134         init(getStatus()-1);
135     }
136 }
137
138
139 void RS_ActionDrawLineAngle::preparePreview() {
140     Vector p1, p2;
141     // End:
142     if (snpPoint == 2) {
143         p2.setPolar(length * -1, angle);
144     } else {
145         p2.setPolar(length, angle);
146     }
147
148     // Middle:
149     if (snpPoint == 1) {
150         p1 = pos - (p2 / 2);
151     } else {
152         p1 = pos;
153     }
154
155     p2 += p1;
156     data = RS_LineData(p1, p2);
157 }
158
159
160 void RS_ActionDrawLineAngle::coordinateEvent(RS_CoordinateEvent* e) {
161     if (e==NULL) {
162         return;
163     }
164
165     switch (getStatus()) {
166     case SetPos:
167         pos = e->getCoordinate();
168         trigger();
169         break;
170
171     default:
172         break;
173     }
174 }
175
176
177
178 void RS_ActionDrawLineAngle::commandEvent(RS_CommandEvent* e) {
179     QString c = e->getCommand().toLower();
180
181     if (checkCommand("help", c)) {
182         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
183                                          + getAvailableCommands().join(", "));
184         return;
185     }
186
187     switch (getStatus()) {
188     case SetPos:
189         if (!fixedAngle && checkCommand("angle", c)) {
190             deleteSnapper();
191             deletePreview();
192             clearPreview();
193             setStatus(SetAngle);
194         } else if (checkCommand("length", c)) {
195             deleteSnapper();
196             deletePreview();
197             clearPreview();
198             setStatus(SetLength);
199         }
200         break;
201
202     case SetAngle: {
203             bool ok;
204             double a = RS_Math::eval(c, &ok);
205             if (ok==true) {
206                 angle = RS_Math::deg2rad(a);
207             } else {
208                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
209             }
210             RS_DIALOGFACTORY->requestOptions(this, true, true);
211             setStatus(SetPos);
212         }
213         break;
214
215     case SetLength: {
216             bool ok;
217             double l = RS_Math::eval(c, &ok);
218             if (ok==true) {
219                 length = l;
220             } else {
221                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
222             }
223             RS_DIALOGFACTORY->requestOptions(this, true, true);
224             setStatus(SetPos);
225         }
226         break;
227
228     default:
229         break;
230     }
231 }
232
233
234
235 QStringList RS_ActionDrawLineAngle::getAvailableCommands() {
236     QStringList cmd;
237
238     switch (getStatus()) {
239     case SetPos:
240         if (!fixedAngle) {
241             cmd += command("angle");
242         }
243         cmd += command("length");
244         break;
245     default:
246         break;
247     }
248
249     return cmd;
250 }
251
252
253 void RS_ActionDrawLineAngle::updateMouseButtonHints() {
254     switch (getStatus()) {
255     case SetPos:
256         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify position"),
257                                             tr("Cancel"));
258         break;
259
260     case SetAngle:
261         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), tr("Back"));
262         break;
263
264     case SetLength:
265         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter length:"), tr("Back"));
266         break;
267
268     default:
269         break;
270     }
271 }
272
273
274
275 void RS_ActionDrawLineAngle::showOptions() {
276     RS_ActionInterface::showOptions();
277
278     RS_DIALOGFACTORY->requestOptions(this, true);
279 }
280
281
282
283 void RS_ActionDrawLineAngle::hideOptions() {
284     RS_ActionInterface::hideOptions();
285
286     RS_DIALOGFACTORY->requestOptions(this, false);
287 }
288
289
290
291 void RS_ActionDrawLineAngle::updateMouseCursor() {
292     graphicView->setMouseCursor(RS2::CadCursor);
293 }
294
295
296
297 void RS_ActionDrawLineAngle::updateToolBar() {
298     if (!isFinished()) {
299         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
300     } else {
301         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
302     }
303 }
304
305 // EOF