VPTissue Reference Manual
PTreeUndoCommands.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 "PTreeUndoCommands.h"
21 
22 #include <boost/property_tree/ptree.hpp>
23 
24 namespace SimShell {
25 namespace Gui {
26 
27 using boost::property_tree::ptree;
28 
29 PTreeModel::EditKeyCommand::EditKeyCommand(PTreeModel* m, Item* it, QModelIndex const & in,
30  QVariant const & ov, QVariant const & nv)
31  : model(m), item(it), index(in), old_value(ov), new_value(nv)
32 {
33  setText("Edit");
34 }
35 
36 PTreeModel::EditKeyCommand::~EditKeyCommand()
37 {
38 }
39 
41 {
42  item->key = old_value;
43  emit model->dataChanged(index, index);
44 }
45 
47 {
48  item->key = new_value;
49  emit model->dataChanged(index, index);
50 }
51 
52 PTreeModel::EditDataCommand::EditDataCommand(PTreeModel* m, Item* it, QModelIndex const & in,
53  QVariant const & ov, QVariant const & nv)
54  : model(m), item(it), index(in), old_value(ov), new_value(nv)
55 {
56  setText("Edit");
57 }
58 
59 PTreeModel::EditDataCommand::~EditDataCommand()
60 {
61 }
62 
64 {
65  item->data = old_value;
66  emit model->dataChanged(index, index);
67 }
68 
70 {
71  item->data = new_value;
72  emit model->dataChanged(index, index);
73 }
74 
75 PTreeModel::InsertRowsCommand::InsertRowsCommand(PTreeModel* m, Item* p, QModelIndex const & pi, int r, int c)
76  : model(m), parent(p), parent_index(pi), row(r), count(c), undone(false)
77 {
78  items = new Item*[count];
79  for (int i = 0; i < count; i++) {
80  items[i] = new Item(parent, "(empty)", i + row);
81  }
82 
83 setText("Insert");
84 }
85 
87  QModelIndex const & pi, int r, ptree const & tree)
88  : model(m), parent(p), parent_index(pi), row(r), count(tree.size()), undone(false)
89 {
90  items = new Item*[count];
91  int i = 0;
92  for (ptree::const_iterator it = tree.begin(); it != tree.end(); it++) {
93  items[i] = new Item(parent, it->first.c_str(), i + row);
94  items[i]->Load(it->second);
95  i++;
96  }
97  setText("Insert");
98 }
99 
100 PTreeModel::InsertRowsCommand::~InsertRowsCommand()
101 {
102  // only delete created rows if this command was undone.
103  if (undone) {
104  for (int i = 0; i < count; i++) {
105  delete items[i];
106  }
107  }
108  delete[] items;
109 }
110 
112 {
113  model->beginRemoveRows(parent_index, row, row + count - 1);
114  for (int i = 0; i < count; i++) {
115  parent->RemoveChild(row);
116  }
117  undone = true;
118  model->endRemoveRows();
119 }
120 
122 {
123  undone = false;
124  model->beginInsertRows(parent_index, row, row + count - 1);
125  for (int i = 0; i < count; i++) {
126  parent->InsertChild(row + i, items[i]);
127  }
128  model->endInsertRows();
129 }
130 
131 PTreeModel::RemoveRowsCommand::RemoveRowsCommand(PTreeModel* m, Item* p, QModelIndex const & pi, int r, int c)
132  : model(m), parent(p), parent_index(pi), row(r), count(c), undone(false)
133 {
134  items = new Item*[count];
135  for (int i = 0; i < count; i++) {
136  items[i] = parent->GetChild(i + row);
137  }
138  setText("Remove");
139 }
140 
141 PTreeModel::RemoveRowsCommand::~RemoveRowsCommand()
142 {
143  // only delete removed rows if command was not undone.
144  if (!undone) {
145  for (int i = 0; i < count; i++) {
146  delete items[i];
147  }
148  }
149  delete[] items;
150 }
151 
153 {
154  undone = true;
155  model->beginInsertRows(parent_index, row, row + count - 1);
156  for (int i = 0; i < count; i++) {
157  parent->InsertChild(row + i, items[i]);
158  }
159  model->endInsertRows();
160 }
161 
163 {
164  model->beginRemoveRows(parent_index, row, row + count - 1);
165  for (int i = 0; i < count; i++) {
166  parent->RemoveChild(row);
167  }
168  undone = false;
169  model->endRemoveRows();
170 }
171 
172 PTreeModel::MoveRowsCommand::MoveRowsCommand(PTreeModel* m, Item* op, QModelIndex const & opi,
173  int r, Item* np, QModelIndex const & npi, int nr, int c)
174  : model(m), old_parent(op), new_parent(np), old_parent_index(opi), new_parent_index(npi),
175  old_row(r), new_row(nr), count(c)
176 {
177  items = new Item*[count];
178  for (int i = 0; i < count; i++) {
179  items[i] = old_parent->GetChild(old_row + i);
180  }
181  setText("Move");
182 }
183 
184 PTreeModel::MoveRowsCommand::~MoveRowsCommand()
185 {
186  // delete array, don't delete individual items.
187  delete[] items;
188 }
189 
191 {
192  model->beginRemoveRows(new_parent_index, new_row, new_row + count - 1);
193  for (int i = 0; i < count; i++) {
194  new_parent->RemoveChild(new_row + i);
195  }
196  model->endRemoveRows();
197 
198  model->beginInsertRows(old_parent_index, old_row, old_row + count - 1);
199  for (int i = 0; i < count; i++) {
200  items[i]->row = old_row + i;
201  old_parent->InsertChild(old_row + i, items[i]);
202  }
203  model->endInsertRows();
204 }
205 
207 {
208  // TODO: try this out:
209  // model->beginMoveRows(old_parent_index, old_row, old_row+count-1, new_parent_index, new_row);
210  model->beginRemoveRows(old_parent_index, old_row, old_row + count - 1);
211  for (int i = 0; i < count; i++) {
212  old_parent->RemoveChild(old_row + i);
213  }
214  model->endRemoveRows();
215 
216  model->beginInsertRows(new_parent_index, new_row, new_row + count - 1);
217  for (int i = 0; i < count; i++) {
218  items[i]->row = new_row + i;
219  new_parent->InsertChild(new_row + i, items[i]);
220  }
221  model->endInsertRows();
222  // model->endMoveRows();
223 }
224 
225 } // end of namespace Gui
226 } // end of namespace SimShell
InsertRowsCommand(PTreeModel *m, Item *parent, QModelIndex const &parent_index, int row, int count)
Interface for PTree undo commands.
RemoveRowsCommand(PTreeModel *m, Item *parent, QModelIndex const &parent_index, int row, int count)
virtual void undo()
See QUndoCommand documentation for details.
EditKeyCommand(PTreeModel *m, Item *i, QModelIndex const &x, QVariant const &old_value, QVariant const &new_value)
virtual void redo()
See QUndoCommand documentation for details.
Qt model reflecting hierarchical structure of a ptree.
Definition: PTreeModel.h:37
virtual void undo()
See QUndoCommand documentation for details.
virtual void redo()
See QUndoCommand documentation for details.
virtual void redo()
See QUndoCommand documentation for details.
virtual void undo()
See QUndoCommand documentation for details.
virtual void redo()
See QUndoCommand documentation for details.
virtual void undo()
See QUndoCommand documentation for details.
MoveRowsCommand(PTreeModel *m, Item *old_parent, QModelIndex const &old_parent_index, int old_row, Item *new_parent, QModelIndex const &new_parent_index, int new_row, int count)
EditDataCommand(PTreeModel *m, Item *i, QModelIndex const &x, QVariant const &old_value, QVariant const &new_value)
virtual void undo()
See QUndoCommand documentation for details.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
virtual void redo()
See QUndoCommand documentation for details.
constexpr double pi()
Math constant pi.
Definition: constants.h:29