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_actioninfoangle.h"
28
29 #include <QAction>
30 #include <cmath>
31 #include <QMouseEvent>
32 #include "rs_dialogfactory.h"
33 #include "rs_graphicview.h"
34 #include "rs_information.h"
35 #include "rs_graphic.h"
36 #include "rs_preview.h"
37 #include "rs_debug.h"
38
39 #ifdef EMU_C99
40 #include "emu_c99.h"
41 #endif
42
43 struct RS_ActionInfoAngle::Points {
44 RS_Vector point1;
45 RS_Vector point2;
46
47 RS_Vector intersection;
48 };
49
RS_ActionInfoAngle(RS_EntityContainer & container,RS_GraphicView & graphicView)50 RS_ActionInfoAngle::RS_ActionInfoAngle(RS_EntityContainer& container,
51 RS_GraphicView& graphicView)
52 :RS_PreviewActionInterface("Info Angle",
53 container, graphicView)
54 ,entity1(nullptr)
55 ,entity2(nullptr)
56 , pPoints(new Points{})
57 {
58 actionType=RS2::ActionInfoAngle;
59 }
60
61 RS_ActionInfoAngle::~RS_ActionInfoAngle() = default;
62
init(int status)63 void RS_ActionInfoAngle::init(int status) {
64 RS_ActionInterface::init(status);
65 }
66
trigger()67 void RS_ActionInfoAngle::trigger() {
68
69 RS_DEBUG->print("RS_ActionInfoAngle::trigger()");
70
71 if (entity1 && entity2) {
72 RS_VectorSolutions const& sol =
73 RS_Information::getIntersection(entity1, entity2, false);
74
75 if (sol.hasValid()) {
76 pPoints->intersection = sol.get(0);
77
78 if (pPoints->intersection.valid &&
79 pPoints->point1.valid &&
80 pPoints->point2.valid) {
81 double angle1 = pPoints->intersection.angleTo(pPoints->point1);
82 double angle2 = pPoints->intersection.angleTo(pPoints->point2);
83 double angle = remainder(angle2 - angle1, 2.*M_PI);
84
85 QString str = RS_Units::formatAngle(angle,
86 graphic->getAngleFormat(), graphic->getAnglePrecision());
87
88 if(angle<0.){
89 str += " or ";
90 str += RS_Units::formatAngle(angle + 2.*M_PI,
91 graphic->getAngleFormat(), graphic->getAnglePrecision());
92 }
93 RS_DIALOGFACTORY->commandMessage(tr("Angle: %1").arg(str));
94 }
95 } else {
96 RS_DIALOGFACTORY->commandMessage(tr("Lines are parallel"));
97 }
98 }
99 }
100
mouseReleaseEvent(QMouseEvent * e)101 void RS_ActionInfoAngle::mouseReleaseEvent(QMouseEvent* e) {
102 if (e->button()==Qt::LeftButton) {
103
104 RS_Vector mouse{graphicView->toGraphX(e->x()),
105 graphicView->toGraphY(e->y())};
106
107 switch (getStatus()) {
108 case SetEntity1:
109 entity1 = catchEntity(e, RS2::ResolveAll);
110 if (entity1 && entity1->rtti()==RS2::EntityLine) {
111 pPoints->point1 = entity1->getNearestPointOnEntity(mouse);
112 setStatus(SetEntity2);
113 }
114 break;
115
116 case SetEntity2:
117 entity2 = catchEntity(e, RS2::ResolveAll);
118 if (entity2 && entity2->rtti()==RS2::EntityLine) {
119 pPoints->point2 = entity2->getNearestPointOnEntity(mouse);
120 trigger();
121 setStatus(SetEntity1);
122 }
123 break;
124
125 default:
126 break;
127 }
128 } else if (e->button()==Qt::RightButton) {
129 deletePreview();
130 init(getStatus()-1);
131 }
132 }
133
updateMouseButtonHints()134 void RS_ActionInfoAngle::updateMouseButtonHints() {
135 switch (getStatus()) {
136 case SetEntity1:
137 RS_DIALOGFACTORY->updateMouseWidget(
138 tr("Specify first line"),
139 tr("Cancel"));
140 break;
141 case SetEntity2:
142 RS_DIALOGFACTORY->updateMouseWidget(
143 tr("Specify second line"),
144 tr("Back"));
145 break;
146 default:
147 RS_DIALOGFACTORY->updateMouseWidget();
148 break;
149 }
150 }
151
updateMouseCursor()152 void RS_ActionInfoAngle::updateMouseCursor() {
153 graphicView->setMouseCursor(RS2::SelectCursor);
154 }
155
156 // EOF
157