Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qpaintevent></qpaintevent>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/marksbar.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace stuff
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void crop(QVector<int> &values, int min, int max)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QVector<int>::iterator it, end = values.end();</int>
Toshihiro Shimizu 890ddd
	for (it = values.begin(); it != end; ++it)
Toshihiro Shimizu 890ddd
		*it = tcrop(*it, min, max);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void rollDown(QVector<int> &values, int max, int sortDist)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(!values.empty());
Toshihiro Shimizu 890ddd
	values.back() = tmin(max, values.back());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QVector<int>::iterator it, jt, beg = values.begin();</int>
Toshihiro Shimizu 890ddd
	for (it = values.end(), jt = --it; jt != beg; jt = it) {
Toshihiro Shimizu 890ddd
		--it;
Toshihiro Shimizu 890ddd
		val = *jt - sortDist;
Toshihiro Shimizu 890ddd
		if (*it > val)
Toshihiro Shimizu 890ddd
			*it = val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void rollUp(QVector<int> &values, int min, int sortDist)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(!values.empty());
Toshihiro Shimizu 890ddd
	values.front() = tmax(min, values.front());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QVector<int>::iterator it, jt, end = values.end();</int>
Toshihiro Shimizu 890ddd
	for (jt = values.begin(), it = jt++; jt != end; it = jt, ++jt) {
Toshihiro Shimizu 890ddd
		val = *it + sortDist;
Toshihiro Shimizu 890ddd
		if (val > *jt)
Toshihiro Shimizu 890ddd
			*jt = val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
//    MarksBar implementation
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
MarksBar::MarksBar(QWidget *parent, bool markUp)
Toshihiro Shimizu 890ddd
	: QFrame(parent), m_markUp(markUp), m_min(0), m_max(100), m_sortDist(-1), m_selectedMark(-1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setMinimumWidth(100);
Toshihiro Shimizu 890ddd
	setFixedHeight(6);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::setRange(int min, int max, int sortDist)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(min <= max);
Toshihiro Shimizu 890ddd
	m_min = min, m_max = max, m_sortDist = sortDist;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	conformValues();
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int MarksBar::valToPos(int val)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const QRect contsRect = contentsRect();
Toshihiro Shimizu 890ddd
	return contsRect.left() + contsRect.width() * ((val - m_min) / (double)(m_max - m_min));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int MarksBar::posToVal(int pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const QRect contsRect = contentsRect();
Toshihiro Shimizu 890ddd
	return m_min + (m_max - m_min) * ((pos - contsRect.left()) / (double)contsRect.width());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::conformValues(bool preferRollDown)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_values.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_sortDist < 0)
Toshihiro Shimizu 890ddd
		::crop(m_values, m_min, m_max);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		if (preferRollDown) {
Toshihiro Shimizu 890ddd
			::rollDown(m_values, m_max, m_sortDist);
Toshihiro Shimizu 890ddd
			::rollUp(m_values, m_min, m_sortDist);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			::rollUp(m_values, m_min, m_sortDist);
Toshihiro Shimizu 890ddd
			::rollDown(m_values, m_max, m_sortDist);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
	emit marksUpdated();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::mousePressEvent(QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int newDist, currDist = (std::numeric_limits<int>::max)();</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int val = posToVal(me->x());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Select the nearest mark. If the distance is too great, select none.
Toshihiro Shimizu 890ddd
	int i, size = m_values.size();
Toshihiro Shimizu 890ddd
	for (i = 0; i < size; ++i) {
Toshihiro Shimizu 890ddd
		newDist = abs(m_values[i] - val);
Toshihiro Shimizu 890ddd
		if (newDist < 7 && newDist < currDist)
Toshihiro Shimizu 890ddd
			m_selectedMark = i;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::mouseMoveEvent(QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_selectedMark < 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int newVal = tcrop(posToVal(me->x()), m_min, m_max);
Toshihiro Shimizu 890ddd
	bool left = newVal < m_values[m_selectedMark];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_values[m_selectedMark] = newVal;
Toshihiro Shimizu 890ddd
	conformValues(left);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::mouseReleaseEvent(QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_selectedMark = -1;
Toshihiro Shimizu 890ddd
	emit marksReleased();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::drawMark(QPainter &p, int pos, const QColor &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPolygon poly(3);
Toshihiro Shimizu 890ddd
	if (m_markUp) {
Toshihiro Shimizu 890ddd
		poly[0] = QPoint(pos - 5, 5);
Toshihiro Shimizu 890ddd
		poly[1] = QPoint(pos + 5, 5);
Toshihiro Shimizu 890ddd
		poly[2] = QPoint(pos, 0);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		poly[0] = QPoint(pos - 5, 0);
Toshihiro Shimizu 890ddd
		poly[1] = QPoint(pos + 5, 0);
Toshihiro Shimizu 890ddd
		poly[2] = QPoint(pos, 5);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setBrush(color);
Toshihiro Shimizu 890ddd
	p.drawPolygon(poly);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void MarksBar::paintEvent(QPaintEvent *pe)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(m_values.size() == m_colors.size());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Draw markers
Toshihiro Shimizu 890ddd
	int i, size = m_values.size();
Toshihiro Shimizu 890ddd
	for (i = 0; i < size; ++i)
Toshihiro Shimizu 890ddd
		drawMark(p, valToPos(m_values[i]), m_colors[i]);
Toshihiro Shimizu 890ddd
}