All Classes Namespaces Functions Variables Enumerations Properties Pages
editor.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 
18 #ifndef EDITOR_H
19 #define EDITOR_H
20 
21 #include <functional>
22 #include <memory>
23 #include <QObject>
24 #include "pencilerror.h"
25 #include "pencildef.h"
26 
27 
28 class QClipboard;
29 class QTemporaryDir;
30 class Object;
31 class KeyFrame;
32 class BitmapImage;
33 class VectorImage;
34 class LayerCamera;
35 class MainWindow2;
36 class BaseManager;
37 class ColorManager;
38 class ToolManager;
39 class LayerManager;
40 class PlaybackManager;
41 class ViewManager;
42 class PreferenceManager;
43 class SelectionManager;
44 class SoundManager;
45 class OverlayManager;
46 class ClipboardManager;
47 class ScribbleArea;
48 class TimeLine;
49 class BackupElement;
50 class ActiveFramePool;
51 class Layer;
52 
53 enum class SETTING;
54 
55 class Editor : public QObject
56 {
57  Q_OBJECT
58 
59  Q_PROPERTY(ColorManager* color READ color)
60  Q_PROPERTY(ToolManager* tools READ tools)
61  Q_PROPERTY(LayerManager* layers READ layers)
62  Q_PROPERTY(PlaybackManager* playback READ playback)
63  Q_PROPERTY(ViewManager* view READ view)
64  Q_PROPERTY(PreferenceManager* preference READ preference)
65  Q_PROPERTY(SoundManager* sound READ sound)
66  Q_PROPERTY(SelectionManager* select READ select)
67  Q_PROPERTY(OverlayManager* overlays READ overlays)
68  Q_PROPERTY(ClipboardManager* clipboards READ clipboards)
69 
70 public:
71  explicit Editor(QObject* parent = nullptr);
72  ~Editor() override;
73 
74  bool init();
75 
76  /************************************************************************/
77  /* Managers */
78  /************************************************************************/
79  ColorManager* color() const { return mColorManager; }
80  ToolManager* tools() const { return mToolManager; }
81  LayerManager* layers() const { return mLayerManager; }
82  PlaybackManager* playback() const { return mPlaybackManager; }
83  ViewManager* view() const { return mViewManager; }
84  PreferenceManager* preference() const { return mPreferenceManager; }
85  SoundManager* sound() const { return mSoundManager; }
86  SelectionManager* select() const { return mSelectionManager; }
87  OverlayManager* overlays() const { return mOverlayManager; }
88  ClipboardManager* clipboards() const { return mClipboardManager; }
89 
90  Object* object() const { return mObject.get(); }
91  Status openObject(const QString& strFilePath, const std::function<void(int)>& progressChanged, const std::function<void(int)>& progressRangeChanged);
92  Status setObject(Object* object);
93  void updateObject();
94  void prepareSave();
95 
96  void setScribbleArea(ScribbleArea* pScirbbleArea) { mScribbleArea = pScirbbleArea; }
97  ScribbleArea* getScribbleArea() { return mScribbleArea; }
98 
99  int currentFrame() const;
100  int fps();
101  void setFps(int fps);
102 
103  int currentLayerIndex() const { return mCurrentLayerIndex; }
104  void setCurrentLayerIndex(int i);
105 
106  void scrubTo(int frameNumber);
107 
111  void setLayerVisibility(LayerVisibility visibility);
112  LayerVisibility layerVisibility();
113 
114  qreal viewScaleInversed();
115  void deselectAll() const;
116  void selectAll() const;
117 
118  void clipboardChanged();
119 
120  // backup
121  int mBackupIndex;
122  BackupElement* currentBackup();
123  QList<BackupElement*> mBackupList;
124 
125 signals:
126 
128  void scrubbed(int frameNumber);
129 
131  void frameModified(int frameNumber);
132 
134  void framesModified();
135  void selectedFramesChanged();
136 
137  void updateTimeLine() const;
138  void updateLayerCount();
139  void updateBackup();
140 
141  void objectLoaded();
142 
143  void fpsChanged(int fps);
144 
145  void needSave();
146  void needDisplayInfo(const QString& title, const QString& body);
147  void needDisplayInfoNoTitle(const QString& body);
148 
149  void canCopyChanged(bool enabled);
150  void canPasteChanged(bool enabled);
151 
152 public: //slots
153 
158  void updateCurrentFrame();
159 
163  void updateFrame(int frameNumber);
164 
165  void clearCurrentFrame();
166 
167  bool importImage(const QString& filePath);
168  bool importGIF(const QString& filePath, int numOfImages = 0);
169  void restoreKey();
170 
171  void scrubNextKeyFrame();
172  void scrubPreviousKeyFrame();
173  void scrubForward();
174  void scrubBackward();
175 
176  KeyFrame* addNewKey();
177  void removeKey();
178 
179  void switchVisibilityOfLayer(int layerNumber);
180  void swapLayers(int i, int j);
181 
182  void onModified(int layer, int frame);
183  void backup(const QString& undoText);
184  bool backup(int layerNumber, int frameNumber, const QString& undoText);
195  void sanitizeBackupElementsAfterLayerDeletion(int layerIndex);
196  void undo();
197  void redo();
198 
199  void copy();
200  void copyAndCut();
201  void pasteFromPreviousFrame();
202  void paste();
203 
204  bool canCopy() const;
205  bool canPaste() const;
206 
207  void increaseLayerVisibilityIndex();
208  void decreaseLayerVisibilityIndex();
209  void flipSelection(bool flipVertical);
210  void repositionImage(QPoint transform, int frame);
211 
212  void clearTemporary();
213  void addTemporaryDir(QTemporaryDir* dir);
214 
215  void settingUpdated(SETTING);
216 
217  void dontAskAutoSave(bool b) { mAutosaveNeverAskAgain = b; }
218  bool autoSaveNeverAskAgain() const { return mAutosaveNeverAskAgain; }
219  void resetAutoSaveCounter();
220 
221 private:
222  bool importBitmapImage(const QString&, int space = 0);
223  bool importVectorImage(const QString&);
224 
225  void pasteToCanvas(BitmapImage* bitmapImage, int frameNumber);
226  void pasteToCanvas(VectorImage* vectorImage, int frameNumber);
227  void pasteToFrames();
228 
229  bool canCopyBitmapImage(BitmapImage* bitmapImage) const;
230  bool canCopyFrames(const Layer* layer) const;
231  bool canCopyVectorImage(const VectorImage* vectorImage) const;
232 
233  // the object to be edited by the editor
234  std::unique_ptr<Object> mObject;
235 
236  int mFrame = 1; // current frame number.
237  int mCurrentLayerIndex = 0; // the current layer to be edited/displayed
238 
239  ScribbleArea* mScribbleArea = nullptr;
240 
241  ColorManager* mColorManager = nullptr;
242  ToolManager* mToolManager = nullptr;
243  LayerManager* mLayerManager = nullptr;
244  PlaybackManager* mPlaybackManager = nullptr;
245  ViewManager* mViewManager = nullptr;
246  PreferenceManager* mPreferenceManager = nullptr;
247  SoundManager* mSoundManager = nullptr;
248  SelectionManager* mSelectionManager = nullptr;
249  OverlayManager* mOverlayManager = nullptr;
250  ClipboardManager* mClipboardManager = nullptr;
251 
252  std::vector< BaseManager* > mAllManagers;
253 
254  bool mIsAutosave = true;
255  int mAutosaveNumber = 12;
256  int mAutosaveCounter = 0;
257  bool mAutosaveNeverAskAgain = false;
258 
259  void makeConnections();
260  KeyFrame* addKeyFrame(int layerNumber, int frameNumber);
261 
262  QList<QTemporaryDir*> mTemporaryDirs;
263 
264  // backup
265  void clearUndoStack();
266  void updateAutoSaveCounter();
267  int mLastModifiedFrame = -1;
268  int mLastModifiedLayer = -1;
269 };
270 
271 #endif
void setLayerVisibility(LayerVisibility visibility)
The visibility value should match any of the VISIBILITY enum values.
Definition: editor.cpp:736
void scrubbed(int frameNumber)
This should be emitted after scrubbing.
void frameModified(int frameNumber)
This should be emitted after modifying the frame content.
Q_OBJECTQ_OBJECT
Definition: layer.h:38
Q_PROPERTY(...)
void updateFrame(int frameNumber)
Will call update() and update the canvas Only call this directly If you need the cache to be intact a...
Definition: editor.cpp:1052
Definition: object.h:41
ActiveFramePool implemented a LRU cache to keep tracking the most recent accessed key frames A key fr...
Definition: editor.h:55
QObject * parent() const const
void updateCurrentFrame()
Will call update() and update the canvas Only call this directly If you need the cache to be intact a...
Definition: editor.cpp:1057
void sanitizeBackupElementsAfterLayerDeletion(int layerIndex)
Restores integrity of the backup elements after a layer has been deleted.
Definition: editor.cpp:313
void framesModified()
This should be emitted after modifying multiple frames.