comparison OrthancStone/Sources/Deprecated/Viewport/WidgetViewport.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/Viewport/WidgetViewport.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 "WidgetViewport.h"
23
24 #include <Images/ImageProcessing.h>
25 #include <OrthancException.h>
26
27 namespace Deprecated
28 {
29 WidgetViewport::WidgetViewport() :
30 statusBar_(NULL),
31 isMouseOver_(false),
32 lastMouseX_(0),
33 lastMouseY_(0),
34 backgroundChanged_(false)
35 {
36 }
37
38
39 void WidgetViewport::FitContent()
40 {
41 if (centralWidget_.get() != NULL)
42 {
43 centralWidget_->FitContent();
44 }
45 }
46
47
48 void WidgetViewport::SetStatusBar(IStatusBar& statusBar)
49 {
50 statusBar_ = &statusBar;
51
52 if (centralWidget_.get() != NULL)
53 {
54 centralWidget_->SetStatusBar(statusBar);
55 }
56 }
57
58
59 void WidgetViewport::SetCentralWidget(boost::shared_ptr<IWidget> widget)
60 {
61 if (widget == NULL)
62 {
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
64 }
65
66 mouseTracker_.reset(NULL);
67
68 centralWidget_ = widget;
69 centralWidget_->SetViewport(*this);
70
71 if (statusBar_ != NULL)
72 {
73 centralWidget_->SetStatusBar(*statusBar_);
74 }
75
76 NotifyBackgroundChanged();
77 }
78
79
80 void WidgetViewport::NotifyBackgroundChanged()
81 {
82 backgroundChanged_ = true;
83 NotifyContentChanged();
84 }
85
86
87 void WidgetViewport::SetSize(unsigned int width,
88 unsigned int height)
89 {
90 background_.SetSize(width, height, false /* no alpha */);
91
92 if (centralWidget_.get() != NULL)
93 {
94 centralWidget_->SetSize(width, height);
95 }
96
97 NotifyBackgroundChanged();
98 }
99
100
101 bool WidgetViewport::Render(Orthanc::ImageAccessor& surface)
102 {
103 if (centralWidget_.get() == NULL)
104 {
105 return false;
106 }
107
108 Orthanc::ImageAccessor background;
109 background_.GetWriteableAccessor(background);
110
111 if (backgroundChanged_ &&
112 !centralWidget_->Render(background))
113 {
114 return false;
115 }
116
117 if (background.GetWidth() != surface.GetWidth() ||
118 background.GetHeight() != surface.GetHeight())
119 {
120 return false;
121 }
122
123 Orthanc::ImageProcessing::Convert(surface, background);
124
125 if (mouseTracker_.get() != NULL)
126 {
127 mouseTracker_->Render(surface);
128 }
129 else if (isMouseOver_)
130 {
131 centralWidget_->RenderMouseOver(surface, lastMouseX_, lastMouseY_);
132 }
133
134 return true;
135 }
136
137 void WidgetViewport::TouchStart(const std::vector<Touch>& displayTouches)
138 {
139 MouseDown(OrthancStone::MouseButton_Left, (int)displayTouches[0].x, (int)displayTouches[0].y, OrthancStone::KeyboardModifiers_None, displayTouches); // one touch is equivalent to a mouse tracker without left button -> set the mouse coordinates to the first touch coordinates
140 }
141
142 void WidgetViewport::TouchMove(const std::vector<Touch>& displayTouches)
143 {
144 MouseMove((int)displayTouches[0].x, (int)displayTouches[0].y, displayTouches); // one touch is equivalent to a mouse tracker without left button -> set the mouse coordinates to the first touch coordinates
145 }
146
147 void WidgetViewport::TouchEnd(const std::vector<Touch>& displayTouches)
148 {
149 // note: TouchEnd is not triggered when a single touch gesture ends (it is only triggered when
150 // going from 2 touches to 1 touch, ...)
151 MouseUp();
152 }
153
154 void WidgetViewport::MouseDown(OrthancStone::MouseButton button,
155 int x,
156 int y,
157 OrthancStone::KeyboardModifiers modifiers,
158 const std::vector<Touch>& displayTouches
159 )
160 {
161 lastMouseX_ = x;
162 lastMouseY_ = y;
163
164 if (centralWidget_.get() != NULL)
165 {
166 mouseTracker_.reset(centralWidget_->CreateMouseTracker(button, x, y, modifiers, displayTouches));
167 }
168 else
169 {
170 mouseTracker_.reset(NULL);
171 }
172
173 NotifyContentChanged();
174 }
175
176
177 void WidgetViewport::MouseUp()
178 {
179 if (mouseTracker_.get() != NULL)
180 {
181 mouseTracker_->MouseUp();
182 mouseTracker_.reset(NULL);
183 NotifyContentChanged();
184 }
185 }
186
187
188 void WidgetViewport::MouseMove(int x,
189 int y,
190 const std::vector<Touch>& displayTouches)
191 {
192 if (centralWidget_.get() == NULL)
193 {
194 return;
195 }
196
197 lastMouseX_ = x;
198 lastMouseY_ = y;
199
200 bool repaint = false;
201
202 if (mouseTracker_.get() != NULL)
203 {
204 mouseTracker_->MouseMove(x, y, displayTouches);
205 repaint = true;
206 }
207 else
208 {
209 repaint = centralWidget_->HasRenderMouseOver();
210 }
211
212 if (repaint)
213 {
214 // The scene must be repainted, notify the observers
215 NotifyContentChanged();
216 }
217 }
218
219
220 void WidgetViewport::MouseEnter()
221 {
222 isMouseOver_ = true;
223 NotifyContentChanged();
224 }
225
226
227 void WidgetViewport::MouseLeave()
228 {
229 isMouseOver_ = false;
230
231 if (mouseTracker_.get() != NULL)
232 {
233 mouseTracker_->MouseUp();
234 mouseTracker_.reset(NULL);
235 }
236
237 NotifyContentChanged();
238 }
239
240
241 void WidgetViewport::MouseWheel(OrthancStone::MouseWheelDirection direction,
242 int x,
243 int y,
244 OrthancStone::KeyboardModifiers modifiers)
245 {
246 if (centralWidget_.get() != NULL &&
247 mouseTracker_.get() == NULL)
248 {
249 centralWidget_->MouseWheel(direction, x, y, modifiers);
250 }
251 }
252
253
254 void WidgetViewport::KeyPressed(OrthancStone::KeyboardKeys key,
255 char keyChar,
256 OrthancStone::KeyboardModifiers modifiers)
257 {
258 if (centralWidget_.get() != NULL &&
259 mouseTracker_.get() == NULL)
260 {
261 centralWidget_->KeyPressed(key, keyChar, modifiers);
262 }
263 }
264
265
266 bool WidgetViewport::HasAnimation()
267 {
268 if (centralWidget_.get() != NULL)
269 {
270 return centralWidget_->HasAnimation();
271 }
272 else
273 {
274 return false;
275 }
276 }
277
278
279 void WidgetViewport::DoAnimation()
280 {
281 if (centralWidget_.get() != NULL)
282 {
283 centralWidget_->DoAnimation();
284 }
285 }
286 }