comparison Framework/Deprecated/Widgets/WorldSceneWidget.cpp @ 732:c35e98d22764

move Deprecated classes to a separate folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 May 2019 14:27:35 +0200
parents Framework/Widgets/WorldSceneWidget.cpp@4f2416d519b4
children f8bff27f1314
comparison
equal deleted inserted replaced
729:529189f399ec 732:c35e98d22764
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-2019 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 <Core/Logging.h>
29 #include <Core/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::auto_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::auto_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 printf("has default mouse events\n");
183 if (touches.size() == 2)
184 {
185 printf("2 touches !\n");
186 return new SceneMouseTracker(view_, new PanZoomMouseTracker(*this, touches));
187 }
188 else
189 {
190 switch (button)
191 {
192 case OrthancStone::MouseButton_Middle:
193 return new SceneMouseTracker(view_, new PanMouseTracker(*this, x, y));
194
195 case OrthancStone::MouseButton_Right:
196 return new SceneMouseTracker(view_, new ZoomMouseTracker(*this, x, y));
197
198 default:
199 return NULL;
200 }
201 }
202 }
203 else
204 {
205 return NULL;
206 }
207 }
208
209
210 void WorldSceneWidget::MouseWheel(OrthancStone::MouseWheelDirection direction,
211 int x,
212 int y,
213 OrthancStone::KeyboardModifiers modifiers)
214 {
215 if (interactor_)
216 {
217 interactor_->MouseWheel(*this, direction, modifiers, GetStatusBar());
218 }
219 }
220
221
222 void WorldSceneWidget::KeyPressed(OrthancStone::KeyboardKeys key,
223 char keyChar,
224 OrthancStone::KeyboardModifiers modifiers)
225 {
226 if (interactor_)
227 {
228 interactor_->KeyPressed(*this, key, keyChar, modifiers, GetStatusBar());
229 }
230 }
231 }