comparison OrthancStone/Sources/Scene2DViewport/ViewportController.h @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Scene2DViewport/ViewportController.h@30deba7bc8e2
children 301571299212
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21 #pragma once
22
23 #include "PredeclaredTypes.h"
24
25 #include "../Messages/IObservable.h"
26 #include "../Scene2D/Scene2D.h"
27 #include "../Scene2DViewport/IFlexiblePointerTracker.h"
28
29 #include <Compatibility.h>
30
31 #include <boost/enable_shared_from_this.hpp>
32 #include <stack>
33
34 namespace OrthancStone
35 {
36 // TODO - Move this to another file
37 class IViewportInteractor : public boost::noncopyable
38 {
39 public:
40 virtual ~IViewportInteractor()
41 {
42 }
43
44 virtual IFlexiblePointerTracker* CreateTracker(boost::shared_ptr<IViewport> viewport,
45 const PointerEvent& event,
46 unsigned int viewportWidth,
47 unsigned int viewportHeight) = 0;
48 };
49
50
51 // TODO - Move this to another file
52 class DefaultViewportInteractor : public IViewportInteractor
53 {
54 public:
55 virtual IFlexiblePointerTracker* CreateTracker(boost::shared_ptr<IViewport> viewport,
56 const PointerEvent& event,
57 unsigned int viewportWidth,
58 unsigned int viewportHeight) ORTHANC_OVERRIDE;
59 };
60
61
62 class UndoStack;
63
64 const double ARC_RADIUS_CANVAS_COORD = 30.0;
65 const double TEXT_CENTER_DISTANCE_CANVAS_COORD = 90;
66
67 const double HANDLE_SIDE_LENGTH_CANVAS_COORD = 10.0;
68 const double HIT_TEST_MAX_DISTANCE_CANVAS_COORD = 15.0;
69
70 const uint8_t TEXT_COLOR_RED = 0;
71 const uint8_t TEXT_COLOR_GREEN = 223;
72 const uint8_t TEXT_COLOR_BLUE = 81;
73
74 const uint8_t TOOL_ANGLE_LINES_COLOR_RED = 0;
75 const uint8_t TOOL_ANGLE_LINES_COLOR_GREEN = 183;
76 const uint8_t TOOL_ANGLE_LINES_COLOR_BLUE = 17;
77
78 const uint8_t TOOL_ANGLE_LINES_HL_COLOR_RED = 0;
79 const uint8_t TOOL_ANGLE_LINES_HL_COLOR_GREEN = 17;
80 const uint8_t TOOL_ANGLE_LINES_HL_COLOR_BLUE = 183;
81
82 const uint8_t TOOL_LINES_COLOR_RED = 0;
83 const uint8_t TOOL_LINES_COLOR_GREEN = 223;
84 const uint8_t TOOL_LINES_COLOR_BLUE = 21;
85
86 const uint8_t TOOL_LINES_HL_COLOR_RED = 0;
87 const uint8_t TOOL_LINES_HL_COLOR_GREEN = 21;
88 const uint8_t TOOL_LINES_HL_COLOR_BLUE = 223;
89
90 const uint8_t TEXT_OUTLINE_COLOR_RED = 0;
91 const uint8_t TEXT_OUTLINE_COLOR_GREEN = 56;
92 const uint8_t TEXT_OUTLINE_COLOR_BLUE = 21;
93
94 /**
95 This object is responsible for hosting a scene, responding to messages from
96 the model and updating the scene accordingly.
97
98 It contains the list of active measuring tools as well as the stack
99 where measuring tool commands are stored.
100
101 The active tracker is also stored in the viewport controller.
102
103 Each canvas or other GUI area where we want to display a 2D image, either
104 directly or through slicing must be assigned a ViewportController.
105 */
106 class ViewportController :
107 public IObservable,
108 public boost::enable_shared_from_this<ViewportController>
109 {
110 public:
111 ORTHANC_STONE_DEFINE_ORIGIN_MESSAGE(__FILE__, __LINE__, \
112 SceneTransformChanged, \
113 ViewportController);
114
115 ViewportController(boost::shared_ptr<IViewport> viewport);
116
117 ~ViewportController();
118
119 /**
120 This method returns the list of measure tools containing the supplied point
121 (in scene coords). A tracker can then be requested from the chosen
122 measure tool, if needed
123 */
124 std::vector<boost::shared_ptr<MeasureTool> > HitTestMeasureTools(
125 ScenePoint2D p);
126
127 /**
128 This function will traverse the measuring tools and will clear their
129 highlighted state
130 */
131 void ResetMeasuringToolsHighlight();
132
133 /**
134 With this method, the object takes ownership of the supplied tracker and
135 updates it according to user interaction
136 */
137 void AcquireActiveTracker(IFlexiblePointerTracker* tracker);
138
139 /** Forwarded to the underlying scene */
140 AffineTransform2D GetCanvasToSceneTransform() const;
141
142 /** Forwarded to the underlying scene */
143 AffineTransform2D GetSceneToCanvasTransform() const;
144
145 /** Forwarded to the underlying scene, and broadcasted to the observers */
146 void SetSceneToCanvasTransform(const AffineTransform2D& transform);
147
148 /** Forwarded to the underlying scene, and broadcasted to the observers */
149 void FitContent(unsigned int viewportWidth,
150 unsigned int viewportHeight);
151
152 /** Adds a new measure tool */
153 void AddMeasureTool(boost::shared_ptr<MeasureTool> measureTool);
154
155 /** Removes a measure tool or throws if it cannot be found */
156 void RemoveMeasureTool(boost::shared_ptr<MeasureTool> measureTool);
157
158 /**
159 The square handle side length in *scene* coordinates
160 */
161 double GetHandleSideLengthS() const;
162
163 /**
164 The angle measure too arc radius in *scene* coordinates
165
166 Note: you might wonder why this is not part of the AngleMeasureTool itself,
167 but we prefer to put all such constants in the same location, to ease
168 */
169 double GetAngleToolArcRadiusS() const;
170
171 /**
172 The hit test maximum distance in *scene* coordinates.
173 If a pointer event is less than GetHandleSideLengthS() to a GUI element,
174 the hit test for this GUI element is seen as true
175 */
176 double GetHitTestMaximumDistanceS() const;
177
178 /**
179 Distance between the top of the angle measuring tool and the center of
180 the label showing the actual measure, in *scene* coordinates
181 */
182 double GetAngleTopTextLabelDistanceS() const;
183
184
185 /** forwarded to the UndoStack */
186 void PushCommand(boost::shared_ptr<MeasureCommand> command);
187
188 /** forwarded to the UndoStack */
189 void Undo();
190
191 /** forwarded to the UndoStack */
192 void Redo();
193
194 /** forwarded to the UndoStack */
195 bool CanUndo() const;
196
197 /** forwarded to the UndoStack */
198 bool CanRedo() const;
199
200
201 // Must be expressed in canvas coordinates
202 void HandleMousePress(IViewportInteractor& interactor,
203 const PointerEvent& event,
204 unsigned int viewportWidth,
205 unsigned int viewportHeight);
206
207 // Must be expressed in canvas coordinates. Returns "true" if the
208 // state has changed, so that "Invalidate()" can be called.
209 bool HandleMouseMove(const PointerEvent& event);
210
211 // Must be expressed in canvas coordinates
212 void HandleMouseRelease(const PointerEvent& event);
213
214 const Scene2D& GetScene() const
215 {
216 return *scene_;
217 }
218
219 Scene2D& GetScene()
220 {
221 return *scene_;
222 }
223
224 /**
225 This method is used in a move pattern: when the ownership of the scene
226 managed by this viewport controller must be transferred to another
227 controller.
228 */
229 Scene2D* ReleaseScene()
230 {
231 return scene_.release();
232 }
233
234 /**
235 This method is used when one wishes to replace the scene that is currently
236 managed by the controller. The previous scene is deleted and the controller
237 now has ownership of the new one.
238 */
239 void AcquireScene(Scene2D* scene)
240 {
241 scene_.reset(scene);
242 }
243
244 /**
245 Sets the undo stack that is used by PushCommand, Undo...
246 */
247 void SetUndoStack(boost::weak_ptr<UndoStack> undoStackW)
248 {
249 undoStackW_ = undoStackW;
250 }
251
252 bool HasActiveTracker() const
253 {
254 return activeTracker_.get() != NULL;
255 }
256
257 private:
258 double GetCanvasToSceneFactor() const;
259
260 boost::shared_ptr<IViewport> viewport_;
261 boost::weak_ptr<UndoStack> undoStackW_; // Global stack, possibly shared by all viewports
262 std::vector<boost::shared_ptr<MeasureTool> > measureTools_;
263 boost::shared_ptr<IFlexiblePointerTracker> activeTracker_; // TODO - Couldn't this be a "std::unique_ptr"?
264
265 std::unique_ptr<Scene2D> scene_;
266
267 // this is cached
268 double canvasToSceneFactor_;
269 };
270 }