]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actionblocksinsert.cpp
Initial import
[architektonas] / src / actions / rs_actionblocksinsert.cpp
1 /****************************************************************************
2 ** $Id: rs_actionblocksinsert.cpp 1148 2004-09-11 21:38:11Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actionblocksinsert.h"
28
29 #include "rs_creation.h"
30 #include "commands.h"
31 #include "rs_modification.h"
32
33
34 /**
35  * Constructor.
36  */
37 RS_ActionBlocksInsert::RS_ActionBlocksInsert(RS_EntityContainer& container,
38         RS_GraphicView& graphicView)
39         :RS_PreviewActionInterface("Blocks Insert",
40                            container, graphicView) {}
41
42
43
44 RS_ActionBlocksInsert::~RS_ActionBlocksInsert() {}
45
46 QAction* RS_ActionBlocksInsert::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
47 {
48     QAction * action = new QAction(tr("&Insert Block"), 0);
49 //    QAction* action = new QAction(tr("Insert Block"), tr("&Insert Block"),
50 //                                  QKeySequence(), NULL);
51     action->setStatusTip(tr("Insert Block"));
52
53     return action;
54 }
55
56 void RS_ActionBlocksInsert::init(int status) {
57     RS_PreviewActionInterface::init(status);
58
59     reset();
60
61     if (graphic!=NULL) {
62         block = graphic->getActiveBlock();
63         if (block!=NULL) {
64             data.name = block->getName();
65         } else {
66             finish();
67         }
68     }
69 }
70
71
72
73 void RS_ActionBlocksInsert::reset() {
74     data = RS_InsertData("",
75                          Vector(0.0,0.0),
76                          Vector(1.0,1.0),
77                          0.0,
78                          1, 1,
79                          Vector(1.0,1.0),
80                          NULL,
81                          RS2::Update);
82 }
83
84
85
86 void RS_ActionBlocksInsert::trigger() {
87     deleteSnapper();
88     deletePreview();
89     clearPreview();
90
91     //RS_Modification m(*container, graphicView);
92     //m.paste(data.insertionPoint);
93     //std::cout << *RS_Clipboard::instance();
94
95     if (block!=NULL) {
96         RS_Creation creation(container, graphicView);
97                 data.updateMode = RS2::Update;
98         creation.createInsert(data);
99     }
100
101     graphicView->redraw();
102     //finish();
103 }
104
105
106 void RS_ActionBlocksInsert::mouseMoveEvent(QMouseEvent* e) {
107     switch (getStatus()) {
108     case SetTargetPoint:
109         data.insertionPoint = snapPoint(e);
110
111         if (block!=NULL) {
112             deletePreview();
113             clearPreview();
114             //preview->addAllFrom(*block);
115             //preview->move(data.insertionPoint);
116             RS_Creation creation(preview, NULL, false);
117                         // Create insert as preview only
118                         data.updateMode = RS2::PreviewUpdate;
119             creation.createInsert(data);
120             drawPreview();
121         }
122         break;
123
124     default:
125         break;
126     }
127 }
128
129
130
131 void RS_ActionBlocksInsert::mouseReleaseEvent(QMouseEvent* e) {
132     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
133         RS_CoordinateEvent ce(snapPoint(e));
134         coordinateEvent(&ce);
135     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
136         deleteSnapper();
137         init(getStatus()-1);
138     }
139 }
140
141
142
143 void RS_ActionBlocksInsert::coordinateEvent(RS_CoordinateEvent* e) {
144     if (e==NULL) {
145         return;
146     }
147
148     data.insertionPoint = e->getCoordinate();
149     trigger();
150 }
151
152
153
154 void RS_ActionBlocksInsert::commandEvent(RS_CommandEvent* e) {
155     QString c = e->getCommand().toLower();
156
157     if (checkCommand("help", c)) {
158         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
159                                          + getAvailableCommands().join(", "));
160         return;
161     }
162
163     switch (getStatus()) {
164     case SetTargetPoint:
165         if (checkCommand("angle", c)) {
166             deleteSnapper();
167             deletePreview();
168             clearPreview();
169             lastStatus = (Status)getStatus();
170             setStatus(SetAngle);
171         } else if (checkCommand("factor", c)) {
172             deleteSnapper();
173             deletePreview();
174             clearPreview();
175             lastStatus = (Status)getStatus();
176             setStatus(SetFactor);
177         } else if (checkCommand("columns", c)) {
178             deleteSnapper();
179             deletePreview();
180             clearPreview();
181             lastStatus = (Status)getStatus();
182             setStatus(SetColumns);
183         } else if (checkCommand("rows", c)) {
184             deleteSnapper();
185             deletePreview();
186             clearPreview();
187             lastStatus = (Status)getStatus();
188             setStatus(SetRows);
189         } else if (checkCommand("columnspacing", c)) {
190             deleteSnapper();
191             deletePreview();
192             clearPreview();
193             lastStatus = (Status)getStatus();
194             setStatus(SetColumnSpacing);
195         } else if (checkCommand("rowspacing", c)) {
196             deleteSnapper();
197             deletePreview();
198             clearPreview();
199             lastStatus = (Status)getStatus();
200             setStatus(SetRowSpacing);
201         }
202         break;
203
204     case SetAngle: {
205             bool ok;
206             double a = RS_Math::eval(c, &ok);
207             if (ok==true) {
208                 data.angle = RS_Math::deg2rad(a);
209             } else {
210                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
211             }
212             RS_DIALOGFACTORY->requestOptions(this, true, true);
213             setStatus(lastStatus);
214         }
215         break;
216
217     case SetFactor: {
218             bool ok;
219             double f = RS_Math::eval(c, &ok);
220             if (ok==true) {
221                 setFactor(f);
222             } else {
223                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
224             }
225             RS_DIALOGFACTORY->requestOptions(this, true, true);
226             setStatus(lastStatus);
227         }
228         break;
229
230     case SetColumns: {
231             bool ok;
232             int cols = (int)RS_Math::eval(c, &ok);
233             if (ok==true) {
234                 data.cols = cols;
235             } else {
236                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
237             }
238             RS_DIALOGFACTORY->requestOptions(this, true, true);
239             setStatus(lastStatus);
240         }
241         break;
242
243     case SetRows: {
244             bool ok;
245             int rows = (int)RS_Math::eval(c, &ok);
246             if (ok==true) {
247                 data.rows = rows;
248             } else {
249                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
250             }
251             RS_DIALOGFACTORY->requestOptions(this, true, true);
252             setStatus(lastStatus);
253         }
254         break;
255
256     case SetColumnSpacing: {
257             bool ok;
258             double cs = (int)RS_Math::eval(c, &ok);
259             if (ok==true) {
260                 data.spacing.x = cs;
261             } else {
262                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
263             }
264             RS_DIALOGFACTORY->requestOptions(this, true, true);
265             setStatus(lastStatus);
266         }
267         break;
268
269     case SetRowSpacing: {
270             bool ok;
271             int rs = (int)RS_Math::eval(c, &ok);
272             if (ok==true) {
273                 data.spacing.y = rs;
274             } else {
275                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
276             }
277             RS_DIALOGFACTORY->requestOptions(this, true, true);
278             setStatus(lastStatus);
279         }
280         break;
281
282     default:
283         break;
284     }
285 }
286
287
288
289 QStringList RS_ActionBlocksInsert::getAvailableCommands() {
290     QStringList cmd;
291
292     switch (getStatus()) {
293     case SetTargetPoint:
294         cmd += command("angle");
295         cmd += command("factor");
296         ;
297         cmd += command("columns");
298         cmd += command("rows");
299         cmd += command("columnspacing");
300         cmd += command("rowspacing");
301         break;
302     default:
303         break;
304     }
305
306     return cmd;
307 }
308
309
310 void RS_ActionBlocksInsert::showOptions() {
311     RS_ActionInterface::showOptions();
312
313     RS_DIALOGFACTORY->requestOptions(this, true);
314 }
315
316
317
318 void RS_ActionBlocksInsert::hideOptions() {
319     RS_ActionInterface::hideOptions();
320
321     RS_DIALOGFACTORY->requestOptions(this, false);
322 }
323
324
325 void RS_ActionBlocksInsert::updateMouseButtonHints() {
326     switch (getStatus()) {
327     case SetTargetPoint:
328         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
329                                             tr("Cancel"));
330         break;
331     case SetAngle:
332         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"),
333                                             "");
334         break;
335     case SetFactor:
336         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"),
337                                             "");
338         break;
339     case SetColumns:
340         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter columns:"),
341                                             "");
342         break;
343     case SetRows:
344         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter rows:"),
345                                             "");
346         break;
347     case SetColumnSpacing:
348         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter column spacing:"),
349                                             "");
350         break;
351     case SetRowSpacing:
352         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter row spacing:"),
353                                             "");
354         break;
355     default:
356         RS_DIALOGFACTORY->updateMouseWidget("", "");
357         break;
358     }
359 }
360
361
362
363 void RS_ActionBlocksInsert::updateMouseCursor() {
364     graphicView->setMouseCursor(RS2::CadCursor);
365 }
366
367
368
369 void RS_ActionBlocksInsert::updateToolBar() {
370     if (!isFinished()) {
371         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
372     } else {
373         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
374     }
375 }
376
377
378 // EOF