Mercurial > hg > orthanc-stone
comparison Framework/Widgets/LayoutWidget.cpp @ 0:351ab0da0150
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Oct 2016 15:34:11 +0200 |
parents | |
children | ff1e935768e7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:351ab0da0150 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "LayoutWidget.h" | |
34 | |
35 #include "../Orthanc/Core/Logging.h" | |
36 #include "../Orthanc/Core/OrthancException.h" | |
37 | |
38 #include <boost/math/special_functions/round.hpp> | |
39 | |
40 namespace OrthancStone | |
41 { | |
42 class LayoutWidget::LayoutMouseTracker : public IMouseTracker | |
43 { | |
44 private: | |
45 std::auto_ptr<IMouseTracker> tracker_; | |
46 int left_; | |
47 int top_; | |
48 unsigned int width_; | |
49 unsigned int height_; | |
50 | |
51 public: | |
52 LayoutMouseTracker(IMouseTracker* tracker, | |
53 int left, | |
54 int top, | |
55 unsigned int width, | |
56 unsigned int height) : | |
57 tracker_(tracker), | |
58 left_(left), | |
59 top_(top), | |
60 width_(width), | |
61 height_(height) | |
62 { | |
63 if (tracker == NULL) | |
64 { | |
65 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
66 } | |
67 } | |
68 | |
69 virtual void Render(Orthanc::ImageAccessor& surface) | |
70 { | |
71 Orthanc::ImageAccessor accessor = surface.GetRegion(left_, top_, width_, height_); | |
72 tracker_->Render(accessor); | |
73 } | |
74 | |
75 virtual void MouseUp() | |
76 { | |
77 tracker_->MouseUp(); | |
78 } | |
79 | |
80 virtual void MouseMove(int x, | |
81 int y) | |
82 { | |
83 tracker_->MouseMove(x - left_, y - top_); | |
84 } | |
85 }; | |
86 | |
87 | |
88 class LayoutWidget::ChildWidget : public boost::noncopyable | |
89 { | |
90 private: | |
91 std::auto_ptr<IWidget> widget_; | |
92 int left_; | |
93 int top_; | |
94 unsigned int width_; | |
95 unsigned int height_; | |
96 | |
97 public: | |
98 ChildWidget(IWidget* widget) : | |
99 widget_(widget) | |
100 { | |
101 assert(widget != NULL); | |
102 SetEmpty(); | |
103 } | |
104 | |
105 IWidget& GetWidget() const | |
106 { | |
107 return *widget_; | |
108 } | |
109 | |
110 void SetRectangle(unsigned int left, | |
111 unsigned int top, | |
112 unsigned int width, | |
113 unsigned int height) | |
114 { | |
115 left_ = left; | |
116 top_ = top; | |
117 width_ = width; | |
118 height_ = height; | |
119 | |
120 widget_->SetSize(width, height); | |
121 } | |
122 | |
123 void SetEmpty() | |
124 { | |
125 SetRectangle(0, 0, 0, 0); | |
126 } | |
127 | |
128 bool Contains(int x, | |
129 int y) const | |
130 { | |
131 return (x >= left_ && | |
132 y >= top_ && | |
133 x < left_ + static_cast<int>(width_) && | |
134 y < top_ + static_cast<int>(height_)); | |
135 } | |
136 | |
137 bool Render(Orthanc::ImageAccessor& target) | |
138 { | |
139 if (width_ == 0 || | |
140 height_ == 0) | |
141 { | |
142 return true; | |
143 } | |
144 else | |
145 { | |
146 Orthanc::ImageAccessor accessor = target.GetRegion(left_, top_, width_, height_); | |
147 return widget_->Render(accessor); | |
148 } | |
149 } | |
150 | |
151 IMouseTracker* CreateMouseTracker(MouseButton button, | |
152 int x, | |
153 int y, | |
154 KeyboardModifiers modifiers) | |
155 { | |
156 if (Contains(x, y)) | |
157 { | |
158 IMouseTracker* tracker = widget_->CreateMouseTracker(button, | |
159 x - left_, | |
160 y - top_, | |
161 modifiers); | |
162 if (tracker) | |
163 { | |
164 return new LayoutMouseTracker(tracker, left_, top_, width_, height_); | |
165 } | |
166 } | |
167 | |
168 return NULL; | |
169 } | |
170 | |
171 void RenderMouseOver(Orthanc::ImageAccessor& target, | |
172 int x, | |
173 int y) | |
174 { | |
175 if (Contains(x, y)) | |
176 { | |
177 Orthanc::ImageAccessor accessor = target.GetRegion(left_, top_, width_, height_); | |
178 | |
179 widget_->RenderMouseOver(accessor, x - left_, y - top_); | |
180 } | |
181 } | |
182 | |
183 void MouseWheel(MouseWheelDirection direction, | |
184 int x, | |
185 int y, | |
186 KeyboardModifiers modifiers) | |
187 { | |
188 if (Contains(x, y)) | |
189 { | |
190 widget_->MouseWheel(direction, x - left_, y - top_, modifiers); | |
191 } | |
192 } | |
193 }; | |
194 | |
195 | |
196 void LayoutWidget::ComputeChildrenExtents() | |
197 { | |
198 if (children_.size() == 0) | |
199 { | |
200 return; | |
201 } | |
202 | |
203 float internal = static_cast<float>(paddingInternal_); | |
204 | |
205 if (width_ <= paddingLeft_ + paddingRight_ || | |
206 height_ <= paddingTop_ + paddingBottom_) | |
207 { | |
208 for (size_t i = 0; i < children_.size(); i++) | |
209 { | |
210 children_[i]->SetEmpty(); | |
211 } | |
212 } | |
213 else if (isHorizontal_) | |
214 { | |
215 unsigned int padding = paddingLeft_ + paddingRight_ + (children_.size() - 1) * paddingInternal_; | |
216 float childWidth = ((static_cast<float>(width_) - static_cast<float>(padding)) / | |
217 static_cast<float>(children_.size())); | |
218 | |
219 for (size_t i = 0; i < children_.size(); i++) | |
220 { | |
221 float left = static_cast<float>(paddingLeft_) + static_cast<float>(i) * (childWidth + internal); | |
222 float right = left + childWidth; | |
223 | |
224 if (left >= right) | |
225 { | |
226 children_[i]->SetEmpty(); | |
227 } | |
228 else | |
229 { | |
230 children_[i]->SetRectangle(static_cast<unsigned int>(left), | |
231 paddingTop_, | |
232 boost::math::iround(right - left), | |
233 height_ - paddingTop_ - paddingBottom_); | |
234 } | |
235 } | |
236 } | |
237 else | |
238 { | |
239 unsigned int padding = paddingTop_ + paddingBottom_ + (children_.size() - 1) * paddingInternal_; | |
240 float childHeight = ((static_cast<float>(height_) - static_cast<float>(padding)) / | |
241 static_cast<float>(children_.size())); | |
242 | |
243 for (size_t i = 0; i < children_.size(); i++) | |
244 { | |
245 float top = static_cast<float>(paddingTop_) + static_cast<float>(i) * (childHeight + internal); | |
246 float bottom = top + childHeight; | |
247 | |
248 if (top >= bottom) | |
249 { | |
250 children_[i]->SetEmpty(); | |
251 } | |
252 else | |
253 { | |
254 children_[i]->SetRectangle(paddingTop_, | |
255 static_cast<unsigned int>(top), | |
256 width_ - paddingLeft_ - paddingRight_, | |
257 boost::math::iround(bottom - top)); | |
258 } | |
259 } | |
260 } | |
261 | |
262 NotifyChange(*this); | |
263 } | |
264 | |
265 | |
266 void LayoutWidget::UpdateStep() | |
267 { | |
268 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
269 } | |
270 | |
271 | |
272 LayoutWidget::LayoutWidget() : | |
273 isHorizontal_(true), | |
274 started_(false), | |
275 width_(0), | |
276 height_(0), | |
277 paddingLeft_(0), | |
278 paddingTop_(0), | |
279 paddingRight_(0), | |
280 paddingBottom_(0), | |
281 paddingInternal_(0) | |
282 { | |
283 } | |
284 | |
285 | |
286 LayoutWidget::~LayoutWidget() | |
287 { | |
288 for (size_t i = 0; i < children_.size(); i++) | |
289 { | |
290 children_[i]->GetWidget().Unregister(*this); | |
291 delete children_[i]; | |
292 } | |
293 } | |
294 | |
295 | |
296 void LayoutWidget::NotifyChange(const IWidget& widget) | |
297 { | |
298 // One of the children has changed | |
299 WidgetBase::NotifyChange(); | |
300 } | |
301 | |
302 | |
303 void LayoutWidget::SetHorizontal() | |
304 { | |
305 isHorizontal_ = true; | |
306 ComputeChildrenExtents(); | |
307 } | |
308 | |
309 | |
310 void LayoutWidget::SetVertical() | |
311 { | |
312 isHorizontal_ = false; | |
313 ComputeChildrenExtents(); | |
314 } | |
315 | |
316 | |
317 void LayoutWidget::SetPadding(unsigned int left, | |
318 unsigned int top, | |
319 unsigned int right, | |
320 unsigned int bottom, | |
321 unsigned int spacing) | |
322 { | |
323 paddingLeft_ = left; | |
324 paddingTop_ = top; | |
325 paddingRight_ = right; | |
326 paddingBottom_ = bottom; | |
327 paddingInternal_ = spacing; | |
328 } | |
329 | |
330 | |
331 void LayoutWidget::SetPadding(unsigned int padding) | |
332 { | |
333 paddingLeft_ = padding; | |
334 paddingTop_ = padding; | |
335 paddingRight_ = padding; | |
336 paddingBottom_ = padding; | |
337 paddingInternal_ = padding; | |
338 } | |
339 | |
340 | |
341 IWidget& LayoutWidget::AddWidget(IWidget* widget) // Takes ownership | |
342 { | |
343 if (started_) | |
344 { | |
345 LOG(ERROR) << "Cannot add child once Start() has been invoked"; | |
346 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
347 } | |
348 | |
349 if (widget == NULL) | |
350 { | |
351 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
352 } | |
353 | |
354 if (GetStatusBar() != NULL) | |
355 { | |
356 widget->SetStatusBar(*GetStatusBar()); | |
357 } | |
358 else | |
359 { | |
360 widget->ResetStatusBar(); | |
361 } | |
362 | |
363 children_.push_back(new ChildWidget(widget)); | |
364 widget->Register(*this); | |
365 | |
366 ComputeChildrenExtents(); | |
367 | |
368 return *widget; | |
369 } | |
370 | |
371 | |
372 void LayoutWidget::SetStatusBar(IStatusBar& statusBar) | |
373 { | |
374 WidgetBase::SetStatusBar(statusBar); | |
375 | |
376 for (size_t i = 0; i < children_.size(); i++) | |
377 { | |
378 children_[i]->GetWidget().SetStatusBar(statusBar); | |
379 } | |
380 } | |
381 | |
382 | |
383 void LayoutWidget::ResetStatusBar() | |
384 { | |
385 WidgetBase::ResetStatusBar(); | |
386 | |
387 for (size_t i = 0; i < children_.size(); i++) | |
388 { | |
389 children_[i]->GetWidget().ResetStatusBar(); | |
390 } | |
391 } | |
392 | |
393 | |
394 void LayoutWidget::Start() | |
395 { | |
396 for (size_t i = 0; i < children_.size(); i++) | |
397 { | |
398 children_[i]->GetWidget().Start(); | |
399 } | |
400 | |
401 WidgetBase::Start(); | |
402 } | |
403 | |
404 | |
405 void LayoutWidget::Stop() | |
406 { | |
407 WidgetBase::Stop(); | |
408 | |
409 for (size_t i = 0; i < children_.size(); i++) | |
410 { | |
411 children_[i]->GetWidget().Stop(); | |
412 } | |
413 } | |
414 | |
415 | |
416 void LayoutWidget::SetSize(unsigned int width, | |
417 unsigned int height) | |
418 { | |
419 width_ = width; | |
420 height_ = height; | |
421 ComputeChildrenExtents(); | |
422 } | |
423 | |
424 | |
425 bool LayoutWidget::Render(Orthanc::ImageAccessor& surface) | |
426 { | |
427 if (!WidgetBase::Render(surface)) | |
428 { | |
429 return false; | |
430 } | |
431 | |
432 for (size_t i = 0; i < children_.size(); i++) | |
433 { | |
434 if (!children_[i]->Render(surface)) | |
435 { | |
436 return false; | |
437 } | |
438 } | |
439 | |
440 return true; | |
441 } | |
442 | |
443 | |
444 IMouseTracker* LayoutWidget::CreateMouseTracker(MouseButton button, | |
445 int x, | |
446 int y, | |
447 KeyboardModifiers modifiers) | |
448 { | |
449 for (size_t i = 0; i < children_.size(); i++) | |
450 { | |
451 IMouseTracker* tracker = children_[i]->CreateMouseTracker(button, x, y, modifiers); | |
452 if (tracker != NULL) | |
453 { | |
454 return tracker; | |
455 } | |
456 } | |
457 | |
458 return NULL; | |
459 } | |
460 | |
461 | |
462 void LayoutWidget::RenderMouseOver(Orthanc::ImageAccessor& target, | |
463 int x, | |
464 int y) | |
465 { | |
466 for (size_t i = 0; i < children_.size(); i++) | |
467 { | |
468 children_[i]->RenderMouseOver(target, x, y); | |
469 } | |
470 } | |
471 | |
472 | |
473 void LayoutWidget::MouseWheel(MouseWheelDirection direction, | |
474 int x, | |
475 int y, | |
476 KeyboardModifiers modifiers) | |
477 { | |
478 for (size_t i = 0; i < children_.size(); i++) | |
479 { | |
480 children_[i]->MouseWheel(direction, x, y, modifiers); | |
481 } | |
482 } | |
483 | |
484 | |
485 void LayoutWidget::KeyPressed(char key, | |
486 KeyboardModifiers modifiers) | |
487 { | |
488 for (size_t i = 0; i < children_.size(); i++) | |
489 { | |
490 children_[i]->GetWidget().KeyPressed(key, modifiers); | |
491 } | |
492 } | |
493 } |