Walls And Holes  1
changevaluecommand.h
Go to the documentation of this file.
1 #ifndef CHANGEVALUECOMMAND_H
2 #define CHANGEVALUECOMMAND_H
3 
4 #include <functional>
5 #include <QUndoCommand>
6 
7 
8 template< typename ValueType >
9 class ChangeValueCommand : public QUndoCommand
10 {
11 public:
12 
13 
14 
25  template< typename Func >
27  ValueType *variable,
28  ValueType newValue,
29  ValueType oldValue,
30  const QString &text, // "change value" is not a useful default text (since the user can't see the code)
31  Func postChangeFunc,
32  QUndoCommand *parent = nullptr)
33  {
34  std::function<void()> func = postChangeFunc;
35 
36  return new ChangeValueCommand(variable, oldValue, newValue, text, func, parent);
37  }
38 
39 
49  template< typename Func >
51  ValueType *variable,
52  ValueType newValue,
53  const QString &text, // "change value" is not a useful default text (since the user can't see the code)
54  Func postChangeFunc,
55  QUndoCommand *parent = nullptr)
56  {
57  ValueType oldValue = *variable;
58  return make(variable, newValue, oldValue, text, postChangeFunc, parent);
59  }
60 
70  ValueType *variable,
71  ValueType newValue,
72  const QString &text,
73  QUndoCommand *parent = nullptr)
74  {
75  return make(variable, newValue, text, [](){}, parent);
76  // isn't this beautiful ^
77  }
78 
79 
80 
81 
82  void redo() override
83  {
84  *mVal = mNewVal;
85  mPostChangeFunc();
86  }
87 
88  void undo() override
89  {
90  *mVal = mOldVal;
91  mPostChangeFunc();
92  }
93 
94 private:
95 
96  ChangeValueCommand(ValueType *variable,
97  ValueType oldValue,
98  ValueType newValue,
99  const QString &text,
100  std::function<void()> postChange,
101  QUndoCommand *parent)
102  : QUndoCommand(text, parent)
103  , mVal(variable)
104  , mOldVal(oldValue)
105  , mNewVal(newValue)
106  , mPostChangeFunc(postChange) {}
107 
108  ValueType *mVal;
109 
110  ValueType mOldVal;
111  ValueType mNewVal;
112 
113  std::function<void()> mPostChangeFunc;
114 };
115 
116 #endif // CHANGEVALUECOMMAND_H
Definition: changevaluecommand.h:9
void redo() override
Definition: changevaluecommand.h:82
static ChangeValueCommand * make(ValueType *variable, ValueType newValue, ValueType oldValue, const QString &text, Func postChangeFunc, QUndoCommand *parent=nullptr)
make Creates a change-value command.
Definition: changevaluecommand.h:26
void undo() override
Definition: changevaluecommand.h:88
static ChangeValueCommand * make(ValueType *variable, ValueType newValue, const QString &text, QUndoCommand *parent=nullptr)
make Creates a change-value command.
Definition: changevaluecommand.h:69
static ChangeValueCommand * make(ValueType *variable, ValueType newValue, const QString &text, Func postChangeFunc, QUndoCommand *parent=nullptr)
make Creates a change-value command.
Definition: changevaluecommand.h:50