diff --git a/toonz/sources/include/toonz/studiopalette.h b/toonz/sources/include/toonz/studiopalette.h index dfa350a..90c2950 100644 --- a/toonz/sources/include/toonz/studiopalette.h +++ b/toonz/sources/include/toonz/studiopalette.h @@ -107,6 +107,9 @@ public: void save(const TFilePath &path, TPalette *palette); + void removeEntry(const std::wstring paletteId); + void addEntry(const std::wstring paletteId, const TFilePath &path); + private: StudioPalette(); TFilePath m_root; diff --git a/toonz/sources/toonzlib/studiopalette.cpp b/toonz/sources/toonzlib/studiopalette.cpp index 625c3bf..5a9a959 100644 --- a/toonz/sources/toonzlib/studiopalette.cpp +++ b/toonz/sources/toonzlib/studiopalette.cpp @@ -20,6 +20,8 @@ #include #include +#include + //=================================================================== //------------------------------------------------------------------- @@ -143,6 +145,10 @@ TFilePath searchPalette(TFilePath path, std::wstring paletteId) { bool studioPaletteHasBeenReferred = false; static std::map table; +// table loaded from the cache. verify once before storing in the table +static std::map table_cached; + +const std::string pathTableFileName = "palette_paths.ini"; //------------------------------------------------------------------- } // namespace @@ -174,6 +180,19 @@ StudioPalette::StudioPalette() { } catch (...) { } } + + // load [global id] - [path] table file + TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()}; + for (auto rootFp : rootFps) { + if (rootFp.isEmpty()) continue; + TFilePath tablePath = rootFp + pathTableFileName; + if (!TFileStatus(tablePath).doesExist()) continue; + QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()), + QSettings::IniFormat); + for (auto key : tableSettings.allKeys()) + table_cached[key.toStdWString()] = + rootFp + TFilePath(tableSettings.value(key, "").toString()); + } } //------------------------------------------------------------------- @@ -263,6 +282,7 @@ void StudioPalette::movePalette(const TFilePath &dstPath, } std::wstring id = readPaletteGlobalName(dstPath); table.erase(id); + removeEntry(id); FolderListenerManager::instance()->notifyFolderChanged( dstPath.getParentDir()); notifyMove(dstPath, srcPath); @@ -489,9 +509,26 @@ static void foobar(std::wstring paletteId) { table.erase(paletteId); } TFilePath StudioPalette::getPalettePath(std::wstring paletteId) { std::map::iterator it = table.find(paletteId); if (it != table.end()) return it->second; - TFilePath fp = searchPalette(m_root, paletteId); - if (fp == TFilePath()) { - fp = searchPalette(getProjectPalettesRoot(), paletteId); + TFilePath fp; + // not found in the verified table, then check for the cached table + it = table_cached.find(paletteId); + // found in the cached table + if (it != table_cached.end()) { + fp = it->second; + // verify if cached path is correct + if (fp.getType() != "tpl" || + readPaletteGlobalName(it->second) != paletteId) { + fp = TFilePath(); + // erase the entry + it = table_cached.erase(it); + removeEntry(paletteId); + } + } + if (fp.isEmpty()) { + fp = searchPalette(m_root, paletteId); + if (fp.isEmpty()) fp = searchPalette(getProjectPalettesRoot(), paletteId); + + addEntry(paletteId, fp); } table[paletteId] = fp; return fp; @@ -544,7 +581,7 @@ bool StudioPalette::updateLinkedColors(TPalette *palette) { it = table.find(paletteId); TPalette *spPalette = 0; if (it == table.end()) { - spPalette = StudioPalette::instance()->getPalette(paletteId); + spPalette = getPalette(paletteId); if (!spPalette) continue; table[paletteId] = spPalette; // spPalette->release(); @@ -670,3 +707,37 @@ void StudioPalette::notifyPaletteChange(const TFilePath &palette) { it != m_listeners.end(); ++it) (*it)->onStudioPaletteChange(palette); } + +//------------------------------------------------------------------- + +void StudioPalette::removeEntry(const std::wstring paletteId) { + TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()}; + for (auto rootFp : rootFps) { + if (rootFp.isEmpty()) continue; + TFilePath tablePath = rootFp + pathTableFileName; + if (!TFileStatus(tablePath).doesExist()) continue; + QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()), + QSettings::IniFormat); + if (tableSettings.contains(QString::fromStdWString(paletteId))) { + tableSettings.remove(QString::fromStdWString(paletteId)); + break; + } + } +} + +//------------------------------------------------------------------- + +void StudioPalette::addEntry(const std::wstring paletteId, + const TFilePath &path) { + TFilePath rootFps[2] = {m_root, getProjectPalettesRoot()}; + for (auto rootFp : rootFps) { + if (rootFp.isEmpty()) continue; + if (!rootFp.isAncestorOf(path)) continue; + + TFilePath tablePath = rootFp + pathTableFileName; + QSettings tableSettings(QString::fromStdWString(tablePath.getWideString()), + QSettings::IniFormat); + QString pathValue = (path - rootFp).getQString(); + tableSettings.setValue(QString::fromStdWString(paletteId), pathValue); + } +} \ No newline at end of file diff --git a/toonz/sources/toonzqt/styleselection.cpp b/toonz/sources/toonzqt/styleselection.cpp index 26d95b6..8a5d799 100644 --- a/toonz/sources/toonzqt/styleselection.cpp +++ b/toonz/sources/toonzqt/styleselection.cpp @@ -1793,7 +1793,7 @@ void TStyleSelection::getBackOriginalStyle() { } else spPalette = palIt->second.getPointer(); - // j is StudioPaletteID + // j is StyleID int j = std::stoi(gname.substr(k + 1)); if (spPalette && 0 <= j && j < spPalette->getStyleCount()) {