1 #pragma once
2 
3 #ifndef ICON_GENERATOR_H
4 #define ICON_GENERATOR_H
5 
6 // TnzCore includes
7 #include "tthread.h"
8 #include "tgeometry.h"
9 #include "tfilepath.h"
10 #include "traster.h"
11 #include "timage.h"
12 #include "tpixel.h"
13 
14 // Qt includes
15 #include <QPixmap>
16 #include <QThreadStorage>
17 #include <QEventLoop>
18 
19 // STD includes
20 #include <map>
21 #include <vector>
22 #include <set>
23 
24 #undef DVAPI
25 #undef DVVAR
26 #ifdef TOONZQT_EXPORTS
27 #define DVAPI DV_EXPORT_API
28 #define DVVAR DV_EXPORT_VAR
29 #else
30 #define DVAPI DV_IMPORT_API
31 #define DVVAR DV_IMPORT_VAR
32 #endif
33 
34 //==============================================================
35 
36 //    Forward declarations
37 
38 class TFilePath;
39 class TXshLevel;
40 class TStageObjectSpline;
41 class ToonzScene;
42 class TOfflineGL;
43 
44 //==============================================================
45 
46 //**********************************************************************************
47 //    IconGenerator  definition
48 //**********************************************************************************
49 
50 /*!
51   \brief    The class responsible for icons management in Toonz.
52 
53   \details  It's a singleton class - in particular, rendered icons are stored in
54             a shared map container for fast retrieval upon repeated icon
55   requests.
56 
57             IconGenerator provides methods to submit icon requests, and to
58   invalidate or
59             remove icons from the internal database. In order to keep outer
60   entities
61             informed of the icon generation status, an iconGenerated() signal is
62   emitted
63             once an icon has been generated.
64 */
65 
66 class DVAPI IconGenerator final : public QObject {
67   Q_OBJECT
68 
69 public:
70   class Settings {
71   public:
72     bool m_blackBgCheck;
73     bool m_transparencyCheck;
74     bool m_inksOnly;
75     int m_inkIndex;
76     int m_paintIndex;
77 
Settings()78     Settings()
79         : m_transparencyCheck(false)
80         , m_blackBgCheck(false)
81         , m_inksOnly(false)
82         , m_inkIndex(-1)
83         , m_paintIndex(-1) {}
84   };
85 
86 public:
87   IconGenerator();
88   ~IconGenerator();
89 
90   static IconGenerator *instance();
91 
setSettings(const Settings & settings)92   void setSettings(const Settings &settings) { m_settings = settings; }
getSettings()93   const Settings getSettings() const { return m_settings; }
94   static void setFilmstripIconSize(const TDimension &dim);
95 
96   TDimension getIconSize() const;
97 
98   TOfflineGL *getOfflineGLContext();
99 
100   // icons from splines
101   QPixmap getIcon(TStageObjectSpline *spline);
102   void invalidate(TStageObjectSpline *spline);
103   void remove(TStageObjectSpline *spline);
104 
105   // icons from toonz levels
106   QPixmap getIcon(TXshLevel *sl, const TFrameId &fid, bool filmStrip = true,
107                   bool onDemand = false);
108   QPixmap getSizedIcon(TXshLevel *sl, const TFrameId &fid, std::string newId,
109                        TDimension dim = TDimension(0, 0));
110   void invalidate(TXshLevel *sl, const TFrameId &fid,
111                   bool onlyFilmStrip = false);
112   void remove(TXshLevel *sl, const TFrameId &fid, bool onlyFilmStrip = false);
113 
114   // icons from files
115   QPixmap getIcon(const TFilePath &path,
116                   const TFrameId &fid = TFrameId::NO_FRAME);
117   void invalidate(const TFilePath &path,
118                   const TFrameId &fid = TFrameId::NO_FRAME);
119   void remove(const TFilePath &path, const TFrameId &fid = TFrameId::NO_FRAME);
120 
121   QPixmap getSceneIcon(ToonzScene *scene);  // Unused in Toonz
122   void invalidateSceneIcon();
123 
124   void remap(const std::string &newIconId, const std::string &oldIconId);
125 
126   void clearRequests();
127   void clearSceneIcons();
128 
129   static TRaster32P generateVectorFileIcon(const TFilePath &path,
130                                            const TDimension &iconSize,
131                                            const TFrameId &fid);
132   static TRaster32P generateRasterFileIcon(const TFilePath &path,
133                                            const TDimension &iconSize,
134                                            const TFrameId &fid);
135   static TRaster32P generateSceneFileIcon(const TFilePath &path,
136                                           const TDimension &iconSize, int row);
137   static TRaster32P generateSplineFileIcon(const TFilePath &path,
138                                            const TDimension &iconSize);
139   static TRaster32P generateMeshFileIcon(const TFilePath &path,
140                                          const TDimension &iconSize,
141                                          const TFrameId &fid);
142 
143   // This function is called when only colors of styles are changed in toonz
144   // raster levels. In such case it doesn't need to re-compute icons but needs
145   // to let panels to update. See TApp::onLevelColorStyleChanged() for details.
notifyIconGenerated()146   void notifyIconGenerated() { emit iconGenerated(); }
147 signals:
148 
149   void iconGenerated();
150 
151 public slots:
152 
153   void onStarted(TThread::RunnableP iconRenderer);
154   void onCanceled(TThread::RunnableP iconRenderer);
155   void onFinished(TThread::RunnableP iconRenderer);
156   void onException(TThread::RunnableP iconRenderer);
157   void onTerminated(TThread::RunnableP iconRenderer);
158 
159 private:
160   TThread::Executor m_executor;
161   QThreadStorage<TOfflineGL *> m_contexts;
162   TDimension m_iconSize;
163 
164   QEventLoop m_iconsTerminationLoop;  //!< Event loop used to wait for icons
165                                       //! termination.
166 
167   Settings m_settings;
168 
169 private:
170   void addTask(const std::string &id, TThread::RunnableP iconRenderer);
171 };
172 
173 //**********************************************************************************
174 //    Related non-member functions
175 //**********************************************************************************
176 
177 template <class It>
178 inline void invalidateIcons(TXshLevel *sl, It fBegin, It fEnd,
179                             bool onlyFilmStrip = false) {
180   for (It ft = fBegin; ft != fEnd; ++ft)
181     IconGenerator::instance()->invalidate(sl, *ft, onlyFilmStrip);
182 }
183 
184 //------------------------------------------------------------------------
185 
186 template <class C>
187 inline void invalidateIcons(TXshLevel *sl, const C &fids,
188                             bool onlyFilmStrip = false) {
189   invalidateIcons(sl, fids.begin(), fids.end(), onlyFilmStrip);
190 }
191 
192 //------------------------------------------------------------------------
193 
194 template <typename It>
195 inline void removeIcons(TXshLevel *sl, It fBegin, It fEnd,
196                         bool onlyFilmStrip = false) {
197   for (It ft = fBegin; ft != fEnd; ++ft)
198     IconGenerator::instance()->remove(sl, *ft, onlyFilmStrip);
199 }
200 
201 //------------------------------------------------------------------------
202 
203 template <typename C>
204 inline void removeIcons(TXshLevel *sl, const C &fids,
205                         bool onlyFilmStrip = false) {
206   removeIcons(sl, fids.begin(), fids.end(), onlyFilmStrip);
207 }
208 
209 #endif  // ICON_GENERATOR_H
210