1 #pragma once
2 
3 #ifndef CLEANUPSETTINGSMODEL_H
4 #define CLEANUPSETTINGSMODEL_H
5 
6 // TnzCore includes
7 #include "trasterimage.h"
8 
9 // ToonzLib includes
10 #include "toonz/cleanupparameters.h"
11 
12 // Qt includes
13 #include <QObject>
14 
15 //================================================
16 
17 //  Forward declarations
18 
19 class TXshLevel;
20 class TXshSimpleLevel;
21 class CleanupSettingsPopup;
22 
23 //================================================
24 
25 //********************************************************************
26 //    CleanupSettingsModel declaration
27 //********************************************************************
28 
29 //! The CleanupSettingsModel is the class responsible for concrete management of
30 //! cleanup settings in Toonz.
31 
32 /*!
33   In Toonz, the interface to cleanup settings follows the model/view
34 architecture,
35   where each Cleanup Settings panel is a view to a singleton model of this
36 class.
37 \n\n
38   The CleanupSettingsModel is responsible for handling signals that involve
39 cleanup
40   changes, such as user changes in the parameters coming from views, settings
41 reset or
42   switches from commands, and so on.
43 \n\n
44   A cleanup operation is roughly split in 2 parts: main processing and
45 post-processing.
46 \n\n
47   The main processing part takes the full original image and applies geometric
48 transforms
49   and other full-image analysis and processing. This is typically slow, and is
50 thus
51   performed by this class once for all views.
52 \n\n
53   Post-processing performs further local or pixel-per-pixel adjustments that are
54 sufficiently
55   fast to be carried out by single views on their preview area. This includes
56 <I> colors recognition,
57   colors adjustment, despeckling <\I> and \a antialiasing-related effects.
58 
59 \note This class also acts as a wrapper to CleanupParamaters' palette, attaching
60 to the
61       "current cleanup palette handle" accessible through the
62 TPaletteController.
63 */
64 class CleanupSettingsModel final : public QObject {
65   Q_OBJECT
66 
67   int m_listenersCount;    //!< Number of attached clients
68   int m_previewersCount;   //!< Number of attached *active* previewers
69   int m_cameraTestsCount;  //!< Number of attached camera tests
70 
71   CleanupParameters
72       m_backupParams;    //!< Stores a copy of current configuration.
73                          //!< It is used to track parameter changes.
74   int m_allowedActions;  //!< Actions allowed on changes commit.
75   int m_action;          //!< Cumulative parameter changes action.
76 
77   int m_c;                //!< Current xsheet position
78   TXshSimpleLevel *m_sl;  //!< Currently referenced level
79   TFrameId m_fid;         //!< Frame Id of currently referenced image
80   TFilePath m_clnPath;  //!< Path of the cln file associated with current level
81                         //!(if any)
82 
83   TRasterImageP m_original;  //!< The original preview/camera test image
84   TRasterImageP m_previewTransformed;     //!< The preview-transformed image
85   TRasterImageP m_cameraTestTransformed;  //!< The camera test-transformed image
86   TAffine m_transform;  //!< The preview transform (no camera test for now)
87 
88 public:
89   enum { LISTENER = 0x1, PREVIEWER = 0x2, CAMERATEST = 0x4 };
90   enum CommitMask { NONE = 0, INTERFACE, POSTPROCESS, FULLPROCESS };
91 
92 public:
93   CleanupSettingsModel();
94   ~CleanupSettingsModel();
95 
96   static CleanupSettingsModel *instance();
97 
98   CleanupParameters *getCurrentParameters();
99   void getCleanupFrame(TXshSimpleLevel *&sl, TFrameId &fid);
100 
clnPath()101   const TFilePath &clnPath() const { return m_clnPath; }
102 
103   //! Attaches an object to the model. It is not necessary to supply the object
104   //! itself, but a description of its role
105   //! is required. The object could be any combination of the LISTENER,
106   //! PREVIEWER or CAMERATEST flags. The role information
107   //! is used to activate centralized preview or camera test processing.
108   void attach(int objectFlag, bool doPreview = true);
109 
110   //! The inverse of attach(), it's used to deactivate the specified role
111   //! activities when no more attached objects to that role
112   //! remain.
113   void detach(int objectFlag);
114 
115   //! Returns the interesting info about a cleanup preview process.
116   //! Specifically, we have both the original and
117   //! affine-transformed images, and the applied transform.
getPreviewData(TRasterImageP & original,TRasterImageP & transformed,TAffine & transform)118   void getPreviewData(TRasterImageP &original, TRasterImageP &transformed,
119                       TAffine &transform) {
120     original    = m_original;
121     transformed = m_previewTransformed;
122     transform   = m_transform;
123   }
124 
125   //! Returns the interesting info about a camera test process. This includes
126   //! both the original and affine-transformed
127   //! image.
getCameraTestData(TRasterImageP & original,TRasterImageP & transformed)128   void getCameraTestData(TRasterImageP &original, TRasterImageP &transformed) {
129     original    = m_original;
130     transformed = m_cameraTestTransformed;
131   }
132 
133   //! Specifies the model behavior upon a cleanup parameters change commit. Once
134   //! the model receives a commitChanges()
135   //! notification, it tracks the changed parameters and decides how deep the
136   //! image under cleanup focus must be
137   //! re-processed to stick to the changes. The commit mask can be specified to
138   //! force the model to limit the performed
139   //! cleanup up to the specified stage. This is especially useful in case a
140   //! parameter changes frequently enough
141   //! that only the last of a long sequence of updates should be processed (e.g.
142   //! when dragging a parameter slider).
143   void setCommitMask(CommitMask allowedProcessing);
144 
145   void saveSettings(const TFilePath &clnPath);
146   bool loadSettings(const TFilePath &clnPath);
147 
148   //! Prompts to save current settings in case they have been modified. Returns
149   //! false if the
150   //! user decided to keep the settings and stop (cancel) any action affecting
151   //! them.
152   bool saveSettingsIfNeeded();
153 
154 public:
155   // NOTE: These should be moved to TCleanupper as soon as I get the chance
156 
157   static TFilePath getInputPath(TXshSimpleLevel *sl);  //!< Returns the \b
158                                                        //! encoded input cleanup
159   //! path for the specified
160   //! level.
161   static TFilePath getOutputPath(TXshSimpleLevel *sl,
162                                  const CleanupParameters *params);  //!< Returns
163                                                                     //! the \b
164   //! encoded
165   //! output
166   //! cleanup
167   //! path for
168   //! the
169   //! specified
170   //! level.
171 
172   static TFilePath getClnPath(TXshSimpleLevel *sl);  //!< Returns the \b decoded
173                                                      //! path for the level's \a
174   //! cln file.
175   static void saveSettings(CleanupParameters *params, const TFilePath &clnPath);
176   static bool loadSettings(CleanupParameters *params, const TFilePath &clnPath);
177 
178 public slots:
179 
180   //! Informs the model that current settings have changed. Settings analysis
181   //! and cleanup refreshes ensue. The modelChanged() signal is then emitted to
182   //! inform connected views of said change.
183   void commitChanges();
184 
185   void promptSave();  //!< Prompts to save current settings
186   void promptLoad();  //!< Prompts to load settings
187 
188   void restoreGlobalSettings();  //!< Reloads the project's cleanup settings (no
189                                  //! prompts)
190 
191 signals:
192 
193   //! Emitted whenever the model has finished processing a cleanup settings
194   //! change.
195   //! This happens after an explicit commitChange() has been invoked from a
196   //! view,
197   //! or other command-related settings change occur.
198   void modelChanged(bool needsPostProcess);
199 
200   //! Emitted every time the cleanup focus switches to a new image.
201   void imageSwitched();
202 
203   //! Emitted when the data retrievable with getPreviewData() and
204   //! getCameraTestData()
205   //! have been updated.
206   void previewDataChanged();
207 
208   //! Emitted when the model loads (or unloads) a cln settings file. It is also
209   //! invoked
210   //! when a cln is saved, since saving should put us in the same situation we
211   //! have after
212   //! a cln has been loaded.
213   void clnLoaded();
214 
215 private:
216   void commitChanges(int action);
217 
218   void connectSignals();
219   void disconnectSignals();
220 
221   void rebuildPreview();
222   void processFrame(TXshSimpleLevel *sl, TFrameId fid);
223 
224 private slots:
225 
226   void onSceneSwitched();
227   void onCellChanged();
228   void onPaletteChanged();
229 };
230 
231 #endif  // CLEANUPSETTINGSMODEL_H
232