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