1 #pragma once
2 
3 #ifndef FLIPBOOK_H
4 #define FLIPBOOK_H
5 
6 #include "toonzqt/flipconsole.h"
7 #include "imageviewer.h"
8 
9 #include "tlevel_io.h"
10 #include "tpalette.h"
11 #include "tsound.h"
12 #include "filebrowserpopup.h"
13 
14 #include "tfx.h"
15 #include "toonz/txsheet.h"
16 
17 #include <QTimer>
18 
19 #include "toonzqt/flipconsoleowner.h"
20 
21 class QPoint;
22 class TPalette;
23 class TFilePath;
24 class FlipBook;
25 
26 //=============================================================================
27 
28 class SaveImagesPopup final : public FileBrowserPopup {
29   Q_OBJECT
30 
31   FlipBook *m_flip;
32 
33 public:
34   SaveImagesPopup(FlipBook *flip);
35 
36   bool execute() override;
37 };
38 
39 class LoadImagesPopup final : public FileBrowserPopup {
40   Q_OBJECT
41 
42   DVGui::LineEdit *m_fromField;
43   DVGui::LineEdit *m_toField;
44   DVGui::LineEdit *m_stepField;
45   DVGui::LineEdit *m_shrinkField;
46 
47   int m_minFrame, m_maxFrame;
48   int m_from, m_to, m_step, m_shrink;
49 
50   QStringList m_appendFilterTypes;
51   QStringList m_loadFilterTypes;
52 
53   FlipBook *m_flip;
54 
55 public:
56   LoadImagesPopup(FlipBook *flip);
57 
58   bool execute() override;
59   bool executeApply() override;
60   bool doLoad(bool append);
61 
62 public slots:
63 
64   void onFilePathClicked(const TFilePath &);
65   void onEditingFinished();
66 };
67 
68 //=============================================================================
69 // FlipBookPool
70 //-----------------------------------------------------------------------------
71 
72 class FlipBook;
73 
74 class FlipBookPool {
75   std::map<int, FlipBook *> m_pool;
76   std::map<int, QRect> m_geometryPool;
77   int m_overallFlipCount;
78 
79   TFilePath m_historyPath;
80 
81 public:
82   FlipBookPool();
83   ~FlipBookPool();
84 
85   static FlipBookPool *instance();
86 
87   void load(const TFilePath &historyPath);
88   void save() const;
89 
90   FlipBook *pop();
91   void push(FlipBook *flipbook);
92 };
93 
94 //=============================================================================
95 // FlipBook
96 //-----------------------------------------------------------------------------
97 class TSoundOutputDevice;
98 class TSoundTrack;
99 class SaveImagesPopup;
100 class LoadImagesPopup;
101 class TFx;
102 class TXsheet;
103 class TXshSimpleLevel;
104 
105 class TPanelTitleBar;
106 class TPanelTitleBarButton;
107 
108 class FlipBook : public QWidget,
109                  public TSoundOutputDeviceListener,
110                  public FlipConsoleOwner {
111   Q_OBJECT
112 
113 protected:
114   int m_poolIndex;
115 
116   QString m_viewerTitle;
117   QString m_title, m_title1;
118   ImageViewer *m_imageViewer;
119   FlipConsole *m_flipConsole;
120 
121   int m_shrink;
122   int m_framesCount;
123   TRect m_loadbox;
124   TDimension m_dim;
125   std::map<std::string, TRect>
126       m_loadboxes;  // id in the cash, rect loaded actually
127   class Level {
128   public:
Level(const TLevelP & level,const TFilePath & fp,int fromIndex,int toIndex,int step)129     Level(const TLevelP &level, const TFilePath &fp, int fromIndex, int toIndex,
130           int step)
131         : m_level(level)
132         , m_fp(fp)
133         , m_fromIndex(fromIndex)
134         , m_toIndex(toIndex)
135         , m_step(step)
136         , m_randomAccessRead(false)
137         , m_incrementalIndexing(false)
138         , m_premultiply(false) {}
139     TLevelP m_level;
140     int m_fromIndex, m_toIndex, m_step;
141     bool m_incrementalIndexing;
142     bool m_randomAccessRead;
143 
144     // Specified if the level is needed to be premultiplied on display.
145     // It will be true for the files of which the "premultiply" option is
146     // activated in the Preferences > Loading > "Level Settings by File Format".
147     // By default, PNG will be loaded with being premultiplied so that it will
148     // be displayed properly.
149     bool m_premultiply;
150 
151     TFilePath m_fp;
152 
153     TFrameId flipbookIndexToLevelFrame(int index);
154     int getIndexesCount();
155   };
156 
157   std::vector<Level> m_levels;
158   std::vector<QString> m_levelNames;
159   TPalette *m_palette;
160 
161   bool m_playSound;
162   std::vector<std::string> m_addedInCache;
163   TSoundOutputDevice *m_player;
164   TSoundTrack *m_snd;
165 
166   // bool m_doCompare;
167 
168   // questi sono per il saving dei frame
169   TLevelWriterP m_lw;
170   TLevelReaderP m_lr;
171   TXshSimpleLevel *m_xl;
172   int m_currentFrameToSave;
173   SaveImagesPopup *m_savePopup;
174   LoadImagesPopup *m_loadPopup;
175 
176   bool m_isPreviewFx;
177   TFxP m_previewedFx;
178   TXsheetP m_previewXsh;
179   QTimer m_previewUpdateTimer;
180   bool m_freezed;
181   UCHAR m_flags;
182 
183   TPanelTitleBarButton *m_freezeButton;
184 
185 public:
186   enum Flags { eDontKeepFilesOpened = 0x1 };
187 
188   FlipBook(QWidget *parent = 0, QString viewerTitle = QString(),
189            std::vector<int> flipConsoleButtonMask =
190                {FlipConsole::eFilledRaster, FlipConsole::eDefineSubCamera,
191                 FlipConsole::eLocator, FlipConsole::eZoomIn,
192                 FlipConsole::eZoomOut, FlipConsole::eFlipHorizontal,
193                 FlipConsole::eFlipVertical, FlipConsole::eResetView},
194            UCHAR flags = 0, bool isColorModel = false);
195   ~FlipBook();
196   void setLevel(const TFilePath &path, TPalette *palette = 0, int from = -1,
197                 int to = -1, int step = 1, int shrink = 1, TSoundTrack *snd = 0,
198                 bool append = false, bool isToonzOutput = false);
199   void setLevel(TFx *previewedFx, TXsheet *xsh, TLevel *level,
200                 TPalette *palette = 0, int from = -1, int to = -1, int step = 1,
201                 int currentFrame = 1, TSoundTrack *snd = 0);
202   void setLevel(TXshSimpleLevel *xl);
onPlayCompleted()203   void onPlayCompleted() override {}
204   bool doSaveImages(TFilePath fp);
getCurrentFrame()205   int getCurrentFrame() { return m_flipConsole->getCurrentFrame(); }
getLevelZoomTitle()206   QString getLevelZoomTitle() const { return m_title1 + m_title; }
207   void setTitle(const QString &title);
getTitle()208   QString getTitle() const { return m_viewerTitle; }
209   void showFrame(int frame);
210 
211   TFx *getPreviewedFx() const;
212   TXsheet *getPreviewXsheet() const;
isFreezed()213   bool isFreezed() const { return m_freezed; }
214   TRectD getPreviewedImageGeometry() const;
215   void schedulePreviewedFxUpdate();
216 
217   bool canAppend();
218   bool isSavable() const;
219 
setPoolIndex(int poolIndex)220   void setPoolIndex(int poolIndex) { m_poolIndex = poolIndex; }
getPoolIndex()221   int getPoolIndex() const { return m_poolIndex; }
222 
223   // show / hide the red border line indicating the frame is under rendering
setIsRemakingPreviewFx(bool on)224   void setIsRemakingPreviewFx(bool on) {
225     m_imageViewer->setIsRemakingPreviewFx(on);
226   }
227   void addFreezeButtonToTitleBar();
228 
229   void setProgressBarStatus(const std::vector<UCHAR> *pbStatus);
230   const std::vector<UCHAR> *getProgressBarStatus() const;
231 
232   void adaptGeometry(const TRect &interestingImgRect, const TRect &imgRect);
233 
234   // When Fx preview is called without the subcamera,
235   // render the full region of camera by resize flipbook and
236   // zoom-out the rendered image.
237   void adaptGeometryForFullPreview(const TRect &imgRect);
238 
239   void reset();
240 
241   void onDrawFrame(int frame, const ImagePainter::VisualSettings &vs) override;
242 
243   void minimize(bool doMinimize);
244 
getImageViewer()245   ImageViewer *getImageViewer() { return m_imageViewer; }
getLoadbox()246   TRect getLoadbox() const { return m_loadbox; }
247   void setLoadbox(const TRect &box);
getImageSize()248   TDimension getImageSize() const { return m_dim; }
249 
250   void swapBuffers() override;
251   void changeSwapBehavior(bool enable) override;
252 
253 private:
254   // When viewing the tlv, try to cache all frames at the beginning.
255   // NOTE : fromFrame and toFrame are frame numbers displayed on the flipbook
256   void loadAndCacheAllTlvImages(Level level, int fromFrame, int toFrame);
257 
258   friend class PreviewFxManager;
259 
260 protected:
261   void clearCache();
262   void adaptWidGeometry(const TRect &interestWidRect, const TRect &imgWidRect,
263                         bool keepPosition);
264 
265   void dragEnterEvent(QDragEnterEvent *e) override;
266   void dropEvent(QDropEvent *e) override;
267 
268   void playAudioFrame(int frame);
269   TImageP getCurrentImage(int frame);
270 
271   void showEvent(QShowEvent *e) override;
272   void hideEvent(QHideEvent *e) override;
273   void focusInEvent(QFocusEvent *e) override;
274   void resizeEvent(QResizeEvent *e) override;
275 
enterEvent(QEvent * e)276   void enterEvent(QEvent *e) override { m_flipConsole->makeCurrent(); }
277 
278 signals:
279 
280   void closeFlipBook(FlipBook *);
281   // when freeze button is released, emit the signal to PreviewFxManager for
282   // re-rendering
283   void unfreezed(FlipBook *);
284   // when using the flip module, this signal is to show the loaded level names
285   // in application's title bar
286   void imageLoaded(QString &);
287 
288 protected slots:
289 
290   void onDoubleClick(QMouseEvent *me);
291   void onButtonPressed(FlipConsole::EGadget button);
292   void onCloseButtonPressed();
293   void saveImage();
294 
295   void freeze(bool on);
296 
297 public slots:
298 
299   void saveImages();
300   void loadImages();
301 
302   void performFxUpdate();
303   void regenerate();
304   void regenerateFrame();
305   void clonePreview();
306   void freezePreview();
307   void unfreezePreview();
308 };
309 
310 // utility
311 // returns pointer to the opened flipbook to control modality.
312 
313 FlipBook *viewFile(const TFilePath &fp, int from = -1, int to = -1,
314                    int step = -1, int shrink = -1, TSoundTrack *snd = 0,
315                    FlipBook *flipbook = 0, bool append = false,
316                    bool isToonzOutput = false);
317 
318 #endif  // FLIPBOOK_H
319