1 #pragma once
2 
3 #ifndef IOCOMMAND_H
4 #define IOCOMMAND_H
5 
6 #include <memory>
7 
8 // TnzLib includes
9 #include "toonz/preferences.h"
10 
11 // TnzCore includes
12 #include "tfilepath.h"
13 #include "tundo.h"
14 
15 // Qt includes
16 #include <QDialog>
17 
18 // boost includes
19 #include <boost/optional.hpp>
20 
21 // STD includes
22 #include <set>
23 
24 //====================================================
25 
26 //  Forward declarations
27 
28 class TXshLevel;
29 class TXshSimpleLevel;
30 class TXshSoundLevel;
31 class ToonzScene;
32 class TCamera;
33 class TPropertyGroup;
34 class TXsheet;
35 
36 namespace DVGui {
37 class ProgressDialog;
38 }
39 
40 //====================================================
41 
42 /*! \file       iocommand.h
43     \brief      Contains command functions related to Toonz I/O.          */
44 
45 /*! \brief      Contains command functions related to Toonz I/O.          */
46 
47 namespace IoCmd {
48 
49 /*!
50   \brief    Composite data used describing a group of Toonz resources
51             to be loaded.
52 
53   \details  A Toonz resource can refer to either a level, audio
54             file, or Toonz scene to be loaded as sub-xsheet.
55 
56   \sa       The loadResources() function.
57 */
58 
59 struct LoadResourceArguments {
60   /*! \details    A loading ScopedBlock represents a monolithic loading
61             procedure, with integrated progress dialog and undo block.        */
62 
63   struct ScopedBlock
64       : private TUndoScopedBlock  //!  Scope variable hosting a loading process.
65   {
66     struct Data;
67 
68   public:
69     ScopedBlock();
70     ~ScopedBlock();  //!< Invokes all necessary Toonz updates before control is
71                      //! returned,
72     //!  included posting of the embedded undo block.
73     DVGui::ProgressDialog &progressDialog()
74         const;  //!< Progress dialog shown on multiple resource loads.
75 
dataLoadResourceArguments::ScopedBlock76     Data &data() const {
77       return *m_data;
78     }  //!< Internal data used by the loading procedure.
79 
80   private:
81     std::unique_ptr<Data> m_data;
82   };
83 
84   struct ResourceData  //!  Data about a single resource to be loaded.
85   {
86     TFilePath m_path;  //!< Path of the resource to be loaded.
87 
88     boost::optional<LevelOptions>
89         m_options;  //!< User-defined properties to be applied as a level.
90 
91   public:
ResourceDataLoadResourceArguments::ResourceData92     ResourceData() {}
ResourceDataLoadResourceArguments::ResourceData93     ResourceData(const TFilePath &path) : m_path(path) {}
94   };
95 
96   enum ImportPolicy  //!  Policy adopted for resources external to current
97                      //!  scene.
98   { ASK_USER,        //!< User is prompted for a resolution.
99     IMPORT,          //!< Resources are copied to scene folders (\a overwrites).
100     LOAD,            //!< Resources are loaded from their original paths.
101   };
102 
103 public:
104   std::vector<ResourceData>
105       resourceDatas;  //!< [\p In/Out]  Data identifying a single resource.
106 
107   // reuse TFrameIds retrieved by FileBrowser
108   std::vector<std::vector<TFrameId>> frameIdsSet;
109 
110   TFilePath castFolder;  //!< [\p In]      Cast panel folder where the resources
111                          //! will be inserted.
112 
113   int row0,  //!< [\p In/Out]  Starting xsheet row where resources have been
114              //! inserted.
115       col0,  //!< [\p In/Out]  Starting xsheet column where resources have been
116              //! inserted.
117       row1,  //!< [\p Out]     Ending xsheet row where resources have been
118              //! inserted (included).
119       col1;  //!< [\p Out]     Ending xsheet column where resources have been
120              //! inserted (included).
121 
122   ImportPolicy importPolicy;  //!< [\p In]      Policy adopted for resources
123                               //! external to current scene.
124   bool expose;  //!< [\p In]      Whether resources must be exposed in the
125                 //! xsheet.
126 
127   std::vector<TXshLevel *> loadedLevels;  //!< [\p Out]     Levels loaded by
128                                           //! resource loading procedures.
129 
130   int xFrom, xTo;
131   std::wstring levelName;
132   int step, inc, frameCount;
133   bool doesFileActuallyExist;
134 
135   enum CacheTlvBehavior {
136     ON_DEMAND = 0,  // image data will be loaded when needed
137     ALL_ICONS,      // icon data of all frames will be cached at the begininng
138     ALL_ICONS_AND_IMAGES  // both icon and image data of all frames will be
139                           // cached at the begininng
140   } cachingBehavior;
141   //! Multiple
142   //!               levels \a may be loaded for a single resource data.
143 public:
144   LoadResourceArguments(const TFilePath &fp   = TFilePath(),
145                         const TFilePath &cast = TFilePath())
castFolderLoadResourceArguments146       : castFolder(cast)
147       , row0(-1)
148       , col0(-1)
149       , row1(-1)
150       , col1(-1)
151       , importPolicy(static_cast<ImportPolicy>(
152             Preferences::instance()->getDefaultImportPolicy()))
153       , expose(Preferences::instance()->isAutoExposeEnabled())
154       , xFrom(-1)
155       , xTo(-1)
156       , levelName(L"")
157       , step(-1)
158       , inc(-1)
159       , frameCount(-1)
160       , doesFileActuallyExist(true)
161       , cachingBehavior(ON_DEMAND) {
162     if (!fp.isEmpty()) resourceDatas.push_back(fp);
163   }
164 };
165 
166 //------------------------------------------------------------------------
167 
168 class ConvertingPopup final : public QDialog {
169 public:
170   ConvertingPopup(QWidget *parent, QString fileName);
171   ~ConvertingPopup();
172 };
173 
174 //------------------------------------------------------------------------
175 
176 void newScene();
177 
178 bool loadScene(ToonzScene &scene, const TFilePath &scenePath, bool import);
179 bool loadScene(const TFilePath &scenePath, bool updateRecentFile = true,
180                bool checkSaveOldScene = true);
181 bool loadScene();
182 
183 bool loadSubScene();
184 bool loadSubScene(const TFilePath &scenePath);
185 
186 enum SaveSceneFlags {
187   SILENTLY_OVERWRITE = 0x1,
188   SAVE_SUBXSHEET     = 0x2,
189 };
190 
191 // ritorna true sse la scena e' stata salvata
192 // se fp esiste gia': se (flags&SILENTLY_OVERWRITE) != 0 sovrascrive
193 // silenziosamente, altrimenti
194 // chiede il permesso all'utente
195 // se l'xsheet corrente non e' l'xsheet principale: se (flags&SAVE_SUBXSHEET) ==
196 // 0 salva comunque
197 // tutta la scena, altrimenti solo il sottoxsheet
198 bool saveScene(const TFilePath &fp, int flags);
199 bool saveScene();
200 
201 bool saveLevel(const TFilePath &fp);
202 bool saveLevel();
203 
204 bool saveLevel(const TFilePath &fp, TXshSimpleLevel *sl, bool overwrite);
205 bool saveLevel(TXshSimpleLevel *sl);
206 
207 bool saveAll();
208 
209 void saveNonSceneFiles();
210 
211 bool saveSound(const TFilePath &fp, TXshSoundLevel *sc, bool overwrite);
212 bool saveSound(TXshSoundLevel *sc);
213 
214 /*! \note     Will fallback to loadResourceFolders() in case all
215               argument paths are folders.                                 */
216 
217 int loadResources(
218     LoadResourceArguments &args,  //!< Resources to be loaded.
219     bool updateRecentFiles =
220         true,  //!< Whether Toonz's <I>Recent Files</I> list must be updated.
221     LoadResourceArguments::ScopedBlock *sb =
222         0  //!< Load block. May be nonzero in order to extend block data
223            //!  access and finalization.
224            //!< Loads a group of resources by path.
225            //!  \return  The actually loaded levels count.
226 );
227 
228 int loadResourceFolders(
229     LoadResourceArguments &args,  //!< Resource folders to be loaded.
230     LoadResourceArguments::ScopedBlock *sb =
231         0  //!< Load block. May be nonzero in order to extend block data
232            //!  access and finalization.
233 );         //!< Loads the specified folders in current xsheet.
234            //!  \return  The actually loaded levels count.
235 bool exposeLevel(TXshSimpleLevel *sl, int row, int col, bool insert = false,
236                  bool overWrite = false);
237 bool exposeLevel(TXshSimpleLevel *sl, int row, int col,
238                  const std::vector<TFrameId> &fids, bool insert = false,
239                  bool overWrite = false);
240 
241 // se e' necessario salvare la scena chiede il permesso all'utente
242 // (save,discard,cancel).
243 // Ritorna false se l'utente ha risposto Cancel o se il salvataggio della scena
244 // e' fallito per qualche motivo
245 bool saveSceneIfNeeded(QString msg);
246 
247 //! Create and expose column with comment in \b commentList.
248 bool exposeComment(int row, int &col, QList<QString> commentList,
249                    QString fileName);
250 
251 bool importLipSync(TFilePath levelPath, QList<TFrameId> frameList,
252                    QList<QString> commentList, QString fileName);
253 
254 // If the scene will be saved in the different folder, check out the scene
255 // cast.
256 // if the cast contains the level specified with $scenefolder alias,
257 // open a warning popup notifying that such level will lose link.
258 bool takeCareSceneFolderItemsOnSaveSceneAs(
259     ToonzScene *scene, const TFilePath &newPath, TXsheet *subxsh,
260     QHash<TXshLevel *, TFilePath> &orgLevelPaths);
261 
262 }  // namespace IoCmd
263 
264 #endif  // IOCOMMAND_H
265