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 <QAction>
28 #include <QMouseEvent>
29 #include "rs_actioninfoarea.h"
30 
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_debug.h"
34 #include "rs_line.h"
35 #include "rs_infoarea.h"
36 #include "rs_graphic.h"
37 #include "rs_coordinateevent.h"
38 #include "rs_preview.h"
39 
RS_ActionInfoArea(RS_EntityContainer & container,RS_GraphicView & graphicView)40 RS_ActionInfoArea::RS_ActionInfoArea(RS_EntityContainer& container,
41                                      RS_GraphicView& graphicView)
42     :RS_PreviewActionInterface("Info Area",
43 							   container, graphicView)
44 	, ia(new RS_InfoArea{})
45 {
46 	actionType=RS2::ActionInfoArea;
47 }
48 
49 RS_ActionInfoArea::~RS_ActionInfoArea() = default;
50 
init(int status)51 void RS_ActionInfoArea::init(int status) {
52     RS_ActionInterface::init(status);
53 
54     if(status==SetFirstPoint){
55         deletePreview();
56 		ia->reset();
57     }
58 
59     //RS_DEBUG->print( "RS_ActionInfoArea::init: %d" ,status );
60 }
61 
62 
63 
trigger()64 void RS_ActionInfoArea::trigger() {
65 
66     RS_DEBUG->print("RS_ActionInfoArea::trigger()");
67     display();
68 
69     init(SetFirstPoint);
70 }
71 
72 //todo: we regenerate the whole preview, it's possible to generate needed lines only
73 /** display area circumference and preview of polygon **/
display()74 void RS_ActionInfoArea::display() {
75     deletePreview();
76 	if(ia->size() < 1) {
77         return;
78     }
79 	switch(ia->size()){
80     case 2:
81 		preview->addEntity(new RS_Line(preview.get(), ia->at(0), ia->at(1)));
82         break;
83     default:
84 		for(int i=0;i<ia->size();i++){
85 			preview->addEntity(new RS_Line(preview.get(), ia->at(i), ia->at((i+1) % ia->size())));
86 		}
87 		QString const linear = RS_Units::formatLinear(ia->getCircumference(),
88 													  graphic->getUnit(),
89 													  graphic->getLinearFormat(),
90 													  graphic->getLinearPrecision());
91 		RS_DIALOGFACTORY->commandMessage(tr("Circumference: %1").arg(linear));
92 		RS_DIALOGFACTORY->commandMessage(tr("Area: %1 %2^2")
93 										 .arg(ia->getArea())
94 										 .arg(RS_Units::unitToString(graphic->getUnit())));
95         break;
96     }
97     drawPreview();
98 }
99 
100 
mouseMoveEvent(QMouseEvent * e)101 void RS_ActionInfoArea::mouseMoveEvent(QMouseEvent* e) {
102     //RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent begin");
103 
104     RS_Vector mouse = snapPoint(e);
105     if ( getStatus()==SetNextPoint) {
106 		ia->push_back(mouse);
107         display();
108 		ia->pop_back();
109     }
110 
111     //RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent end");
112 }
113 
114 
115 
mouseReleaseEvent(QMouseEvent * e)116 void RS_ActionInfoArea::mouseReleaseEvent(QMouseEvent* e) {
117     if (e->button()==Qt::LeftButton) {
118         RS_CoordinateEvent ce(snapPoint(e));
119         coordinateEvent(&ce);
120     } else if (e->button()==Qt::RightButton) {
121 
122         init(getStatus()-1);
123     }
124 }
125 
126 
127 
coordinateEvent(RS_CoordinateEvent * e)128 void RS_ActionInfoArea::coordinateEvent(RS_CoordinateEvent* e) {
129     if (e==NULL) {
130         return;
131     }
132 
133     RS_Vector mouse = e->getCoordinate();
134 	if(ia->duplicated(mouse)) {
135 		ia->push_back(mouse);
136         RS_DIALOGFACTORY->commandMessage(tr("Closing Point: %1/%2")
137                                          .arg(mouse.x).arg(mouse.y));
138         trigger();
139         return;
140     }
141     graphicView->moveRelativeZero(mouse);
142 
143 	ia->push_back(mouse);
144     RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2")
145                                      .arg(mouse.x).arg(mouse.y));
146     switch (getStatus()) {
147     case SetFirstPoint:
148         setStatus(SetNextPoint);
149         break;
150     case SetNextPoint:
151         display();
152         break;
153 
154     default:
155         break;
156     }
157 }
158 
159 
updateMouseButtonHints()160 void RS_ActionInfoArea::updateMouseButtonHints() {
161     switch (getStatus()) {
162     case SetFirstPoint:
163         RS_DIALOGFACTORY->updateMouseWidget(
164                     tr("Specify first point of polygon"),
165                     tr("Cancel"));
166         break;
167     case SetNextPoint:
168         RS_DIALOGFACTORY->updateMouseWidget(
169                     tr("Specify next point of polygon"),
170 					tr("Cancel"));
171         break;
172     default:
173         RS_DIALOGFACTORY->updateMouseWidget();
174         break;
175     }
176 }
177 
178 
179 
updateMouseCursor()180 void RS_ActionInfoArea::updateMouseCursor() {
181     graphicView->setMouseCursor(RS2::CadCursor);
182 }
183 
184 // EOF
185