VPTissue Reference Manual
ViewerActions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ViewerActions.h"
21 
22 #include <QAction>
23 #include <QMenu>
24 
25 using namespace std;
26 
27 namespace SimShell {
28 namespace Gui {
29 namespace ProjectActions {
30 
31 ViewerActions::ViewerActions(const shared_ptr<Viewer::IViewerNode>& root_node)
32 {
33  function<QMenu*(const shared_ptr<Viewer::IViewerNode>&, QAction*)> init_recursive;
34  init_recursive = [&](const shared_ptr<Viewer::IViewerNode>& root_node, QAction* parent_action)
35  -> QMenu*
36  {
37  QMenu* result = new QMenu("Viewers");
38  for (auto& node : *root_node) {
39  result->addSeparator();
40  //result->addAction(QString::fromStdString(node.first))->setEnabled(false);
41  QAction* enable_action = result->addAction(QString::fromStdString(node.first));
42  enable_action->setCheckable(true);
43  if (node.second->IsEnabled())
44  enable_action->setChecked(true);
45  enable_action->setEnabled(node.second->IsParentEnabled());
46  m_node_map.insert({enable_action, node.second});
47  m_children_map[parent_action].push_back(enable_action);
48  connect(enable_action, SIGNAL(toggled(bool)), this, SLOT(SLOT_Toggle(bool)));
49  auto children_menu = init_recursive(node.second, enable_action);
50  if (children_menu->actions().size())
51  result->addMenu(children_menu);
52  }
53  return result;
54  };
55  m_menu = init_recursive(root_node, nullptr);
56 }
57 
58 ViewerActions::~ViewerActions()
59 {
60 }
61 
62 shared_ptr<ViewerActions> ViewerActions::Create(const shared_ptr<Viewer::IViewerNode>& root_node)
63 {
64  auto result = shared_ptr<ViewerActions>(new ViewerActions(root_node));
65 
66  for (auto& val : result->m_node_map) {
67  val.second->Register(val.second, [&](const Viewer::Event::ViewerEvent& e) {
68  auto type = e.GetType();
69  bool checked = type == Viewer::Event::ViewerEvent::Enabled;
70  static_cast<QAction*>(val.first)->setChecked(checked);
71  });
72  }
73 
74  return result;
75 }
76 
77 QMenu* ViewerActions::GetMenu() const
78 {
79  return m_menu;
80 }
81 
82 void ViewerActions::SLOT_Toggle(bool checked)
83 {
84  auto& node = m_node_map[sender()];
85 
86  if (checked) {
87  node->Enable();
88  } else {
89  node->Disable();
90  }
91 
92  function<void(vector<QAction*>& children)> work_on_children;
93  work_on_children = [&](vector<QAction*>& children) {
94  for (auto a : children) {
95  a->setEnabled(m_node_map[a]->IsParentEnabled());
96  work_on_children(m_children_map[a]);
97  }
98  };
99  work_on_children(m_children_map[sender()]);
100 }
101 
102 } // namespace ProjectActions
103 } // namespace Gui
104 } // namespace SimShell
STL namespace.
Given a so-called 'root' viewer instance, constructs a menu representing the hierarchical structure o...
Definition: ViewerActions.h:41
An event transmitted by viewers to notify whether they are enabled or disabled.
Definition: ViewerEvent.h:34
see the online Qt documentation
ViewerActions header.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32