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