]> Shamusworld >> Repos - architektonas/blob - src/actions/actionblocksinsert.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actionblocksinsert.cpp
1 // actionblocksinsert.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/03/2010  Added this text. :-)
15 // JLH  06/03/2010  Scrubbed out all occurances of CoordinateEvent
16 //
17
18 #include "actionblocksinsert.h"
19
20 #include "commands.h"
21 #include "commandevent.h"
22 #include "creation.h"
23 #include "dialogfactory.h"
24 #include "modification.h"
25 #include "preview.h"
26
27 /**
28  * Constructor.
29  */
30 ActionBlocksInsert::ActionBlocksInsert(EntityContainer & container, GraphicView & graphicView):
31         ActionInterface("Blocks Insert", container, graphicView)
32 {
33 }
34
35 ActionBlocksInsert::~ActionBlocksInsert()
36 {
37 }
38
39 /*virtual*/ RS2::ActionType ActionBlocksInsert::rtti()
40 {
41         return RS2::ActionBlocksInsert;
42 }
43
44 void ActionBlocksInsert::init(int status /*= 0*/)
45 {
46         ActionInterface::init(status);
47         reset();
48
49         if (graphic)
50         {
51                 block = graphic->getActiveBlock();
52
53                 if (block)
54                         data.name = block->getName();
55                 else
56                         finish();
57         }
58 }
59
60 void ActionBlocksInsert::reset()
61 {
62         data = InsertData("", Vector(0.0, 0.0), Vector(1.0, 1.0), 0.0, 1, 1,
63                 Vector(1.0, 1.0), NULL, RS2::Update);
64 }
65
66 void ActionBlocksInsert::trigger()
67 {
68 //      deleteSnapper();
69 //      deletePreview();
70 //      clearPreview();
71
72         //Modification m(*container, graphicView);
73         //m.paste(data.insertionPoint);
74         //std::cout << *Clipboard::instance();
75
76         if (block)
77         {
78                 Creation creation(container, graphicView);
79                 data.updateMode = RS2::Update;
80                 creation.createInsert(data);
81         }
82
83         graphicView->redraw();
84 }
85
86 void ActionBlocksInsert::mouseMoveEvent(QMouseEvent * e)
87 {
88         switch (getStatus())
89         {
90         case SetTargetPoint:
91 //              data.insertionPoint = snapPoint(e);
92                 data.insertionPoint = graphicView->SnapPoint(e);
93
94                 if (block != NULL)
95                 {
96 //                      deletePreview();
97 //                      clearPreview();
98                         //preview->addAllFrom(*block);
99                         //preview->move(data.insertionPoint);
100 //                      Creation creation(preview, NULL, false);
101                         Creation creation(&(graphicView->preview), NULL, false);
102                         // Create insert as preview only
103                         data.updateMode = RS2::PreviewUpdate;
104                         creation.createInsert(data);
105 //                      drawPreview();
106                 }
107                 break;
108
109         default:
110                 break;
111         }
112 }
113
114 void ActionBlocksInsert::mouseReleaseEvent(QMouseEvent * e)
115 {
116         if (e->button() == Qt::LeftButton)
117         {
118 //              Vector ce(snapPoint(e));
119                 Vector ce(graphicView->SnapPoint(e));
120                 coordinateEvent(&ce);
121         }
122         else if (e->button() == Qt::RightButton)
123         {
124 //              deleteSnapper();
125                 init(getStatus() - 1);
126         }
127 }
128
129 void ActionBlocksInsert::coordinateEvent(Vector * e)
130 {
131         if (!e)
132                 return;
133
134         data.insertionPoint = *e;
135         trigger();
136 }
137
138 void ActionBlocksInsert::commandEvent(CommandEvent * e)
139 {
140         QString c = e->getCommand().toLower();
141
142         if (checkCommand("help", c))
143         {
144                 DIALOGFACTORY->commandMessage(msgAvailableCommands()
145                         + getAvailableCommands().join(", "));
146                 return;
147         }
148
149         switch (getStatus())
150         {
151         case SetTargetPoint:
152
153                 if (checkCommand("angle", c))
154                 {
155 //                      deleteSnapper();
156 //                      deletePreview();
157 //                      clearPreview();
158                         lastStatus = (Status)getStatus();
159                         setStatus(SetAngle);
160                 }
161                 else if (checkCommand("factor", c))
162                 {
163 //                      deleteSnapper();
164 //                      deletePreview();
165 //                      clearPreview();
166                         lastStatus = (Status)getStatus();
167                         setStatus(SetFactor);
168                 }
169                 else if (checkCommand("columns", c))
170                 {
171 //                      deleteSnapper();
172 //                      deletePreview();
173 //                      clearPreview();
174                         lastStatus = (Status)getStatus();
175                         setStatus(SetColumns);
176                 }
177                 else if (checkCommand("rows", c))
178                 {
179 //                      deleteSnapper();
180 //                      deletePreview();
181 //                      clearPreview();
182                         lastStatus = (Status)getStatus();
183                         setStatus(SetRows);
184                 }
185                 else if (checkCommand("columnspacing", c))
186                 {
187 //                      deleteSnapper();
188 //                      deletePreview();
189 //                      clearPreview();
190                         lastStatus = (Status)getStatus();
191                         setStatus(SetColumnSpacing);
192                 }
193                 else if (checkCommand("rowspacing", c))
194                 {
195 //                      deleteSnapper();
196 //                      deletePreview();
197 //                      clearPreview();
198                         lastStatus = (Status)getStatus();
199                         setStatus(SetRowSpacing);
200                 }
201                 break;
202
203         case SetAngle:
204         {
205                 bool ok;
206                 double a = Math::eval(c, &ok);
207
208                 if (ok)
209                         data.angle = Math::deg2rad(a);
210                 else
211                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
212
213                 DIALOGFACTORY->requestOptions(this, true, true);
214                 setStatus(lastStatus);
215         }
216         break;
217
218         case SetFactor:
219         {
220                 bool ok;
221                 double f = Math::eval(c, &ok);
222
223                 if (ok)
224                         setFactor(f);
225                 else
226                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
227
228                 DIALOGFACTORY->requestOptions(this, true, true);
229                 setStatus(lastStatus);
230         }
231         break;
232
233         case SetColumns:
234         {
235                 bool ok;
236                 int cols = (int)Math::eval(c, &ok);
237
238                 if (ok)
239                         data.cols = cols;
240                 else
241                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
242
243                 DIALOGFACTORY->requestOptions(this, true, true);
244                 setStatus(lastStatus);
245         }
246         break;
247
248         case SetRows:
249         {
250                 bool ok;
251                 int rows = (int)Math::eval(c, &ok);
252
253                 if (ok)
254                         data.rows = rows;
255                 else
256                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
257
258                 DIALOGFACTORY->requestOptions(this, true, true);
259                 setStatus(lastStatus);
260         }
261         break;
262
263         case SetColumnSpacing:
264         {
265                 bool ok;
266                 double cs = (int)Math::eval(c, &ok);
267
268                 if (ok)
269                         data.spacing.x = cs;
270                 else
271                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
272
273                 DIALOGFACTORY->requestOptions(this, true, true);
274                 setStatus(lastStatus);
275         }
276         break;
277
278         case SetRowSpacing:
279         {
280                 bool ok;
281                 int rs = (int)Math::eval(c, &ok);
282
283                 if (ok)
284                         data.spacing.y = rs;
285                 else
286                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
287
288                 DIALOGFACTORY->requestOptions(this, true, true);
289                 setStatus(lastStatus);
290         }
291         break;
292
293         default:
294                 break;
295         }
296 }
297
298 QStringList ActionBlocksInsert::getAvailableCommands()
299 {
300         QStringList cmd;
301
302         switch (getStatus())
303         {
304         case SetTargetPoint:
305                 cmd += command("angle");
306                 cmd += command("factor");
307                 cmd += command("columns");
308                 cmd += command("rows");
309                 cmd += command("columnspacing");
310                 cmd += command("rowspacing");
311                 break;
312
313         default:
314                 break;
315         }
316
317         return cmd;
318 }
319
320 void ActionBlocksInsert::showOptions()
321 {
322         ActionInterface::showOptions();
323         DIALOGFACTORY->requestOptions(this, true);
324 }
325
326 void ActionBlocksInsert::hideOptions()
327 {
328         ActionInterface::hideOptions();
329         DIALOGFACTORY->requestOptions(this, false);
330 }
331
332 void ActionBlocksInsert::updateMouseButtonHints()
333 {
334         switch (getStatus())
335         {
336         case SetTargetPoint:
337                 DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
338                 break;
339
340         case SetAngle:
341                 DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), "");
342                 break;
343
344         case SetFactor:
345                 DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"), "");
346                 break;
347
348         case SetColumns:
349                 DIALOGFACTORY->updateMouseWidget(tr("Enter columns:"), "");
350                 break;
351
352         case SetRows:
353                 DIALOGFACTORY->updateMouseWidget(tr("Enter rows:"), "");
354                 break;
355
356         case SetColumnSpacing:
357                 DIALOGFACTORY->updateMouseWidget(tr("Enter column spacing:"), "");
358                 break;
359
360         case SetRowSpacing:
361                 DIALOGFACTORY->updateMouseWidget(tr("Enter row spacing:"), "");
362                 break;
363
364         default:
365                 DIALOGFACTORY->updateMouseWidget("", "");
366                 break;
367         }
368 }
369
370 void ActionBlocksInsert::updateMouseCursor()
371 {
372         graphicView->setMouseCursor(RS2::CadCursor);
373 }
374
375 void ActionBlocksInsert::updateToolBar()
376 {
377         if (!isFinished())
378                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
379         else
380                 DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
381 }
382
383 double ActionBlocksInsert::getAngle()
384 {
385         return data.angle;
386 }
387
388 void ActionBlocksInsert::setAngle(double a)
389 {
390         data.angle = a;
391 }
392
393 double ActionBlocksInsert::getFactor()
394 {
395         return data.scaleFactor.x;
396 }
397
398 void ActionBlocksInsert::setFactor(double f)
399 {
400         data.scaleFactor = Vector(f, f);
401 }
402
403 int ActionBlocksInsert::getColumns()
404 {
405         return data.cols;
406 }
407
408 void ActionBlocksInsert::setColumns(int c)
409 {
410         data.cols = c;
411 }
412
413 int ActionBlocksInsert::getRows()
414 {
415         return data.rows;
416 }
417
418 void ActionBlocksInsert::setRows(int r)
419 {
420         data.rows = r;
421 }
422
423 double ActionBlocksInsert::getColumnSpacing()
424 {
425         return data.spacing.x;
426 }
427
428 void ActionBlocksInsert::setColumnSpacing(double cs)
429 {
430         data.spacing.x = cs;
431 }
432
433 double ActionBlocksInsert::getRowSpacing()
434 {
435         return data.spacing.y;
436 }
437
438 void ActionBlocksInsert::setRowSpacing(double rs)
439 {
440         data.spacing.y = rs;
441 }