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 
39 class Layer : public QObject
40 {
41  Q_OBJECT
42 
43 public:
44  enum LAYER_TYPE
45  {
46  UNDEFINED = 0,
47  BITMAP = 1,
48  VECTOR = 2,
49  MOVIE = 3, // not supported yet
50  SOUND = 4,
51  CAMERA = 5,
52  };
53 
54  explicit Layer(Object*, LAYER_TYPE);
55  ~Layer() override;
56 
57  int id() const { return mId; }
58  LAYER_TYPE type() const { return meType; }
59 
60  Object* object() const { return mObject; }
61  void setObject(Object* obj);
62 
63  void setName(QString name) { mName = name; }
64  QString name() const { return mName; }
65 
66  void switchVisibility() { mVisible = !mVisible; }
67 
68  bool visible() const { return mVisible; }
69  void setVisible(bool b) { mVisible = b; }
70 
71  virtual Status saveKeyFrameFile(KeyFrame*, QString dataPath) = 0;
72  virtual void loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressForward) = 0;
73  virtual QDomElement createDomElement(QDomDocument& doc) const = 0;
74  QDomElement createBaseDomElement(QDomDocument& doc) const;
75  void loadBaseDomElement(const QDomElement& elem);
76 
77  // KeyFrame interface
78  int getMaxKeyFramePosition() const;
79  int firstKeyFramePosition() const;
80 
81  bool keyExists(int position) const;
82  int getPreviousKeyFramePosition(int position) const;
83  int getNextKeyFramePosition(int position) const;
84  int getPreviousFrameNumber(int position, bool isAbsolute) const;
85  int getNextFrameNumber(int position, bool isAbsolute) const;
86 
87  int keyFrameCount() const { return static_cast<int>(mKeyFrames.size()); }
88 
89  bool addNewKeyFrameAt(int position);
90  bool addKeyFrame(int position, KeyFrame*);
91  bool removeKeyFrame(int position);
92  bool swapKeyFrames(int position1, int position2);
93  bool moveKeyFrameForward(int position);
94  bool moveKeyFrameBackward(int position);
95  bool loadKey(KeyFrame*);
96  KeyFrame* getKeyFrameAt(int position) const;
97  KeyFrame* getLastKeyFrameAtPosition(int position) const;
98  bool keyExistsWhichCovers(int frameNumber);
99  KeyFrame *getKeyFrameWhichCovers(int frameNumber) const;
100  bool getVisibility() const { return mVisible; }
101 
102  void foreachKeyFrame(std::function<void(KeyFrame*)>) const;
103 
104  void setModified(int position, bool isModified);
105 
106  // Handle selection
107  bool isFrameSelected(int position) const;
108  void setFrameSelected(int position, bool isSelected);
109  void toggleFrameSelected(int position, bool allowMultiple = false);
110  void extendSelectionTo(int position);
111  void selectAllFramesAfter(int position);
112  void deselectAll();
113 
114  bool moveSelectedFrames(int offset);
115 
116  Status save(const QString& sDataFolder, QStringList& attachedFiles, ProgressCallback progressStep);
117  virtual Status presave(const QString& sDataFolder) { Q_UNUSED(sDataFolder); return Status::SAFE; }
118 
119  bool isPaintable() const;
120 
121 protected:
122  void setId(int LayerId) { mId = LayerId; }
123  virtual KeyFrame* createKeyFrame(int position, Object*) = 0;
124 
125 private:
126  LAYER_TYPE meType = UNDEFINED;
127  Object* mObject = nullptr;
128  int mId = 0;
129  bool mVisible = true;
130  QString mName;
131 
132  std::map<int, KeyFrame*, std::greater<int>> mKeyFrames;
133 
134  // We need to keep track of selected frames ordered by last selected
135  // and by position.
136  // Both should be pre-sorted on each selection for optimization purpose when moving frames.
137  //
138  QList<int> mSelectedFrames_byLast; // Used to handle selection range (based on last selected
139  QList<int> mSelectedFrames_byPosition; // Used to handle frames movements on the timeline
140 };
141 
142 #endif
Q_OBJECTQ_OBJECT
Definition: layer.h:39
Definition: object.h:54