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