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
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/22/2010 Added this text. :-)
15 #include "actiondrawimage.h"
17 #include "rs_commandevent.h"
18 #include "rs_creation.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
26 ActionDrawImage::ActionDrawImage(RS_EntityContainer & container, GraphicView & graphicView):
27 ActionInterface("Image", container, graphicView)
31 ActionDrawImage::~ActionDrawImage()
35 /*virtual*/ RS2::ActionType ActionDrawImage::rtti()
37 return RS2::ActionDrawImage;
40 void ActionDrawImage::init(int status)
42 ActionInterface::init(status);
46 data.file = RS_DIALOGFACTORY->requestImageOpenDialog();
47 graphicView->redraw();
49 if (!data.file.isEmpty())
51 //std::cout << "file: " << data.file << "\n";
53 img = QImage(data.file);
54 setStatus(SetTargetPoint);
60 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
64 void ActionDrawImage::reset()
66 data = RS_ImageData(0,
75 void ActionDrawImage::trigger()
81 if (!data.file.isEmpty())
83 RS_Creation creation(container, graphicView);
84 creation.createImage(data);
87 graphicView->redraw();
92 void ActionDrawImage::mouseMoveEvent(QMouseEvent * e)
97 data.insertionPoint = snapPoint(e);
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);
124 void ActionDrawImage::mouseReleaseEvent(QMouseEvent * e)
126 if (e->button() == Qt::LeftButton)
128 Vector ce(snapPoint(e));
129 coordinateEvent(&ce);
131 else if (e->button() == Qt::RightButton)
138 void ActionDrawImage::coordinateEvent(Vector * e)
143 data.insertionPoint = *e;
147 void ActionDrawImage::commandEvent(RS_CommandEvent * e)
149 QString c = e->getCommand().toLower();
151 if (checkCommand("help", c))
153 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
154 + getAvailableCommands().join(", "));
162 if (checkCommand("angle", c))
167 lastStatus = (Status)getStatus();
170 else if (checkCommand("factor", c))
175 lastStatus = (Status)getStatus();
176 setStatus(SetFactor);
183 double a = RS_Math::eval(c, &ok);
186 setAngle(RS_Math::deg2rad(a));
188 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
190 RS_DIALOGFACTORY->requestOptions(this, true, true);
191 setStatus(lastStatus);
198 double f = RS_Math::eval(c, &ok);
203 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
205 RS_DIALOGFACTORY->requestOptions(this, true, true);
206 setStatus(lastStatus);
215 QStringList ActionDrawImage::getAvailableCommands()
222 cmd += command("angle");
223 cmd += command("factor");
233 void ActionDrawImage::showOptions()
235 ActionInterface::showOptions();
237 RS_DIALOGFACTORY->requestOptions(this, true);
240 void ActionDrawImage::hideOptions()
242 ActionInterface::hideOptions();
244 RS_DIALOGFACTORY->requestOptions(this, false);
247 void ActionDrawImage::updateMouseButtonHints()
252 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
256 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), "");
260 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"), "");
264 RS_DIALOGFACTORY->updateMouseWidget("", "");
269 void ActionDrawImage::updateMouseCursor()
271 graphicView->setMouseCursor(RS2::CadCursor);
274 void ActionDrawImage::updateToolBar()
277 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
279 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
282 double ActionDrawImage::getAngle()
284 return data.uVector.angle();
287 void ActionDrawImage::setAngle(double a)
289 double l = data.uVector.magnitude();
290 data.uVector.setPolar(l, a);
291 data.vVector.setPolar(l, a + M_PI / 2);
294 double ActionDrawImage::getFactor()
296 return data.uVector.magnitude();
299 void ActionDrawImage::setFactor(double f)
301 double a = data.uVector.angle();
302 data.uVector.setPolar(f, a);
303 data.vVector.setPolar(f, a + M_PI / 2);