1 
2 
3 #include "toonz/txshpalettelevel.h"
4 #include "toonz/txshleveltypes.h"
5 #include "toonz/toonzscene.h"
6 #include "tconvert.h"
7 #include "tstream.h"
8 #include "tfilepath_io.h"
9 #include "tsystem.h"
10 
11 //-----------------------------------------------------------------------------
12 
13 DEFINE_CLASS_CODE(TXshPaletteLevel, 52)
14 
15 PERSIST_IDENTIFIER(TXshPaletteLevel, "paletteLevel")
16 
17 //=============================================================================
18 // TXshPaletteLevel
19 
TXshPaletteLevel(std::wstring name)20 TXshPaletteLevel::TXshPaletteLevel(std::wstring name)
21     : TXshLevel(m_classCode, name), m_palette(0) {
22   m_type = PLT_XSHLEVEL;
23 }
24 
25 //-----------------------------------------------------------------------------
26 
~TXshPaletteLevel()27 TXshPaletteLevel::~TXshPaletteLevel() {}
28 
29 //-----------------------------------------------------------------------------
30 
getPalette() const31 TPalette *TXshPaletteLevel::getPalette() const { return m_palette; }
32 
33 //-----------------------------------------------------------------------------
34 
setPalette(TPalette * palette)35 void TXshPaletteLevel::setPalette(TPalette *palette) {
36   if (m_palette != palette) {
37     if (m_palette) m_palette->release();
38     m_palette = palette;
39     if (m_palette) m_palette->addRef();
40   }
41 }
42 
43 //-----------------------------------------------------------------------------
44 
setPath(const TFilePath & path)45 void TXshPaletteLevel::setPath(const TFilePath &path) { m_path = path; }
46 
47 //-----------------------------------------------------------------------------
48 
loadData(TIStream & is)49 void TXshPaletteLevel::loadData(TIStream &is) {
50   std::string tagName;
51   while (is.matchTag(tagName)) {
52     if (tagName == "name") {
53       std::wstring name;
54       is >> name;
55       setName(name);
56     } else if (tagName == "path") {
57       is >> m_path;
58     } else {
59       throw TException("TXshPaletteLevel, unknown tag: " + tagName);
60     }
61     is.closeChild();
62   }
63 }
64 
65 //-----------------------------------------------------------------------------
66 
saveData(TOStream & os)67 void TXshPaletteLevel::saveData(TOStream &os) {
68   os.child("path") << m_path;
69   os.child("name") << getName();
70 }
71 
72 //-----------------------------------------------------------------------------
73 
load()74 void TXshPaletteLevel::load() {
75   TFilePath path = getScene()->decodeFilePath(m_path);
76   if (TSystem::doesExistFileOrLevel(path)) {
77     TFileStatus fs(path);
78     TPersist *p = 0;
79     TIStream is(path);
80     TPalette *palette = nullptr;
81 
82     if (is && fs.doesExist()) {
83       std::string tagName;
84       if (is.matchTag(tagName) && tagName == "palette") {
85         std::string gname;
86         is.getTagParam("name", gname);
87         palette = new TPalette();
88         palette->loadData(is);
89         palette->setGlobalName(::to_wstring(gname));
90         is.matchEndTag();
91         palette->setPaletteName(path.getWideName());
92         setPalette(palette);
93       }
94     }
95     assert(m_palette);
96   }
97 }
98 
99 //-----------------------------------------------------------------------------
100 
save()101 void TXshPaletteLevel::save() {
102   TFilePath path = getScene()->decodeFilePath(m_path);
103   if (TSystem::doesExistFileOrLevel(path) && m_palette) {
104     TFileStatus fs(path);
105     if (!fs.isWritable()) {
106       throw TSystemException(
107           path, "The palette cannot be saved: it is a read only palette.");
108     }
109     TOStream os(path);
110     os << m_palette;
111   }
112 }
113 
114 //-----------------------------------------------------------------------------
115 
getFrameCount() const116 int TXshPaletteLevel::getFrameCount() const { return 0; }
117