Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/palettecmd.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/tpalettehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/studiopalette.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcell.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/cleanupcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "toonz/studiopalette.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/cleanupcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tundo.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tcolorutils.h"
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "tregion.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "historytypes.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "tcg/boost/range_utility.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// boost includes
Toshihiro Shimizu 890ddd
#include <boost bind.hpp=""></boost>
Toshihiro Shimizu 890ddd
#include <boost counting_range.hpp="" range=""></boost>
Toshihiro Shimizu 890ddd
#include <boost adaptor="" range="" transformed.hpp=""></boost>
Toshihiro Shimizu 890ddd
#include <boost adaptor="" filtered.hpp="" range=""></boost>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void findPaletteLevels(set<txshsimplelevel *=""> &levels, int &rowIndex, int &columnIndex,</txshsimplelevel>
Toshihiro Shimizu 890ddd
					   TPalette *palette, TXsheet *xsheet)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	rowIndex = columnIndex = -1;
Toshihiro Shimizu 890ddd
	int columnCount = xsheet->getColumnCount();
Toshihiro Shimizu 890ddd
	int c;
Toshihiro Shimizu 890ddd
	for (c = 0; c < columnCount; c++) {
Toshihiro Shimizu 890ddd
		TXshColumn *column = xsheet->getColumn(c);
Toshihiro Shimizu 890ddd
		if (!column || column->isEmpty())
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TXshLevelColumn *levelColumn = column->getLevelColumn();
Toshihiro Shimizu 890ddd
		if (!levelColumn || levelColumn->isEmpty())
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int r0, r1;
Toshihiro Shimizu 890ddd
		if (!column->getRange(r0, r1))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int r;
Toshihiro Shimizu 890ddd
		for (r = r0; r <= r1; r++) {
Toshihiro Shimizu 890ddd
			TXshCell cell = levelColumn->getCell(r);
Toshihiro Shimizu 890ddd
			if (cell.isEmpty())
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			TXshSimpleLevel *level = cell.getSimpleLevel();
Toshihiro Shimizu 890ddd
			if (!level || level->getPalette() != palette)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			levels.insert(level);
Toshihiro Shimizu 890ddd
			if (rowIndex < 0) {
Toshihiro Shimizu 890ddd
				rowIndex = r;
Toshihiro Shimizu 890ddd
				columnIndex = c;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool isStyleUsed(const TVectorImageP vi, int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int strokeCount = vi->getStrokeCount();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = strokeCount - 1; i >= 0; i--) {
Toshihiro Shimizu 890ddd
		TStroke *stroke = vi->getStroke(i);
Toshihiro Shimizu 890ddd
		if (stroke && stroke->getStyle() == styleId)
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int regionCount = vi->getRegionCount();
Toshihiro Shimizu 890ddd
	for (i = 0; i < regionCount; i++) {
Toshihiro Shimizu 890ddd
		TRegion *region = vi->getRegion(i);
Toshihiro Shimizu 890ddd
		if (region || region->getStyle() != styleId)
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool isStyleUsed(const TToonzImageP vi, int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterCM32P ras = vi->getRaster();
Toshihiro Shimizu 890ddd
	for (int y = 0; y < ras->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
		while (pix < endPix) {
Toshihiro Shimizu 890ddd
			if (pix->getPaint() == styleId)
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
			if (pix->getInk() == styleId)
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
			pix++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Return true if one style is used. */
Toshihiro Shimizu 890ddd
bool areStylesUsed(const TImageP image, const vector<int> styleIds)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	for (j = 0; j < (int)styleIds.size(); j++)
Toshihiro Shimizu 890ddd
		if (isStyleUsed(image, styleIds[j]))
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool isStyleUsed(const TImageP image, int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TVectorImageP vi = image;
Toshihiro Shimizu 890ddd
	TToonzImageP ti = image;
Toshihiro Shimizu 890ddd
	if (vi)
Toshihiro Shimizu 890ddd
		return isStyleUsed(vi, styleId);
Toshihiro Shimizu 890ddd
	if (ti)
Toshihiro Shimizu 890ddd
		return isStyleUsed(ti, styleId);
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! Return true if one style is used. */
Toshihiro Shimizu 890ddd
bool areStylesUsed(const set<txshsimplelevel *=""> levels, const vector<int> styleIds)</int></txshsimplelevel>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	set<txshsimplelevel *="">::const_iterator it = levels.begin();</txshsimplelevel>
Toshihiro Shimizu 890ddd
	for (it; it != levels.end(); it++) {
Toshihiro Shimizu 890ddd
		TXshSimpleLevel *level = *it;
Toshihiro Shimizu 890ddd
		vector<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
		level->getFids(fids);
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		for (i = 0; i < (int)fids.size(); i++) {
Toshihiro Shimizu 890ddd
			TImageP image = level->getFrame(fids[i], true);
Toshihiro Shimizu 890ddd
			if (areStylesUsed(image, styleIds))
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// arrangeStyles
Toshihiro Shimizu 890ddd
// srcPage : {a0,a1,...an } ==> dstPage : {b,b+1,...b+n-1}
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \namespace PaletteCmd
Toshihiro Shimizu 890ddd
		\brief Provides a collection of methods to manage \b TPalette.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ArrangeStylesUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_dstPageIndex;
Toshihiro Shimizu 890ddd
	int m_dstIndexInPage;
Toshihiro Shimizu 890ddd
	int m_srcPageIndex;
Toshihiro Shimizu 890ddd
	std::set<int> m_srcIndicesInPage;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ArrangeStylesUndo(TPaletteHandle *paletteHandle,
Toshihiro Shimizu 890ddd
					  int dstPageIndex,
Toshihiro Shimizu 890ddd
					  int dstIndexInPage,
Toshihiro Shimizu 890ddd
					  int srcPageIndex,
Toshihiro Shimizu 890ddd
					  const std::set<int> &srcIndicesInPage)</int>
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_dstPageIndex(dstPageIndex), m_dstIndexInPage(dstIndexInPage), m_srcPageIndex(srcPageIndex), m_srcIndicesInPage(srcIndicesInPage)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		assert(0 <= dstPageIndex && dstPageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		assert(0 <= srcPageIndex && srcPageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		TPalette::Page *dstPage = m_palette->getPage(dstPageIndex);
Toshihiro Shimizu 890ddd
		assert(dstPage);
Toshihiro Shimizu 890ddd
		assert(0 <= dstIndexInPage && dstIndexInPage <= dstPage->getStyleCount());
Toshihiro Shimizu 890ddd
		assert(!srcIndicesInPage.empty());
Toshihiro Shimizu 890ddd
		TPalette::Page *srcPage = m_palette->getPage(srcPageIndex);
Toshihiro Shimizu 890ddd
		assert(srcPage);
Toshihiro Shimizu 890ddd
		assert(0 <= *srcIndicesInPage.begin() && *srcIndicesInPage.rbegin() < srcPage->getStyleCount());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *srcPage = m_palette->getPage(m_srcPageIndex);
Toshihiro Shimizu 890ddd
		assert(srcPage);
Toshihiro Shimizu 890ddd
		TPalette::Page *dstPage = m_palette->getPage(m_dstPageIndex);
Toshihiro Shimizu 890ddd
		assert(dstPage);
Toshihiro Shimizu 890ddd
		std::vector<int> styles;</int>
Toshihiro Shimizu 890ddd
		int count = m_srcIndicesInPage.size();
Toshihiro Shimizu 890ddd
		int h = m_dstIndexInPage;
Toshihiro Shimizu 890ddd
		std::set<int>::const_iterator i;</int>
Toshihiro Shimizu 890ddd
		for (i = m_srcIndicesInPage.begin(); i != m_srcIndicesInPage.end(); ++i) {
Toshihiro Shimizu 890ddd
			if (srcPage == dstPage && (*i) <= h)
Toshihiro Shimizu 890ddd
				h--;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		assert(h + count - 1 <= dstPage->getStyleCount());
Toshihiro Shimizu 890ddd
		int k;
Toshihiro Shimizu 890ddd
		for (k = 0; k < count; k++) {
Toshihiro Shimizu 890ddd
			styles.push_back(dstPage->getStyleId(h));
Toshihiro Shimizu 890ddd
			dstPage->removeStyle(h);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		k = 0;
Toshihiro Shimizu 890ddd
		for (i = m_srcIndicesInPage.begin(); i != m_srcIndicesInPage.end(); ++i, ++k)
Toshihiro Shimizu 890ddd
			srcPage->insertStyle(*i, styles[k]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *srcPage = m_palette->getPage(m_srcPageIndex);
Toshihiro Shimizu 890ddd
		assert(srcPage);
Toshihiro Shimizu 890ddd
		TPalette::Page *dstPage = m_palette->getPage(m_dstPageIndex);
Toshihiro Shimizu 890ddd
		assert(dstPage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::vector<int> styles;</int>
Toshihiro Shimizu 890ddd
		std::set<int>::const_reverse_iterator i;</int>
Toshihiro Shimizu 890ddd
		std::vector<int>::iterator j;</int>
Toshihiro Shimizu 890ddd
		int k = m_dstIndexInPage;
Toshihiro Shimizu 890ddd
		for (i = m_srcIndicesInPage.rbegin(); i != m_srcIndicesInPage.rend(); ++i) {
Toshihiro Shimizu 890ddd
			int index = *i;
Toshihiro Shimizu 890ddd
			if (m_dstPageIndex == m_srcPageIndex && index < k)
Toshihiro Shimizu 890ddd
				k--;
Toshihiro Shimizu 890ddd
			styles.push_back(srcPage->getStyleId(index));
Toshihiro Shimizu 890ddd
			srcPage->removeStyle(index);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		for (j = styles.begin(); j != styles.end(); ++j)
Toshihiro Shimizu 890ddd
			dstPage->insertStyle(k, *j);
Toshihiro Shimizu 890ddd
		m_palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this + m_srcIndicesInPage.size() * sizeof(int); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Arrange Styles  in Palette %1")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::arrangeStyles(TPaletteHandle *paletteHandle,
Toshihiro Shimizu 890ddd
							   int dstPageIndex, int dstIndexInPage,
Toshihiro Shimizu 890ddd
							   int srcPageIndex, const std::set<int> &srcIndicesInPage)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (dstPageIndex == srcPageIndex && dstIndexInPage == *srcIndicesInPage.begin())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	ArrangeStylesUndo *undo =
Toshihiro Shimizu 890ddd
		new ArrangeStylesUndo(paletteHandle, dstPageIndex,
Toshihiro Shimizu 890ddd
							  dstIndexInPage, srcPageIndex,
Toshihiro Shimizu 890ddd
							  srcIndicesInPage);
Toshihiro Shimizu 890ddd
	undo->redo();
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(undo);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
//CreateStyle
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class CreateStyleUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_pageIndex;
Toshihiro Shimizu 890ddd
	int m_styleId;
Toshihiro Shimizu 890ddd
	TColorStyle *m_style;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CreateStyleUndo(
Toshihiro Shimizu 890ddd
		TPaletteHandle *paletteHandle,
Toshihiro Shimizu 890ddd
		int pageIndex,
Toshihiro Shimizu 890ddd
		int styleId)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_pageIndex(pageIndex), m_styleId(styleId)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		m_style = m_palette->getStyle(m_styleId)->clone();
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		assert(0 <= pageIndex && pageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		assert(0 <= styleId && styleId < m_palette->getStyleCount());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		int indexInPage = page->search(m_styleId);
Toshihiro Shimizu 890ddd
		assert(indexInPage >= 0);
Toshihiro Shimizu 890ddd
		page->removeStyle(indexInPage);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		assert(m_palette->getStylePage(m_styleId) == 0);
Toshihiro Shimizu 890ddd
		int indexInPage = page->addStyle(m_styleId);
Toshihiro Shimizu 890ddd
		if (indexInPage == -1) {
Toshihiro Shimizu 890ddd
			indexInPage = page->getStyleCount();
Toshihiro Shimizu 890ddd
			page->insertStyle(indexInPage, m_style->getMainColor());
Toshihiro Shimizu 890ddd
			assert(m_styleId == page->getStyleId(indexInPage));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		m_palette->getStyle(m_styleId)->setMainColor(m_style->getMainColor());
Toshihiro Shimizu 890ddd
		m_palette->getStyle(m_styleId)->setName(m_style->getName());
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this; }
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Create Style#%1  in Palette %2")
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_styleId))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::createStyle(
Toshihiro Shimizu 890ddd
	TPaletteHandle *paletteHandle,
Toshihiro Shimizu 890ddd
	TPalette::Page *page)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int index = paletteHandle->getStyleIndex();
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	int newIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int UnpagedId = palette->getFirstUnpagedStyle();
Toshihiro Shimizu 890ddd
	if (UnpagedId != -1 && !palette->isCleanupPalette()) {
Toshihiro Shimizu 890ddd
		if (index == -1)
Toshihiro Shimizu 890ddd
			palette->getStyle(UnpagedId)->setMainColor(TPixel32::Black);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			palette->getStyle(UnpagedId)->setMainColor(palette->getStyle(index)->getMainColor());
Toshihiro Shimizu 890ddd
		newIndex = page->addStyle(UnpagedId);
Toshihiro Shimizu 890ddd
	} else if (!palette->isCleanupPalette()) {
Toshihiro Shimizu 890ddd
		if (index == -1)
Toshihiro Shimizu 890ddd
			newIndex = page->addStyle(TPixel32::Black);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			TColorStyle *style = palette->getStyle(index);
Toshihiro Shimizu 890ddd
			TCleanupStyle *cleanupStyle = dynamic_cast<tcleanupstyle *="">(style);</tcleanupstyle>
Toshihiro Shimizu 890ddd
			if ((cleanupStyle || index == 0) && palette->isCleanupPalette()) {
Toshihiro Shimizu 890ddd
				TColorCleanupStyle *newCleanupStyle = new TColorCleanupStyle();
Toshihiro Shimizu 890ddd
				if (cleanupStyle) {
Toshihiro Shimizu 890ddd
					int i;
Toshihiro Shimizu 890ddd
					for (i = 0; i < cleanupStyle->getParamCount(); i++)
Toshihiro Shimizu 890ddd
						newCleanupStyle->setColorParamValue(i, cleanupStyle->getColorParamValue(i));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				newIndex = page->addStyle(newCleanupStyle);
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				newIndex = page->addStyle(style->getMainColor());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else /*- CleanupPaletteの場合 -*/
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		newIndex = page->addStyle(new TColorCleanupStyle(TPixel32::Red));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int newStyleId = page->getStyleId(newIndex);
Toshihiro Shimizu 890ddd
	/*-  StudioPalette上でStyleを追加した場合、GlobalNameを自動で割り振る -*/
Toshihiro Shimizu 890ddd
	if (palette->getGlobalName() != L"") {
Toshihiro Shimizu 890ddd
		TColorStyle *cs = palette->getStyle(newStyleId);
Toshihiro Shimizu 890ddd
		wstring gname = L"-" + palette->getGlobalName() + L"-" + toWideString(newStyleId);
Toshihiro Shimizu 890ddd
		cs->setGlobalName(gname);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	page->getStyle(newIndex)->setName(QString("color_%1").arg(newStyleId).toStdWString());
Toshihiro Shimizu 890ddd
	paletteHandle->setStyleIndex(newStyleId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(new CreateStyleUndo(paletteHandle, page->getIndex(), page->getStyleId(newIndex)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// addStyles
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// AddStylesUndo
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class AddStylesUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_pageIndex;
Toshihiro Shimizu 890ddd
	int m_indexInPage;
Toshihiro Shimizu 890ddd
	std::vector<std::pair<tcolorstyle *,="" int="">> m_styles;</std::pair<tcolorstyle>
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	// creare DOPO l'inserimento
Toshihiro Shimizu 890ddd
	AddStylesUndo(
Toshihiro Shimizu 890ddd
		const TPaletteP &palette,
Toshihiro Shimizu 890ddd
		int pageIndex,
Toshihiro Shimizu 890ddd
		int indexInPage,
Toshihiro Shimizu 890ddd
		int count,
Toshihiro Shimizu 890ddd
		TPaletteHandle *paletteHandle)
Toshihiro Shimizu 890ddd
		: m_palette(palette), m_pageIndex(pageIndex), m_indexInPage(indexInPage), m_paletteHandle(paletteHandle)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		assert(0 <= m_pageIndex && m_pageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		assert(0 <= indexInPage && indexInPage + count <= page->getStyleCount());
Toshihiro Shimizu 890ddd
		for (int i = 0; i < count; i++) {
Toshihiro Shimizu 890ddd
			std::pair<tcolorstyle *,="" int=""> p;</tcolorstyle>
Toshihiro Shimizu 890ddd
			p.second = page->getStyleId(m_indexInPage + i);
Toshihiro Shimizu 890ddd
			p.first = m_palette->getStyle(p.second)->clone();
Toshihiro Shimizu 890ddd
			m_styles.push_back(p);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~AddStylesUndo()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)m_styles.size(); i++)
Toshihiro Shimizu 890ddd
			delete m_styles[i].first;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		int count = m_styles.size();
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		for (i = count - 1; i >= 0; i--)
Toshihiro Shimizu 890ddd
			page->removeStyle(m_indexInPage + i);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)m_styles.size(); i++) {
Toshihiro Shimizu 890ddd
			TColorStyle *cs = m_styles[i].first->clone();
Toshihiro Shimizu 890ddd
			int styleId = m_styles[i].second;
Toshihiro Shimizu 890ddd
			assert(m_palette->getStylePage(styleId) == 0);
Toshihiro Shimizu 890ddd
			m_palette->setStyle(styleId, cs);
Toshihiro Shimizu 890ddd
			page->insertStyle(m_indexInPage + i, styleId);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this + m_styles.size() * sizeof(TColorStyle); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Add Style  to Palette %1")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*- ドラッグ&ドロップ時に呼ばれる -*/
Toshihiro Shimizu 890ddd
void PaletteCmd::addStyles(TPaletteHandle *paletteHandle, int pageIndex,
Toshihiro Shimizu 890ddd
						   int indexInPage, const std::vector<tcolorstyle *=""> &styles)</tcolorstyle>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	assert(0 <= pageIndex && pageIndex < palette->getPageCount());
Toshihiro Shimizu 890ddd
	TPalette::Page *page = palette->getPage(pageIndex);
Toshihiro Shimizu 890ddd
	assert(page);
Toshihiro Shimizu 890ddd
	assert(0 <= indexInPage && indexInPage <= page->getStyleCount());
Toshihiro Shimizu 890ddd
	int count = styles.size();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < count; i++) {
Toshihiro Shimizu 890ddd
		page->insertStyle(indexInPage + i, styles[i]->clone());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*-- StudioPaletteから持ち込んだ場合、その元の名前をoriginalNameに入れる --*/
Toshihiro Shimizu 890ddd
		if (styles[i]->getGlobalName() != L"") {
Toshihiro Shimizu 890ddd
			/*-- もし元のStyleにOriginalNameが無ければ(コピー元がStudioPaletteからということ)--*/
Toshihiro Shimizu 890ddd
			if (styles[i]->getOriginalName() == L"") {
Toshihiro Shimizu 890ddd
				/*-- 元のStyleの名前をペースト先のOriginalNameに入れる --*/
Toshihiro Shimizu 890ddd
				page->getStyle(indexInPage + i)->setOriginalName(styles[i]->getName());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		/*-- それ以外の場合は、clone()でそれぞれの名前をコピーしているので、そのままでOK --*/
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(new AddStylesUndo(palette, pageIndex, indexInPage, count, paletteHandle));
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// eraseStyles
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void eraseStylesInLevels(const set<txshsimplelevel *=""> &levels, const vector<int> styleIds)</int></txshsimplelevel>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	set<txshsimplelevel *="">::const_iterator it = levels.begin();</txshsimplelevel>
Toshihiro Shimizu 890ddd
	for (it; it != levels.end(); it++) {
Toshihiro Shimizu 890ddd
		TXshSimpleLevel *level = *it;
Toshihiro Shimizu 890ddd
		vector<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
		level->getFids(fids);
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		for (i = 0; i < (int)fids.size(); i++) {
Toshihiro Shimizu 890ddd
			TImageP image = level->getFrame(fids[i], true);
Toshihiro Shimizu 890ddd
			TVectorImageP vi = image;
Toshihiro Shimizu 890ddd
			TToonzImageP ti = image;
Toshihiro Shimizu 890ddd
			if (vi)
Toshihiro Shimizu 890ddd
				vi->eraseStyleIds(styleIds);
Toshihiro Shimizu 890ddd
			else if (ti)
Toshihiro Shimizu 890ddd
				TRop::eraseStyleIds(ti.getPointer(), styleIds);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::eraseStyles(const std::set<txshsimplelevel *=""> &levels,</txshsimplelevel>
Toshihiro Shimizu 890ddd
							 const std::vector<int> &styleIds)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	typedef std::pair<const std::vector<tvectorimagep="" txshsimplelevelp,="">> LevelImages;</const>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	struct Undo : public TUndo {
Toshihiro Shimizu 890ddd
		std::set<txshsimplelevel *=""> m_levels;</txshsimplelevel>
Toshihiro Shimizu 890ddd
		std::vector<int> m_styleIds;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		mutable std::map<txshsimplelevelp, std::vector<tvectorimagep="">> m_imagesByLevel;</txshsimplelevelp,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		Undo(const std::set<txshsimplelevel *=""> &levels, const std::vector<int> &styleIds)</int></txshsimplelevel>
Toshihiro Shimizu 890ddd
			: m_levels(levels), m_styleIds(styleIds)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			tcg::substitute(m_imagesByLevel, levels | boost::adaptors::filtered(isVector) | boost::adaptors::transformed(toEmptyLevelImages));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool isValid() const { return !m_levels.empty(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		void redo() const
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			std::for_each(m_imagesByLevel.begin(), m_imagesByLevel.end(), cloneImages);
Toshihiro Shimizu 890ddd
			eraseStylesInLevels(m_levels, m_styleIds);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		void undo() const
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			std::for_each(m_imagesByLevel.begin(), m_imagesByLevel.end(), restoreImages);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int getSize() const { return 10 << 20; } // At max 10 per 100 MB
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	private:
Toshihiro Shimizu 890ddd
		static bool isVector(const TXshSimpleLevel *level)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			return (assert(level), level->getType() == PLI_XSHLEVEL);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static LevelImages toEmptyLevelImages(TXshSimpleLevel *level)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			return LevelImages(level, std::vector<tvectorimagep>());</tvectorimagep>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static void copyStrokeIds(const TVectorImage &src, TVectorImage &dst)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			UINT s, sCount = src.getStrokeCount();
Toshihiro Shimizu 890ddd
			for (s = 0; s != sCount; ++s)
Toshihiro Shimizu 890ddd
				dst.getStroke(s)->setId(src.getStroke(s)->getId());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static TVectorImageP cloneImage(const TXshSimpleLevel &level, int f)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			TVectorImageP src = static_cast<tvectorimagep>(level.getFrame(level.getFrameId(f), false));</tvectorimagep>
Toshihiro Shimizu 890ddd
			TVectorImageP dst = src->clone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			copyStrokeIds(*src, *dst);
Toshihiro Shimizu 890ddd
			return dst;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static void cloneImages(LevelImages &levelImages)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			tcg::substitute(levelImages.second, boost::counting_range(0, levelImages.first->getFrameCount()) | boost::adaptors::transformed(boost::bind(cloneImage, boost::cref(*levelImages.first), _1)));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static void restoreImage(const TXshSimpleLevelP &level, int f, const TVectorImageP &vi)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			level->setFrame(level->getFrameId(f), vi.getPointer());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		static void restoreImages(LevelImages &levelImages)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			int f, fCount = tmin(levelImages.first->getFrameCount(), int(levelImages.second.size()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (f = 0; f != fCount; ++f)
Toshihiro Shimizu 890ddd
				restoreImage(levelImages.first, f, levelImages.second[f]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	}; // Undo
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (levels.empty() || styleIds.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::auto_ptr<tundo> undo(new Undo(levels, styleIds));</tundo>
Toshihiro Shimizu 890ddd
	if (static_cast<undo &="">(*undo).isValid()) {</undo>
Toshihiro Shimizu 890ddd
		undo->redo();
Toshihiro Shimizu 890ddd
		TUndoManager::manager()->add(undo.release());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// addPage
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class AddPageUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_pageIndex;
Toshihiro Shimizu 890ddd
	wstring m_pageName;
Toshihiro Shimizu 890ddd
	std::vector<std::pair<tcolorstyle *,="" int="">> m_styles;</std::pair<tcolorstyle>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	// creare DOPO l'inserimento
Toshihiro Shimizu 890ddd
	AddPageUndo(
Toshihiro Shimizu 890ddd
		TPaletteHandle *paletteHandle,
Toshihiro Shimizu 890ddd
		int pageIndex,
Toshihiro Shimizu 890ddd
		wstring pageName)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_pageIndex(pageIndex), m_pageName(pageName)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		assert(0 <= m_pageIndex && m_pageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		for (int i = 0; i < page->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
			std::pair<tcolorstyle *,="" int=""> p;</tcolorstyle>
Toshihiro Shimizu 890ddd
			p.first = page->getStyle(i)->clone();
Toshihiro Shimizu 890ddd
			p.second = page->getStyleId(i);
Toshihiro Shimizu 890ddd
			m_styles.push_back(p);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~AddPageUndo()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)m_styles.size(); i++)
Toshihiro Shimizu 890ddd
			delete m_styles[i].first;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->erasePage(m_pageIndex);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->addPage(m_pageName);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		assert(page->getIndex() == m_pageIndex);
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)m_styles.size(); i++) {
Toshihiro Shimizu 890ddd
			TColorStyle *cs = m_styles[i].first->clone();
Toshihiro Shimizu 890ddd
			int styleId = m_styles[i].second;
Toshihiro Shimizu 890ddd
			assert(m_palette->getStylePage(styleId) == 0);
Toshihiro Shimizu 890ddd
			m_palette->setStyle(styleId, cs);
Toshihiro Shimizu 890ddd
			page->addStyle(styleId);
Toshihiro Shimizu 890ddd
		};
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this + m_styles.size() * sizeof(TColorStyle); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Add Page %1 to Palette %2")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_pageName))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::addPage(TPaletteHandle *paletteHandle, wstring name, bool withUndo)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	if (name == L"")
Toshihiro Shimizu 890ddd
		name = L"page " + toWideString(palette->getPageCount() + 1);
Toshihiro Shimizu 890ddd
	TPalette::Page *page = palette->addPage(name);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	if (withUndo)
Toshihiro Shimizu 890ddd
		TUndoManager::manager()->add(new AddPageUndo(paletteHandle, page->getIndex(), name));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// destroyPage
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DestroyPageUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_pageIndex;
Toshihiro Shimizu 890ddd
	wstring m_pageName;
Toshihiro Shimizu 890ddd
	std::vector<int> m_styles;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	DestroyPageUndo(TPaletteHandle *paletteHandle, int pageIndex)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_pageIndex(pageIndex)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		assert(0 <= pageIndex && pageIndex < m_palette->getPageCount());
Toshihiro Shimizu 890ddd
		assert(m_palette->getPageCount() > 1);
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		m_pageName = page->getName();
Toshihiro Shimizu 890ddd
		m_styles.resize(page->getStyleCount());
Toshihiro Shimizu 890ddd
		for (int i = 0; i < page->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
			m_styles[i] = page->getStyleId(i);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->addPage(m_pageName);
Toshihiro Shimizu 890ddd
		m_palette->movePage(page, m_pageIndex);
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)m_styles.size(); i++)
Toshihiro Shimizu 890ddd
			page->addStyle(m_styles[i]);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->erasePage(m_pageIndex);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this; }
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Delete Page %1 from Palette %2")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_pageName))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::destroyPage(TPaletteHandle *paletteHandle, int pageIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	assert(0 <= pageIndex && pageIndex < palette->getPageCount());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(new DestroyPageUndo(paletteHandle, pageIndex));
Toshihiro Shimizu 890ddd
	palette->erasePage(pageIndex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// ReferenceImage
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// SetReferenceImageUndo
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class SetReferenceImageUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	TPaletteP m_oldPalette, m_newPalette;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	SetReferenceImageUndo(TPaletteP palette, TPaletteHandle *paletteHandle)
Toshihiro Shimizu 890ddd
		: m_palette(palette), m_oldPalette(palette->clone()), m_paletteHandle(paletteHandle)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void onAdd()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_newPalette = m_palette->clone();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->assign(m_oldPalette.getPointer());
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->assign(m_newPalette.getPointer());
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return sizeof(*this) + sizeof(TPalette) * 2;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Load Color Model %1  to Palette %2")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdString(m_newPalette->getRefImgPath().getLevelName()))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// loadRefImage
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int loadRefImage(TPaletteHandle *paletteHandle, bool replace,
Toshihiro Shimizu 890ddd
				 TPaletteP levelPalette, const TFilePath &_fp, int &frame, ToonzScene *scene,
Toshihiro Shimizu 890ddd
				 const std::vector<int> &frames)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool paletteAlreadyLoaded = false;
Toshihiro Shimizu 890ddd
	TFilePath fp = scene->decodeFilePath(_fp);
Toshihiro Shimizu 890ddd
	if (_fp == TFilePath()) {
Toshihiro Shimizu 890ddd
		paletteAlreadyLoaded = true;
Toshihiro Shimizu 890ddd
		fp = levelPalette->getRefImgPath();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TImageP img;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TLevelReaderP lr(fp);
Toshihiro Shimizu 890ddd
		if (fp != TFilePath() && lr) {
Toshihiro Shimizu 890ddd
			TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
			if (level && level->getFrameCount() > 0) {
Toshihiro Shimizu 890ddd
				TLevel::Iterator it;
Toshihiro Shimizu 890ddd
				if (!paletteAlreadyLoaded) {
Toshihiro Shimizu 890ddd
					std::vector<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
					for (it = level->begin(); it != level->end(); ++it) {
Toshihiro Shimizu 890ddd
						if (it->first == -1 || it->first == -2) {
Toshihiro Shimizu 890ddd
							assert(level->getFrameCount() == 1);
Toshihiro Shimizu 890ddd
							fids.push_back(0);
Toshihiro Shimizu 890ddd
							break;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
						// if the frame list is empty, store all fids of the level
Toshihiro Shimizu 890ddd
						if (frames.empty()) {
Toshihiro Shimizu 890ddd
							fids.push_back(it->first);
Toshihiro Shimizu 890ddd
							continue;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
						// if the frame list is specified, load only the frames matches with the list
Toshihiro Shimizu 890ddd
						else {
Toshihiro Shimizu 890ddd
							std::vector<int>::const_iterator framesIt;</int>
Toshihiro Shimizu 890ddd
							for (framesIt = frames.begin(); framesIt != frames.end(); framesIt++) {
Toshihiro Shimizu 890ddd
								if (it->first.getNumber() == *framesIt) {
Toshihiro Shimizu 890ddd
									fids.push_back(it->first);
Toshihiro Shimizu 890ddd
									break;
Toshihiro Shimizu 890ddd
								}
Toshihiro Shimizu 890ddd
							}
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					levelPalette->setRefLevelFids(fids);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (frame <= 0)
Toshihiro Shimizu 890ddd
					frame = level->begin()->first.getNumber();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				const TLevel::Table *table = level->getTable();
Toshihiro Shimizu 890ddd
				TFrameId fid(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (table->find(fid) != table->end()) {
Toshihiro Shimizu 890ddd
					img = lr->getFrameReader(fid)->load();
Toshihiro Shimizu 890ddd
					if (img && img->getPalette() == 0) {
Toshihiro Shimizu 890ddd
						if (paletteAlreadyLoaded || level->getPalette() != 0)
Toshihiro Shimizu 890ddd
							img->setPalette(level->getPalette());
Toshihiro Shimizu 890ddd
						else if ((fp.getType() == "tzp" || fp.getType() == "tzu"))
Toshihiro Shimizu 890ddd
							img->setPalette(ToonzImageUtils::loadTzPalette(fp.withType("plt").withNoFrame()));
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			img = levelPalette->getRefImg();
Toshihiro Shimizu 890ddd
	} catch (TException &e) {
Toshihiro Shimizu 890ddd
		std::wcout << L"error: " << e.getMessage() << std::endl;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		std::cout << "error for other reasons" << std::endl;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!img)
Toshihiro Shimizu 890ddd
		return 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (paletteAlreadyLoaded) {
Toshihiro Shimizu 890ddd
		img->setPalette(0);
Toshihiro Shimizu 890ddd
		levelPalette->setRefImgPath(_fp);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUndo *undo = new SetReferenceImageUndo(levelPalette, paletteHandle);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!replace) //ret==1)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPaletteP imagePalette;
Toshihiro Shimizu 890ddd
		if (TRasterImageP ri = img) {
Toshihiro Shimizu 890ddd
			TRaster32P raster = ri->getRaster();
Toshihiro Shimizu 890ddd
			if (raster) {
Toshihiro Shimizu 890ddd
				std::set<tpixel32> colors;</tpixel32>
Toshihiro Shimizu 890ddd
				int colorCount = 256;
Toshihiro Shimizu 890ddd
				if (Preferences::instance()->getPaletteTypeOnLoadRasterImageAsColorModel() == 0)
Toshihiro Shimizu 890ddd
					/*-- 全ての異なるピクセルの色を別のStyleにする --*/
Toshihiro Shimizu 890ddd
					TColorUtils::buildPrecisePalette(colors, raster, colorCount);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					/*-- 似ている色をまとめて1つのStyleにする --*/
Toshihiro Shimizu 890ddd
					TColorUtils::buildPalette(colors, raster, colorCount);
Toshihiro Shimizu 890ddd
				colors.erase(TPixel::Black); //il nero viene messo dal costruttore della TPalette
Toshihiro Shimizu 890ddd
				imagePalette = new TPalette();
Toshihiro Shimizu 890ddd
				std::set<tpixel32>::const_iterator it = colors.begin();</tpixel32>
Toshihiro Shimizu 890ddd
				for (; it != colors.end(); ++it)
Toshihiro Shimizu 890ddd
					imagePalette->getPage(0)->addStyle(*it);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			imagePalette = img->getPalette();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (imagePalette) {
Toshihiro Shimizu 890ddd
			std::wstring gName = levelPalette->getGlobalName();
Toshihiro Shimizu 890ddd
			//Se sto caricando un reference image su una studio palette
Toshihiro Shimizu 890ddd
			if (!gName.empty()) {
Toshihiro Shimizu 890ddd
				imagePalette->setGlobalName(gName);
Toshihiro Shimizu 890ddd
				StudioPalette::instance()->setStylesGlobalNames(imagePalette.getPointer());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			// voglio evitare di sostituire una palette con pochi colori ad una con tanti colori
Toshihiro Shimizu 890ddd
			/*-- ColorModelの色数が少ないのにOverwriteしようとした場合は、余分の分だけStyleが追加される --*/
Toshihiro Shimizu 890ddd
			while (imagePalette->getStyleCount() < levelPalette->getStyleCount()) {
Toshihiro Shimizu 890ddd
				int index = imagePalette->getStyleCount();
Toshihiro Shimizu 890ddd
				assert(index < levelPalette->getStyleCount());
Toshihiro Shimizu 890ddd
				TColorStyle *style = levelPalette->getStyle(index)->clone();
Toshihiro Shimizu 890ddd
				imagePalette->addStyle(style);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			levelPalette->assign(imagePalette.getPointer());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->setPalette(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	levelPalette->setRefImgPath(_fp);
Toshihiro Shimizu 890ddd
	if (!replace)
Toshihiro Shimizu 890ddd
		levelPalette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(undo);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// loadReferenceImage
Toshihiro Shimizu 890ddd
// load frames specified in frames. if frames is empty, then load all frames of the level.
Toshihiro Shimizu 890ddd
// return values -- 2: failed_to_get_palette, 1: failed_to_get_image, 0: OK
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int PaletteCmd::loadReferenceImage(TPaletteHandle *paletteHandle, bool replace,
Toshihiro Shimizu 890ddd
								   const TFilePath &_fp, int &frame, ToonzScene *scene,
Toshihiro Shimizu 890ddd
								   const std::vector<int> &frames)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP levelPalette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	if (!levelPalette)
Toshihiro Shimizu 890ddd
		return 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int ret = loadRefImage(paletteHandle, replace, levelPalette, _fp, frame, scene, frames);
Toshihiro Shimizu 890ddd
	if (ret != 0)
Toshihiro Shimizu 890ddd
		return ret;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// when choosing replace(Keep the destination palette), dirty flag is unchanged
Toshihiro Shimizu 890ddd
	if (!replace)
Toshihiro Shimizu 890ddd
		levelPalette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// removeReferenceImage
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::removeReferenceImage(TPaletteHandle *paletteHandle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP levelPalette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	if (!levelPalette)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TUndo *undo = new SetReferenceImageUndo(levelPalette, paletteHandle);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	levelPalette->setRefImg(TImageP());
Toshihiro Shimizu 890ddd
	levelPalette->setRefImgPath(TFilePath());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::vector<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
	levelPalette->setRefLevelFids(fids);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	levelPalette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(undo);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// MovePage
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class MovePageUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_srcIndex;
Toshihiro Shimizu 890ddd
	int m_dstIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MovePageUndo(TPaletteHandle *paletteHandle, int srcIndex, int dstIndex)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_srcIndex(srcIndex), m_dstIndex(dstIndex)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->movePage(m_palette->getPage(m_dstIndex), m_srcIndex);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette->movePage(m_palette->getPage(m_srcIndex), m_dstIndex);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this; }
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Move Page");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::movePalettePage(TPaletteHandle *paletteHandle, int srcIndex, int dstIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	palette->movePage(palette->getPage(srcIndex), dstIndex);
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(new MovePageUndo(paletteHandle, srcIndex, dstIndex));
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// RenamePage
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RenamePageUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	int m_pageIndex;
Toshihiro Shimizu 890ddd
	std::wstring m_newName;
Toshihiro Shimizu 890ddd
	std::wstring m_oldName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RenamePageUndo(TPaletteHandle *paletteHandle, int pageIndex, const std::wstring &newName)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_pageIndex(pageIndex), m_newName(newName)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = m_paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		m_oldName = m_palette->getPage(pageIndex)->getName();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		page->setName(m_oldName);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPalette::Page *page = m_palette->getPage(m_pageIndex);
Toshihiro Shimizu 890ddd
		assert(page);
Toshihiro Shimizu 890ddd
		page->setName(m_newName);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this; }
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Rename Page  %1 > %2")
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_oldName))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_newName));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::renamePalettePage(TPaletteHandle *paletteHandle, int pageIndex, const std::wstring &newName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!paletteHandle)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	if (!palette)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (pageIndex < 0 || pageIndex >= palette->getPageCount())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	RenamePageUndo *undo = new RenamePageUndo(paletteHandle, pageIndex, newName);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	TPalette::Page *page = palette->getPage(pageIndex);
Toshihiro Shimizu 890ddd
	assert(page);
Toshihiro Shimizu 890ddd
	page->setName(newName);
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(undo);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// RenamePaletteStyle
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RenamePaletteStyleUndo : public TUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteHandle *m_paletteHandle; //Usato nell'undo e nel redo per lanciare la notifica di cambiamento
Toshihiro Shimizu 890ddd
	int m_styleId;
Toshihiro Shimizu 890ddd
	TPaletteP m_palette;
Toshihiro Shimizu 890ddd
	std::wstring m_newName;
Toshihiro Shimizu 890ddd
	std::wstring m_oldName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RenamePaletteStyleUndo(TPaletteHandle *paletteHandle, const std::wstring &newName)
Toshihiro Shimizu 890ddd
		: m_paletteHandle(paletteHandle), m_newName(newName)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
		assert(m_palette);
Toshihiro Shimizu 890ddd
		m_styleId = paletteHandle->getStyleIndex();
Toshihiro Shimizu 890ddd
		TColorStyle *style = m_palette->getStyle(m_styleId);
Toshihiro Shimizu 890ddd
		assert(style);
Toshihiro Shimizu 890ddd
		m_oldName = style->getName();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TColorStyle *style = m_palette->getStyle(m_styleId);
Toshihiro Shimizu 890ddd
		assert(style);
Toshihiro Shimizu 890ddd
		style->setName(m_oldName);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyColorStyleChanged(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TColorStyle *style = m_palette->getStyle(m_styleId);
Toshihiro Shimizu 890ddd
		assert(style);
Toshihiro Shimizu 890ddd
		style->setName(m_newName);
Toshihiro Shimizu 890ddd
		m_paletteHandle->notifyColorStyleChanged(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getSize() const { return sizeof *this; }
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Rename Style#%1 in Palette%2  : %3 > %4")
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_styleId))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_palette->getPaletteName()))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_oldName))
Toshihiro Shimizu 890ddd
			.arg(QString::fromStdWString(m_newName));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int getHistoryType()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return HistoryType::Palette;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PaletteCmd::renamePaletteStyle(TPaletteHandle *paletteHandle, const std::wstring &newName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!paletteHandle)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TPalette *palette = paletteHandle->getPalette();
Toshihiro Shimizu 890ddd
	if (!palette)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TColorStyle *style = paletteHandle->getStyle();
Toshihiro Shimizu 890ddd
	if (!style)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (style->getName() == newName)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	RenamePaletteStyleUndo *undo = new RenamePaletteStyleUndo(paletteHandle, newName);
Toshihiro Shimizu 890ddd
	style->setName(newName);
Toshihiro Shimizu 890ddd
	palette->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	paletteHandle->notifyColorStyleChanged(false);
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(undo);
Toshihiro Shimizu 890ddd
}