]> Shamusworld >> Repos - architektonas/blob - src/actions/actioninfoarea.cpp
3b877408d2fef9d1ec192651f6732c43a4ea604a
[architektonas] / src / actions / actioninfoarea.cpp
1 // 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 "actioninfoarea.h"
16
17 #include "rs_dialogfactory.h"
18 #include "graphicview.h"
19 #include "rs_preview.h"
20
21 ActionInfoArea::ActionInfoArea(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Info Area",
22                 container, graphicView)
23 {
24 }
25
26 ActionInfoArea::~ActionInfoArea()
27 {
28 }
29
30 void ActionInfoArea::init(int status)
31 {
32         ActionInterface::init(status);
33
34         currentLine = NULL;
35         closingLine = NULL;
36
37         //std::cout << "ActionInfoArea::init: " << status << "\n";
38 }
39
40 void ActionInfoArea::trigger()
41 {
42         RS_DEBUG->print("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("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 ActionInfoArea::mouseMoveEvent(QMouseEvent * e)
69 {
70         //RS_DEBUG->print("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)
89                                 {
90 //                                      preview->removeEntity(currentLine);
91                                         currentLine = NULL;
92                                 }
93
94                                 if (closingLine)
95                                 {
96 //                                      preview->removeEntity(closingLine);
97                                         closingLine = NULL;
98                                 }
99
100 //                              currentLine = new RS_Line(preview, RS_LineData(prev, mouse));
101 //                              preview->addEntity(currentLine);
102
103 //                              if (preview->count() > 1)
104 //                              {
105 //                                      closingLine = new RS_Line(preview, RS_LineData(mouse, point1));
106 //                                      preview->addEntity(closingLine);
107 //                              }
108
109                                 drawPreview();
110                         }
111                         break;
112
113                 default:
114                         break;
115                 }
116         }
117 }
118
119 void ActionInfoArea::mouseReleaseEvent(QMouseEvent * e)
120 {
121         if (e->button() == Qt::LeftButton)
122         {
123                 Vector ce(snapPoint(e));
124                 coordinateEvent(&ce);
125         }
126         else if (e->button() == Qt::RightButton)
127         {
128                 // close the polygon (preview)
129                 if (getStatus() == SetNextPoint && prev.valid)
130                 {
131                         deletePreview();
132
133                         if (currentLine != NULL)
134                         {
135 //                              preview->removeEntity(currentLine);
136                                 currentLine = NULL;
137                         }
138
139                         if (closingLine != NULL)
140                         {
141 //                              preview->removeEntity(closingLine);
142                                 closingLine = NULL;
143                         }
144
145 //                      currentLine = new RS_Line(preview, RS_LineData(prev, point1));
146 //                      preview->addEntity(currentLine);
147                         drawPreview();
148                 }
149
150                 deleteSnapper();
151                 trigger();
152                 init(getStatus() - 1);
153         }
154 }
155
156 void ActionInfoArea::coordinateEvent(Vector * e)
157 {
158         if (e == NULL)
159                 return;
160
161         Vector mouse = *e;
162
163         switch (getStatus())
164         {
165         case SetFirstPoint:
166                 point1 = mouse;
167
168                 deletePreview();
169                 clearPreview();
170
171                 ia.addPoint(mouse);
172                 RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2").arg(mouse.x).arg(mouse.y));
173
174                 graphicView->moveRelativeZero(point1);
175                 prev = mouse;
176
177                 setStatus(SetNextPoint);
178                 break;
179
180         case SetNextPoint:
181
182                 if (point1.valid)
183                 {
184                         ia.addPoint(mouse);
185                         RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2").arg(mouse.x).arg(mouse.y));
186
187                         currentLine = NULL;
188
189                         graphicView->moveRelativeZero(mouse);
190                         prev = mouse;
191
192                         // automatically detect that the polyline is now closed
193                         if (ia.isClosed())
194                         {
195                                 trigger();
196                                 setStatus(SetFirstPoint);
197                         }
198                 }
199                 break;
200
201         default:
202                 break;
203         }
204 }
205
206 void ActionInfoArea::updateMouseButtonHints()
207 {
208         switch (getStatus())
209         {
210         case SetFirstPoint:
211                 RS_DIALOGFACTORY->updateMouseWidget(
212                         tr("Specify first point of polygon"),
213                         tr("Cancel"));
214                 break;
215
216         case SetNextPoint:
217                 RS_DIALOGFACTORY->updateMouseWidget(
218                         tr("Specify next point of polygon"),
219                         tr("Terminate"));
220                 break;
221
222         default:
223                 RS_DIALOGFACTORY->updateMouseWidget("", "");
224                 break;
225         }
226 }
227
228 void ActionInfoArea::updateMouseCursor()
229 {
230         graphicView->setMouseCursor(RS2::CadCursor);
231 }
232
233 void ActionInfoArea::updateToolBar()
234 {
235         switch (getStatus())
236         {
237         case SetFirstPoint:
238         case SetNextPoint:
239                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
240                 break;
241
242         default:
243                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarInfo);
244                 break;
245         }
246 }