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