All Classes Namespaces Functions Variables Enumerations Properties Pages
layer.h
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #ifndef LAYER_H
18 #define LAYER_H
19 
20 #include <map>
21 #include <functional>
22 #include <QObject>
23 #include <QString>
24 #include <QDomElement>
25 #include "pencilerror.h"
26 
27 class QMouseEvent;
28 class QPainter;
29 
30 class KeyFrame;
31 class Object;
32 class TimeLineCells;
33 class Status;
34 
35 #define ProgressCallback std::function<void()>
36 
37 class Layer : public QObject
38 {
39  Q_OBJECT
40 
41 public:
42  enum LAYER_TYPE
43  {
44  UNDEFINED = 0,
45  BITMAP = 1,
46  VECTOR = 2,
47  MOVIE = 3, // not supported yet
48  SOUND = 4,
49  CAMERA = 5,
50  };
51 
52  explicit Layer(Object*, LAYER_TYPE);
53  ~Layer() override;
54 
55  int id() const { return mId; }
56  LAYER_TYPE type() const { return meType; }
57 
58  Object* object() const { return mObject; }
59  void setObject(Object* obj);
60 
61  void setName(QString name) { mName = name; }
62  QString name() const { return mName; }
63 
64  void switchVisibility() { mVisible = !mVisible; }
65 
66  bool visible() const { return mVisible; }
67  void setVisible(bool b) { mVisible = b; }
68 
70  QList<int> selectedKeyFramesPositions() const { return mSelectedFrames_byPosition; }
71 
73  QList<int> selectedKeyFramesByLast() const { return mSelectedFrames_byLast; }
74 
75  virtual Status saveKeyFrameFile(KeyFrame*, QString dataPath) = 0;
76  virtual void loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressForward) = 0;
77  virtual QDomElement createDomElement(QDomDocument& doc) const = 0;
78  QDomElement createBaseDomElement(QDomDocument& doc) const;
79  void loadBaseDomElement(const QDomElement& elem);
80 
81  // KeyFrame interface
82  int getMaxKeyFramePosition() const;
83  int firstKeyFramePosition() const;
84 
85  bool keyExists(int position) const;
86  int getPreviousKeyFramePosition(int position) const;
87  int getNextKeyFramePosition(int position) const;
88  int getPreviousFrameNumber(int position, bool isAbsolute) const;
89  int getNextFrameNumber(int position, bool isAbsolute) const;
90 
91  int keyFrameCount() const { return static_cast<int>(mKeyFrames.size()); }
92  int selectedKeyFrameCount() const { return mSelectedFrames_byPosition.count(); }
93  bool hasAnySelectedFrames() const { return !mSelectedFrames_byLast.empty() && !mSelectedFrames_byPosition.empty(); }
94 
98  bool insertExposureAt(int position);
99 
100  bool addNewKeyFrameAt(int position);
101  void addOrReplaceKeyFrame(int position, KeyFrame* pKeyFrame);
102  virtual bool addKeyFrame(int position, KeyFrame* pKeyFrame);
103  virtual bool removeKeyFrame(int position);
104  bool swapKeyFrames(int position1, int position2);
105  bool moveKeyFrame(int position, int offset);
106  KeyFrame* getKeyFrameAt(int position) const;
107  KeyFrame* getLastKeyFrameAtPosition(int position) const;
108  bool keyExistsWhichCovers(int frameNumber);
109  KeyFrame *getKeyFrameWhichCovers(int frameNumber) const;
110 
111  void foreachKeyFrame(std::function<void(KeyFrame*)>) const;
112 
113  void setModified(int position, bool isModified) const;
114 
115  // Handle selection
116  bool isFrameSelected(int position) const;
117  void setFrameSelected(int position, bool isSelected);
118  void toggleFrameSelected(int position, bool allowMultiple = false);
119  void extendSelectionTo(int position);
120  void selectAllFramesAfter(int position);
121 
131  bool newSelectionOfConnectedFrames(int position);
132 
136  void setExposureForSelectedFrames(int offset);
137 
142 
143  void deselectAll();
144 
145  bool moveSelectedFrames(int offset);
146  QList<int> getSelectedFramesByPos() const { return mSelectedFrames_byPosition; }
147 
153  bool canMoveSelectedFramesToOffset(int offset) const;
154 
155  Status save(const QString& sDataFolder, QStringList& attachedFiles, ProgressCallback progressStep);
156  virtual Status presave(const QString& sDataFolder) { Q_UNUSED(sDataFolder); return Status::SAFE; }
157 
158  bool isPaintable() const;
159 
161  QList<int> dirtyFrames() const { return mDirtyFrames; }
162 
165  void markFrameAsDirty(const int frameNumber) { mDirtyFrames << frameNumber; }
166 
168  void clearDirtyFrames() { mDirtyFrames.clear(); }
169 
170 protected:
171  void setId(int LayerId) { mId = LayerId; }
172  virtual KeyFrame* createKeyFrame(int position, Object*) = 0;
173  bool loadKey(KeyFrame*);
174 
175 private:
176  void removeFromSelectionList(int position);
177 
178  LAYER_TYPE meType = UNDEFINED;
179  Object* mObject = nullptr;
180  int mId = 0;
181  bool mVisible = true;
182  QString mName;
183 
184  std::map<int, KeyFrame*, std::greater<int>> mKeyFrames;
185 
186  // We need to keep track of selected frames ordered by last selected
187  // and by position.
188  // Both should be pre-sorted on each selection for optimization purpose when moving frames.
189  //
190  QList<int> mSelectedFrames_byLast; // Used to handle selection range (based on last selected
191  QList<int> mSelectedFrames_byPosition; // Used to handle frames movements on the timeline
192 
193  // Used for clearing cache for modified frames.
194  QList<int> mDirtyFrames;
195 };
196 
197 #endif
void clear()
bool insertExposureAt(int position)
Will insert an empty frame (exposure) after the given position.
Definition: layer.cpp:208
QList< int > dirtyFrames() const
Returns a list of dirty frame positions.
Definition: layer.h:161
bool newSelectionOfConnectedFrames(int position)
Make a selection from specified position until a blank spot appears The search is only looking forwar...
Definition: layer.cpp:461
void setExposureForSelectedFrames(int offset)
Add or subtract exposure from selected frames.
Definition: layer.cpp:525
bool canMoveSelectedFramesToOffset(int offset) const
Predetermines whether the frames can be moved to a new position depending on the offset.
Definition: layer.cpp:510
int count(const T &value) const const
QList< int > selectedKeyFramesPositions() const
Get selected keyframe positions sorted by position.
Definition: layer.h:70
bool empty() const const
void markFrameAsDirty(const int frameNumber)
Mark the frame position as dirty.
Definition: layer.h:165
Q_OBJECTQ_OBJECT
Definition: layer.h:37
void clearDirtyFrames()
Clear the list of dirty keyframes.
Definition: layer.h:168
bool reverseOrderOfSelection()
Reverse order of selected frames.
Definition: layer.cpp:615
Definition: object.h:41
QList< int > selectedKeyFramesByLast() const
Get selected keyframe positions based on the order they were selected.
Definition: layer.h:73