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