1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 #include "rs_actionlibraryinsert.h"
28 
29 #include <QAction>
30 #include <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_commandevent.h"
34 #include "rs_coordinateevent.h"
35 #include "rs_math.h"
36 #include "rs_preview.h"
37 
38 struct RS_ActionLibraryInsert::Points {
39 	RS_Graphic prev;
40 	RS_LibraryInsertData data;
41 };
42 
43 /**
44  * Constructor.
45  */
RS_ActionLibraryInsert(RS_EntityContainer & container,RS_GraphicView & graphicView)46 RS_ActionLibraryInsert::RS_ActionLibraryInsert(RS_EntityContainer& container,
47         RS_GraphicView& graphicView)
48         :RS_PreviewActionInterface("Library Insert",
49 						   container, graphicView)
50 		, pPoints(new Points{})
51 		,lastStatus(SetTargetPoint)
52 {
53 	actionType=RS2::ActionLibraryInsert;
54 }
55 
56 RS_ActionLibraryInsert::~RS_ActionLibraryInsert() = default;
57 
createGUIAction(RS2::ActionType,QObject *)58 QAction* RS_ActionLibraryInsert::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
59    QAction* action = new QAction(tr("Insert Library Object"), NULL);
60     return action;
61 }
62 
63 
init(int status)64 void RS_ActionLibraryInsert::init(int status) {
65     RS_PreviewActionInterface::init(status);
66 
67     reset();
68 
69 }
70 
71 
72 
setFile(const QString & file)73 void RS_ActionLibraryInsert::setFile(const QString& file) {
74 	pPoints->data.file = file;
75 
76 	if (!pPoints->prev.open(file, RS2::FormatUnknown)) {
77         RS_DIALOGFACTORY->commandMessage(tr("Cannot open file '%1'").arg(file));
78     }
79 }
80 
81 
reset()82 void RS_ActionLibraryInsert::reset() {
83 
84 	pPoints->data.insertionPoint = {};
85 	pPoints->data.factor = 1.0;
86 	pPoints->data.angle = 0.0;
87 }
88 
89 
90 
trigger()91 void RS_ActionLibraryInsert::trigger() {
92     deletePreview();
93 
94     RS_Creation creation(container, graphicView);
95 	creation.createLibraryInsert(pPoints->data);
96 
97 	graphicView->redraw(RS2::RedrawDrawing);
98 
99 }
100 
101 
mouseMoveEvent(QMouseEvent * e)102 void RS_ActionLibraryInsert::mouseMoveEvent(QMouseEvent* e) {
103     switch (getStatus()) {
104     case SetTargetPoint:
105 		pPoints->data.insertionPoint = snapPoint(e);
106 
107         //if (block) {
108         deletePreview();
109 		preview->addAllFrom(pPoints->prev);
110 		preview->move(pPoints->data.insertionPoint);
111 		preview->scale(pPoints->data.insertionPoint,
112 					   RS_Vector(pPoints->data.factor, pPoints->data.factor));
113         // unit conversion:
114         if (graphic) {
115 			double const uf = RS_Units::convert(1.0, pPoints->prev.getUnit(),
116                                           graphic->getUnit());
117 			preview->scale(pPoints->data.insertionPoint,
118 			{uf, uf});
119         }
120 		preview->rotate(pPoints->data.insertionPoint, pPoints->data.angle);
121         // too slow:
122         //RS_Creation creation(preview, NULL, false);
123         //creation.createInsert(data);
124         drawPreview();
125         //}
126         break;
127 
128     default:
129         break;
130     }
131 }
132 
133 
134 
mouseReleaseEvent(QMouseEvent * e)135 void RS_ActionLibraryInsert::mouseReleaseEvent(QMouseEvent* e) {
136     if (e->button()==Qt::LeftButton) {
137         RS_CoordinateEvent ce(snapPoint(e));
138         coordinateEvent(&ce);
139     } else if (e->button()==Qt::RightButton) {
140         init(getStatus()-1);
141     }
142 }
143 
144 
145 
coordinateEvent(RS_CoordinateEvent * e)146 void RS_ActionLibraryInsert::coordinateEvent(RS_CoordinateEvent* e) {
147     if (e==NULL) {
148         return;
149     }
150 
151 	pPoints->data.insertionPoint = e->getCoordinate();
152     trigger();
153 }
154 
155 
156 
commandEvent(RS_CommandEvent * e)157 void RS_ActionLibraryInsert::commandEvent(RS_CommandEvent* e) {
158     QString c = e->getCommand().toLower();
159 
160     if (checkCommand("help", c)) {
161         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
162                                          + getAvailableCommands().join(", "));
163         return;
164     }
165 
166     switch (getStatus()) {
167     case SetTargetPoint:
168         if (checkCommand("angle", c)) {
169             deletePreview();
170             lastStatus = (Status)getStatus();
171             setStatus(SetAngle);
172         } else if (checkCommand("factor", c)) {
173             deletePreview();
174             lastStatus = (Status)getStatus();
175             setStatus(SetFactor);
176         }
177         break;
178 
179     case SetAngle: {
180             bool ok;
181             double a = RS_Math::eval(c, &ok);
182             if (ok) {
183 				pPoints->data.angle = RS_Math::deg2rad(a);
184             } else {
185                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
186             }
187             RS_DIALOGFACTORY->requestOptions(this, true, true);
188             setStatus(lastStatus);
189         }
190         break;
191 
192     case SetFactor: {
193             bool ok;
194             double f = RS_Math::eval(c, &ok);
195             if (ok) {
196                 setFactor(f);
197             } else {
198                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
199             }
200             RS_DIALOGFACTORY->requestOptions(this, true, true);
201             setStatus(lastStatus);
202         }
203         break;
204 
205     default:
206         break;
207     }
208 }
209 
210 
211 
getAvailableCommands()212 QStringList RS_ActionLibraryInsert::getAvailableCommands() {
213     QStringList cmd;
214 
215     switch (getStatus()) {
216     case SetTargetPoint:
217         cmd += command("angle");
218         cmd += command("factor");
219         ;
220         break;
221     default:
222         break;
223     }
224 
225     return cmd;
226 }
227 
228 
showOptions()229 void RS_ActionLibraryInsert::showOptions() {
230     RS_ActionInterface::showOptions();
231 
232     RS_DIALOGFACTORY->requestOptions(this, true);
233 }
234 
235 
236 
hideOptions()237 void RS_ActionLibraryInsert::hideOptions() {
238     RS_ActionInterface::hideOptions();
239 
240     RS_DIALOGFACTORY->requestOptions(this, false);
241 }
242 
243 
updateMouseButtonHints()244 void RS_ActionLibraryInsert::updateMouseButtonHints() {
245     switch (getStatus()) {
246     case SetTargetPoint:
247         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
248                                             tr("Cancel"));
249         break;
250     case SetAngle:
251         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"),
252                                             "");
253         break;
254     case SetFactor:
255         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"),
256                                             "");
257         break;
258     default:
259 		RS_DIALOGFACTORY->updateMouseWidget();
260         break;
261     }
262 }
263 
getAngle() const264 double RS_ActionLibraryInsert::getAngle() const{
265 	return pPoints->data.angle;
266 }
267 
setAngle(double a)268 void RS_ActionLibraryInsert::setAngle(double a) {
269 	pPoints->data.angle = a;
270 }
271 
getFactor() const272 double RS_ActionLibraryInsert::getFactor() const{
273 	return pPoints->data.factor;
274 }
275 
setFactor(double f)276 void RS_ActionLibraryInsert::setFactor(double f) {
277 	pPoints->data.factor = f;
278 }
279 
updateMouseCursor()280 void RS_ActionLibraryInsert::updateMouseCursor() {
281     graphicView->setMouseCursor(RS2::CadCursor);
282 }
283 
284 // EOF
285