]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawimage.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawimage.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawimage.cpp 1134 2004-07-13 23:26:13Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actiondrawimage.h"
28
29 #include "rs_creation.h"
30 #include "commands.h"
31 #include "rs_modification.h"
32
33 /**
34  * Constructor.
35  */
36 RS_ActionDrawImage::RS_ActionDrawImage(RS_EntityContainer & container,
37         RS_GraphicView & graphicView):
38         RS_PreviewActionInterface("Image", container, graphicView)
39 {
40 }
41
42 RS_ActionDrawImage::~RS_ActionDrawImage()
43 {
44 }
45
46 QAction * RS_ActionDrawImage::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
47 {
48         QAction * action = new QAction(tr("&Image"), 0);
49 //      QAction* action = new QAction(tr("Image"), tr("&Image"),
50 //                                                      QKeySequence(), NULL);
51         action->setStatusTip(tr("Insert Image (Bitmap)"));
52         return action;
53 }
54
55 void RS_ActionDrawImage::init(int status)
56 {
57         RS_PreviewActionInterface::init(status);
58
59         reset();
60
61         data.file = RS_DIALOGFACTORY->requestImageOpenDialog();
62         graphicView->redraw();
63
64         if (!data.file.isEmpty())
65         {
66                 //std::cout << "file: " << data.file << "\n";
67
68                 img = QImage(data.file);
69                 setStatus(SetTargetPoint);
70         }
71         else
72         {
73                 finish();
74                 updateToolBar();
75                 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
76         }
77 }
78
79 void RS_ActionDrawImage::reset()
80 {
81         data = RS_ImageData(0,
82                 Vector(0.0,0.0),
83                 Vector(1.0,0.0),
84                 Vector(0.0,1.0),
85                 Vector(1.0,1.0),
86                 "",
87                 50, 50, 0);
88 }
89
90 void RS_ActionDrawImage::trigger()
91 {
92         deleteSnapper();
93         deletePreview();
94         clearPreview();
95
96         if (!data.file.isEmpty())
97         {
98                 RS_Creation creation(container, graphicView);
99                 creation.createImage(data);
100         }
101
102         graphicView->redraw();
103         //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
104         finish();
105         updateToolBar();
106 }
107
108 void RS_ActionDrawImage::mouseMoveEvent(QMouseEvent * e)
109 {
110         switch (getStatus())
111         {
112         case SetTargetPoint:
113                 data.insertionPoint = snapPoint(e);
114
115                 deletePreview();
116                 clearPreview();
117                 //RS_Creation creation(preview, NULL, false);
118                 //creation.createInsert(data);
119                 RS_Line * line;
120                 line = new RS_Line(preview, RS_LineData(Vector(0, 0), Vector(img.width(), 0)));
121                 preview->addEntity(line);
122                 line = new RS_Line(preview,
123                         RS_LineData(Vector(img.width(), 0), Vector(img.width(), img.height())));
124                 preview->addEntity(line);
125                 line = new RS_Line(preview,
126                         RS_LineData(Vector(img.width(), img.height()), Vector(0, img.height())));
127                 preview->addEntity(line);
128                 line = new RS_Line(preview, RS_LineData(Vector(0, img.height()), Vector(0, 0)));
129                 preview->addEntity(line);
130                 preview->scale(Vector(0,0), Vector(data.uVector.magnitude(), data.uVector.magnitude()));
131                 preview->rotate(Vector(0,0), data.uVector.angle());
132                 preview->move(data.insertionPoint);
133
134                 drawPreview();
135                 break;
136
137         default:
138                 break;
139         }
140 }
141
142 void RS_ActionDrawImage::mouseReleaseEvent(QMouseEvent * e)
143 {
144         if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
145         {
146                 RS_CoordinateEvent ce(snapPoint(e));
147                 coordinateEvent(&ce);
148         }
149         else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
150         {
151                 deleteSnapper();
152                 //init(getStatus()-1);
153                 finish();
154         }
155 }
156
157 void RS_ActionDrawImage::coordinateEvent(RS_CoordinateEvent * e)
158 {
159         if (e == NULL)
160                 return;
161
162         data.insertionPoint = e->getCoordinate();
163         trigger();
164 }
165
166 void RS_ActionDrawImage::commandEvent(RS_CommandEvent * e)
167 {
168         QString c = e->getCommand().toLower();
169
170         if (checkCommand("help", c))
171         {
172                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
173                         + getAvailableCommands().join(", "));
174                 return;
175         }
176
177         switch (getStatus())
178         {
179         case SetTargetPoint:
180                 if (checkCommand("angle", c))
181                 {
182                         deleteSnapper();
183                         deletePreview();
184                         clearPreview();
185                         lastStatus = (Status)getStatus();
186                         setStatus(SetAngle);
187                 }
188                 else if (checkCommand("factor", c))
189                 {
190                         deleteSnapper();
191                         deletePreview();
192                         clearPreview();
193                         lastStatus = (Status)getStatus();
194                         setStatus(SetFactor);
195                 }
196                 break;
197
198         case SetAngle:
199         {
200                         bool ok;
201                         double a = RS_Math::eval(c, &ok);
202                         if (ok==true) {
203                                 setAngle(RS_Math::deg2rad(a));
204                         } else {
205                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
206                         }
207                         RS_DIALOGFACTORY->requestOptions(this, true, true);
208                         setStatus(lastStatus);
209         }
210                 break;
211
212         case SetFactor:
213         {
214                         bool ok;
215                         double f = RS_Math::eval(c, &ok);
216                         if (ok==true) {
217                                 setFactor(f);
218                         } else {
219                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
220                         }
221                         RS_DIALOGFACTORY->requestOptions(this, true, true);
222                         setStatus(lastStatus);
223         }
224                 break;
225
226         default:
227                 break;
228         }
229 }
230
231 QStringList RS_ActionDrawImage::getAvailableCommands()
232 {
233         QStringList cmd;
234
235         switch (getStatus())
236         {
237         case SetTargetPoint:
238                 cmd += command("angle");
239                 cmd += command("factor");
240                 break;
241         default:
242                 break;
243         }
244
245         return cmd;
246 }
247
248 void RS_ActionDrawImage::showOptions()
249 {
250         RS_ActionInterface::showOptions();
251
252         RS_DIALOGFACTORY->requestOptions(this, true);
253 }
254
255 void RS_ActionDrawImage::hideOptions()
256 {
257         RS_ActionInterface::hideOptions();
258
259         RS_DIALOGFACTORY->requestOptions(this, false);
260 }
261
262 void RS_ActionDrawImage::updateMouseButtonHints()
263 {
264         switch (getStatus())
265         {
266         case SetTargetPoint:
267                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"), tr("Cancel"));
268                 break;
269         case SetAngle:
270                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), "");
271                 break;
272         case SetFactor:
273                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"), "");
274                 break;
275         default:
276                 RS_DIALOGFACTORY->updateMouseWidget("", "");
277                 break;
278         }
279 }
280
281 void RS_ActionDrawImage::updateMouseCursor()
282 {
283         graphicView->setMouseCursor(RS2::CadCursor);
284 }
285
286 void RS_ActionDrawImage::updateToolBar()
287 {
288         if (!isFinished()) {
289                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
290         } else {
291                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
292         }
293 }