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