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