]> Shamusworld >> Repos - architektonas/blob - src/actions/actionlibraryinsert.cpp
In the middle of major refactoring...
[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 // (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 "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 ActionLibraryInsert::ActionLibraryInsert(RS_EntityContainer & container, GraphicView & graphicView):
27         ActionInterface("Library Insert", container, graphicView)
28 {
29 }
30
31 ActionLibraryInsert::~ActionLibraryInsert()
32 {
33 }
34
35 /*virtual*/ RS2::ActionType ActionLibraryInsert::rtti()
36 {
37         return RS2::ActionLibraryInsert;
38 }
39
40 void ActionLibraryInsert::init(int status)
41 {
42         ActionInterface::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 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 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 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 ActionLibraryInsert::mouseMoveEvent(QMouseEvent * e)
92 {
93         switch (getStatus())
94         {
95         case SetTargetPoint:
96                 data.insertionPoint = snapPoint(e);
97
98                 deletePreview();
99                 clearPreview();
100 //              preview->addAllFrom(prev);
101 //              preview->move(data.insertionPoint);
102 //              preview->scale(data.insertionPoint, Vector(data.factor, data.factor));
103
104                 // unit conversion:
105                 if (graphic)
106                 {
107                         double uf = RS_Units::convert(1.0, prev.getUnit(), graphic->getUnit());
108 //                      preview->scale(data.insertionPoint, Vector(uf, uf));
109                 }
110
111 //              preview->rotate(data.insertionPoint, data.angle);
112                 drawPreview();
113                 break;
114
115         default:
116                 break;
117         }
118 }
119
120 void ActionLibraryInsert::mouseReleaseEvent(QMouseEvent * e)
121 {
122         if (e->button() == Qt::LeftButton)
123         {
124                 Vector ce(snapPoint(e));
125                 coordinateEvent(&ce);
126         }
127         else if (e->button() == Qt::RightButton)
128         {
129                 deleteSnapper();
130                 init(getStatus() - 1);
131         }
132 }
133
134 void ActionLibraryInsert::coordinateEvent(Vector * e)
135 {
136         if (e == NULL)
137                 return;
138
139         data.insertionPoint = *e;
140         trigger();
141 }
142
143 void ActionLibraryInsert::commandEvent(RS_CommandEvent * e)
144 {
145         QString c = e->getCommand().toLower();
146
147         if (checkCommand("help", c))
148         {
149                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands() + getAvailableCommands().join(", "));
150                 return;
151         }
152
153         switch (getStatus())
154         {
155         case SetTargetPoint:
156
157                 if (checkCommand("angle", c))
158                 {
159                         deleteSnapper();
160                         deletePreview();
161                         clearPreview();
162                         lastStatus = (Status)getStatus();
163                         setStatus(SetAngle);
164                 }
165                 else if (checkCommand("factor", c))
166                 {
167                         deleteSnapper();
168                         deletePreview();
169                         clearPreview();
170                         lastStatus = (Status)getStatus();
171                         setStatus(SetFactor);
172                 }
173                 break;
174
175         case SetAngle:
176         {
177                 bool ok;
178                 double a = RS_Math::eval(c, &ok);
179
180                 if (ok == true)
181                         data.angle = RS_Math::deg2rad(a);
182                 else
183                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
184
185                 RS_DIALOGFACTORY->requestOptions(this, true, true);
186                 setStatus(lastStatus);
187         }
188         break;
189
190         case SetFactor:
191         {
192                 bool ok;
193                 double f = RS_Math::eval(c, &ok);
194
195                 if (ok == true)
196                         setFactor(f);
197                 else
198                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
199
200                 RS_DIALOGFACTORY->requestOptions(this, true, true);
201                 setStatus(lastStatus);
202         }
203         break;
204
205         default:
206                 break;
207         }
208 }
209
210 QStringList ActionLibraryInsert::getAvailableCommands()
211 {
212         QStringList cmd;
213
214         switch (getStatus())
215         {
216         case SetTargetPoint:
217                 cmd += command("angle");
218                 cmd += command("factor");
219                 break;
220
221         default:
222                 break;
223         }
224
225         return cmd;
226 }
227
228 void ActionLibraryInsert::showOptions()
229 {
230         ActionInterface::showOptions();
231
232         RS_DIALOGFACTORY->requestOptions(this, true);
233 }
234
235 void ActionLibraryInsert::hideOptions()
236 {
237         ActionInterface::hideOptions();
238
239         RS_DIALOGFACTORY->requestOptions(this, false);
240 }
241
242 void ActionLibraryInsert::updateMouseButtonHints()
243 {
244         switch (getStatus())
245         {
246         case SetTargetPoint:
247                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
248                 break;
249
250         case SetAngle:
251                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), "");
252                 break;
253
254         case SetFactor:
255                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"), "");
256                 break;
257
258         default:
259                 RS_DIALOGFACTORY->updateMouseWidget("", "");
260                 break;
261         }
262 }
263
264 void ActionLibraryInsert::updateMouseCursor()
265 {
266         graphicView->setMouseCursor(RS2::CadCursor);
267 }
268
269 void ActionLibraryInsert::updateToolBar()
270 {
271         if (!isFinished())
272                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
273         else
274                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
275 }
276
277 double ActionLibraryInsert::getAngle()
278 {
279         return data.angle;
280 }
281
282 void ActionLibraryInsert::setAngle(double a)
283 {
284         data.angle = a;
285 }
286
287 double ActionLibraryInsert::getFactor()
288 {
289         return data.factor;
290 }
291
292 void ActionLibraryInsert::setFactor(double f)
293 {
294         data.factor = f;
295 }