Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "palettesscanpopup.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
/*! \class PalettesScanPopup
Toshihiro Shimizu 890ddd
		\brief The PalettesScanPopup class provides a modal dialog to import
Toshihiro Shimizu 890ddd
					 palette in StudioPalette folder.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Inherits \b Dialog.
Toshihiro Shimizu 890ddd
		This object is composed of three sub-object: a \b FileField to choose a
Toshihiro Shimizu 890ddd
		folder, a label \b QLabel, generally empty, to show palette folder during
Toshihiro Shimizu 890ddd
		them import, and a button bar to start import or cancel command.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		The class provides a method, \b setCurrentFolder(TFilePath path), to set
Toshihiro Shimizu 890ddd
		folder wherein import palettes.
Toshihiro Shimizu 890ddd
		Protected function allows to manage palette import.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
PalettesScanPopup::PalettesScanPopup()
Toshihiro Shimizu 890ddd
	: Dialog(0, true, true, "PalettesScan"), m_folderPath(TFilePath()), m_timerId(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setWindowTitle(tr("Search for Palettes"));
Toshihiro Shimizu 890ddd
	setFixedWidth(250);
Toshihiro Shimizu 890ddd
Shinya Kitaoka bb1d04
	m_field = new DVGui::FileField();
Toshihiro Shimizu 890ddd
	addWidget(m_field);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_label = new QLabel();
Toshihiro Shimizu 890ddd
	m_label->setFixedWidth(200);
Toshihiro Shimizu 890ddd
	addWidget(m_label);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPushButton *okBtn = new QPushButton(tr("Ok"), this);
Toshihiro Shimizu 890ddd
	okBtn->setDefault(true);
Toshihiro Shimizu 890ddd
	QPushButton *cancelBtn = new QPushButton(tr("Cancel"), this);
Toshihiro Shimizu 890ddd
	connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkBtnClicked()));
Toshihiro Shimizu 890ddd
	connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
Toshihiro Shimizu 890ddd
	addButtonBarWidget(okBtn, cancelBtn);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set current folder path to \b path.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::setCurrentFolder(TFilePath path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_folderPath = path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return current folder path.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
TFilePath PalettesScanPopup::getCurrentFolder()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_folderPath;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Start palette import. Add \b FileField path to directories stack and start
Toshihiro Shimizu 890ddd
		a timer.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::onOkBtnClicked()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_timerId = startTimer(3);
Toshihiro Shimizu 890ddd
	TFilePathSet fps;
Shinya Kitaoka 3bfa54
	std::wstring s = m_field->getPath().toStdWString();
Toshihiro Shimizu 890ddd
	int i = 0, len = s.length();
Toshihiro Shimizu 890ddd
	while (i < len) {
Toshihiro Shimizu 890ddd
		while (i < len && (s[i] == ' ' || s[i] == '\t'))
Toshihiro Shimizu 890ddd
			i++;
Toshihiro Shimizu 890ddd
		if (i >= len)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		int j = i;
Toshihiro Shimizu 890ddd
		while (j < len && s[j] != ',')
Toshihiro Shimizu 890ddd
			j++;
Toshihiro Shimizu 890ddd
		int k = j;
Toshihiro Shimizu 890ddd
		while (k > i && (s[k - 1] == ' ' || s[k - 1] == '\t'))
Toshihiro Shimizu 890ddd
			k--;
Shinya Kitaoka 3bfa54
		std::wstring token = s.substr(i, k - i);
Toshihiro Shimizu 890ddd
		fps.push_back(TFilePath(token));
Toshihiro Shimizu 890ddd
		i = j + 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	push(fps);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set label text to path \b fp.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::setLabel(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString elideStr = elideText(toQString(fp), m_label->font(), m_label->width());
Toshihiro Shimizu 890ddd
	m_label->setText(elideStr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Proceed in palette import recalling \b step(); if step() return false
Toshihiro Shimizu 890ddd
		stop timer, clear directories stack, and close popup sending accept event.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::timerEvent(QTimerEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool ret = step();
Toshihiro Shimizu 890ddd
	if (!ret) {
Toshihiro Shimizu 890ddd
		killTimer(m_timerId);
Toshihiro Shimizu 890ddd
		clearStack();
Toshihiro Shimizu 890ddd
		QDialog::accept();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Push TFilePath \b fp to the top of directories stack. Set label text to fp.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::push(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setLabel(fp);
Toshihiro Shimizu 890ddd
	Directory *dir = new Directory();
Toshihiro Shimizu 890ddd
	m_stack.push_back(dir);
Toshihiro Shimizu 890ddd
	dir->m_path = fp;
Toshihiro Shimizu 890ddd
	dir->m_files = TSystem::readDirectory(fp);
Toshihiro Shimizu 890ddd
	dir->m_it = dir->m_files.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Push TFilePathSet \b fs to the top of directories stack. Set label text to
Toshihiro Shimizu 890ddd
		"<files>".</files>
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::push(const TFilePathSet &fs)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_label->setText(tr("<files>"));</files>
Toshihiro Shimizu 890ddd
	Directory *dir = new Directory();
Toshihiro Shimizu 890ddd
	m_stack.push_back(dir);
Toshihiro Shimizu 890ddd
	dir->m_path = TFilePath();
Toshihiro Shimizu 890ddd
	dir->m_files = fs;
Toshihiro Shimizu 890ddd
	dir->m_it = dir->m_files.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Removes the top item from the stack. If stack is empty return immediately.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::pop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_stack.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	Directory *dir = m_stack.back();
Toshihiro Shimizu 890ddd
	delete dir;
Toshihiro Shimizu 890ddd
	m_stack.pop_back();
Toshihiro Shimizu 890ddd
	if (!m_stack.empty()) {
Toshihiro Shimizu 890ddd
		dir = m_stack.back();
Toshihiro Shimizu 890ddd
		setLabel(dir->m_path);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		m_label->setText(tr(""));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return true if direcories stack is not empty; otherwise return false.
Toshihiro Shimizu 890ddd
\n	If stack is not empty check current directory:
Toshihiro Shimizu 890ddd
\li	if all files are checked recall \b pop();
Toshihiro Shimizu 890ddd
\li	if its path is a directory recall \b push(const TFilePath &fp);
Toshihiro Shimizu 890ddd
\li	if its path is a file check its extension and recall \b onPlt(const TFilePath &fp).
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
bool PalettesScanPopup::step()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_stack.empty())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	Directory *dir = m_stack.back();
Toshihiro Shimizu 890ddd
	if (dir->m_it == dir->m_files.end())
Toshihiro Shimizu 890ddd
		pop();
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		TFilePath fp = *(dir->m_it)++;
Toshihiro Shimizu 890ddd
		if (TFileStatus(fp).isDirectory())
Toshihiro Shimizu 890ddd
			push(fp);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			setLabel(fp);
Shinya Kitaoka 3bfa54
			std::string ext = fp.getType();
Toshihiro Shimizu 890ddd
			if (ext == "plt" || ext == "tpl" || ext == "pli")
Toshihiro Shimizu 890ddd
				onPlt(fp);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Resets the content of the directories stack and set label text empty.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::clearStack()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)m_stack.size(); i++)
Toshihiro Shimizu 890ddd
		delete m_stack[i];
Toshihiro Shimizu 890ddd
	m_stack.clear();
Toshihiro Shimizu 890ddd
	m_label->setText(tr(""));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Import palette, defined in path \b fp, in current \b StudioPalette folder.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void PalettesScanPopup::onPlt(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath root(m_field->getPath().toStdString());
Toshihiro Shimizu 890ddd
	assert(root.isAncestorOf(fp));
Toshihiro Shimizu 890ddd
	TFilePath q = fp.getParentDir() - root;
Toshihiro Shimizu 890ddd
	StudioPalette::instance()->importPalette(m_folderPath + q, fp);
Toshihiro Shimizu 890ddd
}