VPTissue Reference Manual
ViewerNode.h
Go to the documentation of this file.
1 #ifndef VIEWER_VIEWERNODE_H_INCLUDED
2 #define VIEWER_VIEWERNODE_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "IViewerNode.h"
25 
26 #include <iostream>
27 #include <type_traits>
28 
29 class QWidget;
30 
31 namespace SimShell {
32 namespace Viewer {
33 
34 template <class ViewerType>
36  static const bool value = false;
37 };
38 
39 template <class ViewerTYpe>
41  static const bool value = false;
42 };
43 
49 template <class ViewerType, class SubjectType>
50 class ViewerNode : public IViewerNodeWithParent<SubjectType>,
51  public std::enable_shared_from_this<ViewerNode<ViewerType, SubjectType>>
52 {
53 public:
54  ViewerNode(std::shared_ptr<Ws::MergedPreferences> p,
55  IViewerNode::ChildrenMap&& c,
56  Gui::Controller::AppController* app = nullptr);
57 
58  ViewerNode(std::shared_ptr<Ws::MergedPreferences> p,
59  Gui::Controller::AppController* app = nullptr);
60 
62  virtual ~ViewerNode() {}
63 
65  virtual void Disable();
66 
68  virtual void Enable();
69 
71  virtual bool IsEnabled() const;
72 
74  virtual bool IsParentEnabled() const;
75 
77  virtual IViewerNode::ChildrenMap::const_iterator begin() const;
78 
80  virtual IViewerNode::ChildrenMap::const_iterator end() const;
81 
82 protected:
84  virtual void ParentDisabled();
85 
87  virtual void ParentEnabled(std::shared_ptr<SubjectType>);
88 
89 private:
90  void Check();
91 
92  template <class T = ViewerType>
93  typename std::enable_if<!viewer_is_widget<T>::value, std::shared_ptr<T>>::type CreateViewer();
94 
95  template <class T = ViewerType>
96  typename std::enable_if<viewer_is_widget<T>::value, std::shared_ptr<T>>::type CreateViewer();
97 
98 private:
99  std::shared_ptr<Ws::MergedPreferences> m_preferences;
100  std::shared_ptr<SubjectType> m_subject;
101  IViewerNode::ChildrenMap m_children;
102  bool m_enabled;
103  bool m_parent_enabled;
104  std::shared_ptr<ViewerType> m_viewer;
106 };
107 
108 template <class ViewerType, class SubjectType>
109 ViewerNode<ViewerType, SubjectType>::ViewerNode(std::shared_ptr<Ws::MergedPreferences> p,
110  IViewerNode::ChildrenMap&& c,
112  : m_preferences(p),
113  m_children(c),
114  m_enabled(m_preferences->Get<bool>("enabled_at_startup")),
115  m_parent_enabled(false),
116  m_app(a)
117 {
118 }
119 
120 template <class ViewerType, class SubjectType>
121 ViewerNode<ViewerType, SubjectType>::ViewerNode(std::shared_ptr<Ws::MergedPreferences> p,
122  Gui::Controller::AppController* a)
123  : m_preferences(p),
124  m_enabled(m_preferences->Get<bool>("enabled_at_startup")),
125  m_parent_enabled(false),
126  m_app(a)
127 {
128 }
129 
130 template <bool enabled>
131 struct Register
132 {
133  template <class ViewerType>
134  static void Do(std::shared_ptr<ViewerType>, IViewerNode::ChildrenMap&) {}
135 
136  template <class ViewerType>
137  static void Undo(std::shared_ptr<ViewerType> , IViewerNode::ChildrenMap& ) {}
138 };
139 
140 template <>
141 struct Register<true>
142 {
143  template <class ViewerType>
144  static void Do(std::shared_ptr<ViewerType> v, IViewerNode::ChildrenMap& m)
145  {
146  for (auto& child : m) {
147  std::static_pointer_cast<IViewerNodeWithParent<ViewerType>>(child.second)->ParentEnabled(v);
148  }
149  }
150 
151  template <class ViewerType>
152  static void Undo(std::shared_ptr<ViewerType> , IViewerNode::ChildrenMap& m)
153  {
154  for (auto& child : m) {
155  std::static_pointer_cast<IViewerNodeWithParent<ViewerType>>(child.second)->ParentDisabled();
156  }
157  }
158 };
159 
160 template <class ViewerType, class SubjectType>
161 template <class T>
162 typename std::enable_if<!viewer_is_widget<T>::value, std::shared_ptr<T>>::type
164 {
165  return std::make_shared<T>(m_preferences);
166 }
167 
168 template <class ViewerType, class SubjectType>
169 template <class T>
170 typename std::enable_if<viewer_is_widget<T>::value, std::shared_ptr<T>>::type
171 ViewerNode<ViewerType, SubjectType>::CreateViewer()
172 {
173  if (m_app) {
174  return std::make_shared<ViewerType>(m_preferences, m_app, [this](){ this->Disable();});
175  } else {
176  return nullptr;
177  }
178 }
179 
180 // Wrapped in struct because we want to partially specialize it.
181 template <class ViewerType, class SubjectType>
183 {
185  template <typename V = ViewerType, typename S = SubjectType,
186  typename S::EventType::Type init_type = S::EventType::Type::Initialized>
187  static void Do(std::shared_ptr<V> v, typename S::EventType::Source s)
188  {
189  //std::cout << "InitNotify - version that does something - called" << std::endl;
190  v->Update(typename SubjectType::EventType({s, 0, init_type}));
191  }
192 
193  template <typename T, typename U>
194  static void Do(T, U)
195  {
196  //std::cout << "InitNotify - version that does nothing - called" << std::endl;
197  }
198 };
199 
200 template <class ViewerType, class SubjectType>
201 void ViewerNode<ViewerType, SubjectType>::Check()
202 {
203  if (m_enabled && m_parent_enabled)
204  {
205  // Create viewer
206  if (!m_viewer && (m_viewer = CreateViewer())) {
207  //m_viewer = std::make_shared<ViewerType>(m_preferences);
208  if (m_subject) {
209  typedef typename SubjectType::EventType EventType;
210  auto handler = std::bind(&ViewerType::template Update<EventType>, m_viewer.get(), std::placeholders::_1);
211  m_subject->Register(m_viewer, handler);
212  InitNotifier<ViewerType, SubjectType>::Do(m_viewer, m_subject);
213  }
214 
215  // Enable children after this is enabled.
216  Register<viewer_is_subject<ViewerType>::value>::Do(m_viewer, m_children);
217  }
218  } else {
219  if (m_viewer) {
220  // Disable children before this is disabled.
221  Register<viewer_is_subject<ViewerType>::value>::Undo(m_viewer, m_children);
222 
223  // Disable viewer
224  if (m_subject) {
225  m_subject->Unregister(m_viewer);
226  }
227  m_viewer.reset();
228  }
229  }
230 }
231 
232 template <class ViewerType, class SubjectType>
234 {
235  m_enabled = false;
236  m_preferences->Put("enabled_at_startup", false);
237  Check();
238  IViewerNode::Notify({ ViewerNode<ViewerType, SubjectType>::shared_from_this(), Event::ViewerEvent::Disabled });
239 }
240 
241 template <class ViewerType, class SubjectType>
243 {
244  m_enabled = true;
245  m_preferences->Put("enabled_at_startup", true);
246  Check();
247  IViewerNode::Notify({ ViewerNode<ViewerType, SubjectType>::shared_from_this(), Event::ViewerEvent::Enabled });
248 }
249 
250 template <class ViewerType, class SubjectType>
252 {
253  return m_enabled;
254 }
255 
256 template <class ViewerType, class SubjectType>
258 {
259  return m_parent_enabled;
260 }
261 
262 template <class ViewerType, class SubjectType>
263 IViewerNode::ChildrenMap::const_iterator ViewerNode<ViewerType, SubjectType>::begin() const
264 {
265  return m_children.begin();
266 }
267 
268 template <class ViewerType, class SubjectType>
269 IViewerNode::ChildrenMap::const_iterator ViewerNode<ViewerType, SubjectType>::end() const
270 {
271  return m_children.end();
272 }
273 
274 template <class ViewerType, class SubjectType>
275 void ViewerNode<ViewerType, SubjectType>::ParentEnabled(std::shared_ptr<SubjectType> s)
276 {
277  m_subject = s;
278  m_parent_enabled = true;
279  Check();
280 }
281 
282 template <class ViewerType, class SubjectType>
284 {
285  m_parent_enabled = false;
286  Check();
287 }
288 
289 } // namespace
290 } // namespace
291 
292 #endif // end_of_inclde_guard
AppController header.
virtual ~ViewerNode()
Virtual destructor.
Definition: ViewerNode.h:62
Type
Enumerates the wall types.
Definition: WallType.h:27
virtual bool IsParentEnabled() const
Definition: ViewerNode.h:257
static void Do(std::shared_ptr< V > v, typename S::EventType::Source s)
Try to initialize viewer with "Initialized" type of event.
Definition: ViewerNode.h:187
virtual IViewerNode::ChildrenMap::const_iterator begin() const
Definition: ViewerNode.h:263
virtual bool IsEnabled() const
Definition: ViewerNode.h:251
Interface for MergedPreferences.
virtual void ParentDisabled()
Definition: ViewerNode.h:283
virtual IViewerNode::ChildrenMap::const_iterator end() const
Definition: ViewerNode.h:269
see the online Qt documentation
Template implementing a node in a hierarchical tree of viewers.Parameters:
Definition: ViewerNode.h:50
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
Interface for IViewerNode.
see the online C++11 documentation
virtual void ParentEnabled(std::shared_ptr< SubjectType >)
Definition: ViewerNode.h:275
A viewer node with a parent.
Definition: IViewerNode.h:64