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