1 
2 
3 #include "castselection.h"
4 #include "castviewer.h"
5 #include "menubarcommandids.h"
6 #include "toonzqt/icongenerator.h"
7 #include "toonzqt/gutil.h"
8 
9 #include "toonz/toonzscene.h"
10 #include "toonz/txshlevel.h"
11 #include "toonz/txshsimplelevel.h"
12 #include "toonz/txshpalettelevel.h"
13 #include "toonz/txshlevelhandle.h"
14 #include "toonz/tscenehandle.h"
15 #include "toonz/sceneproperties.h"
16 #include "tapp.h"
17 #include "toonz/stage2.h"
18 #include "tsystem.h"
19 #include "toonz/txshsoundlevel.h"
20 #include "toonz/preferences.h"
21 
22 //=============================================================================
23 //
24 // CastSelection
25 //
26 //-----------------------------------------------------------------------------
27 
CastSelection()28 CastSelection::CastSelection() : m_browser(0) {}
29 
30 //-----------------------------------------------------------------------------
31 
~CastSelection()32 CastSelection::~CastSelection() {}
33 
34 //-----------------------------------------------------------------------------
35 
getSelectedLevels(std::vector<TXshLevel * > & levels)36 void CastSelection::getSelectedLevels(std::vector<TXshLevel *> &levels) {
37   assert(m_browser);
38   CastItems const &castItems = m_browser->getCastItems();
39   for (int i = 0; i < castItems.getItemCount(); i++) {
40     if (!isSelected(i)) continue;
41     TXshLevel *level  = castItems.getItem(i)->getSimpleLevel();
42     if (!level) level = castItems.getItem(i)->getPaletteLevel();
43     if (!level) level = castItems.getItem(i)->getSoundLevel();
44     if (level) levels.push_back(level);
45   }
46 }
47 
48 //-----------------------------------------------------------------------------
49 
enableCommands()50 void CastSelection::enableCommands() {
51   DvItemSelection::enableCommands();
52   assert(m_browser);
53   if (m_browser) {
54     enableCommand(m_browser, MI_ExposeResource, &CastBrowser::expose);
55     enableCommand(m_browser, MI_EditLevel, &CastBrowser::edit);
56     // enableCommand(m_browser, MI_ConvertToVectors, &CastBrowser::vectorize);
57     enableCommand(m_browser, MI_ShowFolderContents,
58                   &CastBrowser::showFolderContents);
59   }
60 }
61 
62 // TODO: da qui in avanti: spostare in un altro file
63 
64 //=============================================================================
65 //
66 // CastBrowser::LevelItem
67 //
68 //-----------------------------------------------------------------------------
69 
getName() const70 QString LevelCastItem::getName() const {
71   return QString::fromStdWString(m_level->getName());
72 }
73 
74 //-----------------------------------------------------------------------------
75 
getToolTip() const76 QString LevelCastItem::getToolTip() const {
77   TFilePath path = m_level->getPath();
78   return QString::fromStdWString(path.withoutParentDir().getWideString());
79 }
80 
81 //-----------------------------------------------------------------------------
82 
getFrameCount() const83 int LevelCastItem::getFrameCount() const { return m_level->getFrameCount(); }
84 
85 //-----------------------------------------------------------------------------
86 
getPixmap(bool isSelected) const87 QPixmap LevelCastItem::getPixmap(bool isSelected) const {
88   TXshSimpleLevel *sl = m_level->getSimpleLevel();
89   if (!sl) return QPixmap();
90   ToonzScene *scene = sl->getScene();
91   assert(scene);
92   if (!scene) return QPixmap();
93   bool onDemand = false;
94   if (Preferences::instance()->getColumnIconLoadingPolicy() ==
95       Preferences::LoadOnDemand)
96     onDemand   = !isSelected;
97   QPixmap icon = IconGenerator::instance()->getIcon(sl, sl->getFirstFid(),
98                                                     false, onDemand);
99   return scalePixmapKeepingAspectRatio(icon, m_itemPixmapSize, Qt::transparent);
100 }
101 
102 //-----------------------------------------------------------------------------
103 
exists() const104 bool LevelCastItem::exists() const {
105   ToonzScene *scene = TApp::instance()->getCurrentScene()->getScene();
106   return TSystem::doesExistFileOrLevel(
107              scene->decodeFilePath(m_level->getPath())) ||
108          !getPixmap(false).isNull();
109 }
110 
111 //-------------------------------------------------------
112 
getSimpleLevel() const113 TXshSimpleLevel *LevelCastItem::getSimpleLevel() const {
114   return m_level ? m_level->getSimpleLevel() : 0;
115 }
116 
117 //=============================================================================
118 //
119 // CastBrowser::SoundItem
120 //
121 //-----------------------------------------------------------------------------
122 
getName() const123 QString SoundCastItem::getName() const {
124   return QString::fromStdWString(m_soundLevel->getName());
125 }
126 
127 //-----------------------------------------------------------------------------
128 
getToolTip() const129 QString SoundCastItem::getToolTip() const {
130   TFilePath path = m_soundLevel->getPath();
131   return QString::fromStdWString(path.withoutParentDir().getWideString());
132 }
133 //-----------------------------------------------------------------------------
134 
getFrameCount() const135 int SoundCastItem::getFrameCount() const {
136   return m_soundLevel->getFrameCount();
137 }
138 //-----------------------------------------------------------------------------
139 
getPixmap(bool isSelected) const140 QPixmap SoundCastItem::getPixmap(bool isSelected) const {
141   static QPixmap loudspeaker(
142       svgToPixmap(getIconThemePath("mimetypes/60/audio_icon.svg"),
143                   m_itemPixmapSize, Qt::KeepAspectRatio));
144   return loudspeaker;
145 }
146 
147 //-----------------------------------------------------------------------------
148 
exists() const149 bool SoundCastItem::exists() const {
150   ToonzScene *scene = TApp::instance()->getCurrentScene()->getScene();
151   return TSystem::doesExistFileOrLevel(
152       scene->decodeFilePath(m_soundLevel->getPath()));
153 }
154 
155 //=============================================================================
156 //
157 // CastBrowser::PaletteCastItem
158 //
159 //-----------------------------------------------------------------------------
160 
getName() const161 QString PaletteCastItem::getName() const {
162   return QString::fromStdWString(m_paletteLevel->getName());
163 }
164 //-----------------------------------------------------------------------------
165 
getToolTip() const166 QString PaletteCastItem::getToolTip() const {
167   TFilePath path = m_paletteLevel->getPath();
168   return QString::fromStdWString(path.withoutParentDir().getWideString());
169 }
170 //-----------------------------------------------------------------------------
171 
getFrameCount() const172 int PaletteCastItem::getFrameCount() const {
173   return m_paletteLevel->getFrameCount();
174 }
175 //-----------------------------------------------------------------------------
176 
getPixmap(bool isSelected) const177 QPixmap PaletteCastItem::getPixmap(bool isSelected) const {
178   static QPixmap palette(svgToPixmap(":Resources/paletteicon.svg",
179                                      m_itemPixmapSize, Qt::KeepAspectRatio));
180   return palette;
181 }
182 
exists() const183 bool PaletteCastItem::exists() const {
184   ToonzScene *scene = TApp::instance()->getCurrentScene()->getScene();
185   return TSystem::doesExistFileOrLevel(
186       scene->decodeFilePath(m_paletteLevel->getPath()));
187 }
188 
189 //=============================================================================
190 //
191 // CastItems
192 //
193 //-----------------------------------------------------------------------------
194 
CastItems()195 CastItems::CastItems() : QMimeData() {}
196 
197 //-----------------------------------------------------------------------------
198 
~CastItems()199 CastItems::~CastItems() { clear(); }
200 
201 //-----------------------------------------------------------------------------
202 
clear()203 void CastItems::clear() { clearPointerContainer(m_items); }
204 
205 //-----------------------------------------------------------------------------
206 
addItem(CastItem * item)207 void CastItems::addItem(CastItem *item) { m_items.push_back(item); }
208 
209 //-----------------------------------------------------------------------------
210 
getItem(int index) const211 CastItem *CastItems::getItem(int index) const {
212   assert(0 <= index && index < (int)m_items.size());
213   return m_items[index];
214 }
215 
216 //-----------------------------------------------------------------------------
217 
hasFormat(const QString & mimeType) const218 bool CastItems::hasFormat(const QString &mimeType) const {
219   return mimeType == getMimeFormat();
220 }
221 
222 //-----------------------------------------------------------------------------
223 
getMimeFormat()224 QString CastItems::getMimeFormat() { return "application/vnd.toonz.levels"; }
225 
226 //-----------------------------------------------------------------------------
227 
formats() const228 QStringList CastItems::formats() const {
229   return QStringList(QString("application/vnd.toonz.levels"));
230 }
231 
232 //-----------------------------------------------------------------------------
233 
getSelectedItems(const std::set<int> & indices) const234 CastItems *CastItems::getSelectedItems(const std::set<int> &indices) const {
235   CastItems *c = new CastItems();
236   std::set<int>::const_iterator it;
237   for (it = indices.begin(); it != indices.end(); ++it) {
238     int index = *it;
239     if (0 <= index && index < getItemCount())
240       c->addItem(getItem(index)->clone());
241   }
242   return c;
243 }
244