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