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