23 #include <QDockWidget>
33 string& escape_path(
string& s) {
35 if (c ==
'.') c =
'_';
40 ptree PTreeQtState::GetTreeViewState(
const QTreeView* view)
42 function<ptree(const QModelIndex&)> get_state_recursive;
43 get_state_recursive = [&](
const QModelIndex& root_index)
47 if (view->model()->index(0,0,root_index).isValid()) {
48 result.put(
"expanded", view->isExpanded(root_index));
50 for (
int row = 0;
true; row++) {
51 auto child_index = view->model()->index(row, column, root_index);
52 if (!child_index.isValid())
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);
65 result.put_child(
"items_state", get_state_recursive(view->rootIndex()));
69 void PTreeQtState::SetTreeViewState(
QTreeView* view,
const ptree& state)
71 function<void(const QModelIndex&, const ptree&)> set_state_recursive;
72 set_state_recursive = [&](
const QModelIndex& root_index,
const ptree& state)
74 auto expanded_optional = state.get_optional<
bool>(
"expanded");
75 if (expanded_optional) {
76 view->setExpanded(root_index, expanded_optional.get());
78 for (
int row = 0;
true; row++) {
79 auto child_index = view->model()->index(row, column, root_index);
80 if (!child_index.isValid())
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());
91 set_state_recursive(view->rootIndex(), state.get_child(
"items_state"));
94 ptree PTreeQtState::GetWidgetState(
const QWidget* widget)
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());
107 void PTreeQtState::SetWidgetState(
QWidget* widget,
const ptree& state)
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);
119 catch (ptree_bad_path&) {}
120 catch (ptree_bad_data&) {}
126 result.put(
"floating", dock->isFloating());
130 void PTreeQtState::SetDockWidgetState(
QDockWidget* dock,
const ptree& state)
133 dock->setFloating(state.get<
bool>(
"floating"));
136 catch (ptree_bad_path&) {}
137 catch (ptree_bad_data&) {}
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"}
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}
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);
166 auto value = state.get_optional<
string>(
"area");
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);
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.