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