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