]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actionlibraryinsert.cpp
Initial import
[architektonas] / src / actions / rs_actionlibraryinsert.cpp
1 /****************************************************************************
2 ** $Id: rs_actionlibraryinsert.cpp 1161 2004-12-09 23:10:09Z 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_actionlibraryinsert.h"
28
29 #include "commands.h"
30 #include "rs_creation.h"
31 #include "rs_modification.h"
32 #include "rs_units.h"
33
34 /**
35  * Constructor.
36  */
37 RS_ActionLibraryInsert::RS_ActionLibraryInsert(RS_EntityContainer & container,
38         RS_GraphicView & graphicView):
39         RS_PreviewActionInterface("Library Insert", container, graphicView)
40 {
41 }
42
43 RS_ActionLibraryInsert::~RS_ActionLibraryInsert()
44 {
45 }
46
47 QAction * RS_ActionLibraryInsert::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
48 {
49         QAction * action = new QAction(tr("&Insert Library Object"), 0);
50 //      QAction* action = new QAction(tr("Insert Library Object"),
51 //                                                                      tr("&Insert Library Object"),
52 //                                                                      QKeySequence(), NULL);
53         action->setStatusTip(tr("Inserts an Object from the part library."));
54         return action;
55 }
56
57 void RS_ActionLibraryInsert::init(int status)
58 {
59         RS_PreviewActionInterface::init(status);
60
61         reset();
62
63     /*if (graphic!=NULL) {
64         block = graphic->getActiveBlock();
65         if (block!=NULL) {
66             data.name = block->getName();
67         }
68 }*/
69     //trigger();
70 }
71
72 void RS_ActionLibraryInsert::setFile(const QString & file)
73 {
74         data.file = file;
75
76         if (!prev.open(file, RS2::FormatUnknown))
77                 RS_DIALOGFACTORY->commandMessage(tr("Cannot open file '%1'").arg(file));
78 }
79
80 void RS_ActionLibraryInsert::reset()
81 {
82     /*data = RS_InsertData("",
83                          Vector(0.0,0.0),
84                          Vector(1.0,1.0),
85                          0.0,
86                          1, 1,
87                          Vector(1.0,1.0),
88                          NULL,
89                          RS2::Update);*/
90
91         data.insertionPoint = Vector(false);
92         data.factor = 1.0;
93         data.angle = 0.0;
94 }
95
96 void RS_ActionLibraryInsert::trigger()
97 {
98         deleteSnapper();
99         deletePreview();
100         clearPreview();
101
102         RS_Creation creation(container, graphicView);
103         creation.createLibraryInsert(data);
104
105         graphicView->redraw();
106 }
107
108 void RS_ActionLibraryInsert::mouseMoveEvent(QMouseEvent * e)
109 {
110         switch (getStatus())
111         {
112         case SetTargetPoint:
113                 data.insertionPoint = snapPoint(e);
114
115                 //if (block!=NULL) {
116                 deletePreview();
117                 clearPreview();
118                 preview->addAllFrom(prev);
119                 preview->move(data.insertionPoint);
120                 preview->scale(data.insertionPoint,
121                                                 Vector(data.factor, data.factor));
122                 // unit conversion:
123                 if (graphic != NULL)
124                 {
125                         double uf = RS_Units::convert(1.0, prev.getUnit(), graphic->getUnit());
126                         preview->scale(data.insertionPoint, Vector(uf, uf));
127                 }
128
129                 preview->rotate(data.insertionPoint, data.angle);
130                 // too slow:
131                 //RS_Creation creation(preview, NULL, false);
132                 //creation.createInsert(data);
133                 drawPreview();
134                 //}
135                 break;
136
137         default:
138                 break;
139         }
140 }
141
142 void RS_ActionLibraryInsert::mouseReleaseEvent(QMouseEvent * e)
143 {
144         if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
145         {
146                 RS_CoordinateEvent ce(snapPoint(e));
147                 coordinateEvent(&ce);
148         }
149         else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
150         {
151                 deleteSnapper();
152                 init(getStatus() - 1);
153         }
154 }
155
156 void RS_ActionLibraryInsert::coordinateEvent(RS_CoordinateEvent * e)
157 {
158         if (e == NULL)
159                 return;
160
161         data.insertionPoint = e->getCoordinate();
162         trigger();
163 }
164
165 void RS_ActionLibraryInsert::commandEvent(RS_CommandEvent * e)
166 {
167         QString c = e->getCommand().toLower();
168
169         if (checkCommand("help", c))
170         {
171                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands() + getAvailableCommands().join(", "));
172                 return;
173         }
174
175         switch (getStatus())
176         {
177         case SetTargetPoint:
178                 if (checkCommand("angle", c))
179                 {
180                         deleteSnapper();
181                         deletePreview();
182                         clearPreview();
183                         lastStatus = (Status)getStatus();
184                         setStatus(SetAngle);
185                 }
186                 else if (checkCommand("factor", c))
187                 {
188                         deleteSnapper();
189                         deletePreview();
190                         clearPreview();
191                         lastStatus = (Status)getStatus();
192                         setStatus(SetFactor);
193                 }
194                 break;
195
196         case SetAngle:
197         {
198                 bool ok;
199                 double a = RS_Math::eval(c, &ok);
200
201                 if (ok == true)
202                 {
203                         data.angle = RS_Math::deg2rad(a);
204                 }
205                 else
206                 {
207                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
208                 }
209
210                 RS_DIALOGFACTORY->requestOptions(this, true, true);
211                 setStatus(lastStatus);
212         }
213                 break;
214
215         case SetFactor:
216         {
217                 bool ok;
218                 double f = RS_Math::eval(c, &ok);
219
220                 if (ok == true)
221                 {
222                         setFactor(f);
223                 }
224                 else
225                 {
226                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
227                 }
228
229                 RS_DIALOGFACTORY->requestOptions(this, true, true);
230                 setStatus(lastStatus);
231         }
232                 break;
233
234         default:
235                 break;
236         }
237 }
238
239 QStringList RS_ActionLibraryInsert::getAvailableCommands()
240 {
241         QStringList cmd;
242
243         switch (getStatus())
244         {
245         case SetTargetPoint:
246                 cmd += command("angle");
247                 cmd += command("factor");
248                 ;
249                 break;
250         default:
251                 break;
252         }
253
254         return cmd;
255 }
256
257 void RS_ActionLibraryInsert::showOptions()
258 {
259         RS_ActionInterface::showOptions();
260
261         RS_DIALOGFACTORY->requestOptions(this, true);
262 }
263
264 void RS_ActionLibraryInsert::hideOptions()
265 {
266         RS_ActionInterface::hideOptions();
267
268         RS_DIALOGFACTORY->requestOptions(this, false);
269 }
270
271 void RS_ActionLibraryInsert::updateMouseButtonHints()
272 {
273         switch (getStatus())
274         {
275         case SetTargetPoint:
276                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
277                 break;
278         case SetAngle:
279                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), "");
280                 break;
281         case SetFactor:
282                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"), "");
283                 break;
284         default:
285                 RS_DIALOGFACTORY->updateMouseWidget("", "");
286                 break;
287         }
288 }
289
290 void RS_ActionLibraryInsert::updateMouseCursor()
291 {
292         graphicView->setMouseCursor(RS2::CadCursor);
293 }
294
295 void RS_ActionLibraryInsert::updateToolBar()
296 {
297         if (!isFinished())
298                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
299         else
300                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
301 }
302