VPTissue Reference Manual
PTreeEditorWindow.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 "PTreeEditorWindow.h"
21 
22 #include "PTreeView.h"
23 #include "qtmodel/PTreeModel.h"
24 
25 #include <QCloseEvent>
26 #include <QHeaderView>
27 #include <QMenuBar>
28 #include <QMessageBox>
29 #include <QStatusBar>
30 #include <QToolBar>
31 
32 namespace SimShell {
33 namespace Gui {
34 
35 using std::string;
36 using boost::property_tree::ptree;
37 
38 int const PTreeEditorWindow::g_column_width = 200;
39 
41  : QMainWindow(parent), HasUnsavedChanges({}), m_only_edit_data(false), m_model(0)
42 {
43  /* central widget */
44  m_treeview = new PTreeView(this);
45  m_treeview->SetOnlyEditData(m_only_edit_data);
46  setCentralWidget(m_treeview);
47  connect(m_treeview, SIGNAL(Edited()), this, SLOT(Apply()));
48  connect(m_treeview, SIGNAL(StatusChanged(QString const&)), this, SIGNAL(StatusChanged(QString const&)));
49 
50  /* actions */
51  m_action_show_toolbar_main = new QAction("Main Toolbar", this);
52 
53  /* borrow actions from treeview */
54  QAction* action_find = m_treeview->GetFindDialogAction();
55  QAction* action_clear_highlight = m_treeview->GetClearHighlightAction();
56  QAction* action_undo = m_treeview->GetUndoAction();
57  QAction* action_redo = m_treeview->GetRedoAction();
58  QAction* action_cut = m_treeview->GetCutAction();
59  QAction* action_copy = m_treeview->GetCopyAction();
60  QAction* action_paste = m_treeview->GetPasteAction();
61  QAction* action_expand_all = m_treeview->GetExpandAllAction();
62  QAction* action_expand_none = m_treeview->GetExpandNoneAction();
63 
64  m_action_show_toolbar_main->setCheckable(true);
65  m_action_show_toolbar_main->setChecked(true);
66 
67  /* toolbar */
68  m_toolbar_main = new QToolBar(this);
69  m_toolbar_main->setToolButtonStyle(Qt::ToolButtonIconOnly);
70  m_toolbar_main->setMovable(false);
71  m_toolbar_main->setWindowTitle("Main toolbar");
72  m_toolbar_main->toggleViewAction()->setEnabled(false);
73  m_toolbar_main->setIconSize(QSize(16,16)); // save some space in our already crowded window
74  // add actions
75  m_toolbar_main->addAction(action_undo);
76  m_toolbar_main->addAction(action_redo);
77  m_toolbar_main->addSeparator();
78  m_toolbar_main->addAction(action_cut);
79  m_toolbar_main->addAction(action_copy);
80  m_toolbar_main->addAction(action_paste);
81  m_toolbar_main->addSeparator();
82  m_toolbar_main->addAction(action_expand_all);
83  m_toolbar_main->addAction(action_expand_none);
84  m_toolbar_main->addSeparator();
85  m_toolbar_main->addAction(action_find);
86  m_toolbar_main->addAction(action_clear_highlight);
87  addToolBar(m_toolbar_main);
88 
89  connect(m_action_show_toolbar_main, SIGNAL(toggled(bool)), m_toolbar_main, SLOT(setVisible(bool)));
90 }
91 
93 {
94  if (m_edit_path.empty())
95  m_pt = m_model->Store();
96  else
97  m_pt.put_child(m_edit_path, m_model->Store());
98  m_treeview->SetClean();
99 
100  emit ApplyTriggered(m_pt);
101 }
102 
103 std::string PTreeEditorWindow::GetEditPath() const {
104  return m_edit_path;
105 }
106 
108 {
109  ptree result;
110  result.put("edit_path", m_edit_path);
111  result.put_child("treeview", m_treeview->GetPTreeState());
112  return result;
113 }
114 
116 {
117  if (!m_model)
118  return; // we are already in unopened state
119 
120  // can't use QAbstractItemview::revert(), since it is non-virtual, so it doesn't emit canCopyChanged()
121  m_treeview->setModel(nullptr);
122  delete m_model;
123  m_model = nullptr;
124 }
125 
127 {
128  return m_treeview->IsClean();
129 }
130 
132 {
133  if (!m_treeview->IsClean())
134  Apply();
135  return true;
136 }
137 
139 {
140  return m_only_edit_data;
141 }
142 
144 {
145  return m_model;
146 }
147 
148 bool PTreeEditorWindow::OpenPath(QString const & edit_path)
149 {
150  if (!m_model)
151  return false;
152 
153  if (edit_path.toStdString() == m_edit_path)
154  return true; // don't change anything, but return success
155 
156  if (!m_treeview->IsClean()) {
157  int result = QMessageBox::warning(this, windowTitle(), "Apply changes before opening different subtree?\n",
158  QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No);
159  if (result == QMessageBox::Yes)
160  Apply();
161  else if (result == QMessageBox::Cancel)
162  return false; // user changed his mind, edit path was not set
163  }
164 
165  m_edit_path = edit_path.toStdString();
166  m_treeview->setModel(0);
167  delete m_model;
168  m_model = new PTreeModel(m_pt.get_child(m_edit_path), this);
169  m_model->SetOnlyEditData(m_only_edit_data);
170  m_treeview->setModel(m_model);
171  m_treeview->header()->resizeSection(0, g_column_width);
172  return true; // success
173 }
174 
175 bool PTreeEditorWindow::OpenPTree(ptree const & pt, QString const & edit_path)
176 {
177  if (!SaveAndClose())
178  return false; // must be in unopened state
179 
180  m_pt = pt;
181  m_edit_path = edit_path.toStdString();
182  m_model = new PTreeModel(m_pt.get_child(m_edit_path), this);
183  m_model->SetOnlyEditData(m_only_edit_data);
184  m_treeview->setModel(m_model);
185  m_treeview->header()->resizeSection(0, g_column_width);
186 
187  return true;
188 }
189 
191 {
192  m_treeview->GetRedoAction()->trigger();
193 }
194 
196 {
197  m_only_edit_data = b;
198 
199  m_treeview->SetOnlyEditData(m_only_edit_data);
200 
201  if (m_model)
202  m_model->SetOnlyEditData(m_only_edit_data);
203 }
204 
205 void PTreeEditorWindow::SetPTreeState(const ptree& state)
206 {
207  auto edit_path = state.get_optional<string>("edit_path");
208  if (edit_path) {
209  OpenPath(QString::fromStdString(edit_path.get()));
210  }
211 
212  auto treeview = state.get_child_optional("treeview");
213  if (treeview) {
214  m_treeview->SetPTreeState(treeview.get());
215  }
216 }
217 
219 {
220  return QSize(320, 600);
221 }
222 
224 {
225  m_treeview->GetUndoAction()->trigger();
226 }
227 
228 } // end of namespace Gui
229 } // end of namespace SimShell
std::string GetEditPath() const
Get the current edit path.
bool OpenPTree(const boost::property_tree::ptree &, QString const &edit_path="")
Set ptree to show in editor.
boost::property_tree::ptree Store() const
Creates a new property tree from the current model state.
Definition: PTreeModel.cpp:399
PTreeEditorWindow(QWidget *parent=nullptr)
void SetOnlyEditData(bool)
Set the "only edit values" option.
virtual boost::property_tree::ptree GetPTreeState() const
Definition: MyTreeView.cpp:34
bool OpenPath(const QString &edit_path)
Set subtree to show in editor.
bool IsClean() const
Test whether the view's internal undo stack is in a clean state.
Definition: PTreeView.cpp:309
virtual void setModel(QAbstractItemModel *model)
Reimplemented from QTreeView, also enables/disables certain actions.
Definition: PTreeView.cpp:405
Qt model reflecting hierarchical structure of a ptree.
Definition: PTreeModel.h:37
void Undo()
Undo last manipulation by the user.
void Redo()
Redo last manipulation by the user.
void SetClean()
Set the view's internal undo stack's current state to be the clean state.
Definition: PTreeView.cpp:387
TreeView widget that presents an editable ptree to the user.
Definition: PTreeView.h:37
virtual QSize sizeHint() const
Reimplemented from QWidget.
bool IsOnlyEditData() const
Test whether the "only edit data" option is set.
see the online Qt documentation
Interface for PTreeEditorWindow.
Abstract class that represents the ability be in closed or opened state, and, if the latter...
virtual void SetPTreeState(const boost::property_tree::ptree &)
Definition: MyTreeView.cpp:44
void SetOnlyEditData(bool)
Set the "only edit values" option.
Definition: PTreeModel.cpp:389
see the online Qt documentation
Interface for PTreeView.
Interface for PTreeModel.
void ApplyTriggered(const boost::property_tree::ptree &pt)
Emitted when changes are applied to ptree.
bool SaveAndClose()
Try to save this object (and its children) and, if successful, close them all.
virtual void SetPTreeState(const boost::property_tree::ptree &)
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
void Apply()
Write changes to ptree object. Clean state is achieved as a side effect.
virtual boost::property_tree::ptree GetPTreeState() const