SciDAVis  1.D4
aspectcommands.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : aspectcommands.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2007-2009 by Knut Franke, Tilman Benkert
6  Email (use @ for *) : knut.franke*gmx.de, thzs*gmx.net
7  Description : Undo commands used by AbstractAspect.
8  Only meant to be used within AbstractAspect.cpp
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 
31 #include "AspectPrivate.h"
32 
33 #include <QUndoCommand>
34 
35 class AspectNameChangeCmd : public QUndoCommand
36 {
37  public:
38  AspectNameChangeCmd(AbstractAspect::Private *target, const QString &new_name)
39  : d_target(target), d_other_name(new_name) {
40  setText(QObject::tr("%1: rename to %2").arg(d_target->name()).arg(new_name));
41  }
42 
43  virtual void redo() {
44  QString tmp = d_target->name();
46  d_other_name = tmp;
47  }
48 
49  virtual void undo() { redo(); }
50 
51  private:
53  QString d_other_name;
54 };
55 
56 class AspectCommentChangeCmd : public QUndoCommand
57 {
58  public:
59  AspectCommentChangeCmd(AbstractAspect::Private *target, const QString &new_comment)
60  : d_target(target), d_other_comment(new_comment) {
61  setText(QObject::tr("%1: change comment").arg(d_target->name()));
62  }
63 
64  virtual void redo() {
65  QString tmp = d_target->comment();
67  d_other_comment = tmp;
68  }
69 
70  virtual void undo() { redo(); }
71 
72  private:
74  QString d_other_comment;
75 };
76 
77 class AspectCaptionSpecChangeCmd : public QUndoCommand
78 {
79  public:
80  AspectCaptionSpecChangeCmd(AbstractAspect::Private *target, const QString &new_caption_spec)
81  : d_target(target), d_other_caption_spec(new_caption_spec) {
82  setText(QObject::tr("%1: change caption").arg(d_target->name()));
83  }
84 
85  virtual void redo() {
86  QString tmp = d_target->captionSpec();
89  }
90 
91  virtual void undo() { redo(); }
92 
93  private:
96 };
97 
98 
99 class AspectCreationTimeChangeCmd : public QUndoCommand
100 {
101  public:
102  AspectCreationTimeChangeCmd(AbstractAspect::Private *target, const QDateTime &new_creation_time)
103  : d_target(target), d_other_creation_time(new_creation_time) {
104  setText(QObject::tr("%1: set creation time").arg(d_target->name()));
105  }
106 
107  virtual void redo() {
108  QDateTime tmp = d_target->creationTime();
110  d_other_creation_time = tmp;
111  }
112 
113  virtual void undo() { redo(); }
114 
115  private:
118 };
119 
120 
121 class AspectChildRemoveCmd : public QUndoCommand
122 {
123  public:
125  : d_target(target), d_child(child), d_index(-1), d_removed(false), d_detach(detach) {
126  setText(QObject::tr("%1: remove %2").arg(d_target->name()).arg(d_child->name()));
127  }
129  if (d_removed && !d_detach)
130  delete d_child;
131  }
132 
133  // calling redo transfers ownership of d_child to the undo command
134  virtual void redo() {
136  d_removed = true;
137  }
138 
139  // calling undo transfers ownership of d_child back to its parent aspect
140  virtual void undo() {
141  Q_ASSERT(d_index != -1); // d_child must be a child of d_target->owner()
143  d_removed = false;
144  }
145 
146  protected:
149  int d_index;
151 };
152 
154 {
155  public:
157  : AspectChildRemoveCmd(target, child, false) {
158  setText(QObject::tr("%1: add %2").arg(d_target->name()).arg(d_child->name()));
159  d_index = index;
160  }
161 
162  virtual void redo() { AspectChildRemoveCmd::undo(); }
163 
164  virtual void undo() { AspectChildRemoveCmd::redo(); }
165 };
166 
167 class AspectChildMoveCmd : public QUndoCommand
168 {
169  public:
170  AspectChildMoveCmd(AbstractAspect::Private * target, int from, int to)
171  : d_target(target), d_from(from), d_to(to) {
172  setText(QObject::tr("%1: move child from position %2 to %3.").arg(d_target->name()).arg(d_from+1).arg(d_to+1));
173  }
174 
175  virtual void redo() {
176  // Moving in one go confuses QTreeView, so we would need another two signals
177  // to be mapped to QAbstractItemModel::layoutAboutToBeChanged() and ::layoutChanged().
178  AbstractAspect * child = d_target->child(d_from);
179  d_target->removeChild(child);
180  d_target->insertChild(d_to, child);
181  }
182 
183  virtual void undo() {
184  AbstractAspect * child = d_target->child(d_to);
185  d_target->removeChild(child);
186  d_target->insertChild(d_from, child);
187  }
188 
189  private:
191  int d_from, d_to;
192 };
193 
194 class AspectChildReparentCmd : public QUndoCommand
195 {
196  public:
198  AbstractAspect* child, int new_index)
199  : d_target(target), d_new_parent(new_parent), d_child(child), d_index(-1), d_new_index(new_index)
200  {
201  setText(QObject::tr("%1: move %2 to %3.").arg(d_target->name()).arg(d_child->name()).arg(d_new_parent->name()));
202  }
204 
205  // calling redo transfers ownership of d_child to the new parent aspect
206  virtual void redo()
207  {
210  }
211 
212  // calling undo transfers ownership of d_child back to its previous parent aspect
213  virtual void undo()
214  {
215  Q_ASSERT(d_index != -1);
218  }
219 
220  protected:
224  int d_index;
226 };
227