]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actioninfoarea.cpp
Initial import
[architektonas] / src / actions / rs_actioninfoarea.cpp
1 /****************************************************************************
2 ** $Id: rs_actioninfoarea.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_actioninfoarea.h"
28
29 #include "rs_snapper.h"
30
31
32
33 RS_ActionInfoArea::RS_ActionInfoArea(RS_EntityContainer& container,
34                                      RS_GraphicView& graphicView)
35         :RS_PreviewActionInterface("Info Area",
36                            container, graphicView) {}
37
38
39 QAction* RS_ActionInfoArea::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
40 {
41         QAction * action = new QAction(tr("&Polygonal Area"), 0);
42 //      QAction* action = new QAction(tr("Polygonal Area"),
43 //                                                                      tr("&Polygonal Area"),
44 //                                                                      QKeySequence(), NULL);
45         action->setStatusTip(tr("Measures the area of a polygon"));
46         return action;
47 }
48
49
50 void RS_ActionInfoArea::init(int status) {
51     RS_ActionInterface::init(status);
52
53     currentLine = NULL;
54     closingLine = NULL;
55
56     //std::cout << "RS_ActionInfoArea::init: " << status << "\n";
57 }
58
59
60
61 void RS_ActionInfoArea::trigger() {
62
63     RS_DEBUG->print("RS_ActionInfoArea::trigger()");
64     if (ia.isValid()) {
65         ia.close();
66         ia.calculate();
67         double area = ia.getArea();
68         double circ = ia.getCircumference();
69
70         RS_DEBUG->print("RS_ActionInfoArea::trigger: area: %f", area);
71         RS_DIALOGFACTORY->commandMessage(tr("Area: %1").arg(area));
72         RS_DIALOGFACTORY->commandMessage(tr("Circumference: %1").arg(circ));
73     }
74
75     ia.reset();
76
77     /*
78     if (point1.valid && point2.valid) {
79         double dist = point1.distanceTo(point2);
80         QString str;
81         str.sprintf("%.6f", dist);
82         RS_DIALOGFACTORY->commandMessage(tr("Distance: %1").arg(str));
83 }
84     */
85 }
86
87
88
89 void RS_ActionInfoArea::mouseMoveEvent(QMouseEvent* e) {
90     //RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent begin");
91
92     if (getStatus()==SetFirstPoint ||
93             getStatus()==SetNextPoint) {
94
95         Vector mouse = snapPoint(e);
96
97         switch (getStatus()) {
98         case SetFirstPoint:
99             break;
100
101         case SetNextPoint:
102             if (prev.valid) {
103                 deletePreview();
104                 if (currentLine!=NULL) {
105                     preview->removeEntity(currentLine);
106                     currentLine = NULL;
107                 }
108                 if (closingLine!=NULL) {
109                     preview->removeEntity(closingLine);
110                     closingLine = NULL;
111                 }
112
113                 currentLine = new RS_Line(preview,
114                                           RS_LineData(prev,
115                                                       mouse));
116                 preview->addEntity(currentLine);
117
118                                 if (preview->count()>1) {
119                         closingLine = new RS_Line(preview,
120                                           RS_LineData(mouse,
121                                                       point1));
122
123                         preview->addEntity(closingLine);
124                                 }
125
126                 drawPreview();
127             }
128             break;
129
130         default:
131             break;
132         }
133     }
134
135     //RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent end");
136 }
137
138
139
140 void RS_ActionInfoArea::mouseReleaseEvent(QMouseEvent* e) {
141     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
142         RS_CoordinateEvent ce(snapPoint(e));
143         coordinateEvent(&ce);
144     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
145         //deletePreview();
146         //clearPreview();
147
148         // close the polygon (preview)
149         if (getStatus()==SetNextPoint && prev.valid) {
150             deletePreview();
151
152             if (currentLine!=NULL) {
153                 preview->removeEntity(currentLine);
154                 currentLine = NULL;
155             }
156             if (closingLine!=NULL) {
157                 preview->removeEntity(closingLine);
158                 closingLine = NULL;
159             }
160
161             currentLine = new RS_Line(preview,
162                                       RS_LineData(prev,
163                                                   point1));
164
165             preview->addEntity(currentLine);
166
167             drawPreview();
168         }
169
170         deleteSnapper();
171         trigger();
172         init(getStatus()-1);
173     }
174 }
175
176
177
178 void RS_ActionInfoArea::coordinateEvent(RS_CoordinateEvent* e) {
179     if (e==NULL) {
180         return;
181     }
182
183     Vector mouse = e->getCoordinate();
184
185     switch (getStatus()) {
186     case SetFirstPoint:
187         point1 = mouse;
188
189         deletePreview();
190         clearPreview();
191
192         ia.addPoint(mouse);
193         RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2")
194                                          .arg(mouse.x).arg(mouse.y));
195
196         graphicView->moveRelativeZero(point1);
197         prev = mouse;
198
199         setStatus(SetNextPoint);
200         break;
201
202     case SetNextPoint:
203         if (point1.valid) {
204             //point2 = mouse;
205             /*deletePreview();
206             clearPreview();
207             */
208             ia.addPoint(mouse);
209             RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2")
210                                              .arg(mouse.x).arg(mouse.y));
211
212             currentLine = NULL;
213
214             graphicView->moveRelativeZero(mouse);
215             prev = mouse;
216
217             // automatically detect that the polyline is now closed
218             if (ia.isClosed()) {
219                 trigger();
220                 setStatus(SetFirstPoint);
221             }
222             //trigger();
223             //setStatus(SetFirstPoint);
224         }
225         break;
226
227     default:
228         break;
229     }
230 }
231
232
233 void RS_ActionInfoArea::updateMouseButtonHints() {
234     switch (getStatus()) {
235     case SetFirstPoint:
236         RS_DIALOGFACTORY->updateMouseWidget(
237             tr("Specify first point of polygon"),
238             tr("Cancel"));
239         break;
240     case SetNextPoint:
241         RS_DIALOGFACTORY->updateMouseWidget(
242             tr("Specify next point of polygon"),
243             tr("Terminate"));
244         break;
245     default:
246         RS_DIALOGFACTORY->updateMouseWidget("", "");
247         break;
248     }
249 }
250
251
252
253 void RS_ActionInfoArea::updateMouseCursor() {
254     graphicView->setMouseCursor(RS2::CadCursor);
255 }
256
257
258
259 void RS_ActionInfoArea::updateToolBar() {
260     switch (getStatus()) {
261     case SetFirstPoint:
262     case SetNextPoint:
263         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
264         break;
265     default:
266         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarInfo);
267         break;
268     }
269 }
270
271
272 // EOF