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