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