comparison Framework/Scene2DViewport/LineMeasureTool.cpp @ 751:712ff6ff3c19

- undo redo now works fine for both measure tool creation commands - added LayerHolder to streamline layer index management - added overloads for ORTHANC_ASSERT with no string message (some heavy preprocessor wizardry in there) - fixing wasm BasicScene is *not* finished.
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 22 May 2019 11:55:52 +0200
parents 28b9e3a54200
children 66ac7a2d1e3a
comparison
equal deleted inserted replaced
750:284f37dc1c66 751:712ff6ff3c19
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 #include "LineMeasureTool.h" 21 #include "LineMeasureTool.h"
22 #include "MeasureToolsToolbox.h" 22 #include "MeasureToolsToolbox.h"
23 #include "LayerHolder.h"
23 24
24 #include <Core/Logging.h> 25 #include <Core/Logging.h>
25 26
27 #include <boost/make_shared.hpp>
26 28
27 namespace OrthancStone 29 namespace OrthancStone
28 { 30 {
31
32 LineMeasureTool::LineMeasureTool(
33 MessageBroker& broker, ViewportControllerWPtr controllerW)
34 : MeasureTool(broker, controllerW)
35 , layerHolder_(boost::make_shared<LayerHolder>(controllerW, 1, 5))
36 {
37
38 }
39
29 LineMeasureTool::~LineMeasureTool() 40 LineMeasureTool::~LineMeasureTool()
30 { 41 {
31 // this measuring tool is a RABI for the corresponding visual layers 42 // this measuring tool is a RABI for the corresponding visual layers
32 // stored in the 2D scene 43 // stored in the 2D scene
33 Disable(); 44 Disable();
34 RemoveFromScene(); 45 RemoveFromScene();
35 } 46 }
36 47
37 void LineMeasureTool::RemoveFromScene() 48 void LineMeasureTool::RemoveFromScene()
38 { 49 {
39 if (layersCreated) 50 if (layerHolder_->AreLayersCreated() && IsSceneAlive())
40 { 51 {
41 assert(GetScene()->HasLayer(polylineZIndex_)); 52 layerHolder_->DeleteLayers();
42 assert(GetScene()->HasLayer(textZIndex_));
43 GetScene()->DeleteLayer(polylineZIndex_);
44 GetScene()->DeleteLayer(textZIndex_);
45 } 53 }
46 } 54 }
47 55
48
49 void LineMeasureTool::SetStart(ScenePoint2D start) 56 void LineMeasureTool::SetStart(ScenePoint2D start)
50 { 57 {
51 start_ = start; 58 start_ = start;
52 RefreshScene(); 59 RefreshScene();
53 } 60 }
63 start_ = start; 70 start_ = start;
64 end_ = end; 71 end_ = end;
65 RefreshScene(); 72 RefreshScene();
66 } 73 }
67 74
68 PolylineSceneLayer* LineMeasureTool::GetPolylineLayer()
69 {
70 assert(GetScene()->HasLayer(polylineZIndex_));
71 ISceneLayer* layer = &(GetScene()->GetLayer(polylineZIndex_));
72 PolylineSceneLayer* concreteLayer = dynamic_cast<PolylineSceneLayer*>(layer);
73 assert(concreteLayer != NULL);
74 return concreteLayer;
75 }
76
77 TextSceneLayer* LineMeasureTool::GetTextLayer()
78 {
79 assert(GetScene()->HasLayer(textZIndex_));
80 ISceneLayer* layer = &(GetScene()->GetLayer(textZIndex_));
81 TextSceneLayer* concreteLayer = dynamic_cast<TextSceneLayer*>(layer);
82 assert(concreteLayer != NULL);
83 return concreteLayer;
84 }
85
86 void LineMeasureTool::RefreshScene() 75 void LineMeasureTool::RefreshScene()
87 { 76 {
88 if (IsSceneAlive()) 77 if (IsSceneAlive())
89 { 78 {
90 if (IsEnabled()) 79 if (IsEnabled())
91 { 80 {
92 if (!layersCreated) 81 // get the scaling factor
93 { 82 const double pixelToScene =
94 // Create the layers if need be 83 GetScene()->GetCanvasToSceneTransform().ComputeZoom();
95 84
96 assert(textZIndex_ == -1); 85 layerHolder_->CreateLayersIfNeeded();
97 { 86
98 polylineZIndex_ = GetScene()->GetMaxDepth() + 100;
99 //LOG(INFO) << "set polylineZIndex_ to: " << polylineZIndex_;
100 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer());
101 GetScene()->SetLayer(polylineZIndex_, layer.release());
102 }
103 {
104 textZIndex_ = GetScene()->GetMaxDepth() + 100;
105 //LOG(INFO) << "set textZIndex_ to: " << textZIndex_;
106 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer());
107 GetScene()->SetLayer(textZIndex_, layer.release());
108 }
109 layersCreated = true;
110 }
111 else
112 {
113 assert(GetScene()->HasLayer(polylineZIndex_));
114 assert(GetScene()->HasLayer(textZIndex_));
115 }
116 { 87 {
117 // Fill the polyline layer with the measurement line 88 // Fill the polyline layer with the measurement line
118 89
119 PolylineSceneLayer* polylineLayer = GetPolylineLayer(); 90 PolylineSceneLayer* polylineLayer = layerHolder_->GetPolylineLayer(0);
120 polylineLayer->ClearAllChains(); 91 polylineLayer->ClearAllChains();
121 polylineLayer->SetColor(0, 223, 21); 92 polylineLayer->SetColor(0, 223, 21);
122 93
123 { 94 {
124 PolylineSceneLayer::Chain chain; 95 PolylineSceneLayer::Chain chain;
131 { 102 {
132 //void AddSquare(PolylineSceneLayer::Chain& chain,const Scene2D& scene,const ScenePoint2D& centerS,const double& sideLength) 103 //void AddSquare(PolylineSceneLayer::Chain& chain,const Scene2D& scene,const ScenePoint2D& centerS,const double& sideLength)
133 104
134 { 105 {
135 PolylineSceneLayer::Chain chain; 106 PolylineSceneLayer::Chain chain;
136 AddSquare(chain, *GetScene(), start_, 10.0); //TODO: take DPI into account 107
108 //TODO: take DPI into account
109 AddSquare(chain, GetScene(), start_, 10.0 * pixelToScene);
110
137 polylineLayer->AddChain(chain, true); 111 polylineLayer->AddChain(chain, true);
138 } 112 }
139 113
140 { 114 {
141 PolylineSceneLayer::Chain chain; 115 PolylineSceneLayer::Chain chain;
142 AddSquare(chain, *GetScene(), end_, 10.0); //TODO: take DPI into account 116
117 //TODO: take DPI into account
118 AddSquare(chain, GetScene(), end_, 10.0 * pixelToScene);
119
143 polylineLayer->AddChain(chain, true); 120 polylineLayer->AddChain(chain, true);
144 } 121 }
145
146 //ScenePoint2D startC = start_.Apply(GetScene()->GetSceneToCanvasTransform());
147 //double squareSize = 10.0;
148 //double startHandleLX = startC.GetX() - squareSize/2;
149 //double startHandleTY = startC.GetY() - squareSize / 2;
150 //double startHandleRX = startC.GetX() + squareSize / 2;
151 //double startHandleBY = startC.GetY() + squareSize / 2;
152 //ScenePoint2D startLTC(startHandleLX, startHandleTY);
153 //ScenePoint2D startRTC(startHandleRX, startHandleTY);
154 //ScenePoint2D startRBC(startHandleRX, startHandleBY);
155 //ScenePoint2D startLBC(startHandleLX, startHandleBY);
156
157 //ScenePoint2D startLT = startLTC.Apply(GetScene()->GetCanvasToSceneTransform());
158 //ScenePoint2D startRT = startRTC.Apply(GetScene()->GetCanvasToSceneTransform());
159 //ScenePoint2D startRB = startRBC.Apply(GetScene()->GetCanvasToSceneTransform());
160 //ScenePoint2D startLB = startLBC.Apply(GetScene()->GetCanvasToSceneTransform());
161
162 //PolylineSceneLayer::Chain chain;
163 //chain.push_back(startLT);
164 //chain.push_back(startRT);
165 //chain.push_back(startRB);
166 //chain.push_back(startLB);
167 //polylineLayer->AddChain(chain, true);
168 } 122 }
169 123
170 } 124 }
171 { 125 {
172 // Set the text layer proporeties 126 // Set the text layer proporeties
173 127
174 TextSceneLayer* textLayer = GetTextLayer();
175 double deltaX = end_.GetX() - start_.GetX(); 128 double deltaX = end_.GetX() - start_.GetX();
176 double deltaY = end_.GetY() - start_.GetY(); 129 double deltaY = end_.GetY() - start_.GetY();
177 double squareDist = deltaX * deltaX + deltaY * deltaY; 130 double squareDist = deltaX * deltaX + deltaY * deltaY;
178 double dist = sqrt(squareDist); 131 double dist = sqrt(squareDist);
179 char buf[64]; 132 char buf[64];
180 sprintf(buf, "%0.02f units", dist); 133 sprintf(buf, "%0.02f units", dist);
181 textLayer->SetText(buf);
182 textLayer->SetColor(0, 223, 21);
183 134
184 // TODO: for now we simply position the text overlay at the middle 135 // TODO: for now we simply position the text overlay at the middle
185 // of the measuring segment 136 // of the measuring segment
186 double midX = 0.5 * (end_.GetX() + start_.GetX()); 137 double midX = 0.5 * (end_.GetX() + start_.GetX());
187 double midY = 0.5 * (end_.GetY() + start_.GetY()); 138 double midY = 0.5 * (end_.GetY() + start_.GetY());
188 textLayer->SetPosition(midX, midY); 139
140 SetTextLayerOutlineProperties(
141 GetScene(), layerHolder_, buf, ScenePoint2D(midX, midY));
189 } 142 }
190 } 143 }
191 else 144 else
192 { 145 {
193 if (layersCreated) 146 RemoveFromScene();
194 {
195 RemoveFromScene();
196 layersCreated = false;
197 }
198 } 147 }
199 } 148 }
200 } 149 }
201 } 150 }