comparison OrthancStone/Sources/Deprecated/Widgets/WorldSceneWidget.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Deprecated/Widgets/WorldSceneWidget.cpp@30deba7bc8e2
children
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
22 #include "WorldSceneWidget.h"
23
24 #include "PanMouseTracker.h"
25 #include "ZoomMouseTracker.h"
26 #include "PanZoomMouseTracker.h"
27
28 #include <Logging.h>
29 #include <OrthancException.h>
30
31 #include <math.h>
32 #include <memory>
33 #include <cassert>
34
35 namespace Deprecated
36 {
37 // this is an adapter between a IWorldSceneMouseTracker
38 // that is tracking a mouse in scene coordinates/mm and
39 // an IMouseTracker that is tracking a mouse
40 // in screen coordinates/pixels.
41 class WorldSceneWidget::SceneMouseTracker : public IMouseTracker
42 {
43 private:
44 ViewportGeometry view_;
45 std::unique_ptr<IWorldSceneMouseTracker> tracker_;
46
47 public:
48 SceneMouseTracker(const ViewportGeometry& view,
49 IWorldSceneMouseTracker* tracker) :
50 view_(view),
51 tracker_(tracker)
52 {
53 if (tracker == NULL)
54 {
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
56 }
57 }
58
59 virtual void Render(Orthanc::ImageAccessor& target)
60 {
61 if (tracker_->HasRender())
62 {
63 OrthancStone::CairoSurface surface(target, false /* no alpha */);
64 OrthancStone::CairoContext context(surface);
65 view_.ApplyTransform(context);
66 tracker_->Render(context, view_.GetZoom());
67 }
68 }
69
70 virtual void MouseUp()
71 {
72 tracker_->MouseUp();
73 }
74
75 virtual void MouseMove(int x,
76 int y,
77 const std::vector<Touch>& displayTouches)
78 {
79 double sceneX, sceneY;
80 view_.MapPixelCenterToScene(sceneX, sceneY, x, y);
81
82 std::vector<Touch> sceneTouches;
83 for (size_t t = 0; t < displayTouches.size(); t++)
84 {
85 double sx, sy;
86
87 view_.MapPixelCenterToScene(
88 sx, sy, (int)displayTouches[t].x, (int)displayTouches[t].y);
89
90 sceneTouches.push_back(
91 Touch(static_cast<float>(sx), static_cast<float>(sy)));
92 }
93 tracker_->MouseMove(x, y, sceneX, sceneY, displayTouches, sceneTouches);
94 }
95 };
96
97
98 bool WorldSceneWidget::RenderCairo(OrthancStone::CairoContext& context)
99 {
100 view_.ApplyTransform(context);
101 return RenderScene(context, view_);
102 }
103
104
105 void WorldSceneWidget::RenderMouseOverCairo(OrthancStone::CairoContext& context,
106 int x,
107 int y)
108 {
109 ViewportGeometry view = GetView();
110 view.ApplyTransform(context);
111
112 double sceneX, sceneY;
113 view.MapPixelCenterToScene(sceneX, sceneY, x, y);
114
115 if (interactor_)
116 {
117 interactor_->MouseOver(context, *this, view, sceneX, sceneY, GetStatusBar());
118 }
119 }
120
121
122 void WorldSceneWidget::SetSceneExtent(ViewportGeometry& view)
123 {
124 view.SetSceneExtent(GetSceneExtent());
125 }
126
127
128 void WorldSceneWidget::SetSize(unsigned int width,
129 unsigned int height)
130 {
131 CairoWidget::SetSize(width, height);
132 view_.SetDisplaySize(width, height);
133 }
134
135
136 void WorldSceneWidget::SetInteractor(IWorldSceneInteractor& interactor)
137 {
138 interactor_ = &interactor;
139 }
140
141
142 void WorldSceneWidget::FitContent()
143 {
144 SetSceneExtent(view_);
145 view_.FitContent();
146
147 NotifyContentChanged();
148 }
149
150
151 void WorldSceneWidget::SetView(const ViewportGeometry& view)
152 {
153 view_ = view;
154
155 NotifyContentChanged();
156 }
157
158
159 IMouseTracker* WorldSceneWidget::CreateMouseTracker(OrthancStone::MouseButton button,
160 int x,
161 int y,
162 OrthancStone::KeyboardModifiers modifiers,
163 const std::vector<Touch>& touches)
164 {
165 double sceneX, sceneY;
166 view_.MapPixelCenterToScene(sceneX, sceneY, x, y);
167
168 // asks the Widget Interactor to provide a mouse tracker
169 std::unique_ptr<IWorldSceneMouseTracker> tracker;
170
171 if (interactor_)
172 {
173 tracker.reset(interactor_->CreateMouseTracker(*this, view_, button, modifiers, x, y, sceneX, sceneY, GetStatusBar(), touches));
174 }
175
176 if (tracker.get() != NULL)
177 {
178 return new SceneMouseTracker(view_, tracker.release());
179 }
180 else if (hasDefaultMouseEvents_)
181 {
182 if (touches.size() == 2)
183 {
184 return new SceneMouseTracker(view_, new PanZoomMouseTracker(*this, touches));
185 }
186 else
187 {
188 switch (button)
189 {
190 case OrthancStone::MouseButton_Middle:
191 return new SceneMouseTracker(view_, new PanMouseTracker(*this, x, y));
192
193 case OrthancStone::MouseButton_Right:
194 return new SceneMouseTracker(view_, new ZoomMouseTracker(*this, x, y));
195
196 default:
197 return NULL;
198 }
199 }
200 }
201 else
202 {
203 return NULL;
204 }
205 }
206
207
208 void WorldSceneWidget::MouseWheel(OrthancStone::MouseWheelDirection direction,
209 int x,
210 int y,
211 OrthancStone::KeyboardModifiers modifiers)
212 {
213 if (interactor_)
214 {
215 interactor_->MouseWheel(*this, direction, modifiers, GetStatusBar());
216 }
217 }
218
219
220 void WorldSceneWidget::KeyPressed(OrthancStone::KeyboardKeys key,
221 char keyChar,
222 OrthancStone::KeyboardModifiers modifiers)
223 {
224 if (interactor_)
225 {
226 interactor_->KeyPressed(*this, key, keyChar, modifiers, GetStatusBar());
227 }
228 }
229 }