All Classes Namespaces Functions Variables Enumerations Properties Pages
object.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 OBJECT_H
18 #define OBJECT_H
19 
20 #include <memory>
21 #include <QObject>
22 #include <QList>
23 #include <QColor>
24 #include "layer.h"
25 #include "colorref.h"
26 #include "pencilerror.h"
27 #include "pencildef.h"
28 #include "objectdata.h"
29 
30 class QProgressDialog;
31 class QFile;
32 class LayerBitmap;
33 class LayerVector;
34 class LayerCamera;
35 class LayerSound;
36 class ObjectData;
37 class ActiveFramePool;
38 
39 
41 {
42  int startFrame;
43  int endFrame;
44  QTransform view;
45  Layer* currentLayer;
46  QSize exportSize;
47  QString filePath;
48  int fps;
49  int exportFps;
50  QString exportFormat;
51 };
52 
53 
54 class Object : public QObject
55 {
56  Q_OBJECT
57 
58 public:
59  Object(QObject* parent = nullptr);
60  virtual ~Object();
61 
62  void init();
63  void createWorkingDir();
64  void deleteWorkingDir() const;
65  void setWorkingDir(const QString& path); // used by crash recovery
66  void createDefaultLayers();
67 
68  QString filePath() const { return mFilePath; }
69  void setFilePath(QString strFileName) { mFilePath = strFileName; }
70 
71  QString workingDir() const { return mWorkingDirPath; }
72 
73  QString dataDir() const { return mDataDirPath; }
74  void setDataDir(QString dirPath) { mDataDirPath = dirPath; }
75 
76  QString mainXMLFile() const { return mMainXMLFile; }
77  void setMainXMLFile(QString file) { mMainXMLFile = file; }
78 
79  QDomElement saveXML(QDomDocument& doc) const;
80  bool loadXML(QDomElement element, ProgressCallback progressForward);
81 
82  void paintImage(QPainter& painter, int frameNumber, bool background, bool antialiasing) const;
83 
84  QString copyFileToDataFolder(QString strFilePath);
85 
86  // Color palette
87  ColorRef getColor(int index) const;
88  void setColor(int index, QColor newColor);
89  void setColorRef(int index, ColorRef newColorRef);
90  void addColor(QColor);
91  void movePaletteColor(int start, int end);
92  void moveVectorColor(int start, int end);
93 
94  void addColor(ColorRef newColor) { mPalette.append(newColor); }
95  void addColorAtIndex(int index, ColorRef newColor);
96  void removeColor(int index);
97  bool isColorInUse(int index);
98  void renameColor(int i, QString text);
99  int getColorCount() { return mPalette.size(); }
100  bool importPalette(QString filePath);
101  void importPaletteGPL(QFile& file);
102  void importPalettePencil(QFile& file);
103  void openPalette(QString filePath);
104 
105  bool exportPalette(const QString& filePath) const;
106  void exportPaletteGPL(QFile& file) const;
107  void exportPalettePencil(QFile& file) const;
108  QString savePalette(const QString& filePath) const;
109 
110  void loadDefaultPalette();
111 
112  LayerBitmap* addNewBitmapLayer();
113  LayerVector* addNewVectorLayer();
114  LayerSound* addNewSoundLayer();
115  LayerCamera* addNewCameraLayer();
116 
117  int getLayerCount() const;
118  Layer* getLayer(int i) const;
119  Layer* findLayerByName(QString strName, Layer::LAYER_TYPE type = Layer::UNDEFINED) const;
120  Layer* takeLayer(int layerId); // Note: transfer ownership of the layer
121 
122  bool swapLayers(int i, int j);
123  void deleteLayer(int i);
124  void deleteLayer(Layer*);
125  bool addLayer(Layer* layer);
126 
127  template<typename T>
128  std::vector<T*> getLayersByType() const
129  {
130  std::vector<T*> result;
131  for (Layer* layer : mLayers)
132  {
133  T* t = dynamic_cast<T*>(layer);
134  if (t)
135  result.push_back(t);
136  }
137  return result;
138  }
139 
140  // these functions need to be moved to somewhere...
141  bool exportFrames(int frameStart, int frameEnd, const LayerCamera* cameraLayer, QSize exportSize, QString filePath, QString format,
142  bool transparency, bool exportKeyframesOnly, const QString& layerName, bool antialiasing, QProgressDialog* progress, int progressMax) const;
143  bool exportX(int frameStart, int frameEnd, QTransform view, QSize exportSize, QString filePath, bool antialiasing);
144  bool exportIm(int frameStart, QTransform view, QSize cameraSize, QSize exportSize, QString filePath, QString format, bool antialiasing, bool transparency) const;
145 
146  void modification() { modified = true; }
147  bool isModified() { return modified; }
148  void setModified(bool b) { modified = b; }
149 
150  int getUniqueLayerID();
151 
152  ObjectData* data() const;
153  void setData(ObjectData*);
154 
155  int totalKeyFrameCount() const;
156  void updateActiveFrames(int frame) const;
157  void setActiveFramePoolSize(int sizeInMB);
158 
159 signals:
160  void layerViewChanged();
161  void paletteImported();
162 
163 private:
164  int getMaxLayerID();
165 
166  QString mFilePath; //< where this object come from. (empty if new project)
167  QString mWorkingDirPath; //< the folder that pclx will uncompress to.
168  QString mDataDirPath; //< the folder which contains all bitmap & vector image & sound files.
169  QString mMainXMLFile; //< the location of main.xml
170 
171  QList<Layer*> mLayers;
172  bool modified = false;
173 
174  QList<ColorRef> mPalette;
175 
176  std::unique_ptr<ObjectData> mData;
177  mutable std::unique_ptr<ActiveFramePool> mActiveFramePool;
178 };
179 
180 
181 #endif
int size() const const
void append(const T &value)
Q_OBJECTQ_OBJECT
Definition: layer.h:39
Definition: object.h:54
ActiveFramePool implemented a LRU cache to keep tracking the most recent accessed key frames A key fr...
QObject * parent() const const