Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/intpairfield.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qslider></qslider>
Toshihiro Shimizu 890ddd
#include <qhboxlayout></qhboxlayout>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
#define MARGIN_OFFSET 7
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define MARGIN_OFFSET 2
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// IntPairField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IntPairField::IntPairField(QWidget *parent, bool isMaxRangeLimited)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_values(0, 0), m_minValue(0), m_maxValue(100), m_grabOffset(0), m_grabIndex(-1), m_leftMargin(52), m_rightMargin(52), m_isMaxRangeLimited(isMaxRangeLimited)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setObjectName("IntPairField");
Toshihiro Shimizu 890ddd
	setFixedHeight(WidgetHeight);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_leftLabel = new QLabel("", this);
Shinya Kitaoka bb1d04
	m_leftLineEdit = new DVGui::IntLineEdit(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_rightLabel = new QLabel("", this);
Shinya Kitaoka bb1d04
	m_rightLineEdit = new DVGui::IntLineEdit(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_leftLineEdit->setFixedWidth(40);
Toshihiro Shimizu 890ddd
	m_rightLineEdit->setFixedWidth(40);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//----layout
Toshihiro Shimizu 890ddd
	QHBoxLayout *m_mainLayout = new QHBoxLayout;
Toshihiro Shimizu 890ddd
	m_mainLayout->setMargin(0);
Toshihiro Shimizu 890ddd
	m_mainLayout->setSpacing(5);
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_mainLayout->addWidget(m_leftLabel, 1);
Toshihiro Shimizu 890ddd
		m_mainLayout->addWidget(m_leftLineEdit, 1, Qt::AlignLeft);
Toshihiro Shimizu 890ddd
		m_mainLayout->addSpacing(35);
Toshihiro Shimizu 890ddd
		m_mainLayout->addStretch(100);
Toshihiro Shimizu 890ddd
		m_mainLayout->addWidget(m_rightLabel, 1);
Toshihiro Shimizu 890ddd
		m_mainLayout->addWidget(m_rightLineEdit, 1, Qt::AlignRight);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	setLayout(m_mainLayout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//---signal-slot connections
Toshihiro Shimizu 890ddd
	bool ret = connect(m_leftLineEdit, SIGNAL(editingFinished()), SLOT(onLeftEditingFinished()));
Toshihiro Shimizu 890ddd
	ret = ret && connect(m_rightLineEdit, SIGNAL(editingFinished()), SLOT(onRightEditingFinished()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double IntPairField::pos2value(int x) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int xMin = m_leftMargin, xMax = width() - m_rightMargin - 1;
Toshihiro Shimizu 890ddd
	return m_minValue + (m_maxValue - m_minValue) * (x - xMin) / (xMax - xMin);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int IntPairField::value2pos(double v) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int xMin = m_leftMargin, xMax = width() - m_rightMargin - 1;
Toshihiro Shimizu 890ddd
	return xMin + (int)(((xMax - xMin) * (v - m_minValue)) / (m_maxValue - m_minValue));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::paintEvent(QPaintEvent *)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
	p.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x0 = value2pos(m_minValue);
Toshihiro Shimizu 890ddd
	int x1 = value2pos(m_maxValue);
Toshihiro Shimizu 890ddd
	int y = height() / 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(getLightLineColor());
Toshihiro Shimizu 890ddd
	p.drawLine(x0 - 1, y, x1 + 1, y);
Toshihiro Shimizu 890ddd
	p.drawPoint(x1 + 1, y - 1);
Toshihiro Shimizu 890ddd
	p.drawPoint(x1 + 1, y - 2);
Toshihiro Shimizu 890ddd
	p.drawPoint(x1 + 1, y - 3);
Toshihiro Shimizu 890ddd
	p.setPen(QColor(0, 0, 0));
Toshihiro Shimizu 890ddd
	p.drawLine(x0, y - 2, x1 - 1, y - 2);
Toshihiro Shimizu 890ddd
	p.setPen(getDarkLineColor());
Toshihiro Shimizu 890ddd
	p.drawLine(x0 - 1, y - 3, x1, y - 3);
Toshihiro Shimizu 890ddd
	p.drawPoint(x0 - 1, y - 1);
Toshihiro Shimizu 890ddd
	p.drawPoint(x0 - 1, y - 2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(Qt::black);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int y1 = height() - 1;
Toshihiro Shimizu 890ddd
	int x;
Toshihiro Shimizu 890ddd
	x = value2pos(m_values.first);
Toshihiro Shimizu 890ddd
	QRect sliderRect = QRect(x0, -5, x1 - x0 + 1, 10);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (sliderRect.contains(QPoint(x, 0)))
Toshihiro Shimizu 890ddd
		p.drawPixmap(x - m_handleLeftPixmap.width() + 1, 2, m_handleLeftPixmap);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		p.drawPixmap(sliderRect.right() - m_handleLeftPixmap.width() + 1, 2, m_handleLeftGrayPixmap);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = value2pos(m_values.second);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (sliderRect.contains(QPoint(x, 0)))
Toshihiro Shimizu 890ddd
		p.drawPixmap(x, 2, m_handleRightPixmap);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		p.drawPixmap(sliderRect.right(), 2, m_handleRightGrayPixmap);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setLeftText(const QString &text)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPoint pos = m_leftLabel->pos();
Toshihiro Shimizu 890ddd
	QString oldText = m_leftLabel->text();
Toshihiro Shimizu 890ddd
	int oldLabelSize = fontMetrics().width(oldText);
Toshihiro Shimizu 890ddd
	int newLabelSize = fontMetrics().width(text);
Toshihiro Shimizu 890ddd
	int labelSize = newLabelSize - oldLabelSize;
Toshihiro Shimizu 890ddd
	m_leftMargin += labelSize + MARGIN_OFFSET;
Toshihiro Shimizu 890ddd
	m_leftLabel->setText(text);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setRightText(const QString &text)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString oldText = m_rightLabel->text();
Toshihiro Shimizu 890ddd
	int oldLabelSize = fontMetrics().width(oldText);
Toshihiro Shimizu 890ddd
	int newLabelSize = fontMetrics().width(text);
Toshihiro Shimizu 890ddd
	int labelSize = newLabelSize - oldLabelSize;
Toshihiro Shimizu 890ddd
	m_rightMargin += labelSize + MARGIN_OFFSET;
Toshihiro Shimizu 890ddd
	m_rightLabel->setText(text);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setLabelsEnabled(bool enable)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!enable) {
Toshihiro Shimizu 890ddd
		m_rightLabel->hide();
Toshihiro Shimizu 890ddd
		m_leftLabel->hide();
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_rightLabel->show();
Toshihiro Shimizu 890ddd
		m_leftLabel->show();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setValue(int value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	value = tcrop(value, m_minValue, m_maxValue);
Toshihiro Shimizu 890ddd
	value = tround(value * 100) * 0.01;
Toshihiro Shimizu 890ddd
	if (m_grabIndex == 0) //Left grab
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_values.first = value;
Toshihiro Shimizu 890ddd
		m_leftLineEdit->setValue(m_values.first);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value > m_values.second) {
Toshihiro Shimizu 890ddd
			m_values.second = value;
Toshihiro Shimizu 890ddd
			m_rightLineEdit->setValue(m_values.second);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else //Right grab
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_values.second = value;
Toshihiro Shimizu 890ddd
		m_rightLineEdit->setValue(m_values.second);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value < m_values.first) {
Toshihiro Shimizu 890ddd
			m_values.first = value;
Toshihiro Shimizu 890ddd
			m_leftLineEdit->setValue(m_values.first);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setValues(const std::pair<int, int=""> &values)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(values.first <= values.second);
Toshihiro Shimizu 890ddd
	m_values.first = tcrop(values.first, m_minValue, m_maxValue);
Toshihiro Shimizu 890ddd
	m_leftLineEdit->setValue(m_values.first);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_values.second = values.second;
Toshihiro Shimizu 890ddd
	if (m_isMaxRangeLimited)
Toshihiro Shimizu 890ddd
		m_values.second = tcrop(values.second, m_values.first, m_maxValue);
Toshihiro Shimizu 890ddd
	m_rightLineEdit->setValue(m_values.second);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::setRange(int minValue, int maxValue)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_minValue = minValue;
Toshihiro Shimizu 890ddd
	m_maxValue = maxValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_isMaxRangeLimited)
Toshihiro Shimizu 890ddd
		maxValue = (std::numeric_limits<int>::max)();</int>
Toshihiro Shimizu 890ddd
	m_leftLineEdit->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
	m_rightLineEdit->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::getRange(int &minValue, int &maxValue)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	minValue = m_minValue;
Toshihiro Shimizu 890ddd
	maxValue = m_maxValue;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::mousePressEvent(QMouseEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (event->button() == Qt::LeftButton) {
Toshihiro Shimizu 890ddd
		int x = event->pos().x();
Toshihiro Shimizu 890ddd
		int cur0, cur1;
Toshihiro Shimizu 890ddd
		if (m_values.first > m_maxValue)
Toshihiro Shimizu 890ddd
			cur0 = value2pos(m_maxValue) - 5;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			cur0 = value2pos(m_values.first);
Toshihiro Shimizu 890ddd
		if (m_values.second > m_maxValue)
Toshihiro Shimizu 890ddd
			cur1 = value2pos(m_maxValue);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			cur1 = value2pos(m_values.second);
Toshihiro Shimizu 890ddd
		int d0 = abs(cur0 - x);
Toshihiro Shimizu 890ddd
		int d1 = abs(cur1 - x);
Toshihiro Shimizu 890ddd
		int d, cur;
Toshihiro Shimizu 890ddd
		if (d0 < d1 || (d0 == d1 && x < cur0)) {
Toshihiro Shimizu 890ddd
			d = d0;
Toshihiro Shimizu 890ddd
			cur = cur0;
Toshihiro Shimizu 890ddd
			m_grabIndex = 0;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			d = d1;
Toshihiro Shimizu 890ddd
			cur = cur1;
Toshihiro Shimizu 890ddd
			m_grabIndex = 1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (d < 6)
Toshihiro Shimizu 890ddd
			m_grabOffset = cur - x;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			m_grabOffset = 0;
Toshihiro Shimizu 890ddd
			setValue(pos2value(x));
Toshihiro Shimizu 890ddd
			emit valuesChanged(true);
Toshihiro Shimizu 890ddd
			update();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::mouseMoveEvent(QMouseEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (event->buttons()) {
Toshihiro Shimizu 890ddd
		std::pair<int, int=""> oldValues = m_values;</int,>
Toshihiro Shimizu 890ddd
		int x = event->pos().x() + m_grabOffset;
Toshihiro Shimizu 890ddd
		setValue(pos2value(x));
Toshihiro Shimizu 890ddd
		if (oldValues == m_values)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		emit valuesChanged(true);
Toshihiro Shimizu 890ddd
		update();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::mouseReleaseEvent(QMouseEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_grabOffset = 0;
Toshihiro Shimizu 890ddd
	m_grabIndex = 0;
Toshihiro Shimizu 890ddd
	emit valuesChanged(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::onLeftEditingFinished()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int value = m_leftLineEdit->getValue();
Toshihiro Shimizu 890ddd
	if (value == m_values.first)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (!m_isMaxRangeLimited && value < m_minValue)
Toshihiro Shimizu 890ddd
		value = m_minValue;
Toshihiro Shimizu 890ddd
	else if (m_isMaxRangeLimited)
Toshihiro Shimizu 890ddd
		value = tcrop(value, m_minValue, m_maxValue);
Toshihiro Shimizu 890ddd
	m_values.first = value;
Toshihiro Shimizu 890ddd
	if (m_values.first > m_values.second) {
Toshihiro Shimizu 890ddd
		m_values.second = m_values.first;
Toshihiro Shimizu 890ddd
		m_rightLineEdit->setValue(m_values.second);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	emit valuesChanged(false);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void IntPairField::onRightEditingFinished()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int value = m_rightLineEdit->getValue();
Toshihiro Shimizu 890ddd
	if (value == m_values.second)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_isMaxRangeLimited)
Toshihiro Shimizu 890ddd
		value = tcrop(value, m_minValue, m_maxValue);
Toshihiro Shimizu 890ddd
	m_values.second = value;
Toshihiro Shimizu 890ddd
	if (m_values.second < m_values.first) {
Toshihiro Shimizu 890ddd
		m_values.first = m_values.second;
Toshihiro Shimizu 890ddd
		m_leftLineEdit->setValue(m_values.first);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	emit valuesChanged(false);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}