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