VPTissue Reference Manual
PTreeQtState.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 "PTreeQtState.h"
21 
22 #include <functional>
23 #include <QDockWidget>
24 #include <QTreeView>
25 #include <QWidget>
26 
27 using namespace std;
28 using namespace boost::property_tree;
29 
30 namespace SimShell {
31 
32 namespace {
33  string& escape_path(string& s) {
34  for (auto& c : s)
35  if (c == '.') c = '_';
36  return s;
37  }
38 }
39 
40 ptree PTreeQtState::GetTreeViewState(const QTreeView* view)
41 {
42  function<ptree(const QModelIndex&)> get_state_recursive;
43  get_state_recursive = [&](const QModelIndex& root_index)
44  -> ptree
45  {
46  ptree result;
47  if (view->model()->index(0,0,root_index).isValid()) {
48  result.put("expanded", view->isExpanded(root_index));
49  const int column = 0;
50  for (int row = 0; true; row++) {
51  auto child_index = view->model()->index(row, column, root_index);
52  if (!child_index.isValid())
53  break;
54  auto child_name = child_index.data().toString().toStdString();
55  auto child_path = ptree::path_type(escape_path(child_name));
56  auto child_pt = get_state_recursive(child_index);
57  if (!child_pt.empty())
58  result.add_child(ptree::path_type("children") / child_path, child_pt);
59  }
60  }
61  return result;
62  };
63 
64  ptree result;
65  result.put_child("items_state", get_state_recursive(view->rootIndex()));
66  return result;
67 }
68 
69 void PTreeQtState::SetTreeViewState(QTreeView* view, const ptree& state)
70 {
71  function<void(const QModelIndex&, const ptree&)> set_state_recursive;
72  set_state_recursive = [&](const QModelIndex& root_index, const ptree& state)
73  {
74  auto expanded_optional = state.get_optional<bool>("expanded");
75  if (expanded_optional) {
76  view->setExpanded(root_index, expanded_optional.get());
77  const int column = 0;
78  for (int row = 0; true; row++) {
79  auto child_index = view->model()->index(row, column, root_index);
80  if (!child_index.isValid())
81  break;
82  auto child_name = child_index.data().toString().toStdString();
83  auto child_path = ptree::path_type(escape_path(child_name));
84  auto child_pt_optional = state.get_child_optional(ptree::path_type("children") / child_path);
85  if (child_pt_optional) {
86  set_state_recursive(child_index, child_pt_optional.get());
87  }
88  }
89  }
90  };
91  set_state_recursive(view->rootIndex(), state.get_child("items_state"));
92 }
93 
94 ptree PTreeQtState::GetWidgetState(const QWidget* widget)
95 {
96  ptree result;
97  auto g = widget->geometry();
98  result.put("enabled", widget->isEnabled());
99  result.put("visible", widget->isVisible());
100  result.put("x", g.x());
101  result.put("y", g.y());
102  result.put("width", g.width());
103  result.put("height", g.height());
104  return result;
105 }
106 
107 void PTreeQtState::SetWidgetState(QWidget* widget, const ptree& state)
108 {
109  try {
110  widget->setEnabled(state.get<bool>("enabled"));
111  widget->setVisible(state.get<bool>("visible"));
112  auto x = state.get<int>("x");
113  auto y = state.get<int>("y");
114  auto width = state.get<int>("width");
115  auto height = state.get<int>("height");
116  widget->setGeometry(x, y, width, height);
117  }
118  // Ignore non-existing values.
119  catch (ptree_bad_path&) {}
120  catch (ptree_bad_data&) {}
121 }
122 
123 ptree PTreeQtState::GetDockWidgetState(const QDockWidget* dock)
124 {
125  ptree result;
126  result.put("floating", dock->isFloating());
127  return result;
128 }
129 
130 void PTreeQtState::SetDockWidgetState(QDockWidget* dock, const ptree& state)
131 {
132  try {
133  dock->setFloating(state.get<bool>("floating"));
134  }
135  // Ignore non-existing values.
136  catch (ptree_bad_path&) {}
137  catch (ptree_bad_data&) {}
138 }
139 
140 const map<Qt::DockWidgetArea, string> PTreeQtState::g_dock_widget_area_to_string({
141  {Qt::LeftDockWidgetArea, "left"},
142  {Qt::RightDockWidgetArea, "right"},
143  {Qt::TopDockWidgetArea, "top"},
144  {Qt::BottomDockWidgetArea, "bottom"}
145 });
146 
147 const map<string, Qt::DockWidgetArea> PTreeQtState::g_string_to_dock_widget_area({
148  {"left", Qt::LeftDockWidgetArea},
149  {"right", Qt::RightDockWidgetArea},
150  {"top", Qt::TopDockWidgetArea},
151  {"bottom", Qt::BottomDockWidgetArea}
152 });
153 
154 ptree PTreeQtState::GetDockWidgetArea(const QMainWindow* window, QDockWidget* dock)
155 {
156  ptree result;
157  auto it = g_dock_widget_area_to_string.find(window->dockWidgetArea(dock));
158  if (it != g_dock_widget_area_to_string.end()) {
159  result.put("area", it->second);
160  }
161  return result;
162 }
163 
164 void PTreeQtState::SetDockWidgetArea(QMainWindow* window, QDockWidget* dock, const ptree& state)
165 {
166  auto value = state.get_optional<string>("area");
167  if (value) {
168  auto it = g_string_to_dock_widget_area.find(value.get());
169  if (it != g_string_to_dock_widget_area.end()) {
170  window->addDockWidget(it->second, dock);
171  }
172  }
173 }
174 
175 } // namespace SimShell
STL namespace.
Interface for PTreeQtState.
see the online Qt documentation
see the online Qt documentation
see the online Qt documentation
see the online Qt documentation
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32