Mercurial > hg > orthanc-stone
annotate Framework/Widgets/LayoutWidget.cpp @ 247:3d523c9a8f0d am
trying to use boost::signals2 even more.
author | am@osimis.io |
---|---|
date | Mon, 02 Jul 2018 12:32:02 +0200 |
parents | 7d3b2c4f9ba1 |
children | f21ba2468570 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
134
4cff7b1ed31d
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "LayoutWidget.h" | |
23 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
25 #include <Core/OrthancException.h> |
0 | 26 |
27 #include <boost/math/special_functions/round.hpp> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 class LayoutWidget::LayoutMouseTracker : public IMouseTracker | |
32 { | |
33 private: | |
34 std::auto_ptr<IMouseTracker> tracker_; | |
35 int left_; | |
36 int top_; | |
37 unsigned int width_; | |
38 unsigned int height_; | |
39 | |
40 public: | |
41 LayoutMouseTracker(IMouseTracker* tracker, | |
42 int left, | |
43 int top, | |
44 unsigned int width, | |
45 unsigned int height) : | |
46 tracker_(tracker), | |
47 left_(left), | |
48 top_(top), | |
49 width_(width), | |
50 height_(height) | |
51 { | |
52 if (tracker == NULL) | |
53 { | |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
55 } | |
56 } | |
57 | |
58 virtual void Render(Orthanc::ImageAccessor& surface) | |
59 { | |
60 Orthanc::ImageAccessor accessor = surface.GetRegion(left_, top_, width_, height_); | |
61 tracker_->Render(accessor); | |
62 } | |
63 | |
64 virtual void MouseUp() | |
65 { | |
66 tracker_->MouseUp(); | |
67 } | |
68 | |
69 virtual void MouseMove(int x, | |
70 int y) | |
71 { | |
72 tracker_->MouseMove(x - left_, y - top_); | |
73 } | |
74 }; | |
75 | |
76 | |
77 class LayoutWidget::ChildWidget : public boost::noncopyable | |
78 { | |
79 private: | |
80 std::auto_ptr<IWidget> widget_; | |
81 int left_; | |
82 int top_; | |
83 unsigned int width_; | |
84 unsigned int height_; | |
85 | |
86 public: | |
87 ChildWidget(IWidget* widget) : | |
232
7d3b2c4f9ba1
don't cache hasUpdate_ since its not updated when we update the underlying hierarchy (i.e: when the child of a layout is a layout without child when it is added but childs are added later ...)
am@osimis.io
parents:
212
diff
changeset
|
88 widget_(widget) |
0 | 89 { |
90 assert(widget != NULL); | |
91 SetEmpty(); | |
92 } | |
93 | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
94 void UpdateContent() |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
95 { |
232
7d3b2c4f9ba1
don't cache hasUpdate_ since its not updated when we update the underlying hierarchy (i.e: when the child of a layout is a layout without child when it is added but childs are added later ...)
am@osimis.io
parents:
212
diff
changeset
|
96 if (widget_->HasUpdateContent()) |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
97 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
98 widget_->UpdateContent(); |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
99 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
100 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
101 |
0 | 102 IWidget& GetWidget() const |
103 { | |
104 return *widget_; | |
105 } | |
106 | |
107 void SetRectangle(unsigned int left, | |
108 unsigned int top, | |
109 unsigned int width, | |
110 unsigned int height) | |
111 { | |
112 left_ = left; | |
113 top_ = top; | |
114 width_ = width; | |
115 height_ = height; | |
116 | |
117 widget_->SetSize(width, height); | |
118 } | |
119 | |
120 void SetEmpty() | |
121 { | |
122 SetRectangle(0, 0, 0, 0); | |
123 } | |
124 | |
125 bool Contains(int x, | |
126 int y) const | |
127 { | |
128 return (x >= left_ && | |
129 y >= top_ && | |
130 x < left_ + static_cast<int>(width_) && | |
131 y < top_ + static_cast<int>(height_)); | |
132 } | |
133 | |
134 bool Render(Orthanc::ImageAccessor& target) | |
135 { | |
136 if (width_ == 0 || | |
137 height_ == 0) | |
138 { | |
139 return true; | |
140 } | |
141 else | |
142 { | |
143 Orthanc::ImageAccessor accessor = target.GetRegion(left_, top_, width_, height_); | |
144 return widget_->Render(accessor); | |
145 } | |
146 } | |
147 | |
148 IMouseTracker* CreateMouseTracker(MouseButton button, | |
149 int x, | |
150 int y, | |
151 KeyboardModifiers modifiers) | |
152 { | |
153 if (Contains(x, y)) | |
154 { | |
155 IMouseTracker* tracker = widget_->CreateMouseTracker(button, | |
156 x - left_, | |
157 y - top_, | |
158 modifiers); | |
159 if (tracker) | |
160 { | |
161 return new LayoutMouseTracker(tracker, left_, top_, width_, height_); | |
162 } | |
163 } | |
164 | |
165 return NULL; | |
166 } | |
167 | |
168 void RenderMouseOver(Orthanc::ImageAccessor& target, | |
169 int x, | |
170 int y) | |
171 { | |
172 if (Contains(x, y)) | |
173 { | |
174 Orthanc::ImageAccessor accessor = target.GetRegion(left_, top_, width_, height_); | |
175 | |
176 widget_->RenderMouseOver(accessor, x - left_, y - top_); | |
177 } | |
178 } | |
179 | |
180 void MouseWheel(MouseWheelDirection direction, | |
181 int x, | |
182 int y, | |
183 KeyboardModifiers modifiers) | |
184 { | |
185 if (Contains(x, y)) | |
186 { | |
187 widget_->MouseWheel(direction, x - left_, y - top_, modifiers); | |
188 } | |
189 } | |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
190 |
55 | 191 bool HasRenderMouseOver() |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
192 { |
55 | 193 return widget_->HasRenderMouseOver(); |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
194 } |
0 | 195 }; |
196 | |
197 | |
198 void LayoutWidget::ComputeChildrenExtents() | |
199 { | |
200 if (children_.size() == 0) | |
201 { | |
202 return; | |
203 } | |
204 | |
205 float internal = static_cast<float>(paddingInternal_); | |
206 | |
207 if (width_ <= paddingLeft_ + paddingRight_ || | |
208 height_ <= paddingTop_ + paddingBottom_) | |
209 { | |
210 for (size_t i = 0; i < children_.size(); i++) | |
211 { | |
212 children_[i]->SetEmpty(); | |
213 } | |
214 } | |
215 else if (isHorizontal_) | |
216 { | |
217 unsigned int padding = paddingLeft_ + paddingRight_ + (children_.size() - 1) * paddingInternal_; | |
218 float childWidth = ((static_cast<float>(width_) - static_cast<float>(padding)) / | |
219 static_cast<float>(children_.size())); | |
220 | |
221 for (size_t i = 0; i < children_.size(); i++) | |
222 { | |
223 float left = static_cast<float>(paddingLeft_) + static_cast<float>(i) * (childWidth + internal); | |
224 float right = left + childWidth; | |
225 | |
226 if (left >= right) | |
227 { | |
228 children_[i]->SetEmpty(); | |
229 } | |
230 else | |
231 { | |
232 children_[i]->SetRectangle(static_cast<unsigned int>(left), | |
233 paddingTop_, | |
234 boost::math::iround(right - left), | |
235 height_ - paddingTop_ - paddingBottom_); | |
236 } | |
237 } | |
238 } | |
239 else | |
240 { | |
241 unsigned int padding = paddingTop_ + paddingBottom_ + (children_.size() - 1) * paddingInternal_; | |
242 float childHeight = ((static_cast<float>(height_) - static_cast<float>(padding)) / | |
243 static_cast<float>(children_.size())); | |
244 | |
245 for (size_t i = 0; i < children_.size(); i++) | |
246 { | |
247 float top = static_cast<float>(paddingTop_) + static_cast<float>(i) * (childHeight + internal); | |
248 float bottom = top + childHeight; | |
249 | |
250 if (top >= bottom) | |
251 { | |
252 children_[i]->SetEmpty(); | |
253 } | |
254 else | |
255 { | |
256 children_[i]->SetRectangle(paddingTop_, | |
257 static_cast<unsigned int>(top), | |
258 width_ - paddingLeft_ - paddingRight_, | |
259 boost::math::iround(bottom - top)); | |
260 } | |
261 } | |
262 } | |
263 | |
264 NotifyChange(*this); | |
265 } | |
266 | |
267 | |
268 LayoutWidget::LayoutWidget() : | |
269 isHorizontal_(true), | |
270 width_(0), | |
271 height_(0), | |
272 paddingLeft_(0), | |
273 paddingTop_(0), | |
274 paddingRight_(0), | |
275 paddingBottom_(0), | |
276 paddingInternal_(0) | |
277 { | |
278 } | |
279 | |
280 | |
281 LayoutWidget::~LayoutWidget() | |
282 { | |
283 for (size_t i = 0; i < children_.size(); i++) | |
284 { | |
285 delete children_[i]; | |
286 } | |
287 } | |
288 | |
289 | |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
290 void LayoutWidget::SetDefaultView() |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
291 { |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
292 for (size_t i = 0; i < children_.size(); i++) |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
293 { |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
294 children_[i]->GetWidget().SetDefaultView(); |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
295 } |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
296 } |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
297 |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
298 |
0 | 299 void LayoutWidget::NotifyChange(const IWidget& widget) |
300 { | |
301 // One of the children has changed | |
302 WidgetBase::NotifyChange(); | |
303 } | |
304 | |
305 | |
306 void LayoutWidget::SetHorizontal() | |
307 { | |
308 isHorizontal_ = true; | |
309 ComputeChildrenExtents(); | |
310 } | |
311 | |
312 | |
313 void LayoutWidget::SetVertical() | |
314 { | |
315 isHorizontal_ = false; | |
316 ComputeChildrenExtents(); | |
317 } | |
318 | |
319 | |
320 void LayoutWidget::SetPadding(unsigned int left, | |
321 unsigned int top, | |
322 unsigned int right, | |
323 unsigned int bottom, | |
324 unsigned int spacing) | |
325 { | |
326 paddingLeft_ = left; | |
327 paddingTop_ = top; | |
328 paddingRight_ = right; | |
329 paddingBottom_ = bottom; | |
330 paddingInternal_ = spacing; | |
331 } | |
332 | |
333 | |
334 void LayoutWidget::SetPadding(unsigned int padding) | |
335 { | |
336 paddingLeft_ = padding; | |
337 paddingTop_ = padding; | |
338 paddingRight_ = padding; | |
339 paddingBottom_ = padding; | |
340 paddingInternal_ = padding; | |
341 } | |
342 | |
343 | |
344 IWidget& LayoutWidget::AddWidget(IWidget* widget) // Takes ownership | |
345 { | |
346 if (widget == NULL) | |
347 { | |
348 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
349 } | |
350 | |
351 if (GetStatusBar() != NULL) | |
352 { | |
353 widget->SetStatusBar(*GetStatusBar()); | |
354 } | |
355 | |
356 children_.push_back(new ChildWidget(widget)); | |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
357 widget->SetParent(*this); |
0 | 358 |
359 ComputeChildrenExtents(); | |
360 | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
361 if (widget->HasUpdateContent()) |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
362 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
363 hasUpdateContent_ = true; |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
364 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
365 |
0 | 366 return *widget; |
367 } | |
368 | |
369 | |
370 void LayoutWidget::SetStatusBar(IStatusBar& statusBar) | |
371 { | |
372 WidgetBase::SetStatusBar(statusBar); | |
373 | |
374 for (size_t i = 0; i < children_.size(); i++) | |
375 { | |
376 children_[i]->GetWidget().SetStatusBar(statusBar); | |
377 } | |
378 } | |
379 | |
380 | |
381 void LayoutWidget::SetSize(unsigned int width, | |
382 unsigned int height) | |
383 { | |
384 width_ = width; | |
385 height_ = height; | |
386 ComputeChildrenExtents(); | |
387 } | |
388 | |
389 | |
390 bool LayoutWidget::Render(Orthanc::ImageAccessor& surface) | |
391 { | |
392 if (!WidgetBase::Render(surface)) | |
393 { | |
394 return false; | |
395 } | |
396 | |
397 for (size_t i = 0; i < children_.size(); i++) | |
398 { | |
399 if (!children_[i]->Render(surface)) | |
400 { | |
401 return false; | |
402 } | |
403 } | |
404 | |
405 return true; | |
406 } | |
407 | |
408 | |
409 IMouseTracker* LayoutWidget::CreateMouseTracker(MouseButton button, | |
410 int x, | |
411 int y, | |
412 KeyboardModifiers modifiers) | |
413 { | |
414 for (size_t i = 0; i < children_.size(); i++) | |
415 { | |
416 IMouseTracker* tracker = children_[i]->CreateMouseTracker(button, x, y, modifiers); | |
417 if (tracker != NULL) | |
418 { | |
419 return tracker; | |
420 } | |
421 } | |
422 | |
423 return NULL; | |
424 } | |
425 | |
426 | |
427 void LayoutWidget::RenderMouseOver(Orthanc::ImageAccessor& target, | |
428 int x, | |
429 int y) | |
430 { | |
431 for (size_t i = 0; i < children_.size(); i++) | |
432 { | |
433 children_[i]->RenderMouseOver(target, x, y); | |
434 } | |
435 } | |
436 | |
437 | |
438 void LayoutWidget::MouseWheel(MouseWheelDirection direction, | |
439 int x, | |
440 int y, | |
441 KeyboardModifiers modifiers) | |
442 { | |
443 for (size_t i = 0; i < children_.size(); i++) | |
444 { | |
445 children_[i]->MouseWheel(direction, x, y, modifiers); | |
446 } | |
447 } | |
448 | |
449 | |
450 void LayoutWidget::KeyPressed(char key, | |
451 KeyboardModifiers modifiers) | |
452 { | |
453 for (size_t i = 0; i < children_.size(); i++) | |
454 { | |
455 children_[i]->GetWidget().KeyPressed(key, modifiers); | |
456 } | |
457 } | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
458 |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
459 |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
460 void LayoutWidget::UpdateContent() |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
461 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
462 if (hasUpdateContent_) |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
463 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
464 for (size_t i = 0; i < children_.size(); i++) |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
465 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
466 children_[i]->UpdateContent(); |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
467 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
468 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
469 else |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
470 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
471 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
472 } |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
473 } |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
474 |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
475 |
55 | 476 bool LayoutWidget::HasRenderMouseOver() |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
477 { |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
478 for (size_t i = 0; i < children_.size(); i++) |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
479 { |
55 | 480 if (children_[i]->HasRenderMouseOver()) |
54
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
481 { |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
482 return true; |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
483 } |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
484 } |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
485 |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
486 return false; |
01aa453d4d5b
IWidget::HasRenderMouseOver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
487 } |
0 | 488 } |