]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlineparallelthrough.cpp
In the middle of major refactoring...
[architektonas] / src / actions / actiondrawlineparallelthrough.cpp
1 // actiondrawlineparallelthrough.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/04/2010  Added this text. :-)
13 //
14
15 #include "actiondrawlineparallelthrough.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 ActionDrawLineParallelThrough::ActionDrawLineParallelThrough(
24         RS_EntityContainer & container, GraphicView & graphicView):
25         ActionInterface("Draw Parallels", container, graphicView)
26 {
27         parallel = NULL;
28         entity = NULL;
29 //???   distance = 1.0;
30         number = 1;
31         coord = Vector(false);
32 }
33
34 ActionDrawLineParallelThrough::~ActionDrawLineParallelThrough()
35 {
36 }
37
38 /*virtual*/ RS2::ActionType ActionDrawLineParallelThrough::rtti()
39 {
40         return RS2::ActionDrawLineParallelThrough;
41 }
42
43 void ActionDrawLineParallelThrough::trigger()
44 {
45         ActionInterface::trigger();
46         deleteSnapper();
47
48         if (entity)
49         {
50                 RS_Creation creation(container, graphicView);
51                 RS_Entity * e = creation.createParallelThrough(coord, number, entity);
52
53                 if (!e)
54                         RS_DEBUG->print("ActionDrawLineParallelThrough::trigger:"
55                                 " No parallels added\n");
56         }
57 }
58
59 void ActionDrawLineParallelThrough::mouseMoveEvent(QMouseEvent * e)
60 {
61         RS_DEBUG->print("ActionDrawLineParallelThrough::mouseMoveEvent begin");
62
63         switch (getStatus())
64         {
65         case SetEntity:
66                 entity = catchEntity(e, RS2::ResolveAll);
67                 break;
68
69         case SetPos:
70         {
71                 coord = snapPoint(e);
72                 deletePreview();
73                 clearPreview();
74
75 //              RS_Creation creation(preview, NULL, false);
76 //              creation.createParallelThrough(coord, number, entity);
77
78                 drawPreview();
79         }
80         break;
81
82         default:
83                 break;
84         }
85
86         RS_DEBUG->print("ActionDrawLineParallelThrough::mouseMoveEvent end");
87 }
88
89 void ActionDrawLineParallelThrough::mouseReleaseEvent(QMouseEvent * e)
90 {
91         if (e->button() == Qt::LeftButton)
92         {
93                 switch (getStatus())
94                 {
95                 case SetEntity:
96                         entity = catchEntity(e, RS2::ResolveAll);
97
98                         if (entity)
99                         {
100                                 entity->setHighlighted(true);
101                                 graphicView->drawEntity(entity);
102                                 setStatus(SetPos);
103                         }
104
105                         break;
106
107                 case SetPos:
108                 {
109                         Vector ce(snapPoint(e));
110                         coordinateEvent(&ce);
111                 }
112                 break;
113
114                 default:
115                         break;
116                 }
117         }
118         else if (e->button() == Qt::RightButton)
119         {
120                 deletePreview();
121                 deleteSnapper();
122
123                 if (entity)
124                 {
125                         entity->setHighlighted(false);
126                         graphicView->drawEntity(entity);
127                         entity = NULL;
128                 }
129
130                 init(getStatus() - 1);
131         }
132 }
133
134 void ActionDrawLineParallelThrough::coordinateEvent(Vector * e)
135 {
136         if (e == NULL)
137                 return;
138
139         Vector mouse = *e;
140
141         switch (getStatus())
142         {
143         case SetPos:
144                 coord = mouse;
145                 trigger();
146                 break;
147
148         default:
149                 break;
150         }
151 }
152
153 void ActionDrawLineParallelThrough::updateMouseButtonHints()
154 {
155         switch (getStatus())
156         {
157         case SetEntity:
158                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select entity"), tr("Cancel"));
159                 break;
160
161         case SetPos:
162                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify through point"), tr("Back"));
163                 break;
164
165         case SetNumber:
166                 RS_DIALOGFACTORY->updateMouseWidget(tr("Number:"), tr("Back"));
167                 break;
168
169         default:
170                 RS_DIALOGFACTORY->updateMouseWidget("", "");
171                 break;
172         }
173 }
174
175 void ActionDrawLineParallelThrough::showOptions()
176 {
177         ActionInterface::showOptions();
178         RS_DIALOGFACTORY->requestOptions(this, true);
179         updateMouseButtonHints();
180 }
181
182 void ActionDrawLineParallelThrough::hideOptions()
183 {
184         ActionInterface::hideOptions();
185         RS_DIALOGFACTORY->requestOptions(this, false);
186 }
187
188 void ActionDrawLineParallelThrough::commandEvent(RS_CommandEvent * e)
189 {
190         QString c = e->getCommand().toLower();
191
192         if (checkCommand("help", c))
193         {
194                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
195                         + getAvailableCommands().join(", "));
196                 return;
197         }
198
199         switch (getStatus())
200         {
201         case SetEntity:
202         case SetPos:
203
204                 if (checkCommand("number", c))
205                 {
206                         deleteSnapper();
207                         deletePreview();
208                         clearPreview();
209                         lastStatus = (Status)getStatus();
210                         setStatus(SetNumber);
211                 }
212                 break;
213
214         case SetNumber:
215         {
216                 bool ok;
217                 int n = c.toInt(&ok);
218
219                 if (ok == true)
220                 {
221                         if (n > 0 && n < 100)
222                                 number = n;
223                         else
224                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
225                                                 "Try 1..99"));
226                 }
227                 else
228                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
229                 RS_DIALOGFACTORY->requestOptions(this, true, true);
230                 setStatus(lastStatus);
231         }
232                 break;
233
234         default:
235                 break;
236         }
237 }
238
239 QStringList ActionDrawLineParallelThrough::getAvailableCommands()
240 {
241         QStringList cmd;
242
243         switch (getStatus())
244         {
245         case SetEntity:
246                 cmd += command("number");
247                 break;
248
249         default:
250                 break;
251         }
252
253         return cmd;
254 }
255
256 void ActionDrawLineParallelThrough::updateMouseCursor()
257 {
258         graphicView->setMouseCursor(RS2::CadCursor);
259 }
260
261 void ActionDrawLineParallelThrough::updateToolBar()
262 {
263         if (getStatus() == SetPos && !isFinished())
264                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
265         else
266                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
267 }
268
269 int ActionDrawLineParallelThrough::getNumber()
270 {
271         return number;
272 }
273
274 void ActionDrawLineParallelThrough::setNumber(int n)
275 {
276         number = n;
277 }