]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawimage.cpp
b30d98e0d85f8e09d3dcddf6129d2ee9304a225e
[architektonas] / src / actions / actiondrawimage.cpp
1 // actiondrawimage.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  05/22/2010  Added this text. :-)
13 //
14
15 #include "actiondrawimage.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_creation.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
22
23 /**
24  * Constructor.
25  */
26 ActionDrawImage::ActionDrawImage(RS_EntityContainer & container, GraphicView & graphicView):
27         ActionInterface("Image", container, graphicView)
28 {
29 }
30
31 ActionDrawImage::~ActionDrawImage()
32 {
33 }
34
35 /*virtual*/ RS2::ActionType ActionDrawImage::rtti()
36 {
37         return RS2::ActionDrawImage;
38 }
39
40 void ActionDrawImage::init(int status)
41 {
42         ActionInterface::init(status);
43
44         reset();
45
46         data.file = RS_DIALOGFACTORY->requestImageOpenDialog();
47         graphicView->redraw();
48
49         if (!data.file.isEmpty())
50         {
51                 //std::cout << "file: " << data.file << "\n";
52
53                 img = QImage(data.file);
54                 setStatus(SetTargetPoint);
55         }
56         else
57         {
58                 finish();
59                 updateToolBar();
60                 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
61         }
62 }
63
64 void ActionDrawImage::reset()
65 {
66         data = RS_ImageData(0,
67                         Vector(0.0, 0.0),
68                         Vector(1.0, 0.0),
69                         Vector(0.0, 1.0),
70                         Vector(1.0, 1.0),
71                         "",
72                         50, 50, 0);
73 }
74
75 void ActionDrawImage::trigger()
76 {
77         deleteSnapper();
78         deletePreview();
79         clearPreview();
80
81         if (!data.file.isEmpty())
82         {
83                 RS_Creation creation(container, graphicView);
84                 creation.createImage(data);
85         }
86
87         graphicView->redraw();
88         finish();
89         updateToolBar();
90 }
91
92 void ActionDrawImage::mouseMoveEvent(QMouseEvent * e)
93 {
94         switch (getStatus())
95         {
96         case SetTargetPoint:
97                 data.insertionPoint = snapPoint(e);
98
99 //              deletePreview();
100 //              clearPreview();
101 //              RS_Line * line;
102 //              line = new RS_Line(preview, RS_LineData(Vector(0, 0), Vector(img.width(), 0)));
103 //              preview->addEntity(line);
104 //              line = new RS_Line(preview,
105 //                      RS_LineData(Vector(img.width(), 0), Vector(img.width(), img.height())));
106 //              preview->addEntity(line);
107 //              line = new RS_Line(preview,
108 //                      RS_LineData(Vector(img.width(), img.height()), Vector(0, img.height())));
109 //              preview->addEntity(line);
110 //              line = new RS_Line(preview, RS_LineData(Vector(0, img.height()), Vector(0, 0)));
111 //              preview->addEntity(line);
112 //              preview->scale(Vector(0, 0), Vector(data.uVector.magnitude(), data.uVector.magnitude()));
113 //              preview->rotate(Vector(0, 0), data.uVector.angle());
114 //              preview->move(data.insertionPoint);
115 //
116 //              drawPreview();
117                 break;
118
119         default:
120                 break;
121         }
122 }
123
124 void ActionDrawImage::mouseReleaseEvent(QMouseEvent * e)
125 {
126         if (e->button() == Qt::LeftButton)
127         {
128                 Vector ce(snapPoint(e));
129                 coordinateEvent(&ce);
130         }
131         else if (e->button() == Qt::RightButton)
132         {
133                 deleteSnapper();
134                 finish();
135         }
136 }
137
138 void ActionDrawImage::coordinateEvent(Vector * e)
139 {
140         if (!e)
141                 return;
142
143         data.insertionPoint = *e;
144         trigger();
145 }
146
147 void ActionDrawImage::commandEvent(RS_CommandEvent * e)
148 {
149         QString c = e->getCommand().toLower();
150
151         if (checkCommand("help", c))
152         {
153                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
154                         + 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)
186                         setAngle(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)
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 ActionDrawImage::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 ActionDrawImage::showOptions()
234 {
235         ActionInterface::showOptions();
236
237         RS_DIALOGFACTORY->requestOptions(this, true);
238 }
239
240 void ActionDrawImage::hideOptions()
241 {
242         ActionInterface::hideOptions();
243
244         RS_DIALOGFACTORY->requestOptions(this, false);
245 }
246
247 void ActionDrawImage::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 ActionDrawImage::updateMouseCursor()
270 {
271         graphicView->setMouseCursor(RS2::CadCursor);
272 }
273
274 void ActionDrawImage::updateToolBar()
275 {
276         if (!isFinished())
277                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
278         else
279                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
280 }
281
282 double ActionDrawImage::getAngle()
283 {
284         return data.uVector.angle();
285 }
286
287 void ActionDrawImage::setAngle(double a)
288 {
289         double l = data.uVector.magnitude();
290         data.uVector.setPolar(l, a);
291         data.vVector.setPolar(l, a + M_PI / 2);
292 }
293
294 double ActionDrawImage::getFactor()
295 {
296         return data.uVector.magnitude();
297 }
298
299 void ActionDrawImage::setFactor(double f)
300 {
301         double a = data.uVector.angle();
302         data.uVector.setPolar(f, a);
303         data.vVector.setPolar(f, a + M_PI / 2);
304 }
305