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 #include "pencildef.h"
27 
28 class QMouseEvent;
29 class QPainter;
30 
31 class KeyFrame;
32 class Object;
33 class TimeLineCells;
34 class Status;
35 
36 #define ProgressCallback std::function<void()>
37 
38 class Layer : public QObject
39 {
40  Q_OBJECT
41 
42 public:
43  enum LAYER_TYPE
44  {
45  UNDEFINED = 0,
46  BITMAP = 1,
47  VECTOR = 2,
48  MOVIE = 3, // not supported yet
49  SOUND = 4,
50  CAMERA = 5,
51  };
52 
53  explicit Layer(Object*, LAYER_TYPE);
54  ~Layer() override;
55 
56  int id() const { return mId; }
57  LAYER_TYPE type() const { return meType; }
58 
59  Object* object() const { return mObject; }
60  void setObject(Object* obj);
61 
62  void setName(QString name) { mName = name; }
63  QString name() const { return mName; }
64 
65  void switchVisibility() { mVisible = !mVisible; }
66 
67  bool visible() const { return mVisible; }
68  void setVisible(bool b) { mVisible = b; }
69 
71  QList<int> selectedKeyFramesPositions() const { return mSelectedFrames_byPosition; }
72 
74  QList<int> selectedKeyFramesByLast() const { return mSelectedFrames_byLast; }
75 
76  virtual Status saveKeyFrameFile(KeyFrame*, QString dataPath) = 0;
77  virtual void loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressForward) = 0;
78  virtual QDomElement createDomElement(QDomDocument& doc) const = 0;
79  QDomElement createBaseDomElement(QDomDocument& doc) const;
80  void loadBaseDomElement(const QDomElement& elem);
81 
82  // KeyFrame interface
83  int getMaxKeyFramePosition() const;
84  int firstKeyFramePosition() const;
85 
86  bool keyExists(int position) const;
87  int getPreviousKeyFramePosition(int position) const;
88  int getNextKeyFramePosition(int position) const;
89  int getPreviousFrameNumber(int position, bool isAbsolute) const;
90  int getNextFrameNumber(int position, bool isAbsolute) const;
91 
92  int keyFrameCount() const { return static_cast<int>(mKeyFrames.size()); }
93  int selectedKeyFrameCount() const { return mSelectedFrames_byPosition.count(); }
94  bool hasAnySelectedFrames() const { return !mSelectedFrames_byLast.empty() && !mSelectedFrames_byPosition.empty(); }
95 
99  bool insertExposureAt(int position);
100 
101  bool addNewKeyFrameAt(int position);
102  bool addKeyFrame(int position, KeyFrame*);
103  bool removeKeyFrame(int position);
104  bool swapKeyFrames(int position1, int position2);
105  bool moveKeyFrame(int position, int offset);
106  bool loadKey(KeyFrame*);
107  KeyFrame* getKeyFrameAt(int position) const;
108  KeyFrame* getLastKeyFrameAtPosition(int position) const;
109  bool keyExistsWhichCovers(int frameNumber);
110  KeyFrame *getKeyFrameWhichCovers(int frameNumber) const;
111 
112  void foreachKeyFrame(std::function<void(KeyFrame*)>) const;
113 
114  void setModified(int position, bool isModified) const;
115 
116  // Handle selection
117  bool isFrameSelected(int position) const;
118  void setFrameSelected(int position, bool isSelected);
119  void toggleFrameSelected(int position, bool allowMultiple = false);
120  void extendSelectionTo(int position);
121  void selectAllFramesAfter(int position);
122 
132  bool newSelectionOfConnectedFrames(int position);
133 
137  void setExposureForSelectedFrames(int offset);
138 
143 
144  void deselectAll();
145 
146  bool moveSelectedFrames(int offset);
147  QList<int> getSelectedFramesByPos() const { return mSelectedFrames_byPosition; }
148 
154  bool canMoveSelectedFramesToOffset(int offset) const;
155 
156  Status save(const QString& sDataFolder, QStringList& attachedFiles, ProgressCallback progressStep);
157  virtual Status presave(const QString& sDataFolder) { Q_UNUSED(sDataFolder); return Status::SAFE; }
158 
159  bool isPaintable() const;
160 
162  QList<int> dirtyFrames() const { return mDirtyFrames; }
163 
166  void markFrameAsDirty(const int frameNumber) { mDirtyFrames << frameNumber; }
167 
169  void clearDirtyFrames() { mDirtyFrames.clear(); }
170 
171 protected:
172  void setId(int LayerId) { mId = LayerId; }
173  virtual KeyFrame* createKeyFrame(int position, Object*) = 0;
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:205
QList< int > dirtyFrames() const
Returns a list of dirty frame positions.
Definition: layer.h:162
bool newSelectionOfConnectedFrames(int position)
Make a selection from specified position until a blank spot appears The search is only looking forwar...
Definition: layer.cpp:456
void setExposureForSelectedFrames(int offset)
Add or subtract exposure from selected frames.
Definition: layer.cpp:520
bool canMoveSelectedFramesToOffset(int offset) const
Predetermines whether the frames can be moved to a new position depending on the offset.
Definition: layer.cpp:505
int count(const T &value) const const
QList< int > selectedKeyFramesPositions() const
Get selected keyframe positions sorted by position.
Definition: layer.h:71
bool empty() const const
void markFrameAsDirty(const int frameNumber)
Mark the frame position as dirty.
Definition: layer.h:166
Q_OBJECTQ_OBJECT
Definition: layer.h:38
void clearDirtyFrames()
Clear the list of dirty keyframes.
Definition: layer.h:169
bool reverseOrderOfSelection()
Reverse order of selected frames.
Definition: layer.cpp:610
Definition: object.h:41
QList< int > selectedKeyFramesByLast() const
Get selected keyframe positions based on the order they were selected.
Definition: layer.h:74