VPTissue Reference Manual
SaveChangesDialog.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 "SaveChangesDialog.h"
21 
22 #include "HasUnsavedChanges.h"
24 
25 #include <QCheckBox>
26 #include <QDialogButtonBox>
27 #include <QHeaderView>
28 #include <QLabel>
29 #include <QPushButton>
30 #include <QTreeView>
31 #include <QVBoxLayout>
32 #include <functional>
33 
34 using namespace std;
35 using namespace boost::property_tree;
36 
37 namespace SimShell {
38 namespace Gui {
39 
40 SaveChangesDialog::SaveChangesDialog(const ptree& pt, QWidget* parent)
41  : QDialog(parent)
42 {
43  auto layout = new QVBoxLayout;
44  auto label = new QLabel("The following windows contain unsaved changes:");
45 
46  layout->addWidget(label);
47  auto treeview = new QTreeView(this);
48  m_model = new CheckableTreeModel(pt, treeview);
49  treeview->setModel(m_model);
50  treeview->header()->hide();
51  layout->addWidget(treeview);
52 
53  auto box = new QDialogButtonBox(Qt::Horizontal, this);
54  m_save_button = box->addButton(QDialogButtonBox::Save);
55  m_cancel_button = box->addButton(QDialogButtonBox::Cancel);
56  m_discard_button = box->addButton(QDialogButtonBox::Discard);
57  layout->addWidget(box);
58 
59  connect(box, SIGNAL(clicked(QAbstractButton*)), this, SLOT(SLOT_Clicked(QAbstractButton*)));
60 
61  setLayout(layout);
62  setWindowTitle("Save changes?");
63 }
64 
65 void SaveChangesDialog::SLOT_Clicked(QAbstractButton* button)
66 {
67  if (button == m_save_button) {
68  done(Save);
69  } else if (button == m_cancel_button) {
70  done(Cancel);
71  } else if (button == m_discard_button) {
72  done(Discard);
73  }
74 }
75 
76 bool SaveChangesDialog::Prompt(HasUnsavedChanges* widget, QWidget* parent)
77 {
79  dummy.SetChildren({widget});
80 
81  function<ptree(HasUnsavedChanges*)> work_recursive;
82  auto unsaved_tree = (work_recursive = [&](HasUnsavedChanges* widget) -> ptree {
83  ptree result;
84  for (auto child : *widget) {
85  if (child->IsOpened() && !child->IsClean()) {
86  ptree child_pt;
87  child_pt.put("checked", true);
88  child_pt.put_child("children", work_recursive(child));
89  result.put_child(child->GetTitle(), child_pt);
90  }
91  }
92  return result;
93  })(&dummy);
94 
95  if (unsaved_tree.size())
96  {
97  // Create "Save Changes?"-dialog.
98  SaveChangesDialog dialog(unsaved_tree, parent);
99 
100  switch (dialog.exec()) {
101  case Save: {
102  // Save selected items
103  function<bool(const ptree&, HasUnsavedChanges*)> work_recursive;
104  if (!(work_recursive = [&](const ptree& pt, HasUnsavedChanges* i) {
105  auto pt_it = pt.begin();
106  auto i_it = i->begin();
107 
108  while (pt_it != pt.end())
109  {
110  auto& child_pt = pt_it->second;
111  auto checked = child_pt.get<bool>("checked");
112  if (checked) {
113  if (!(*i_it)->Save())
114  return false;
115  }
116 
117  auto children_optional = child_pt.get_child_optional("children");
118  if (children_optional) {
119  if (!work_recursive(children_optional.get(), *i_it))
120  return false;
121  }
122 
123  pt_it++;
124  i_it++;
125  }
126  return true;
127  })(dialog.m_model->ToPTree(), &dummy)) {
128  return false;
129  }
130  break;
131  }
132  case Discard:
133  break;
134  case Cancel:
135  return false;
136  }
137  }
138 
139  dummy.ForceClose();
140  return true;
141 }
142 
143 } // namespace
144 } // namespace
STL namespace.
Dialog window that displays a tree of objects containing unsaved changes.
Dummy UnsavedChanges class.
ChildrenType::iterator begin()
Get iterator to first child.
void ForceClose()
Force this object and its children to go back to unopened state so it can be safely deleted...
CheckableTreeModel header.
Interface for HasUnsavedChanges.
Abstract class that represents the ability be in closed or opened state, and, if the latter...
boost::property_tree::ptree ToPTree()
Get checked/unchecked state of items, represented by a ptree of the form discussed in the constructor...
see the online Qt documentation
see the online Qt documentation
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
see the online Qt documentation
Interface for SaveChangesDialog.