labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

PropertyChangeCommand.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : PropertyChangeCommand.h
3  Project : SciDAVis / LabPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2010 Knut Franke
6  Email (use @ for *) : Knut.Franke*gmx.net
7  Description : Generic undo command changing a single variable.
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 
30 #ifndef PROPERTY_CHANGE_COMMAND_H
31 #define PROPERTY_CHANGE_COMMAND_H
32 
33 #include <QUndoCommand>
34 
35 /**
36  * \class PropertyChangeCommand
37  * \brief Generic undo command changing a single variable.
38  *
39  * Given a pointer to a variable (usually a member of the class instantiating the command, or of
40  * its private implementation class) and a new value, assigns the value to the variable. A backup
41  * of the old value is made, so that undo/redo can switch back and forth between the two values.
42  * The value type needs to support copy construction and assignment.
43  */
44 
45 template<class T> class PropertyChangeCommand : public QUndoCommand {
46 
47 public:
48  PropertyChangeCommand(const QString &text, T *property, const T &new_value)
49  : m_property(property), m_other_value(new_value) {
50  setText(text);
51  }
52 
53  void redo() override {
54  T tmp = *m_property;
56  m_other_value = tmp;
57  }
58 
59  void undo() override {
60  redo();
61  }
62 
63  int id() const override {
64  return reinterpret_cast<std::intptr_t>(m_property);
65  }
66 
67  bool mergeWith(const QUndoCommand* other) override {
68  if (other->id() != id())
69  return false;
70 
71  if (std::is_same<T, bool>::value)
72  *m_property = *(static_cast<const PropertyChangeCommand*>(other)->m_property);
73  else
74  *m_property += *(static_cast<const PropertyChangeCommand*>(other)->m_property);
75 
76  return true;
77  }
78 
80 
81 private:
83 };
84 
85 #endif // ifndef PROPERTY_CHANGE_COMMAND_H
Generic undo command changing a single variable.
int id() const override
PropertyChangeCommand(const QString &text, T *property, const T &new_value)
bool mergeWith(const QUndoCommand *other) override