Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tpaletteutil.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void mergePalette(const TPaletteP &targetPalette,
Shinya Kitaoka 120a6e
                  std::map<int, int=""> &indexTable,</int,>
Shinya Kitaoka 120a6e
                  const TPaletteP &sourcePalette,
Shinya Kitaoka 120a6e
                  const std::set<int> &sourceIndices) {</int>
Shinya Kitaoka 120a6e
  assert(targetPalette);
Shinya Kitaoka 120a6e
  assert(targetPalette->getPageCount() > 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  indexTable[0]                         = 0;
Shinya Kitaoka 120a6e
  std::set<int>::const_iterator styleIt = sourceIndices.begin();</int>
Shinya Kitaoka 120a6e
  for (styleIt; styleIt != sourceIndices.end(); ++styleIt) {
Shinya Kitaoka 120a6e
    int srcStyleId = *styleIt;
Shinya Kitaoka 120a6e
    if (srcStyleId == 0) continue;
Shinya Kitaoka 120a6e
    assert(0 <= srcStyleId && srcStyleId < sourcePalette->getStyleCount());
Shinya Kitaoka 120a6e
    TColorStyle *srcStyle = sourcePalette->getStyle(srcStyleId);
Shinya Kitaoka 120a6e
    assert(srcStyle);
Shinya Kitaoka 120a6e
    TPalette::Page *page = sourcePalette->getStylePage(srcStyleId);
Shinya Kitaoka 120a6e
    if (page) {
Shinya Kitaoka 120a6e
      std::wstring pageName = page->getName();
Shinya Kitaoka 120a6e
      int j                 = 0;
Shinya Kitaoka 120a6e
      for (j = 0; j < targetPalette->getPageCount(); j++) {
Shinya Kitaoka 120a6e
        if (targetPalette->getPage(j)->getName() != pageName) continue;
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      if (j < targetPalette->getPageCount())
Shinya Kitaoka 120a6e
        page = targetPalette->getPage(j);
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        page = targetPalette->addPage(pageName);
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      page         = targetPalette->getPage(0);
Shinya Kitaoka 120a6e
    int tarStyleId = 0;
Shinya Kitaoka 120a6e
    int i          = 0;
Shinya Kitaoka 120a6e
    for (i = 0; i < targetPalette->getStyleCount(); i++)
Shinya Kitaoka 120a6e
      if (*srcStyle == *targetPalette->getStyle(i) && i == srcStyleId) break;
Shinya Kitaoka 120a6e
    if (i < targetPalette->getStyleCount())
Shinya Kitaoka 120a6e
      tarStyleId = i;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      TColorStyle *dstStyle = srcStyle->clone();
Shinya Kitaoka 120a6e
      tarStyleId            = targetPalette->addStyle(dstStyle);
Shinya Kitaoka 120a6e
      page->addStyle(tarStyleId);
Shinya Kitaoka 120a6e
      targetPalette->setDirtyFlag(true);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    assert(indexTable.count(srcStyleId) == 0);
Shinya Kitaoka 120a6e
    indexTable[srcStyleId] = tarStyleId;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// replace palette and lacking amount of styles will be copied from the other
Shinya Kitaoka 120a6e
// one
Toshihiro Shimizu 890ddd
// return value will be true if the style amount is changed after the operation
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool mergePalette_Overlap(const TPaletteP &dstPalette,
Shinya Kitaoka 120a6e
                          const TPaletteP &copiedPalette,
Shinya Kitaoka 120a6e
                          bool keepOriginalPalette) {
Shinya Kitaoka 120a6e
  if (!dstPalette || !copiedPalette) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool styleAdded = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int dstStyleCount    = dstPalette->getStyleCount();
Shinya Kitaoka 120a6e
  int copiedStyleCount = copiedPalette->getStyleCount();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // keep original
Shinya Kitaoka 120a6e
  if (keepOriginalPalette) {
Shinya Kitaoka 120a6e
    // do nothing if the style amount of the dst is equal or larger than the
Shinya Kitaoka 120a6e
    // copied
Shinya Kitaoka 120a6e
    if (dstStyleCount >= copiedStyleCount) {
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // if the style amount of the dst is less than the copied
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      // for lacking amount of styles
Shinya Kitaoka 120a6e
      for (int i = dstStyleCount; i < copiedStyleCount; i++) {
Shinya Kitaoka 120a6e
        // get the page index of the copied style
Shinya Kitaoka 120a6e
        TPalette::Page *tmpPage = copiedPalette->getStylePage(i);
Shinya Kitaoka 120a6e
        // clone copied style
Shinya Kitaoka 120a6e
        TColorStyle *tmpStyle = copiedPalette->getStyle(i)->clone();
Shinya Kitaoka 120a6e
        // add it to the dst
Shinya Kitaoka 120a6e
        int id = dstPalette->addStyle(tmpStyle);
Shinya Kitaoka 120a6e
        // add to the page if it is not deleted in the copied
Shinya Kitaoka 120a6e
        if (tmpPage) dstPalette->getPage(0)->addStyle(id);
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      styleAdded = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // replace
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    // if the style amount of the dst is larger than the copied
Shinya Kitaoka 120a6e
    if (dstStyleCount > copiedStyleCount) {
Shinya Kitaoka 120a6e
      TPalette *tmpPalette = copiedPalette->clone();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      // for lacking amount of styles
Shinya Kitaoka 120a6e
      for (int i = copiedStyleCount; i < dstStyleCount; i++) {
Shinya Kitaoka 120a6e
        TColorStyle *tmpStyle = dstPalette->getStyle(i)->clone();
Shinya Kitaoka 120a6e
        int id                = tmpPalette->addStyle(tmpStyle);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        TPalette::Page *tmpPage = dstPalette->getStylePage(i);
Shinya Kitaoka 120a6e
        if (!tmpPage) continue;
Shinya Kitaoka 120a6e
        std::wstring pageName = tmpPage->getName();
Shinya Kitaoka 120a6e
        // create new page with the same name if needed
Shinya Kitaoka 120a6e
        int p;
Shinya Kitaoka 120a6e
        for (p = 0; p < tmpPalette->getPageCount(); p++) {
Shinya Kitaoka 120a6e
          if (tmpPalette->getPage(p)->getName() == pageName) break;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if (p != tmpPalette->getPageCount())
Shinya Kitaoka 120a6e
          tmpPalette->getPage(p)->addStyle(id);
Shinya Kitaoka 120a6e
        else {
Shinya Kitaoka 120a6e
          tmpPalette->addPage(pageName)->addStyle(id);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      dstPalette->assign(tmpPalette);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      styleAdded = false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // if the style amount of the dst is equal or less than the copied
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      dstPalette->assign(copiedPalette.getPointer());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      styleAdded = (dstStyleCount < copiedStyleCount);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  dstPalette->setDirtyFlag(true);
Shinya Kitaoka 120a6e
  return styleAdded;
Toshihiro Shimizu 890ddd
}