Swiftray  1.0
command.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <layer.h>
4 #include <shape/shape.h>
5 #include <shape/text-shape.h>
6 
7 // TODO (Create batch command for add / remove shapes)
8 
9 class Document;
10 
15 namespace Commands {
16 
21  class BaseCmd {
22  public:
23 
24  // BaseCmd
25  BaseCmd() = default;
26 
27  virtual void undo(Document *doc) {
28  Q_ASSERT_X(false, "Commands", "This command did not implement undo");
29  }
30 
31  virtual void redo(Document *doc) {
32  Q_ASSERT_X(false, "Commands", "This command did not implement redo");
33  }
34  };
35 
36  typedef shared_ptr<BaseCmd> CmdPtr;
37 
38 
43  class AddLayerCmd : public BaseCmd {
44  public:
45  AddLayerCmd(const LayerPtr &layer) : layer_(layer) {}
46 
47  void undo(Document *doc) override;
48 
49  void redo(Document *doc) override;
50 
52  };
53 
58  class RemoveLayerCmd : public BaseCmd {
59  public:
60  RemoveLayerCmd(const LayerPtr &layer) : layer_(layer) {}
61 
62  void undo(Document *doc) override;
63 
64  void redo(Document *doc) override;
65 
67  };
68 
74  class AddShapeCmd : public BaseCmd {
75  public:
76  AddShapeCmd(Layer *layer, const ShapePtr &shape) :
77  layer_(layer), shape_(shape) {}
78 
79  void undo(Document *doc) override;
80 
81  void redo(Document *doc) override;
82 
85  };
86 
92  class RemoveShapeCmd : public BaseCmd {
93  public:
94  explicit RemoveShapeCmd(const ShapePtr &shape) :
95  shape_(shape) { layer_ = shape->layer(); }
96 
97  RemoveShapeCmd(Layer *layer, const ShapePtr &shape) :
98  layer_(layer), shape_(shape) {}
99 
100  void undo(Document *doc) override;
101 
102  void redo(Document *doc) override;
103 
106  };
107 
112  class SelectCmd : public BaseCmd {
113  public:
114 
115  explicit SelectCmd(Document *doc, const QList<ShapePtr> &new_selections_);
116 
117  void undo(Document *doc) override;
118 
119  void redo(Document *doc) override;
120 
121  QList<ShapePtr> old_selections_;
122  QList<ShapePtr> new_selections_;
123  };
124 
129  class JoinedCmd : public BaseCmd {
130  public:
131 
132  JoinedCmd() = default;
133 
134  JoinedCmd(initializer_list<BaseCmd *> undo_events);
135 
136  JoinedCmd(initializer_list<CmdPtr> undo_events);
137 
138  void undo(Document *doc) override;
139 
140  void redo(Document *doc) override;
141 
142  QList<CmdPtr> events;
143  };
144 
145  typedef shared_ptr<JoinedCmd> JoinedPtr;
146 
151  template<typename T, typename PropType, PropType (T::*PropGetter)() const, void (T::*PropSetter)(
152  PropType)>
153  class SetCmd : public BaseCmd {
154  public:
155 
156  explicit SetCmd(T *target, PropType new_value) : target_(target), new_value_(new_value) {
157  old_value_ = (target_->*PropGetter)();
158  }
159 
160  void undo(Document *doc) override {
161  qInfo() << "[Command] Undo set";
162  (target_->*PropSetter)(old_value_);
163  }
164 
165  void redo(Document *doc) override {
166  qInfo() << "[Command] Do set";
167  (target_->*PropSetter)(new_value_);
168  };
169 
171  PropType new_value_;
172  PropType old_value_;
173  };
174 
179  template<typename T, typename PropType, const PropType &(T::*PropGetter)() const, void (T::*PropSetter)(
180  const PropType &)>
181  class SetRefCmd : public BaseCmd {
182  public:
183 
184  explicit SetRefCmd(T *target, PropType new_value) : target_(target), new_value_(new_value) {
185  old_value_ = (target_->*PropGetter)();
186  }
187 
188  void undo(Document *doc) override {
189  qInfo() << "[Command] Undo setRef";
190  (target_->*PropSetter)(old_value_);
191  }
192 
193  void redo(Document *doc) override {
194  qInfo() << "[Command] Do setRef";
195  (target_->*PropSetter)(new_value_);
196  };
197 
199  PropType new_value_;
200  PropType old_value_;
201  };
202 
203  // Operators overload
204  JoinedPtr operator+(const CmdPtr &a, const CmdPtr &b);
205 
206  JoinedPtr &operator<<(JoinedPtr &a, const CmdPtr &b);
207 
209 
210  // Abbreviations for generating commands
211  template<typename T, typename PropType, PropType (T::*PropGetter)() const, void (T::*PropSetter)(
212  PropType)>
213  CmdPtr Set(T *target, PropType new_value) {
214  return make_shared<SetCmd<T, PropType, PropGetter, PropSetter>>(target, new_value);
215  }
216 
217  template<typename T, typename PropType, const PropType &(T::*PropGetter)() const, void (T::*PropSetter)(
218  const PropType &)>
219  CmdPtr SetRef(T *target, PropType new_value) {
220  return make_shared<SetRefCmd<T, PropType, PropGetter, PropSetter>>
221  (target, new_value);
222  }
223 
224  // Abbreviations for specific set
225  constexpr CmdPtr
226  (*SetTransform)(Shape *, QTransform) =
227  &SetRef<Shape, QTransform, &Shape::transform, &Shape::setTransform>;
228 
229  constexpr CmdPtr (*SetParent)(Shape *, Shape *) =
230  &Set<Shape, Shape *, &Shape::parent, &Shape::setParent>;
231 
232  constexpr CmdPtr (*SetLayer)(Shape *, Layer *) =
233  &Set<Shape, Layer *, &Shape::layer, &Shape::setLayer>;
234 
235  constexpr CmdPtr (*SetFont)(TextShape *, QFont) =
236  &SetRef<TextShape, QFont, &TextShape::font, &TextShape::setFont>;
237 
238  constexpr CmdPtr (*SetLineHeight)(TextShape *, float) =
239  &Set<TextShape, float, &TextShape::lineHeight, &TextShape::setLineHeight>;
240 
241  constexpr CmdPtr (*SetRotation)(Shape *, qreal) =
242  &Set<Shape, qreal, &TextShape::rotation, &TextShape::setRotation>;
243 
244  CmdPtr AddShape(Layer *layer, const ShapePtr &shape);
245 
246  CmdPtr RemoveShape(const ShapePtr &shape);
247 
248  CmdPtr RemoveShape(Layer *layer, const ShapePtr &shape);
249 
250  CmdPtr Select(Document *doc, const QList<ShapePtr> &new_selections);
251 
252  template<typename T, typename PropType, PropType (T::*PropGetter)() const, void (T::*PropSetter)(
253  PropType)>
254  CmdPtr Set(T *target, PropType new_value);
255 
256  template<typename T, typename PropType, const PropType &(T::*PropGetter)() const, void (T::*PropSetter)(
257  const PropType &)>
258  CmdPtr SetRef(T *target, PropType new_value);
259 
260  CmdPtr AddShapes(Layer *layer, const QList<ShapePtr> &shapes);
261 
262  CmdPtr RemoveShapes(const QList<ShapePtr> &shapes);
263 
264  CmdPtr AddLayer(const LayerPtr &layer);
265 
266  CmdPtr RemoveLayer(const LayerPtr &layer);
267 
269 
270  JoinedPtr Joined();
271 }
272 
Command for adding layers.
Definition: command.h:43
LayerPtr layer_
Definition: command.h:51
void undo(Document *doc) override
Definition: command.cpp:19
void redo(Document *doc) override
Definition: command.cpp:24
AddLayerCmd(const LayerPtr &layer)
Definition: command.h:45
Command for adding shapes. The command needs to manage shapes' lifecycle, but doesn't need to manage ...
Definition: command.h:74
ShapePtr shape_
Definition: command.h:84
AddShapeCmd(Layer *layer, const ShapePtr &shape)
Definition: command.h:76
void redo(Document *doc) override
Definition: command.cpp:42
Layer * layer_
Definition: command.h:83
void undo(Document *doc) override
Definition: command.cpp:37
A class template for undoable commands, along with undo() / redo() implementation.
Definition: command.h:21
virtual void redo(Document *doc)
Definition: command.h:31
virtual void undo(Document *doc)
Definition: command.h:27
A group of commands that can be considered as a single step in undo/redo.
Definition: command.h:129
void undo(Document *doc) override
Definition: command.cpp:137
JoinedCmd(initializer_list< BaseCmd * > undo_events)
QList< CmdPtr > events
Definition: command.h:142
JoinedCmd(initializer_list< CmdPtr > undo_events)
void redo(Document *doc) override
Definition: command.cpp:143
Command for removing layers.
Definition: command.h:58
void undo(Document *doc) override
Definition: command.cpp:29
void redo(Document *doc) override
Definition: command.cpp:33
LayerPtr layer_
Definition: command.h:66
RemoveLayerCmd(const LayerPtr &layer)
Definition: command.h:60
Command for removing shapes. The command needs to manage shapes' lifecycle, but doesn't need to manag...
Definition: command.h:92
void undo(Document *doc) override
Definition: command.cpp:48
void redo(Document *doc) override
Definition: command.cpp:53
RemoveShapeCmd(const ShapePtr &shape)
Definition: command.h:94
Layer * layer_
Definition: command.h:104
RemoveShapeCmd(Layer *layer, const ShapePtr &shape)
Definition: command.h:97
ShapePtr shape_
Definition: command.h:105
Command for selection changes in document.
Definition: command.h:112
void redo(Document *doc) override
Definition: command.cpp:68
SelectCmd(Document *doc, const QList< ShapePtr > &new_selections_)
Definition: command.cpp:58
void undo(Document *doc) override
Definition: command.cpp:63
QList< ShapePtr > old_selections_
Definition: command.h:121
QList< ShapePtr > new_selections_
Definition: command.h:122
Command for changing objects' property, and the property can be "passed by value".
Definition: command.h:153
void redo(Document *doc) override
Definition: command.h:165
T * target_
Definition: command.h:168
void undo(Document *doc) override
Definition: command.h:160
PropType old_value_
Definition: command.h:172
PropType new_value_
Definition: command.h:171
SetCmd(T *target, PropType new_value)
Definition: command.h:156
Command for changing objects' property, and the property is usually "passed by reference".
Definition: command.h:181
void undo(Document *doc) override
Definition: command.h:188
void redo(Document *doc) override
Definition: command.h:193
PropType old_value_
Definition: command.h:200
T * target_
Definition: command.h:196
PropType new_value_
Definition: command.h:199
SetRefCmd(T *target, PropType new_value)
Definition: command.h:184
Document state store for layers, shapes, document specfic settings and current view state.
Definition: document.h:19
Definition: layer.h:10
A base class for shape objects that contains transform and parent information.
Definition: shape.h:17
Definition: text-shape.h:6
Commands::CmdPtr CmdPtr
Definition: command.h:273
shared_ptr< Layer > LayerPtr
Definition: layer.h:127
Undoable commands.
Definition: command.h:9
JoinedPtr Joined()
Definition: command.cpp:125
CmdPtr AddLayer(const LayerPtr &layer)
Definition: command.cpp:117
shared_ptr< JoinedCmd > JoinedPtr
Definition: command.h:145
CmdPtr AddShape(Layer *layer, const ShapePtr &shape)
Definition: command.cpp:85
CmdPtr Set(T *target, PropType new_value)
Definition: command.h:213
CmdPtr RemoveLayer(const LayerPtr &layer)
Definition: command.cpp:121
constexpr CmdPtr(* SetLineHeight)(TextShape *, float)
Definition: command.h:238
CmdPtr RemoveShape(const ShapePtr &shape)
Definition: command.cpp:89
JoinedPtr operator+(const CmdPtr &a, const CmdPtr &b)
CmdPtr AddShapes(Layer *layer, const QList< ShapePtr > &shapes)
Definition: command.cpp:101
CmdPtr RemoveShapes(const QList< ShapePtr > &shapes)
Definition: command.cpp:109
constexpr CmdPtr(* SetTransform)(Shape *, QTransform)
Definition: command.h:226
shared_ptr< BaseCmd > CmdPtr
Definition: command.h:36
CmdPtr SetRef(T *target, PropType new_value)
Definition: command.h:219
constexpr CmdPtr(* SetFont)(TextShape *, QFont)
Definition: command.h:235
JoinedPtr & operator<<(JoinedPtr &a, const CmdPtr &b)
Definition: command.cpp:9
constexpr CmdPtr(* SetParent)(Shape *, Shape *)
Definition: command.h:229
constexpr CmdPtr(* SetLayer)(Shape *, Layer *)
Definition: command.h:232
CmdPtr Select(Document *doc, const QList< ShapePtr > &new_selections)
Definition: command.cpp:97
CmdPtr RemoveSelections(Document *doc)
Definition: command.cpp:77
constexpr CmdPtr(* SetRotation)(Shape *, qreal)
Definition: command.h:241
shared_ptr< Shape > ShapePtr
Definition: shape.h:137