Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/spectrumfield.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/styleeditor.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qhboxlayout></qhboxlayout>
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
#include <qslider></qslider>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qlineargradient></qlineargradient>
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qimage></qimage>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
/*! \class DVGui::SpectrumBar
Toshihiro Shimizu 890ddd
		\brief The SpectrumBar class provides a bar to display a spectrum
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Inherits \b QWidget.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		The object allows to manage a spectrum, permit you to insert, move and
Toshihiro Shimizu 890ddd
		remove key.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		You can set spectrum default color in constructor, or set color of current
Toshihiro Shimizu 890ddd
		key, \b getCurrentColor(), using \b setCurrentColor(). It's possible to change
Toshihiro Shimizu 890ddd
		current key, \b getCurrentKeyIndex(), through its index, \b setCurrentKeyIndex().
Toshihiro Shimizu 890ddd
		You can also set current key position, \b getCurrentPos(), using \b setCurrentPos()
Toshihiro Shimizu 890ddd
		or add a new key in a specific position, addKeyAt().
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*!	\fn void DVGui::SpectrumBar::currentPosChanged()
Toshihiro Shimizu 890ddd
		The signal is emitted when current key position change.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*!	\fn void DVGui::SpectrumBar::currentKeyChanged()
Toshihiro Shimizu 890ddd
		The signal is emitted when current key index change.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
SpectrumBar::SpectrumBar(QWidget *parent,
Toshihiro Shimizu 890ddd
						 TPixel32 color)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_x0(10), m_currentKeyIndex(0), m_spectrum(color, color), m_chessBg(":Resources/backg.png")
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setMinimumWidth(200);
Toshihiro Shimizu 890ddd
	setFixedHeight(WidgetHeight);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SpectrumBar::~SpectrumBar()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return current key position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int SpectrumBar::getCurrentPos()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == -1)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return spectrumValueToPos(m_spectrum.getKey(m_currentKeyIndex).first);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return current key color.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
TPixel32 SpectrumBar::getCurrentColor()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == -1) //C'e' sicuramente una key perche' lo spectrum e' strutturato in modo da non avere mai size nulla.
Toshihiro Shimizu 890ddd
		return m_spectrum.getKey(getMaxPosKeyIndex()).second;
Toshihiro Shimizu 890ddd
	return m_spectrum.getKey(m_currentKeyIndex).second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set current key index to \b index. Update spectrum bar and emit a signal to
Toshihiro Shimizu 890ddd
		notify current key changed \b currentKeyChanged().
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::setCurrentKeyIndex(int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == index)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_currentKeyIndex = index;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
	emit currentKeyChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Add a new key to spectrum. Key position is computed \b x using
Toshihiro Shimizu 890ddd
		\b posToSpectrumValue() and color come from position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::addKeyAt(int x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixel32 color = m_spectrum.getValue(posToSpectrumValue(x));
Toshihiro Shimizu 890ddd
	TSpectrum::ColorKey key(posToSpectrumValue(x), color);
Toshihiro Shimizu 890ddd
	m_spectrum.addKey(key);
Toshihiro Shimizu 890ddd
	int newIndex = m_spectrum.getKeyCount() - 1;
Toshihiro Shimizu 890ddd
	setCurrentKeyIndex(newIndex);
Toshihiro Shimizu 890ddd
	emit currentKeyAdded(newIndex);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set current key position to \b pos. Update spectrum bar and emit a signal to
Toshihiro Shimizu 890ddd
		notify current key changed \b currentKeyChanged().
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::setCurrentPos(int pos, bool isDragging)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TSpectrum::ColorKey key = m_spectrum.getKey(m_currentKeyIndex);
Toshihiro Shimizu 890ddd
	key.first = posToSpectrumValue(pos);
Toshihiro Shimizu 890ddd
	m_spectrum.setKey(m_currentKeyIndex, key);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
	emit currentPosChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set current key color to \b color.  Update spectrum bar.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::setCurrentColor(const TPixel32 &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == -1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TSpectrum::ColorKey key = m_spectrum.getKey(m_currentKeyIndex);
Toshihiro Shimizu 890ddd
	TPixel32 oldColor = key.second;
Toshihiro Shimizu 890ddd
	if (oldColor == color)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	key.second = color;
Toshihiro Shimizu 890ddd
	m_spectrum.setKey(m_currentKeyIndex, key);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return spectrum value from widget position \b pos.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
double SpectrumBar::posToSpectrumValue(int pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (double)(pos - m_x0) / (double)(width() - 2 * m_x0 - 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return widget position from spectrum value \b spectrumValue.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int SpectrumBar::spectrumValueToPos(double spectrumValue)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_x0 + int(0.5 + spectrumValue * (width() - 2 * m_x0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Paint widget object:
Toshihiro Shimizu 890ddd
		\li a linear gradient computed with spectrum value;
Toshihiro Shimizu 890ddd
		\li arrows for each spectrum key.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::paintEvent(QPaintEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int h = height() - 1;
Toshihiro Shimizu 890ddd
	int y1 = height() / 2;
Toshihiro Shimizu 890ddd
	int x1 = width() - m_x0;
Toshihiro Shimizu 890ddd
	QRectF rectBar = QRectF(m_x0, 0, x1 - m_x0 + 1, y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Build the linear gradient
Toshihiro Shimizu 890ddd
	QLinearGradient linearGrad(QPointF(m_x0, h), QPointF(x1, h));
Toshihiro Shimizu 890ddd
	int spectrumSize = m_spectrum.getKeyCount();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < spectrumSize; i++) {
Toshihiro Shimizu 890ddd
		TSpectrum::ColorKey key = m_spectrum.getKey(i);
Toshihiro Shimizu 890ddd
		//Linear Gradient
Toshihiro Shimizu 890ddd
		TPixel32 pix = key.second;
Toshihiro Shimizu 890ddd
		double spectrumValue = key.first;
Toshihiro Shimizu 890ddd
		linearGrad.setColorAt(spectrumValue, QColor(pix.r, pix.g, pix.b, pix.m));
Toshihiro Shimizu 890ddd
		//Frecce delle key
Toshihiro Shimizu 890ddd
		int pos = spectrumValueToPos(spectrumValue);
Toshihiro Shimizu 890ddd
		int f = 4;
Toshihiro Shimizu 890ddd
		drawArrow(p, QPointF(pos - f, y1 + f), QPointF(pos, y1), QPointF(pos + f, y1 + f),
Toshihiro Shimizu 890ddd
				  true, (m_currentKeyIndex == i) ? Qt::black : Qt::white);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Draw chess bg
Toshihiro Shimizu 890ddd
	QBrush bg;
Toshihiro Shimizu 890ddd
	bg.setTexture(m_chessBg);
Toshihiro Shimizu 890ddd
	p.setBrush(bg);
Toshihiro Shimizu 890ddd
	p.drawRect(rectBar);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Draw the gradient
Toshihiro Shimizu 890ddd
	p.setBrush(linearGrad);
Toshihiro Shimizu 890ddd
	p.drawRect(rectBar);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Manage mouse press event. Search exsisting key near click position, if
Toshihiro Shimizu 890ddd
		there is set it as current key; otherwise add a new key in click position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::mousePressEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPoint pos = e->pos();
Toshihiro Shimizu 890ddd
	int x = pos.x();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Verifico se esiste una key vicino alla posizione in cui ho cliccato
Toshihiro Shimizu 890ddd
	int index = -1;
Toshihiro Shimizu 890ddd
	int spectrumSize = m_spectrum.getKeyCount();
Toshihiro Shimizu 890ddd
	if (x < m_x0)
Toshihiro Shimizu 890ddd
		index = getMinPosKeyIndex();
Toshihiro Shimizu 890ddd
	else if (x > width() - m_x0)
Toshihiro Shimizu 890ddd
		index = getMaxPosKeyIndex();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		index = getNearPosKeyIndex(x);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Se x e' vicino a una key esistente setto questa come corrente
Toshihiro Shimizu 890ddd
	if (index != -1)
Toshihiro Shimizu 890ddd
		setCurrentKeyIndex(index);
Toshihiro Shimizu 890ddd
	else //Altrimenti aggiungo una nuova key
Toshihiro Shimizu 890ddd
		addKeyAt(x);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Manage mouse move event. If y position value greater than widget height
Toshihiro Shimizu 890ddd
		erase current key; otherwise move current key position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumBar::mouseMoveEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPoint pos = e->pos();
Toshihiro Shimizu 890ddd
	int x = pos.x();
Toshihiro Shimizu 890ddd
	int y = pos.y();
Toshihiro Shimizu 890ddd
	if (x < m_x0 || x > width() - m_x0 - 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Se il valore della y e' maggiore dell'altezza widget, cioe' va fuori dal widget
Toshihiro Shimizu 890ddd
	// elimino la key corrente
Toshihiro Shimizu 890ddd
	if (y > height()) {
Toshihiro Shimizu 890ddd
		if (m_currentKeyIndex == -1 || m_spectrum.getKeyCount() == 1)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_spectrum.removeKey(m_currentKeyIndex);
Toshihiro Shimizu 890ddd
		int keyIndex = m_currentKeyIndex;
Toshihiro Shimizu 890ddd
		setCurrentKeyIndex(-1);
Toshihiro Shimizu 890ddd
		emit currentKeyRemoved(keyIndex);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Se il valore della y e' interno al widget
Toshihiro Shimizu 890ddd
	// Se l'indice della key corrente e' -1 avevo eliminato un key; ora devo riaggiungerla.
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == -1)
Toshihiro Shimizu 890ddd
		addKeyAt(x);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Setto la nuova posizione x alla chiave corrente
Toshihiro Shimizu 890ddd
	setCurrentPos(x, true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SpectrumBar::mouseReleaseEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_currentKeyIndex == -1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	QPoint pos = e->pos();
Toshihiro Shimizu 890ddd
	int x = pos.x();
Toshihiro Shimizu 890ddd
	int y = pos.y();
Toshihiro Shimizu 890ddd
	if (x < m_x0 || x > width() - m_x0 - 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (y > height())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setCurrentPos(x, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return index of key with maximum position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int SpectrumBar::getMaxPosKeyIndex()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int spectrumSize = m_spectrum.getKeyCount();
Toshihiro Shimizu 890ddd
	if (!spectrumSize)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	int maxPosIndex = 0;
Toshihiro Shimizu 890ddd
	int maxPos = spectrumValueToPos(m_spectrum.getKey(0).first);
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < spectrumSize; i++) {
Toshihiro Shimizu 890ddd
		int newPos = spectrumValueToPos(m_spectrum.getKey(i).first);
Toshihiro Shimizu 890ddd
		if (maxPos < newPos) {
Toshihiro Shimizu 890ddd
			maxPos = newPos;
Toshihiro Shimizu 890ddd
			maxPosIndex = i;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return maxPosIndex;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return index of key with minimum position.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int SpectrumBar::getMinPosKeyIndex()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int spectrumSize = m_spectrum.getKeyCount();
Toshihiro Shimizu 890ddd
	if (!spectrumSize)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	int minPosIndex = 0;
Toshihiro Shimizu 890ddd
	int minPos = spectrumValueToPos(m_spectrum.getKey(0).first);
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < spectrumSize; i++) {
Toshihiro Shimizu 890ddd
		int newPos = spectrumValueToPos(m_spectrum.getKey(i).first);
Toshihiro Shimizu 890ddd
		if (minPos > newPos) {
Toshihiro Shimizu 890ddd
			minPos = newPos;
Toshihiro Shimizu 890ddd
			minPosIndex = i;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return minPosIndex;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return index of key "near" \b pos. "near" mean with a dinstace less than
Toshihiro Shimizu 890ddd
		a constant size. If there aren't key near pos return \b -1.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int SpectrumBar::getNearPosKeyIndex(int pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	int gap = 20;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_spectrum.getKeyCount(); i++)
Toshihiro Shimizu 890ddd
		if (areAlmostEqual(double(pos),
Toshihiro Shimizu 890ddd
						   double(spectrumValueToPos(m_spectrum.getKey(i).first)),
Toshihiro Shimizu 890ddd
						   gap))
Toshihiro Shimizu 890ddd
			return i;
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
/*! \class DVGui::SpectrumField
Toshihiro Shimizu 890ddd
		\brief The SpectrumField class provides an object to display and manage a spectrum.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Inherits \b QWidget.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		The object is composed of a vertical layout \b QVBoxLayout which contains a
Toshihiro Shimizu 890ddd
		spectrum bar \b SpectrumBar and a color field \b ColorField. Using color field
Toshihiro Shimizu 890ddd
		it's possible to set spectrum current key color.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		\b Example: Spectrum Field.
Toshihiro Shimizu 890ddd
		\image html SpectrumField.jpg
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
SpectrumField::SpectrumField(QWidget *parent,
Toshihiro Shimizu 890ddd
							 TPixel32 color)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_margin(0), m_spacing(4)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setFixedHeight(60);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QVBoxLayout *layout = new QVBoxLayout();
Toshihiro Shimizu 890ddd
	layout->setMargin(m_margin);
Toshihiro Shimizu 890ddd
	layout->setSpacing(m_spacing);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_spectrumbar = new SpectrumBar(this, color);
Toshihiro Shimizu 890ddd
	connect(m_spectrumbar, SIGNAL(currentPosChanged(bool)), SLOT(onCurrentPosChanged(bool)));
Toshihiro Shimizu 890ddd
	connect(m_spectrumbar, SIGNAL(currentKeyChanged()), SLOT(onCurrentKeyChanged()));
Toshihiro Shimizu 890ddd
	connect(m_spectrumbar, SIGNAL(currentKeyAdded(int)), SIGNAL(keyAdded(int)));
Toshihiro Shimizu 890ddd
	connect(m_spectrumbar, SIGNAL(currentKeyRemoved(int)), SIGNAL(keyRemoved(int)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	layout->addWidget(m_spectrumbar);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_colorField = new ColorField(this, true, color, 36);
Toshihiro Shimizu 890ddd
	connect(m_colorField, SIGNAL(colorChanged(const TPixel32 &, bool)),
Toshihiro Shimizu 890ddd
			this, SLOT(onColorChanged(const TPixel32 &, bool)));
Toshihiro Shimizu 890ddd
	layout->addWidget(m_colorField, 0, Qt::AlignLeft);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setLayout(layout);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Update all widget and emit keyPositionChanged() signal.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumField::onCurrentPosChanged(bool isDragging)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_spectrumbar->getCurrentKeyIndex() == -1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
	emit keyPositionChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set color field to spectrum current key color and update.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumField::onCurrentKeyChanged()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_spectrumbar->getCurrentKeyIndex() != -1)
Toshihiro Shimizu 890ddd
		m_colorField->setColor(m_spectrumbar->getCurrentColor());
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set spectrum current key color to \b color and emit keyColorChanged().
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumField::onColorChanged(const TPixel32 &color, bool isDragging)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_spectrumbar->setCurrentColor(color);
Toshihiro Shimizu 890ddd
	emit keyColorChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Paint an arrow to connect current spectrum key and square color field.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void SpectrumField::paintEvent(QPaintEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int curPos = m_spectrumbar->getCurrentPos();
Toshihiro Shimizu 890ddd
	if (curPos == -1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
	int x0 = 18 + m_margin;
Toshihiro Shimizu 890ddd
	int y0 = 2 * m_margin + WidgetHeight + m_spacing;
Toshihiro Shimizu 890ddd
	int y = m_margin + m_spacing + (WidgetHeight * 0.5 - 4); //4 e' l'altezza della freccia nella spectrum bar
Toshihiro Shimizu 890ddd
	int y1 = y0 - y * 0.5 + 1;
Toshihiro Shimizu 890ddd
	int y2 = y0 - y;
Toshihiro Shimizu 890ddd
	curPos += m_margin;
Toshihiro Shimizu 890ddd
	p.setPen(Qt::black);
Toshihiro Shimizu 890ddd
	p.drawLine(x0, y0, x0, y1);
Toshihiro Shimizu 890ddd
	p.drawLine(x0, y1, curPos, y1);
Toshihiro Shimizu 890ddd
	p.drawLine(curPos, y1, curPos, y2);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SpectrumField::~SpectrumField()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}