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