Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/studiopalette.h"
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
#include "toonz/tproject.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TFilePath makeUniqueName(TFilePath fp) {
Shinya Kitaoka 120a6e
  if (TFileStatus(fp).doesExist() == false) return fp;
Shinya Kitaoka 120a6e
  std::wstring name = fp.getWideName();
Shinya Kitaoka 120a6e
  int index         = 2;
Shinya Kitaoka 120a6e
  int j             = name.find_last_not_of(L"0123456789");
Shinya Kitaoka 120a6e
  if (j != (int)std::wstring::npos && j + 1 < (int)name.length()) {
Shinya Kitaoka 120a6e
    index = std::stoi(name.substr(j + 1)) + 1;
Shinya Kitaoka 120a6e
    name  = name.substr(0, j + 1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (;;) {
Shinya Kitaoka 120a6e
    fp = fp.withName(name + std::to_wstring(index));
Shinya Kitaoka 120a6e
    if (TFileStatus(fp).doesExist() == false) return fp;
Shinya Kitaoka 120a6e
    index++;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TPalette *loadPliPalette(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  TLevelReaderP lr(fp);
Shinya Kitaoka 120a6e
  TLevelP level  = lr->loadInfo();
Shinya Kitaoka 120a6e
  int frameCount = level->getFrameCount();
Shinya Kitaoka 120a6e
  if (frameCount < 1) return 0;
Shinya Kitaoka 120a6e
  TPalette *palette = level->getPalette();
Shinya Kitaoka 120a6e
  if (!palette) return 0;
Shinya Kitaoka 120a6e
  return palette->clone();
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TPalette *loadTplPalette(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  TPersist *p = 0;
Shinya Kitaoka 120a6e
  TIStream is(fp);
Shinya Kitaoka 120a6e
  is >> p;
Shinya Kitaoka 120a6e
  TPalette *palette = dynamic_cast<tpalette *="">(p);</tpalette>
Shinya Kitaoka 120a6e
  return palette;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TPalette *loadToonz46Palette(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  TImageP pltImg;
Shinya Kitaoka 120a6e
  TImageReader::load(fp, pltImg);
Shinya Kitaoka 120a6e
  if (!pltImg) return 0;
Shinya Kitaoka 120a6e
  TRasterImageP pltRasImg(pltImg);
Shinya Kitaoka 120a6e
  if (!pltRasImg) return 0;
Shinya Kitaoka 120a6e
  TRaster32P rasPlt = pltRasImg->getRaster();
Shinya Kitaoka 120a6e
  if (!rasPlt) return 0;
Shinya Kitaoka 120a6e
  TPalette *palette = new TPalette();
Shinya Kitaoka 120a6e
  const int offset  = 0;  // FirstUserStyle-1;
Shinya Kitaoka 120a6e
  assert(rasPlt->getLy() == 2);
Shinya Kitaoka 120a6e
  rasPlt->lock();
Shinya Kitaoka 120a6e
  TPixel32 *pixelRow = rasPlt->pixels(0);
Shinya Kitaoka 120a6e
  int x;
Shinya Kitaoka 120a6e
  for (x = 1; x < rasPlt->getLx(); ++x) {
Shinya Kitaoka 120a6e
    TPixel32 color = pixelRow[x];
Shinya Kitaoka 120a6e
    int styleId    = offset + x;
Shinya Kitaoka 120a6e
    if (styleId < palette->getStyleCount())
Shinya Kitaoka 120a6e
      palette->setStyle(styleId, color);
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      int j = palette->addStyle(color);
Shinya Kitaoka 120a6e
      assert(j == styleId);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // aggiungo solo i colori usati (salvo il BG)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pixelRow             = rasPlt->pixels(1);
Shinya Kitaoka 120a6e
  TPalette::Page *page = palette->getPage(0);
Shinya Kitaoka 120a6e
  for (x = 1; x < rasPlt->getLx(); ++x) {
Shinya Kitaoka 120a6e
    if (pixelRow[x].r == 255)
Shinya Kitaoka 120a6e
      page->addStyle(offset +
Shinya Kitaoka 120a6e
                     x);  // palette->addStyleToPage(offset+x, L"colors");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  rasPlt->unlock();
Shinya Kitaoka 120a6e
  return palette;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
std::wstring readPaletteGlobalName(TFilePath path) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TIStream is(path);
Shinya Kitaoka 120a6e
    if (!is) return L"";
Shinya Kitaoka 120a6e
    std::string tagName;
Shinya Kitaoka 120a6e
    if (!is.matchTag(tagName) || tagName != "palette") return L"";
Shinya Kitaoka 120a6e
    std::string name;
Shinya Kitaoka 120a6e
    if (is.getTagParam("name", name)) return ::to_wstring(name);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return L"";
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TFilePath searchPalette(TFilePath path, std::wstring paletteId) {
Shinya Kitaoka 120a6e
  TFilePathSet q;
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::readDirectory(q, path);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (TFilePathSet::iterator i = q.begin(); i != q.end(); ++i) {
Shinya Kitaoka 120a6e
    TFilePath fp = *i;
Shinya Kitaoka 120a6e
    if (fp.getType() == "tpl") {
Shinya Kitaoka 120a6e
      std::wstring gname = readPaletteGlobalName(fp);
Shinya Kitaoka 120a6e
      if (gname == paletteId) return fp;
Shinya Kitaoka 120a6e
    } else if (TFileStatus(fp).isDirectory()) {
Shinya Kitaoka 120a6e
      TFilePath palettePath = searchPalette(fp, paletteId);
Shinya Kitaoka 120a6e
      if (palettePath != TFilePath()) return palettePath;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TFilePath();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool studioPaletteHasBeenReferred = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
static std::map<std::wstring, tfilepath=""> table;</std::wstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// StudioPalette
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPalette::StudioPalette() {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    m_root = ToonzFolder::getStudioPaletteFolder();
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!TFileStatus(m_root).doesExist()) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TSystem::mkDir(m_root);
Shinya Kitaoka 120a6e
      FolderListenerManager::instance()->notifyFolderChanged(
Shinya Kitaoka 120a6e
          m_root.getParentDir());
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TSystem::mkDir(getLevelPalettesRoot());
Shinya Kitaoka 120a6e
      FolderListenerManager::instance()->notifyFolderChanged(
Shinya Kitaoka 120a6e
          getLevelPalettesRoot().getParentDir());
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPalette::~StudioPalette() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::m_enabled = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::enable(bool enabled) {
Shinya Kitaoka 120a6e
  assert(studioPaletteHasBeenReferred == false);
Shinya Kitaoka 120a6e
  m_enabled = enabled;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPalette *StudioPalette::instance() {
Shinya Kitaoka 120a6e
  static StudioPalette _instance;
Shinya Kitaoka 120a6e
  studioPaletteHasBeenReferred = true;
Shinya Kitaoka 120a6e
  assert(m_enabled);
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::getLevelPalettesRoot() {
Jeremy Bullock 946edb
  return m_root + "Global Palettes";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::getProjectPalettesRoot() {
Shinya Kitaoka 120a6e
  TProjectP p          = TProjectManager::instance()->getCurrentProject();
Shinya Kitaoka 120a6e
  TFilePath folderName = p->getFolder(TProject::Palettes);
Shinya Kitaoka 120a6e
  if (folderName.isEmpty()) return TFilePath();
Shinya Kitaoka 120a6e
  if (folderName.isAbsolute()) return folderName;
Shinya Kitaoka 120a6e
  return p->getProjectFolder() + folderName;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static bool loadRefImg(TPalette *palette, TFilePath dir) {
Shinya Kitaoka 120a6e
  assert(palette);
Shinya Kitaoka 120a6e
  TFilePath fp = palette->getRefImgPath();
Shinya Kitaoka 120a6e
  if (fp == TFilePath() || !TSystem::doesExistFileOrLevel(fp)) return false;
Shinya Kitaoka 120a6e
  if (!fp.isAbsolute()) fp = dir + fp;
Shinya Kitaoka 120a6e
  TLevelReaderP lr(fp);
Shinya Kitaoka 120a6e
  if (!lr) return false;
Shinya Kitaoka 120a6e
  TLevelP level = lr->loadInfo();
Shinya Kitaoka 120a6e
  if (!level || level->getFrameCount() == 0) return false;
Shinya Kitaoka 120a6e
  TLevel::Iterator it = level->begin();
Shinya Kitaoka 120a6e
  TImageP img         = lr->getFrameReader(it->first)->load();
Shinya Kitaoka 120a6e
  if (!img) return false;
Shinya Kitaoka 120a6e
  img->setPalette(0);
Shinya Kitaoka 120a6e
  palette->setRefImg(img);
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPalette *StudioPalette::getPalette(const TFilePath &path,
Shinya Kitaoka 120a6e
                                    bool loadRefImgFlag) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (path.getType() != "tpl") return 0;
Shinya Kitaoka 120a6e
    TPalette *palette = load(path);
Shinya Kitaoka 120a6e
    if (!palette) return 0;
Shinya Kitaoka 120a6e
    if (loadRefImgFlag) loadRefImg(palette, path.getParentDir());
Shinya Kitaoka 120a6e
    // palette->addRef(); // ci va??
Shinya Kitaoka 120a6e
    return palette;
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::movePalette(const TFilePath &dstPath,
Shinya Kitaoka 120a6e
                                const TFilePath &srcPath) {
Shinya Kitaoka 120a6e
  try {
shun-iwasawa ff99e1
    // do not allow overwrite palette
shun-iwasawa ff99e1
    TSystem::renameFile(dstPath, srcPath, false);
Shinya Kitaoka 120a6e
  } catch (...) {
shun-iwasawa ff99e1
    throw;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  std::wstring id = readPaletteGlobalName(dstPath);
Shinya Kitaoka 120a6e
  table.erase(id);
Shinya Kitaoka 120a6e
  FolderListenerManager::instance()->notifyFolderChanged(
Shinya Kitaoka 120a6e
      dstPath.getParentDir());
Shinya Kitaoka 120a6e
  notifyMove(dstPath, srcPath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int StudioPalette::getChildren(std::vector<tfilepath> &fps,</tfilepath>
Shinya Kitaoka 120a6e
                               const TFilePath &folderPath) {
Shinya Kitaoka 120a6e
  TFilePathSet q;
Shinya Kitaoka 120a6e
  if (TFileStatus(folderPath).isDirectory()) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TSystem::readDirectory(q, folderPath, false, false);
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
shun-iwasawa ff99e1
  // put the folders above the palette items
shun-iwasawa ff99e1
  std::vector<tfilepath> palettes;</tfilepath>
shun-iwasawa ff99e1
  for (TFilePathSet::iterator i = q.begin(); i != q.end(); ++i) {
shun-iwasawa ff99e1
    if (isFolder(*i))
shun-iwasawa ff99e1
      fps.push_back(*i);
shun-iwasawa ff99e1
    else if (isPalette(*i))
shun-iwasawa ff99e1
      palettes.push_back(*i);
shun-iwasawa ff99e1
  }
shun-iwasawa ff99e1
  if (!palettes.empty()) {
shun-iwasawa ff99e1
    fps.reserve(fps.size() + palettes.size());
shun-iwasawa ff99e1
    std::copy(palettes.begin(), palettes.end(), std::back_inserter(fps));
shun-iwasawa ff99e1
  }
Shinya Kitaoka 120a6e
  //  fps.push_back(m_root+"butta.tpl");
Shinya Kitaoka 120a6e
  return fps.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int StudioPalette::getChildCount(const TFilePath &folderPath) {
Shinya Kitaoka 120a6e
  TFilePathSet q;
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::readDirectory(q, folderPath);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return q.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool StudioPalette::isFolder(const TFilePath &path) {
Shinya Kitaoka 120a6e
  return TFileStatus(path).isDirectory();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool StudioPalette::isReadOnly(const TFilePath &path) {
Shinya Kitaoka 120a6e
  return !TFileStatus(path).isWritable();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool StudioPalette::isPalette(const TFilePath &path) {
Shinya Kitaoka 120a6e
  return path.getType() == "tpl";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
/*! check if the palette is studio palette or level palette in order to separate
Shinya Kitaoka 120a6e
 * icons in the StudioPaletteTree.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool StudioPalette::hasGlobalName(const TFilePath &path) {
Shinya Kitaoka 120a6e
  return (readPaletteGlobalName(path) != L"");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool StudioPalette::isLevelPalette(const TFilePath &path) {
Shinya Kitaoka 120a6e
  TPalette *palette = getPalette(path);
Shinya Kitaoka 120a6e
  if (!palette) return false;
Shinya Kitaoka 120a6e
  bool ret = !palette->isCleanupPalette();
Shinya Kitaoka 120a6e
  delete palette;
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::createFolder(const TFilePath &parentFolderPath) {
Shinya Kitaoka 120a6e
  TFilePath path = makeUniqueName(parentFolderPath + "new folder");
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::mkDir(path);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  FolderListenerManager::instance()->notifyFolderChanged(parentFolderPath);
Shinya Kitaoka 120a6e
  notifyTreeChange();
Shinya Kitaoka 120a6e
  return path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::createFolder(const TFilePath &parentFolderPath,
Shinya Kitaoka 120a6e
                                 std::wstring name) {
Shinya Kitaoka 120a6e
  TFilePath fp = parentFolderPath + name;
Shinya Kitaoka 120a6e
  if (TFileStatus(fp).doesExist()) return;
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::mkDir(fp);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  FolderListenerManager::instance()->notifyFolderChanged(parentFolderPath);
Shinya Kitaoka 120a6e
  notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::createPalette(const TFilePath &folderPath,
Shinya Kitaoka 120a6e
                                       std::string name) {
Shinya Kitaoka 120a6e
  TPalette *palette    = 0;
Shinya Kitaoka 120a6e
  if (name == "") name = "new palette";
Shinya Kitaoka 120a6e
  palette              = new TPalette();
Shinya Kitaoka 120a6e
  TFilePath fp         = makeUniqueName(folderPath + (name + ".tpl"));
Shinya Kitaoka 120a6e
  time_t ltime;
Shinya Kitaoka 120a6e
  time(<ime);
Shinya Kitaoka 120a6e
  std::wstring gname = std::to_wstring(ltime) + L"_" + std::to_wstring(rand());
Shinya Kitaoka 120a6e
  palette->setGlobalName(gname);
Shinya Kitaoka 120a6e
  setStylesGlobalNames(palette);
Shinya Kitaoka 120a6e
  save(fp, palette);
Shinya Kitaoka 120a6e
  delete palette;
Shinya Kitaoka 120a6e
  notifyTreeChange();
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::setPalette(const TFilePath &palettePath,
Shinya Kitaoka 120a6e
                               const TPalette *plt, bool notifyPaletteChanged) {
Shinya Kitaoka 120a6e
  assert(palettePath.getType() == "tpl");
Shinya Kitaoka 120a6e
  TPalette *palette = plt->clone();
Shinya Kitaoka 120a6e
  palette->setIsLocked(plt->isLocked());
Shinya Kitaoka 120a6e
  palette->addRef();
Shinya Kitaoka 120a6e
  std::wstring pgn = palette->getGlobalName();
Shinya Kitaoka 120a6e
  if (TFileStatus(palettePath).doesExist())
Shinya Kitaoka 120a6e
    pgn = readPaletteGlobalName(palettePath);
Shinya Kitaoka 120a6e
  palette->setGlobalName(pgn);
Shinya Kitaoka 120a6e
  setStylesGlobalNames(palette);
Shinya Kitaoka 120a6e
  save(palettePath, palette);
Shinya Kitaoka 120a6e
  palette->release();
Shinya Kitaoka 120a6e
  if (notifyPaletteChanged) notifyPaletteChange(palettePath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::deletePalette(const TFilePath &palettePath) {
Shinya Kitaoka 120a6e
  assert(palettePath.getType() == "tpl");
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::deleteFile(palettePath);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::deleteFolder(const TFilePath &path) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::rmDirTree(path);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::importPalette(const TFilePath &dstFolder,
Shinya Kitaoka 120a6e
                                       const TFilePath &srcPath) {
Shinya Kitaoka 120a6e
  TPaletteP palette;
Shinya Kitaoka 120a6e
  std::string ext = srcPath.getType();
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (ext == "plt")
Shinya Kitaoka 120a6e
      palette = loadToonz46Palette(srcPath);
Shinya Kitaoka 120a6e
    else if (ext == "pli")
Shinya Kitaoka 120a6e
      palette = loadPliPalette(srcPath);
Shinya Kitaoka 120a6e
    else if (ext == "tpl")
Shinya Kitaoka 120a6e
      palette = loadTplPalette(srcPath);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!palette) return TFilePath();
Shinya Kitaoka 120a6e
  std::wstring name = srcPath.getWideName();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(!palette->isCleanupPalette());
Shinya Kitaoka 120a6e
  //    convertToLevelPalette(palette.getPointer());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePath fp = makeUniqueName(dstFolder + (name + L".tpl"));
Shinya Kitaoka 120a6e
  time_t ltime;
Shinya Kitaoka 120a6e
  time(<ime);
Shinya Kitaoka 120a6e
  std::wstring gname = std::to_wstring(ltime) + L"_" + std::to_wstring(rand());
Shinya Kitaoka 120a6e
  palette->setGlobalName(gname);
Shinya Kitaoka 120a6e
  setStylesGlobalNames(palette.getPointer());
Shinya Kitaoka 120a6e
  TSystem::touchParentDir(fp);
Shinya Kitaoka 120a6e
  save(fp, palette.getPointer());
Shinya Kitaoka 120a6e
  notifyTreeChange();
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
// TFilePath StudioPalette::getRefImage(const TFilePath palette)
Toshihiro Shimizu 890ddd
//{
Toshihiro Shimizu 890ddd
//  return palette.withType("pli");
Toshihiro Shimizu 890ddd
//}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static void foobar(std::wstring paletteId) { table.erase(paletteId); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPalette::getPalettePath(std::wstring paletteId) {
Shinya Kitaoka 120a6e
  std::map<std::wstring, tfilepath="">::iterator it = table.find(paletteId);</std::wstring,>
Shinya Kitaoka 120a6e
  if (it != table.end()) return it->second;
Shinya Kitaoka 120a6e
  TFilePath fp = searchPalette(m_root, paletteId);
Shinya Kitaoka 120a6e
  if (fp == TFilePath()) {
Shinya Kitaoka 120a6e
    fp = searchPalette(getProjectPalettesRoot(), paletteId);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  table[paletteId] = fp;
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPalette *StudioPalette::getPalette(std::wstring paletteId) {
Shinya Kitaoka 120a6e
  TFilePath palettePath = getPalettePath(paletteId);
Shinya Kitaoka 120a6e
  if (palettePath != TFilePath())
Shinya Kitaoka 120a6e
    return getPalette(palettePath);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TColorStyle *StudioPalette::getStyle(std::wstring styleId) { return 0; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::pair<tfilepath, int=""> StudioPalette::getSourceStyle(TColorStyle *cs) {</tfilepath,>
Shinya Kitaoka 120a6e
  std::pair<tfilepath, int=""> ret(TFilePath(), -1);</tfilepath,>
Shinya Kitaoka 120a6e
  if (!cs) return ret;
Shinya Kitaoka 120a6e
  std::wstring gname = cs->getGlobalName();
Shinya Kitaoka 120a6e
  if (gname == L"") return ret;
Shinya Kitaoka 120a6e
  int k = gname.find_first_of(L'-', 1);
Shinya Kitaoka 120a6e
  if (k == (int)std::wstring::npos) return ret;
Shinya Kitaoka 120a6e
  std::wstring paletteId = gname.substr(1, k - 1);
Shinya Kitaoka 120a6e
  ret.first              = getPalettePath(paletteId) - m_root;
Shinya Kitaoka 120a6e
  ret.second             = std::stoi(gname.substr(k + 1));
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! return if any style in the palette is changed
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool StudioPalette::updateLinkedColors(TPalette *palette) {
Shinya Kitaoka 120a6e
  bool paletteIsChanged = false;
Shinya Kitaoka 120a6e
  std::map<std::wstring, tpalettep=""> table;</std::wstring,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < palette->getStyleCount(); i++) {
Shinya Kitaoka 120a6e
    TColorStyle *cs    = palette->getStyle(i);
Shinya Kitaoka 120a6e
    std::wstring gname = cs->getGlobalName();
Shinya Kitaoka 120a6e
    if (gname == L"" || gname[0] != L'+') continue;
Shinya Kitaoka 120a6e
    int k = gname.find_first_of(L'-', 1);
Shinya Kitaoka 120a6e
    if (k == (int)std::wstring::npos) continue;
Shinya Kitaoka 120a6e
    std::wstring paletteId = gname.substr(1, k - 1);
Shinya Kitaoka 120a6e
    std::map<std::wstring, tpalettep="">::iterator it;</std::wstring,>
Shinya Kitaoka 120a6e
    it                  = table.find(paletteId);
Shinya Kitaoka 120a6e
    TPalette *spPalette = 0;
Shinya Kitaoka 120a6e
    if (it == table.end()) {
Shinya Kitaoka 120a6e
      spPalette = StudioPalette::instance()->getPalette(paletteId);
Shinya Kitaoka 120a6e
      if (!spPalette) continue;
Shinya Kitaoka 120a6e
      table[paletteId] = spPalette;
Shinya Kitaoka 120a6e
      // spPalette->release();
Shinya Kitaoka 120a6e
      assert(spPalette->getRefCount() == 1);
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      spPalette = it->second.getPointer();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int j = std::stoi(gname.substr(k + 1));
Shinya Kitaoka 120a6e
    if (spPalette && 0 <= j && j < spPalette->getStyleCount()) {
Shinya Kitaoka 120a6e
      TColorStyle *spStyle = spPalette->getStyle(j);
Shinya Kitaoka 120a6e
      assert(spStyle);
Shinya Kitaoka 120a6e
      spStyle = spStyle->clone();
Shinya Kitaoka 120a6e
      spStyle->setGlobalName(gname);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      // put the style name in the studio palette into the original name
Shinya Kitaoka 120a6e
      spStyle->setOriginalName(spStyle->getName());
Shinya Kitaoka 120a6e
      //.. and keep the style name unchanged
Shinya Kitaoka 120a6e
      spStyle->setName(cs->getName());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      palette->setStyle(i, spStyle);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      paletteIsChanged = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return paletteIsChanged;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::setStylesGlobalNames(TPalette *palette) {
Shinya Kitaoka 120a6e
  for (int i = 0; i < palette->getStyleCount(); i++) {
Shinya Kitaoka 120a6e
    TColorStyle *cs = palette->getStyle(i);
Shinya Kitaoka 120a6e
    // set global name only to the styles of which the global name is empty
Shinya Kitaoka 120a6e
    if (cs->getGlobalName() == L"") {
Shinya Kitaoka 120a6e
      std::wstring gname =
Shinya Kitaoka 120a6e
          L"-" + palette->getGlobalName() + L"-" + std::to_wstring(i);
Shinya Kitaoka 120a6e
      cs->setGlobalName(gname);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::save(const TFilePath &path, TPalette *palette) {
John Dancel 35a3a8
  TFileStatus fs(path);
manongjohn 256551
  if (fs.doesExist() && !fs.isWritable()) {
John Dancel 35a3a8
    throw TSystemException(path,
John Dancel 35a3a8
                           "The studio palette cannot be saved: it is a read "
John Dancel 35a3a8
                           "only studio palette.");
John Dancel 35a3a8
  }
John Dancel 35a3a8
Shinya Kitaoka 120a6e
  TOStream os(path);
Shinya Kitaoka 120a6e
  std::map<std::string, std::string=""> attr;</std::string,>
Shinya Kitaoka 120a6e
  attr["name"] = ::to_string(palette->getGlobalName());
Shinya Kitaoka 120a6e
  os.openChild("palette", attr);
Shinya Kitaoka 120a6e
  palette->saveData(os);
Shinya Kitaoka 120a6e
  os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPalette *StudioPalette::load(const TFilePath &path) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TIStream is(path);
Shinya Kitaoka 120a6e
    if (!is) return 0;
Shinya Kitaoka 120a6e
    std::string tagName;
Shinya Kitaoka 120a6e
    if (!is.matchTag(tagName) || tagName != "palette") return 0;
Shinya Kitaoka 120a6e
    std::string gname;
Shinya Kitaoka 120a6e
    is.getTagParam("name", gname);
Shinya Kitaoka 120a6e
    TPalette *palette = new TPalette();
Shinya Kitaoka 120a6e
    palette->loadData(is);
Shinya Kitaoka 120a6e
    palette->setGlobalName(::to_wstring(gname));
Shinya Kitaoka 120a6e
    is.matchEndTag();
Shinya Kitaoka 120a6e
    palette->setPaletteName(path.getWideName());
Shinya Kitaoka 120a6e
    return palette;
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::addListener(Listener *listener) {
Shinya Kitaoka 120a6e
  if (std::find(m_listeners.begin(), m_listeners.end(), listener) ==
Shinya Kitaoka 120a6e
      m_listeners.end())
Shinya Kitaoka 120a6e
    m_listeners.push_back(listener);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::removeListener(Listener *listener) {
Shinya Kitaoka 120a6e
  m_listeners.erase(
Shinya Kitaoka 120a6e
      std::remove(m_listeners.begin(), m_listeners.end(), listener),
Shinya Kitaoka 120a6e
      m_listeners.end());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::notifyTreeChange() {
Shinya Kitaoka 120a6e
  for (std::vector<listener *="">::iterator it = m_listeners.begin();</listener>
Shinya Kitaoka 120a6e
       it != m_listeners.end(); ++it)
Shinya Kitaoka 120a6e
    (*it)->onStudioPaletteTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::notifyMove(const TFilePath &dstPath,
Shinya Kitaoka 120a6e
                               const TFilePath &srcPath) {
Shinya Kitaoka 120a6e
  for (std::vector<listener *="">::iterator it = m_listeners.begin();</listener>
Shinya Kitaoka 120a6e
       it != m_listeners.end(); ++it)
Shinya Kitaoka 120a6e
    (*it)->onStudioPaletteMove(dstPath, srcPath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPalette::notifyPaletteChange(const TFilePath &palette) {
Shinya Kitaoka 120a6e
  for (std::vector<listener *="">::iterator it = m_listeners.begin();</listener>
Shinya Kitaoka 120a6e
       it != m_listeners.end(); ++it)
Shinya Kitaoka 120a6e
    (*it)->onStudioPaletteChange(palette);
Toshihiro Shimizu 890ddd
}