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 #include<cmath>
27 #include <QAction>
28 #include <QMouseEvent>
29 #include "rs_actiondimaligned.h"
30 #include "rs_dimaligned.h"
31 
32 #include "rs_dialogfactory.h"
33 #include "rs_line.h"
34 #include "rs_graphicview.h"
35 #include "rs_commandevent.h"
36 #include "rs_constructionline.h"
37 #include "rs_coordinateevent.h"
38 #include "rs_preview.h"
39 #include "rs_debug.h"
40 
RS_ActionDimAligned(RS_EntityContainer & container,RS_GraphicView & graphicView)41 RS_ActionDimAligned::RS_ActionDimAligned(RS_EntityContainer& container,
42         RS_GraphicView& graphicView)
43         :RS_ActionDimension("Draw aligned dimensions",
44                     container, graphicView) {
45 	actionType=RS2::ActionDimAligned;
46 	reset();
47 }
48 
49 
50 
51 RS_ActionDimAligned::~RS_ActionDimAligned()  = default;
52 
reset()53 void RS_ActionDimAligned::reset() {
54     RS_ActionDimension::reset();
55 
56 	edata.reset(new RS_DimAlignedData(RS_Vector(false),
57 							  RS_Vector(false))
58 				);
59     lastStatus = SetExtPoint1;
60 	RS_DIALOGFACTORY->requestOptions(this, true, true);
61 }
62 
63 
64 
trigger()65 void RS_ActionDimAligned::trigger() {
66     RS_ActionDimension::trigger();
67 
68     preparePreview();
69     graphicView->moveRelativeZero(data->definitionPoint);
70 
71 		//data->text = getText();
72     RS_DimAligned* dim =
73 		new RS_DimAligned(container, *data, *edata);
74     dim->setLayerToActive();
75     dim->setPenToActive();
76     dim->update();
77     container->addEntity(dim);
78 
79     // upd. undo list:
80     if (document) {
81         document->startUndoCycle();
82         document->addUndoable(dim);
83         document->endUndoCycle();
84     }
85 
86     RS_Vector rz = graphicView->getRelativeZero();
87         graphicView->redraw(RS2::RedrawDrawing);
88     graphicView->moveRelativeZero(rz);
89 
90     RS_DEBUG->print("RS_ActionDimAligned::trigger():"
91                     " dim added: %d", dim->getId());
92 }
93 
94 
95 
preparePreview()96 void RS_ActionDimAligned::preparePreview() {
97 	RS_Vector dirV = RS_Vector::polar(100.,
98 				  edata->extensionPoint1.angleTo(
99 					  edata->extensionPoint2)
100 				  +M_PI_2);
101 	RS_ConstructionLine cl(nullptr,
102                            RS_ConstructionLineData(
103 							   edata->extensionPoint2,
104 							   edata->extensionPoint2+dirV));
105 
106     data->definitionPoint =
107         cl.getNearestPointOnEntity(data->definitionPoint);
108 }
109 
110 
111 
mouseMoveEvent(QMouseEvent * e)112 void RS_ActionDimAligned::mouseMoveEvent(QMouseEvent* e) {
113     RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent begin");
114 
115     RS_Vector mouse = snapPoint(e);
116 
117     switch (getStatus()) {
118     case SetExtPoint1:
119         break;
120 
121     case SetExtPoint2:
122 		if (edata->extensionPoint1.valid) {
123             deletePreview();
124             preview->addEntity(
125 				new RS_Line{preview.get(),edata->extensionPoint1, mouse}
126             );
127             drawPreview();
128         }
129         break;
130 
131     case SetDefPoint:
132 		if (edata->extensionPoint1.valid && edata->extensionPoint2.valid) {
133             deletePreview();
134 			data->definitionPoint = mouse;
135 
136             preparePreview();
137 
138 						//data->text = getText();
139 			RS_DimAligned* dim = new RS_DimAligned(preview.get(), *data, *edata);
140             preview->addEntity(dim);
141             dim->update();
142             drawPreview();
143         }
144         break;
145 
146         default:
147                 break;
148     }
149 
150     RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent end");
151 }
152 
153 
154 
mouseReleaseEvent(QMouseEvent * e)155 void RS_ActionDimAligned::mouseReleaseEvent(QMouseEvent* e) {
156     if (e->button()==Qt::LeftButton) {
157         RS_CoordinateEvent ce(snapPoint(e));
158         coordinateEvent(&ce);
159     } else if (e->button()==Qt::RightButton) {
160         deletePreview();
161         init(getStatus()-1);
162     }
163 }
164 
165 
166 
coordinateEvent(RS_CoordinateEvent * e)167 void RS_ActionDimAligned::coordinateEvent(RS_CoordinateEvent* e) {
168 	if (!e) return;
169 
170     RS_Vector pos = e->getCoordinate();
171 
172     switch (getStatus()) {
173     case SetExtPoint1:
174 		edata->extensionPoint1 = pos;
175         graphicView->moveRelativeZero(pos);
176         setStatus(SetExtPoint2);
177         break;
178 
179     case SetExtPoint2:
180 		edata->extensionPoint2 = pos;
181         graphicView->moveRelativeZero(pos);
182         setStatus(SetDefPoint);
183         break;
184 
185     case SetDefPoint:
186 		data->definitionPoint = pos;
187         trigger();
188         reset();
189         setStatus(SetExtPoint1);
190         break;
191 
192     default:
193         break;
194     }
195 }
196 
197 
198 
commandEvent(RS_CommandEvent * e)199 void RS_ActionDimAligned::commandEvent(RS_CommandEvent* e) {
200     QString c = e->getCommand().toLower();
201 
202     if (checkCommand("help", c)) {
203 		RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
204 										 + getAvailableCommands().join(", "));
205         return;
206     }
207 
208     switch (getStatus()) {
209     case SetText: {
210             setText(c);
211 			RS_DIALOGFACTORY->requestOptions(this, true, true);
212             setStatus(lastStatus);
213             graphicView->enableCoordinateInput();
214         }
215         break;
216 
217     default:
218         if (checkCommand("text", c)) {
219             lastStatus = (Status)getStatus();
220             graphicView->disableCoordinateInput();
221             setStatus(SetText);
222         }
223         break;
224     }
225 }
226 
227 
228 
getAvailableCommands()229 QStringList RS_ActionDimAligned::getAvailableCommands() {
230     QStringList cmd;
231 
232     switch (getStatus()) {
233     case SetExtPoint1:
234     case SetExtPoint2:
235     case SetDefPoint:
236         cmd += command("text");
237         break;
238 
239     default:
240         break;
241     }
242 
243     return cmd;
244 }
245 
246 
247 
updateMouseButtonHints()248 void RS_ActionDimAligned::updateMouseButtonHints() {
249 	switch (getStatus()) {
250 	case SetExtPoint1:
251 		RS_DIALOGFACTORY->updateMouseWidget(
252 					tr("Specify first extension line origin"),
253 					tr("Cancel"));
254 		break;
255 	case SetExtPoint2:
256 		RS_DIALOGFACTORY->updateMouseWidget(
257 					tr("Specify second extension line origin"),
258 					tr("Back"));
259 		break;
260 	case SetDefPoint:
261 		RS_DIALOGFACTORY->updateMouseWidget(
262 					tr("Specify dimension line location"),
263 					tr("Back"));
264 		break;
265 	case SetText:
266 		RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
267 		break;
268 	default:
269 		RS_DIALOGFACTORY->updateMouseWidget();
270 		break;
271 	}
272 }
273 
274 
275 
hideOptions()276 void RS_ActionDimAligned::hideOptions() {
277 	RS_DIALOGFACTORY->requestOptions(this, false);
278 
279     RS_ActionDimension::hideOptions();
280 }
281 
282 
283 
showOptions()284 void RS_ActionDimAligned::showOptions() {
285     RS_ActionDimension::showOptions();
286 
287 	RS_DIALOGFACTORY->requestOptions(this, true);
288 }
289 
290 // EOF
291