1 #pragma once
2 
3 #ifndef SCENERESOURCES_INCLUDED
4 #define SCENERESOURCES_INCLUDED
5 
6 #include "tfilepath.h"
7 #include <map>
8 
9 #undef DVAPI
10 #undef DVVAR
11 #ifdef TOONZLIB_EXPORTS
12 #define DVAPI DV_EXPORT_API
13 #define DVVAR DV_EXPORT_VAR
14 #else
15 #define DVAPI DV_IMPORT_API
16 #define DVVAR DV_IMPORT_VAR
17 #endif
18 
19 #include <QString>
20 #include <QSet>
21 
22 //=============================================================================
23 // forward declarations
24 class TProject;
25 class ToonzScene;
26 class TXshSimpleLevel;
27 class TXshPaletteLevel;
28 class TXsheet;
29 
30 //=============================================================================
31 /*!ResourceImportStrategy allows to control the resource import behaviour during
32    load operation. It can be statically defined (i.e. DONT_IMPORT).
33    Subclass it to provide dialog boxes.
34    All the actual file copies are recorded (ImportLog).
35    \sa ResourceImportMap, ResourceImportDialog (defined in toonz)
36 */
37 //=============================================================================
38 
39 class DVAPI ResourceImportStrategy {
40 public:
41   typedef std::map<TFilePath, TFilePath> ImportLog;
42   enum { DONT_IMPORT, IMPORT_AND_OVERWRITE, IMPORT_AND_RENAME };
43 
44   ResourceImportStrategy(int strategy);
aborted()45   virtual bool aborted() { return false; }
46 
47   // note: this attribute is automatically initialized (from the preferences)
isChildFolderEnabled()48   bool isChildFolderEnabled() const { return m_childFolderEnabled; }
setChildFolderEnabled(bool enabled)49   void setChildFolderEnabled(bool enabled) { m_childFolderEnabled = enabled; }
50 
51   // accepts and returns coded file (e.g. +drawings/folder/a.pli)
52   virtual TFilePath process(ToonzScene *dstScene, ToonzScene *srcScene,
53                             TFilePath srcFile) = 0;
54 
getImportLog()55   const ImportLog &getImportLog() const { return m_importLog; }
56 
57 private:
58   bool m_childFolderEnabled;
59   int m_strategy;
60   ImportLog m_importLog;
61 };
62 
63 //=============================================================================
64 //! ResourceProcessor is the base class for resource processing.
65 /*! It is a visitor accepted by SceneResources */
66 //=============================================================================
67 
68 class TXshSoundLevel;
69 
70 class DVAPI ResourceProcessor {
71 public:
process(TXshSimpleLevel * sl)72   virtual void process(TXshSimpleLevel *sl) {}
process(TXshPaletteLevel * sl)73   virtual void process(TXshPaletteLevel *sl) {}
process(TXshSoundLevel * sl)74   virtual void process(TXshSoundLevel *sl) {}
aborted()75   virtual bool aborted() const { return false; }
76 };
77 
78 //=============================================================================
79 //! The SceneResource class is the base class of scene resource.
80 /*!The class contains a pointer to scene, a boolean to know if scene is
81    untiltled and a path to know old save path.
82 */
83 //=============================================================================
84 
85 class DVAPI SceneResource {
86 protected:
87   ToonzScene *m_scene;
88   bool m_untitledScene;
89   TFilePath m_oldSavePath;
90 
91 public:
92   /*!
93 Constructs SceneResource with default value and \b scene.
94 */
95   SceneResource(ToonzScene *scene);
96   /*!
97 Destroys the SceneResource object.
98 */
99   virtual ~SceneResource();
100 
101   //! save the resource to the disk using the updated path. Note: it calls
102   //! SceneResource::updatePath(fp) before
103   virtual void save() = 0;
104   //! change the resource internal path, according to the scene
105   virtual void updatePath() = 0;
106   //! change back the resource internal path to its original value
107   virtual void rollbackPath() = 0;
108 
109   //! visitor pattern
110   virtual void accept(ResourceProcessor *processor) = 0;
111   /*!
112 If scene is untitled update save path.
113 */
114   void updatePath(TFilePath &fp);
115 
116   virtual bool isDirty()                = 0;
117   virtual QStringList getResourceName() = 0;
118 };
119 
120 //=============================================================================
121 //! The ScenePalette class provides a container for scene palette resource and
122 //! allows its management.
123 /*!Inherits \b SceneResource.
124 \n The class, more than \b SceneResources, contains a pointer to a palette level
125    \b TXshPaletteLevel, two path for old save path, one decoded and one no,
126    two path for old scanned path, one decoded and one no. The class allows to
127 save
128    simple level save() and to update or to restore old path (save and scan)
129 updatePath()
130    rollbackPath().
131 */
132 //=============================================================================
133 
134 class DVAPI ScenePalette final : public SceneResource {
135   TXshPaletteLevel *m_pl;
136   TFilePath m_oldPath, m_oldActualPath;
137 
138 public:
139   /*!
140 Constructs SceneLevel with \b ToonzScene \b scene and \b TXshPaletteLevel \b pl.
141 */
142   ScenePalette(ToonzScene *scene, TXshPaletteLevel *pl);
143   /*!
144 Save simple level in right path.
145 */
146   void save() override;
147   /*!
148 Update simple level path.
149 */
150   void updatePath() override;
151   /*!
152 Set simple level path to old path.
153 */
154   void rollbackPath() override;
155 
accept(ResourceProcessor * processor)156   void accept(ResourceProcessor *processor) override {
157     processor->process(m_pl);
158   }
159 
160   bool isDirty() override;
161   QStringList getResourceName() override;
162 };
163 
164 //=============================================================================
165 //! The SceneLevel class provides a container for scene level resource and
166 //! allows its management.
167 /*!Inherits \b SceneResource.
168 \n The class, more than \b SceneResources, contains a pointer to a simple level
169    \b TXshSimpleLevel, two path for old save path, one decoded and one no,
170    two path for old scanned path, one decoded and one no. The class allows to
171 save
172    simple level save() and to update or to restore old path (save and scan)
173 updatePath()
174    rollbackPath().
175 */
176 //=============================================================================
177 
178 class DVAPI SceneLevel final : public SceneResource {
179   TXshSimpleLevel *m_sl;
180   TFilePath m_oldPath, m_oldActualPath;
181   TFilePath m_oldScannedPath, m_oldActualScannedPath;
182   TFilePath m_oldRefImgPath, m_oldActualRefImgPath;
183 
184 public:
185   /*!
186 Constructs SceneLevel with \b ToonzScene \b scene and \b TXshSimpleLevel \b sl.
187 */
188   SceneLevel(ToonzScene *scene, TXshSimpleLevel *sl);
189   /*!
190 Save simple level in right path.
191 */
192   void save() override;
193   /*!
194 Update simple level path.
195 */
196   void updatePath() override;
197   /*!
198 Set simple level path to old path.
199 */
200   void rollbackPath() override;
201 
accept(ResourceProcessor * processor)202   void accept(ResourceProcessor *processor) override {
203     processor->process(m_sl);
204   }
205 
206   bool isDirty() override;
207   QStringList getResourceName() override;
208 };
209 
210 //=============================================================================
211 //! The SceneSoundtrack class provides a container for scene sound track
212 //! resource and allows its management.
213 /*!Inherits \b SceneResource.
214 \n The class, more than \b SceneResources, contains a pointer to a sound column
215    \b TXshSoundColumn, two path for old save path, one decoded and one no.
216    The class allows to save sound track save() and to update or to restore old
217 path
218    updatePath(), rollbackPath().
219 */
220 //=============================================================================
221 
222 class DVAPI SceneSound final : public SceneResource {
223   TXshSoundLevel *m_sl;
224   TFilePath m_oldPath, m_oldActualPath;
225 
226 public:
227   /*!
228 Constructs SceneSoundtrack with \b ToonzScene \b scene and \b TXshSoundLevel \b
229 sl.
230 */
231   SceneSound(ToonzScene *scene, TXshSoundLevel *sl);
232 
233   /*!
234 Save sound column in right path.
235 */
236   void save() override;
237   /*!
238 Update sound track path.
239 */
240   void updatePath() override;
241   /*!
242 Set sound track path to old path.
243 */
244   void rollbackPath() override;
245 
accept(ResourceProcessor * processor)246   void accept(ResourceProcessor *processor) override {
247     processor->process(m_sl);
248   }
249 
isDirty()250   bool isDirty() override { return false; }
getResourceName()251   QStringList getResourceName() override { return QStringList(); }
252 };
253 
254 //=============================================================================
255 //! The SceneResources class provides scene resources and allows their
256 //! management.
257 /*!The class contains a vector of pointer to \b SceneResource, a pointer to
258    scene,
259    a pointer to xsheet, a boolean to know if commit is done and a boolean to
260    know
261    if scene is untitled.
262    The class allows to save scene resources save(), and to update or to restore
263    old
264    scene resources paths updatePaths(), rollbackPaths().
265 */
266 //=============================================================================
267 
268 class DVAPI SceneResources {
269   std::vector<SceneResource *> m_resources;
270   ToonzScene *m_scene;
271   TXsheet *m_subXsheet;
272   bool m_commitDone;
273   bool m_wasUntitled;
274 
275   /*!
276 Set the vector of pointer to \b SceneResource to scene resources.
277 */
278   void getResources();
279 
280 public:
281   // n.b. se subXsheet != 0 salva solo le risorse utilizzate nel subxsheet
282   /*!
283 Constructs SceneResources with \b ToonzScene \b scene and
284 \b TXsheet \b subXsheet.
285 */
286   SceneResources(ToonzScene *scene, TXsheet *subXsheet);
287   /*!
288 Destroys the SceneResources object.
289 */
290   ~SceneResources();
291   /*
292 !Save all scene resources in according to \b newScenePath.
293 \n
294 Set scene save path to \b newScenePath, call \b save()
295 for all scene resources and reset scene save path to
296 old save path.
297 If pointer to subXsheet is different from zero save only resources
298 used in subXsheet.
299 */
300   void save(const TFilePath newScenePath);
301 
302   /*!
303 Update all resouces paths.
304 */
305   void updatePaths();
306   /*!
307 Set all resouces paths to old paths.
308 */
309   void rollbackPaths();
310   /*!
311 Set boolean \b m_commitDone to true.
312 \n
313 If doesn't make \b commit() destroyer calls \b rollbackPaths().
314 */
commit()315   void commit() { m_commitDone = true; }
316 
317   void accept(ResourceProcessor *processor, bool autoCommit = true);
318 
319   // return the name list of dirty resources
320   void getDirtyResources(QStringList &dirtyResources);
321 
322 private:
323   // not implemented
324   SceneResources(const SceneResources &);
325   SceneResources &operator=(const SceneResources &);
326 };
327 
328 //=============================================================================
329 
330 class DVAPI ResourceImporter final : public ResourceProcessor {
331 public:
332   ResourceImporter(ToonzScene *scene, TProject *dstProject,
333                    ResourceImportStrategy &strategy);
334   ~ResourceImporter();
335 
336   // se serve modifica path. path non deve esistere su disco
337   // ritorna true se ha modificato path;
338   // n.b. path puo' essere della forma +drawings/xxx.pli
339   bool makeUnique(TFilePath &path);
340 
341   TFilePath getImportedScenePath() const;
342 
343   TFilePath codePath(const TFilePath &oldCodedPath,
344                      const TFilePath &newActualPath);
345 
346   void process(TXshSimpleLevel *sl) override;
347   void process(TXshPaletteLevel *sl) override;
348   void process(TXshSoundLevel *sl) override;
349 
aborted()350   bool aborted() const override { return m_importStrategy.aborted(); }
351 
352   static std::string extractPsdSuffix(TFilePath &path);
353   static TFilePath buildPsd(const TFilePath &path, const std::string &suffix);
354 
355 private:
356   ToonzScene *m_scene;
357   TProject *m_dstProject;
358   ToonzScene *m_dstScene;
359   ResourceImportStrategy &m_importStrategy;
360 };
361 
362 //=============================================================================
363 
364 // Rende tutte le risorse locali: tutte quelle con path assoluto
365 // vengono copiate dentro il progetto
366 
367 class DVAPI ResourceCollector final : public ResourceProcessor {
368   ToonzScene *m_scene;
369   int m_count;
370   std::map<TFilePath, TFilePath> m_collectedFiles;
371 
372 public:
373   ResourceCollector(ToonzScene *scene);
374   ~ResourceCollector();
375 
376   // se serve modifica path. path non deve esistere su disco
377   // ritorna true se ha modificato path;
378   // n.b. path e' della forma +drawings/xxx.pli
379   bool makeUnique(TFilePath &path);
380 
getCollectedResourceCount()381   int getCollectedResourceCount() const { return m_count; }
382 
383   void process(TXshSimpleLevel *sl) override;
384   void process(TXshSoundLevel *sl) override;
385   void process(TXshPaletteLevel *pl) override;
386 };
387 
388 #endif
389