Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/functionpanel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include "toonzqt/functionselection.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/functionsegmentviewer.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/imageutils.h"
Toshihiro Shimizu 890ddd
#include "functionpaneltools.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/tframehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/doubleparamcmd.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "tdoubleparam.h"
Toshihiro Shimizu 890ddd
#include "tdoublekeyframe.h"
Toshihiro Shimizu 890ddd
#include "tunit.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qwheelevent></qwheelevent>
Toshihiro Shimizu 890ddd
#include <qmenu></qmenu>
Toshihiro Shimizu 890ddd
#include <qsettings></qsettings>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 61dd76
#include <cmath></cmath>
Shinya Kitaoka 61dd76
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void drawCircle(QPainter &painter, double x, double y, double r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	painter.drawEllipse(x - r, y - r, 2 * r, 2 * r);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void drawCircle(QPainter &painter, const QPointF &p, double r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	painter.drawEllipse(p.x() - r, p.y() - r, 2 * r, 2 * r);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void drawSquare(QPainter &painter, double x, double y, double r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	painter.drawRect(x - r, y - r, 2 * r, 2 * r);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
void drawSquare(QPainter &painter, const QPointF &p, double r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	drawSquare(painter, p.x(), p.y(), r);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void drawRoundedSquare(QPainter &painter, const QPointF &p, double r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	painter.drawRoundRect(p.x() - r, p.y() - r, 2 * r, 2 * r, 99, 99);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double norm2(const QPointF &p) { return p.x() * p.x() + p.y() * p.y(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FunctionPanelZoomer : public ImageUtils::ShortcutZoomer
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionPanel *m_panel;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FunctionPanelZoomer(FunctionPanel *panel) : ShortcutZoomer(panel), m_panel(panel) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool zoom(bool zoomin, bool resetZoom)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (resetZoom)
Toshihiro Shimizu 890ddd
			m_panel->fitGraphToWindow();
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			double f = 1.25;
Toshihiro Shimizu 890ddd
			double sc = zoomin ? f : 1.0 / f;
Toshihiro Shimizu 890ddd
			QPoint center(m_panel->width() / 2, m_panel->height() / 2);
Toshihiro Shimizu 890ddd
			m_panel->zoom(sc, sc, center);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return true;
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
// Ruler
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class Ruler
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double m_minValue, m_step;
Toshihiro Shimizu 890ddd
	int m_labelPeriod, m_labelOffset, m_tickCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double m_unit, m_pan, m_vOrigin;
Toshihiro Shimizu 890ddd
	int m_x0, m_x1;
Toshihiro Shimizu 890ddd
	int m_minLabelDistance, m_minDistance;
Toshihiro Shimizu 890ddd
	double m_minStep;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Ruler();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// unit,pan define the world to viewport transformation: pixel = value * unit + pan
Toshihiro Shimizu 890ddd
	// note: unit can be <0 (e.g. in the vertical rulers)
Toshihiro Shimizu 890ddd
	// use vOrigin!=0 when there is a offset between values and labels
Toshihiro Shimizu 890ddd
	// (e.g. frame=0 is visualized as 1)
Toshihiro Shimizu 890ddd
	void setTransform(double unit, double pan, double vOrigin = 0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_unit = unit;
Toshihiro Shimizu 890ddd
		m_pan = pan;
Toshihiro Shimizu 890ddd
		m_vOrigin = vOrigin;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setRange(int x0, int x1)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_x0 = x0;
Toshihiro Shimizu 890ddd
		m_x1 = x1;
Toshihiro Shimizu 890ddd
	} // [x0,x1] is the pixel range
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// set minimum distance (pixel) between two consecutive labels
Toshihiro Shimizu 890ddd
	void setMinLabelDistance(int distance) { m_minLabelDistance = distance; }
Toshihiro Shimizu 890ddd
	// set minimum distance (pixel) between two consecutive ticks
Toshihiro Shimizu 890ddd
	void setMinDistance(int distance) { m_minDistance = distance; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// use setMinStep to define a minimum tick (e.g. for integer rulers as 'frame' call setMinStep(1);)
Toshihiro Shimizu 890ddd
	void setMinStep(double step) { m_minStep = step; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void compute(); // call compute() once, before calling the following methods
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getTickCount() const { return m_tickCount; }
Toshihiro Shimizu 890ddd
	double getTick(int index) const { return m_minValue + index * m_step; }
Toshihiro Shimizu 890ddd
	bool isLabel(int index) const { return ((m_labelOffset + index) % m_labelPeriod) == 0; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Ruler::Ruler()
Toshihiro Shimizu 890ddd
	: m_minValue(0), m_step(1), m_labelPeriod(2), m_labelOffset(0), m_tickCount(0), m_unit(1), m_pan(0), m_x0(0), m_x1(100), m_minLabelDistance(20), m_minDistance(5), m_minStep(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Ruler::compute()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_x0 < m_x1);
Toshihiro Shimizu 890ddd
	assert(m_unit != 0.0);
Toshihiro Shimizu 890ddd
	assert(m_minLabelDistance > 0);
Toshihiro Shimizu 890ddd
	assert(m_minDistance >= 0);
Toshihiro Shimizu 890ddd
	// compute m_step (world distance between two adjacent ticks)
Toshihiro Shimizu 890ddd
	// and m_labelPeriod (number of ticks between two adjacent labels)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// the distance (world units) between two labels must be >= minLabelWorldDistance
MichaƂ Janiszewski 0585ee
	const double absUnit = std::abs(m_unit);
Toshihiro Shimizu 890ddd
	const double minLabelWorldDistance = m_minLabelDistance / absUnit;
Toshihiro Shimizu 890ddd
	const double minWorldDistance = m_minDistance / absUnit;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// we want the minimum step with:
Toshihiro Shimizu 890ddd
	//    step*labelPeriod (i.e. label distance) >=  minLabelWorldDistance
Toshihiro Shimizu 890ddd
	//    step (i.e. tick distance) >= minWorldDistance
Toshihiro Shimizu 890ddd
	// Note: labelPeriod alternates between 5 and 2 => labelPeriod' = 7-labelPeriod
Toshihiro Shimizu 890ddd
	m_step = 1;
Toshihiro Shimizu 890ddd
	m_labelPeriod = 5;
Toshihiro Shimizu 890ddd
	if (m_step * m_labelPeriod >= minLabelWorldDistance && m_step >= minWorldDistance) {
Toshihiro Shimizu 890ddd
		while (m_step >= minLabelWorldDistance && m_step / (7 - m_labelPeriod) >= minWorldDistance) {
Toshihiro Shimizu 890ddd
			m_labelPeriod = 7 - m_labelPeriod;
Toshihiro Shimizu 890ddd
			m_step /= m_labelPeriod;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			m_step *= m_labelPeriod;
Toshihiro Shimizu 890ddd
			m_labelPeriod = 7 - m_labelPeriod; // m_labelPeriod alternates between 5 and 2
Toshihiro Shimizu 890ddd
		} while (m_step * m_labelPeriod < minLabelWorldDistance || m_step < minWorldDistance);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_step >= minLabelWorldDistance) {
Toshihiro Shimizu 890ddd
		m_labelPeriod = 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_step * m_labelPeriod < m_minStep) {
Toshihiro Shimizu 890ddd
		m_step = m_minStep;
Toshihiro Shimizu 890ddd
		m_labelPeriod = 1;
Toshihiro Shimizu 890ddd
	} else if (m_step < m_minStep) {
Toshihiro Shimizu 890ddd
		m_step *= m_labelPeriod;
Toshihiro Shimizu 890ddd
		m_labelPeriod = 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// compute range
Toshihiro Shimizu 890ddd
	double v0 = (m_x0 - m_pan) / m_unit; // left margin (world units)
Toshihiro Shimizu 890ddd
	double v1 = (m_x1 - m_pan) / m_unit; // right margin (world units)
Toshihiro Shimizu 890ddd
	if (m_unit < 0)
Toshihiro Shimizu 890ddd
		tswap(v0, v1);
Toshihiro Shimizu 890ddd
	int i0 = tfloor((v0 - m_vOrigin) / m_step); // largest tick <=v0 is i0 * m_step
Toshihiro Shimizu 890ddd
	int i1 = tceil((v1 - m_vOrigin) / m_step);  // smallest tick >=v1 is i1 * m_step
Toshihiro Shimizu 890ddd
	m_minValue = i0 * m_step + m_vOrigin;
Toshihiro Shimizu 890ddd
	m_tickCount = i1 - i0 + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_labelOffset = i0 >= 0 ? (i0 % m_labelPeriod) : (m_labelPeriod - ((-i0) % m_labelPeriod));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// FunctionPanel::Gadget
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FunctionPanel::Gadget::Gadget(
Toshihiro Shimizu 890ddd
	FunctionPanel::Handle handle,
Toshihiro Shimizu 890ddd
	int kIndex,
Toshihiro Shimizu 890ddd
	const QPointF &p,
Toshihiro Shimizu 890ddd
	int rx, int ry,
Toshihiro Shimizu 890ddd
	const QPointF &pointPos)
Toshihiro Shimizu 890ddd
	: m_handle(handle), m_kIndex(kIndex), m_hitRegion(QRect((int)p.x() - rx, (int)p.y() - ry, 2 * rx, 2 * ry)), m_pos(p), m_pointPos(pointPos), m_channel(0), m_keyframePosition(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// FunctionPanel
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FunctionPanel::FunctionPanel(QWidget *parent)
Toshihiro Shimizu 890ddd
	: QDialog(parent), m_functionTreeModel(0), m_viewTransform(), m_valueAxisX(50), m_frameAxisY(50), m_graphViewportY(50), m_frameHandle(0), m_dragTool(0), m_currentFrameStatus(0), m_selection(0), m_curveShape(SMOOTH)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setWindowTitle(tr("Function Curves"));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_viewTransform.translate(50, 200);
Toshihiro Shimizu 890ddd
	m_viewTransform.scale(5, -1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setFocusPolicy(Qt::ClickFocus);
Toshihiro Shimizu 890ddd
	setMouseTracking(true);
Toshihiro Shimizu 890ddd
	m_highlighted.handle = None;
Toshihiro Shimizu 890ddd
	m_highlighted.gIndex = -1;
Toshihiro Shimizu 890ddd
	m_cursor.visible = false;
Toshihiro Shimizu 890ddd
	m_cursor.frame = m_cursor.value = 0;
Toshihiro Shimizu 890ddd
	m_curveLabel.text = "";
Toshihiro Shimizu 890ddd
	m_curveLabel.curve = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//load the dialog size
Toshihiro Shimizu 890ddd
	TFilePath fp(ToonzFolder::getMyModuleDir() + TFilePath(mySettingsFileName));
Toshihiro Shimizu 890ddd
	QSettings mySettings(toQString(fp), QSettings::IniFormat);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	mySettings.beginGroup("Dialogs");
Toshihiro Shimizu 890ddd
	setGeometry(mySettings.value("FunctionCurves", QRect(500, 500, 400, 300)).toRect());
Toshihiro Shimizu 890ddd
	mySettings.endGroup();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FunctionPanel::~FunctionPanel()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//save the dialog size
Toshihiro Shimizu 890ddd
	TFilePath fp(ToonzFolder::getMyModuleDir() + TFilePath(mySettingsFileName));
Toshihiro Shimizu 890ddd
	QSettings mySettings(toQString(fp), QSettings::IniFormat);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	mySettings.beginGroup("Dialogs");
Toshihiro Shimizu 890ddd
	mySettings.setValue("FunctionCurves", geometry());
Toshihiro Shimizu 890ddd
	mySettings.endGroup();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete m_dragTool;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double FunctionPanel::getPixelRatio(TDoubleParam *curve) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double framePixelSize = xToFrame(1) - xToFrame(0);
Toshihiro Shimizu 890ddd
	assert(framePixelSize > 0);
Toshihiro Shimizu 890ddd
	double valuePixelSize = fabs(yToValue(curve, 1) - yToValue(curve, 0));
Toshihiro Shimizu 890ddd
	assert(valuePixelSize > 0);
Toshihiro Shimizu 890ddd
	return framePixelSize / valuePixelSize;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double FunctionPanel::frameToX(double f) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_viewTransform.m11() * f + m_viewTransform.dx();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double FunctionPanel::xToFrame(double x) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (x - m_viewTransform.dx()) / m_viewTransform.m11();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double FunctionPanel::valueToY(TDoubleParam *curve, double v) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const double bigNumber = 1.0e9;
Toshihiro Shimizu 890ddd
	TMeasure *m = curve->getMeasure();
Toshihiro Shimizu 890ddd
	if (m) {
Toshihiro Shimizu 890ddd
		const TUnit *unit = m->getCurrentUnit();
Toshihiro Shimizu 890ddd
		v = unit->convertTo(v);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return tcrop(m_viewTransform.m22() * v + m_viewTransform.dy(), -bigNumber, bigNumber);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double FunctionPanel::yToValue(TDoubleParam *curve, double y) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double v = (y - m_viewTransform.dy()) / m_viewTransform.m22();
Toshihiro Shimizu 890ddd
	TMeasure *m = curve->getMeasure();
Toshihiro Shimizu 890ddd
	if (m) {
Toshihiro Shimizu 890ddd
		const TUnit *unit = m->getCurrentUnit();
Toshihiro Shimizu 890ddd
		v = unit->convertFrom(v);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::pan(int dx, int dy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QTransform m;
Toshihiro Shimizu 890ddd
	m.translate(dx, dy);
Toshihiro Shimizu 890ddd
	m_viewTransform *= m;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::zoom(double sx, double sy, const QPoint Âąer)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QTransform m;
Toshihiro Shimizu 890ddd
	m.translate(center.x(), center.y());
Toshihiro Shimizu 890ddd
	m.scale(sx, sy);
Toshihiro Shimizu 890ddd
	m.translate(-center.x(), -center.y());
Toshihiro Shimizu 890ddd
	m_viewTransform *= m;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPointF FunctionPanel::getWinPos(TDoubleParam *curve, double frame, double value) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QPointF(frameToX(frame), valueToY(curve, value));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPointF FunctionPanel::getWinPos(TDoubleParam *curve, double frame) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getWinPos(curve, frame, curve->getValue(frame));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPointF FunctionPanel::getWinPos(TDoubleParam *curve, const TDoubleKeyframe &kf) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getWinPos(curve, kf.m_frame, kf.m_value);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FunctionPanel::getCurveDistance(TDoubleParam *curve, const QPoint &winPos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double frame = xToFrame(winPos.x());
Toshihiro Shimizu 890ddd
	double value = curve->getValue(frame);
Toshihiro Shimizu 890ddd
	double curveY = valueToY(curve, value);
MichaƂ Janiszewski 0585ee
	return std::abs(curveY - winPos.y());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FunctionTreeModel::Channel *FunctionPanel::findClosestChannel(const QPoint &winPos, int maxWinDistance)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *closestChannel = 0;
Toshihiro Shimizu 890ddd
	int minDistance = maxWinDistance;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_functionTreeModel->getActiveChannelCount(); i++) {
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *channel = m_functionTreeModel->getActiveChannel(i);
Toshihiro Shimizu 890ddd
		TDoubleParam *curve = channel->getParam();
Toshihiro Shimizu 890ddd
		int distance = getCurveDistance(curve, winPos);
Toshihiro Shimizu 890ddd
		if (distance < minDistance) {
Toshihiro Shimizu 890ddd
			closestChannel = channel;
Toshihiro Shimizu 890ddd
			minDistance = distance;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return closestChannel;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TDoubleParam *FunctionPanel::findClosestCurve(const QPoint &winPos, int maxWinDistance)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *closestChannel = findClosestChannel(winPos, maxWinDistance);
Toshihiro Shimizu 890ddd
	return closestChannel ? closestChannel->getParam() : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// return the gadget index (-1 if no gadget is close enough)
Toshihiro Shimizu 890ddd
int FunctionPanel::findClosestGadget(const QPoint &winPos, Handle &handle, int maxDistance)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// search only handles close enough (i.e. distance
Toshihiro Shimizu 890ddd
	int minDistance = maxDistance;
Toshihiro Shimizu 890ddd
	int k = -1;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_gadgets.size(); i++) {
Toshihiro Shimizu 890ddd
		if (m_gadgets[i].m_hitRegion.contains(winPos)) {
Toshihiro Shimizu 890ddd
			QPoint p;
Toshihiro Shimizu 890ddd
			double d = (m_gadgets[i].m_hitRegion.center() - winPos).manhattanLength();
Toshihiro Shimizu 890ddd
			if (d < minDistance) {
Toshihiro Shimizu 890ddd
				k = i;
Toshihiro Shimizu 890ddd
				minDistance = d;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (k >= 0) {
Toshihiro Shimizu 890ddd
		handle = m_gadgets[k].m_handle;
Toshihiro Shimizu 890ddd
		return k; // m_gadgets[k].m_kIndex;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		handle = None;
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TDoubleParam *FunctionPanel::getCurrentCurve() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *currentChannel =
Toshihiro Shimizu 890ddd
		m_functionTreeModel ? m_functionTreeModel->getCurrentChannel() : 0;
Toshihiro Shimizu 890ddd
	if (!currentChannel)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return currentChannel->getParam();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QPainterPath FunctionPanel::getSegmentPainterPath(TDoubleParam *curve, int segmentIndex, int x0, int x1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double frame0 = xToFrame(x0), frame1 = xToFrame(x1);
Toshihiro Shimizu 890ddd
	int kCount = curve->getKeyframeCount();
Toshihiro Shimizu 890ddd
	int step = 1;
Toshihiro Shimizu 890ddd
	if (kCount > 0) {
Toshihiro Shimizu 890ddd
		if (segmentIndex < 0)
Shinya Kitaoka 12c444
			frame1 = std::min(frame1, curve->keyframeIndexToFrame(0)); // before first keyframe
Toshihiro Shimizu 890ddd
		else if (segmentIndex >= kCount - 1)
Shinya Kitaoka 12c444
			frame0 = std::max(frame0, curve->keyframeIndexToFrame(kCount - 1)); // after last keyframe
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			// between keyframes
Toshihiro Shimizu 890ddd
			TDoubleKeyframe kf = curve->getKeyframe(segmentIndex);
Shinya Kitaoka 12c444
			frame0 = std::max(frame0, kf.m_frame);
Toshihiro Shimizu 890ddd
			double f = curve->keyframeIndexToFrame(segmentIndex + 1);
Shinya Kitaoka 12c444
			frame1 = std::min(frame1, f);
Toshihiro Shimizu 890ddd
			step = kf.m_step;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (frame0 >= frame1)
Toshihiro Shimizu 890ddd
		return QPainterPath();
Toshihiro Shimizu 890ddd
	double frame;
Toshihiro Shimizu 890ddd
	double df = xToFrame(3) - xToFrame(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_curveShape == SMOOTH) {
Toshihiro Shimizu 890ddd
		frame = frame0;
Toshihiro Shimizu 890ddd
	} else //FRAME_BASED
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		frame = (double)tfloor(frame0);
Shinya Kitaoka 12c444
		df = std::max(df, 1.0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPainterPath path;
Toshihiro Shimizu 890ddd
	if (0 <= segmentIndex && segmentIndex < kCount && step > 1) {
Toshihiro Shimizu 890ddd
		// step>1
Toshihiro Shimizu 890ddd
		path.moveTo(getWinPos(curve, frame));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int f0 = curve->keyframeIndexToFrame(segmentIndex);
Toshihiro Shimizu 890ddd
		int vFrame = f0 + tfloor(tfloor(frame - f0), step);
Toshihiro Shimizu 890ddd
		double vValue = curve->getValue(vFrame);
Toshihiro Shimizu 890ddd
		assert(vFrame <= frame);
Toshihiro Shimizu 890ddd
		assert(vFrame + step > frame);
Toshihiro Shimizu 890ddd
		while (vFrame + step < frame1) {
Toshihiro Shimizu 890ddd
			vValue = curve->getValue(vFrame);
Toshihiro Shimizu 890ddd
			path.lineTo(getWinPos(curve, vFrame, vValue));
Toshihiro Shimizu 890ddd
			vFrame += step;
Toshihiro Shimizu 890ddd
			path.lineTo(getWinPos(curve, vFrame, vValue));
Toshihiro Shimizu 890ddd
			vValue = curve->getValue(vFrame);
Toshihiro Shimizu 890ddd
			path.lineTo(getWinPos(curve, vFrame, vValue));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		path.lineTo(getWinPos(curve, frame1, vValue));
Toshihiro Shimizu 890ddd
		path.lineTo(getWinPos(curve, frame1, curve->getValue(frame1, true)));
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// step = 1
Toshihiro Shimizu 890ddd
		path.moveTo(getWinPos(curve, frame));
Toshihiro Shimizu 890ddd
		while (frame + df < frame1) {
Toshihiro Shimizu 890ddd
			frame += df;
Toshihiro Shimizu 890ddd
			path.lineTo(getWinPos(curve, frame));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		path.lineTo(getWinPos(curve, frame1, curve->getValue(frame1, true)));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawCurrentFrame(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int currentframe = 0;
Toshihiro Shimizu 890ddd
	if (m_frameHandle)
Toshihiro Shimizu 890ddd
		currentframe = m_frameHandle->getFrame();
Toshihiro Shimizu 890ddd
	int x = frameToX(currentframe);
Toshihiro Shimizu 890ddd
	if (m_currentFrameStatus == 0)
Toshihiro Shimizu 890ddd
		painter.setPen(Qt::magenta);
Toshihiro Shimizu 890ddd
	else if (m_currentFrameStatus == 1)
Toshihiro Shimizu 890ddd
		painter.setPen(Qt::white);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		painter.setPen(m_selectedColor);
Toshihiro Shimizu 890ddd
	int y = m_graphViewportY + 1;
Toshihiro Shimizu 890ddd
	painter.drawLine(x - 1, y, x - 1, height());
Toshihiro Shimizu 890ddd
	painter.drawLine(x + 1, y, x + 1, height());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawFrameGrid(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QFontMetrics fm(painter.font());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// ruler background
Toshihiro Shimizu 890ddd
	painter.setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
	painter.setBrush(getRulerBackground());
Toshihiro Shimizu 890ddd
	painter.drawRect(0, 0, width(), m_frameAxisY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw ticks and labels
Toshihiro Shimizu 890ddd
	Ruler ruler;
Toshihiro Shimizu 890ddd
	ruler.setTransform(m_viewTransform.m11(), m_viewTransform.dx(), -1);
Toshihiro Shimizu 890ddd
	ruler.setRange(m_valueAxisX, width());
Toshihiro Shimizu 890ddd
	ruler.setMinLabelDistance(fm.width("-8888") + 2);
Toshihiro Shimizu 890ddd
	ruler.setMinDistance(5);
Toshihiro Shimizu 890ddd
	ruler.setMinStep(1);
Toshihiro Shimizu 890ddd
	ruler.compute();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < ruler.getTickCount(); i++) {
Toshihiro Shimizu 890ddd
		double f = ruler.getTick(i);
Toshihiro Shimizu 890ddd
		bool isLabel = ruler.isLabel(i);
Toshihiro Shimizu 890ddd
		int x = frameToX(f);
Toshihiro Shimizu 890ddd
		painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
		int y = m_frameAxisY;
Toshihiro Shimizu 890ddd
		painter.drawLine(x, y - (isLabel ? 4 : 2), x, y);
Toshihiro Shimizu 890ddd
		painter.setPen(getFrameLineColor());
Toshihiro Shimizu 890ddd
		painter.drawLine(x, m_graphViewportY, x, height());
Toshihiro Shimizu 890ddd
		if (isLabel) {
Toshihiro Shimizu 890ddd
			painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
			QString labelText = QString::number(f + 1);
Toshihiro Shimizu 890ddd
			painter.drawText(x - fm.width(labelText) / 2, y - 6, labelText);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawValueGrid(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TDoubleParam *curve = getCurrentCurve();
Toshihiro Shimizu 890ddd
	if (!curve)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QFontMetrics fm(painter.font());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// ruler background
Toshihiro Shimizu 890ddd
	painter.setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
	painter.setBrush(getRulerBackground());
Toshihiro Shimizu 890ddd
	painter.drawRect(0, 0, m_valueAxisX, height());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Ruler ruler;
Toshihiro Shimizu 890ddd
	ruler.setTransform(m_viewTransform.m22(), m_viewTransform.dy());
Toshihiro Shimizu 890ddd
	ruler.setRange(m_graphViewportY, height());
Toshihiro Shimizu 890ddd
	ruler.setMinLabelDistance(fm.height() + 2);
Toshihiro Shimizu 890ddd
	ruler.setMinDistance(5);
Toshihiro Shimizu 890ddd
	ruler.compute();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < ruler.getTickCount(); i++) {
Toshihiro Shimizu 890ddd
		double v = ruler.getTick(i);
Toshihiro Shimizu 890ddd
		bool isLabel = ruler.isLabel(i);
Toshihiro Shimizu 890ddd
		int y = tround(m_viewTransform.m22() * v + m_viewTransform.dy()); // valueToY(curve, v);
Toshihiro Shimizu 890ddd
		painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
		int x = m_valueAxisX;
Toshihiro Shimizu 890ddd
		painter.drawLine(x - (isLabel ? 5 : 2), y, x, y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		painter.setPen(getValueLineColor());
Toshihiro Shimizu 890ddd
		painter.drawLine(x, y, width(), y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (isLabel) {
Toshihiro Shimizu 890ddd
			painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
			QString labelText = QString::number(v);
Shinya Kitaoka 12c444
			painter.drawText(std::max(0, x - 5 - fm.width(labelText)), y + fm.height() / 2, labelText);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (false && ruler.getTickCount() > 10) {
Toshihiro Shimizu 890ddd
		double value = ruler.getTick(9);
Toshihiro Shimizu 890ddd
		double dv = fabs(ruler.getTick(10));
Toshihiro Shimizu 890ddd
		double frame = 10;
Toshihiro Shimizu 890ddd
		double df = dv * getPixelRatio(curve);
Toshihiro Shimizu 890ddd
		QPointF p0 = getWinPos(curve, frame, value);
Toshihiro Shimizu 890ddd
		QPointF p1 = getWinPos(curve, frame + df, value + dv);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		painter.setPen(Qt::magenta);
Toshihiro Shimizu 890ddd
		painter.drawRect(p0.x(), p0.y(), (p1 - p0).x(), (p1 - p0).y());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawOtherCurves(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	painter.setRenderHint(QPainter::Antialiasing, false);
Toshihiro Shimizu 890ddd
	painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
	int x0 = m_valueAxisX;
Toshihiro Shimizu 890ddd
	int x1 = width();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPen solidPen;
Toshihiro Shimizu 890ddd
	QPen dashedPen;
Toshihiro Shimizu 890ddd
	QVector<qreal> dashes;</qreal>
Toshihiro Shimizu 890ddd
	dashes << 4 << 4;
Toshihiro Shimizu 890ddd
	dashedPen.setDashPattern(dashes);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_functionTreeModel->getActiveChannelCount(); i++) {
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *channel = m_functionTreeModel->getActiveChannel(i);
Toshihiro Shimizu 890ddd
		if (channel->isCurrent())
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		TDoubleParam *curve = channel->getParam();
Toshihiro Shimizu 890ddd
		QColor color = curve == m_curveLabel.curve ? m_selectedColor : getOtherCurvesColor();
Toshihiro Shimizu 890ddd
		solidPen.setColor(color);
Toshihiro Shimizu 890ddd
		dashedPen.setColor(color);
Toshihiro Shimizu 890ddd
		painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int kCount = curve->getKeyframeCount();
Toshihiro Shimizu 890ddd
		if (kCount == 0) {
Toshihiro Shimizu 890ddd
			// no control points
Toshihiro Shimizu 890ddd
			painter.setPen(dashedPen);
Toshihiro Shimizu 890ddd
			painter.drawPath(getSegmentPainterPath(curve, 0, x0, x1));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		//draw control points and handles
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			for (int k = -1; k < kCount; k++) {
Toshihiro Shimizu 890ddd
				painter.setPen((k < 0 || k >= kCount - 1) ? dashedPen : solidPen);
Toshihiro Shimizu 890ddd
				painter.drawPath(getSegmentPainterPath(curve, k, x0, x1));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
			painter.setBrush(m_subColor);
Toshihiro Shimizu 890ddd
			for (int k = 0; k < kCount; k++) {
Toshihiro Shimizu 890ddd
				double frame = curve->keyframeIndexToFrame(k);
Toshihiro Shimizu 890ddd
				QPointF p = getWinPos(curve, frame, curve->getValue(frame));
Toshihiro Shimizu 890ddd
				painter.drawRect(p.x() - 1, p.y() - 1, 3, 3);
Toshihiro Shimizu 890ddd
				QPointF p2 = getWinPos(curve, frame, curve->getValue(frame, true));
Toshihiro Shimizu 890ddd
				if (p2.y() != p.y()) {
Toshihiro Shimizu 890ddd
					painter.drawRect(p2.x() - 1, p2.y() - 1, 3, 3);
Toshihiro Shimizu 890ddd
					painter.setPen(solidPen);
Toshihiro Shimizu 890ddd
					painter.drawLine(p, p2);
Toshihiro Shimizu 890ddd
					painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
	painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
	painter.setRenderHint(QPainter::Antialiasing, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::updateGadgets(TDoubleParam *curve)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_gadgets.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDoubleKeyframe oldKf;
Toshihiro Shimizu 890ddd
	oldKf.m_type = TDoubleKeyframe::None;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int keyframeCount = curve->getKeyframeCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i != keyframeCount; ++i) {
Toshihiro Shimizu 890ddd
		const int pointHitRadius = 10, handleHitRadius = 6;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDoubleKeyframe kf = curve->getKeyframe(i);
Toshihiro Shimizu 890ddd
		kf.m_value = curve->getValue(kf.m_frame); // Some keyframe values do NOT correspond to the
Toshihiro Shimizu 890ddd
												  // actual displayed curve value (eg with expressions)
Toshihiro Shimizu 890ddd
		// Build keyframe positions
Toshihiro Shimizu 890ddd
		QPointF p = getWinPos(curve, kf.m_frame);
Toshihiro Shimizu 890ddd
		QPointF pLeft = p;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (i == keyframeCount - 1 && curve->isCycleEnabled())					 // This is probably OBSOLETE. I don't think the
Toshihiro Shimizu 890ddd
			p = getWinPos(curve, kf.m_frame, curve->getValue(kf.m_frame, true)); // GUI allows cycling single curves nowadays...
Toshihiro Shimizu 890ddd
																				 // However, is the assignment correct?
Toshihiro Shimizu 890ddd
		// Add keyframe gadget(s)
Toshihiro Shimizu 890ddd
		m_gadgets.push_back(Gadget(Point, i, p, pointHitRadius, pointHitRadius));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPointD currentPointRight(kf.m_frame, kf.m_value);
Toshihiro Shimizu 890ddd
		TPointD currentPointLeft(currentPointRight);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// If the previous segment or the current segment are not keyframe based,
Toshihiro Shimizu 890ddd
		// the curve can have two different values in kf.m_frame
Toshihiro Shimizu 890ddd
		if (i > 0 && (!TDoubleKeyframe::isKeyframeBased(kf.m_type) ||						// Keyframe-based are the above mentioned curves
Toshihiro Shimizu 890ddd
					  !TDoubleKeyframe::isKeyframeBased(curve->getKeyframe(i - 1).m_type))) // where values stored in keyframes are not used
Toshihiro Shimizu 890ddd
		{																					// to calculate the actual curve values.
Toshihiro Shimizu 890ddd
			currentPointLeft.y = curve->getValue(kf.m_frame, true);
Toshihiro Shimizu 890ddd
			pLeft = getWinPos(curve, currentPointLeft);
Toshihiro Shimizu 890ddd
			m_gadgets.push_back(Gadget(Point, i, pLeft, pointHitRadius, pointHitRadius));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Add handle gadgets (eg the speed or ease handles)
Toshihiro Shimizu 890ddd
		if (getSelection()->isSelected(curve, i)) {
Toshihiro Shimizu 890ddd
			// Left handle
Toshihiro Shimizu 890ddd
			switch (oldKf.m_type) {
Toshihiro Shimizu 890ddd
			case TDoubleKeyframe::SpeedInOut: {
Toshihiro Shimizu 890ddd
				TPointD speedIn = curve->getSpeedIn(i);
Toshihiro Shimizu 890ddd
				if (norm2(speedIn) > 0) {
Toshihiro Shimizu 890ddd
					QPointF q = getWinPos(curve, currentPointLeft + speedIn);
Toshihiro Shimizu 890ddd
					m_gadgets.push_back(Gadget(SpeedIn, i, q, handleHitRadius, handleHitRadius, pLeft));
Toshihiro Shimizu 890ddd
				}
Shinya Kitaoka d4642c
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Shinya Kitaoka d4642c
			case TDoubleKeyframe::EaseInOut: {
Shinya Kitaoka d4642c
				QPointF q = getWinPos(curve, kf.m_frame + kf.m_speedIn.x);
Shinya Kitaoka d4642c
				m_gadgets.push_back(Gadget(EaseIn, i, q, 6, 15));
Shinya Kitaoka d4642c
				break;
Shinya Kitaoka d4642c
			}
Toshihiro Shimizu 890ddd
Shinya Kitaoka d4642c
			case TDoubleKeyframe::EaseInOutPercentage: {
Shinya Kitaoka d4642c
				double easeIn = kf.m_speedIn.x * (kf.m_frame - oldKf.m_frame) * 0.01;
Shinya Kitaoka d4642c
				QPointF q = getWinPos(curve, kf.m_frame + easeIn);
Shinya Kitaoka d4642c
				m_gadgets.push_back(Gadget(EaseInPercentage, i, q, 6, 15));
Shinya Kitaoka d4642c
				break;
Shinya Kitaoka d4642c
			}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// Right handle
Toshihiro Shimizu 890ddd
			if (i != keyframeCount - 1) {
Toshihiro Shimizu 890ddd
				switch (kf.m_type) {
Toshihiro Shimizu 890ddd
				case TDoubleKeyframe::SpeedInOut: {
Toshihiro Shimizu 890ddd
					TPointD speedOut = curve->getSpeedOut(i);
Toshihiro Shimizu 890ddd
					if (norm2(speedOut) > 0) {
Toshihiro Shimizu 890ddd
						QPointF q = getWinPos(curve, currentPointRight + speedOut);
Toshihiro Shimizu 890ddd
						m_gadgets.push_back(Gadget(SpeedOut, i, q, handleHitRadius, handleHitRadius, p));
Toshihiro Shimizu 890ddd
					}
Shinya Kitaoka d4642c
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Shinya Kitaoka d4642c
				case TDoubleKeyframe::EaseInOut: {
Shinya Kitaoka d4642c
					QPointF q = getWinPos(curve, kf.m_frame + kf.m_speedOut.x);
Shinya Kitaoka d4642c
					m_gadgets.push_back(Gadget(EaseOut, i, q, 6, 15));
Shinya Kitaoka d4642c
					break;
Shinya Kitaoka d4642c
				}
Toshihiro Shimizu 890ddd
Shinya Kitaoka d4642c
				case TDoubleKeyframe::EaseInOutPercentage: {
Shinya Kitaoka d4642c
					double segmentWidth = curve->keyframeIndexToFrame(i + 1) - kf.m_frame;
Shinya Kitaoka d4642c
					double easeOut = segmentWidth * kf.m_speedOut.x * 0.01;
Toshihiro Shimizu 890ddd
Shinya Kitaoka d4642c
					QPointF q = getWinPos(curve, kf.m_frame + easeOut);
Shinya Kitaoka d4642c
					m_gadgets.push_back(Gadget(EaseOutPercentage, i, q, 6, 15));
Shinya Kitaoka d4642c
					break;
Shinya Kitaoka d4642c
				}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		oldKf = kf;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Add group gadgets (ie those that can be added when multiple channels share the same keyframe data)
Toshihiro Shimizu 890ddd
	int channelCount = m_functionTreeModel->getActiveChannelCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Using a map of vectors. Yes, really. The *ideal* way would be that of copying the first keyframes
Toshihiro Shimizu 890ddd
	// vector, and then comparing it with the others from each channel - keeping the common data only...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef std::map<double, std::vector<tdoublekeyframe="">> KeyframeTable; // frame -> { keyframes }</double,>
Toshihiro Shimizu 890ddd
	KeyframeTable keyframes;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i != channelCount; ++i) {
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *channel = m_functionTreeModel->getActiveChannel(i);
Toshihiro Shimizu 890ddd
		if (!channel)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDoubleParam *curve = channel->getParam();
Toshihiro Shimizu 890ddd
		for (int j = 0; j != curve->getKeyframeCount(); ++j) {
Toshihiro Shimizu 890ddd
			TDoubleKeyframe kf = curve->getKeyframe(j); // Well... this stuff gets called upon *painting*  o_o'
Toshihiro Shimizu 890ddd
			keyframes[kf.m_frame].push_back(kf);		// It's bound to be slow. Do we really need it?
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int groupHandleY = m_graphViewportY - 6;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	KeyframeTable::iterator it, iEnd(keyframes.end()), iLast(keyframes.empty() ? iEnd : --iEnd);
Toshihiro Shimizu 890ddd
	for (KeyframeTable::iterator it = keyframes.begin(); it != keyframes.end(); ++it) {
Toshihiro Shimizu 890ddd
		assert(!it->second.empty());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double frame = it->first; // redundant, already in the key... oh well
Toshihiro Shimizu 890ddd
		QPointF p(frameToX(frame), groupHandleY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Gadget gadget((FunctionPanel::Handle)100, -1, p, 6, 6); // No idea what the '100' type value mean...
Toshihiro Shimizu 890ddd
		gadget.m_keyframePosition = frame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_gadgets.push_back(gadget);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDoubleKeyframe kf = it->second[0];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((int)it->second.size() < channelCount)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// All channels had this keyframe - so, add further gadgets about stuff...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 1; i < channelCount; ++i) {
Toshihiro Shimizu 890ddd
			// Find out if keyframes data differs
Toshihiro Shimizu 890ddd
			const TDoubleKeyframe &kf2 = it->second[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (kf.m_type != kf2.m_type || kf.m_speedOut.x != kf2.m_speedOut.x)
Toshihiro Shimizu 890ddd
				kf.m_type = TDoubleKeyframe::None;
Toshihiro Shimizu 890ddd
			if (kf.m_prevType != kf2.m_prevType || kf.m_speedIn.x != kf2.m_speedIn.x)
Toshihiro Shimizu 890ddd
				kf.m_prevType = TDoubleKeyframe::None;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// NOTE: EaseInOutPercentage are currently NOT SUPPORTED - they would be harder to code and
Toshihiro Shimizu 890ddd
		//       controversial, since the handle position depends on the *segment size* too.
Toshihiro Shimizu 890ddd
		//       So, keyframe data could be shared, but adjacent segment lengths could not...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (it != iLast &&
Toshihiro Shimizu 890ddd
			(kf.m_type == TDoubleKeyframe::SpeedInOut || kf.m_type == TDoubleKeyframe::EaseInOut) &&
Toshihiro Shimizu 890ddd
			kf.m_speedOut.x != 0) {
Toshihiro Shimizu 890ddd
			QPointF p(frameToX(frame + kf.m_speedOut.x), groupHandleY);
Toshihiro Shimizu 890ddd
			Gadget gadget((FunctionPanel::Handle)101, -1, p, 6, 15); // type value...
Toshihiro Shimizu 890ddd
			gadget.m_keyframePosition = frame;
Toshihiro Shimizu 890ddd
			m_gadgets.push_back(gadget);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((kf.m_prevType == TDoubleKeyframe::SpeedInOut || kf.m_prevType == TDoubleKeyframe::EaseInOut) &&
Toshihiro Shimizu 890ddd
			kf.m_speedIn.x != 0) {
Toshihiro Shimizu 890ddd
			QPointF p(frameToX(frame + kf.m_speedIn.x), groupHandleY);
Toshihiro Shimizu 890ddd
			Gadget gadget((FunctionPanel::Handle)102, -1, p, 6, 15); // type value...
Toshihiro Shimizu 890ddd
			gadget.m_keyframePosition = frame;
Toshihiro Shimizu 890ddd
			m_gadgets.push_back(gadget);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawCurrentCurve(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *channel =
Toshihiro Shimizu 890ddd
		m_functionTreeModel ? m_functionTreeModel->getCurrentChannel() : 0;
Toshihiro Shimizu 890ddd
	if (!channel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TDoubleParam *curve = channel->getParam();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setRenderHint(QPainter::Antialiasing, true);
Toshihiro Shimizu 890ddd
	QColor color = Qt::red;
Toshihiro Shimizu 890ddd
	QPen solidPen(color);
Toshihiro Shimizu 890ddd
	QPen dashedPen(color);
Toshihiro Shimizu 890ddd
	QVector<qreal> dashes;</qreal>
Toshihiro Shimizu 890ddd
	dashes << 4 << 4;
Toshihiro Shimizu 890ddd
	dashedPen.setDashPattern(dashes);
Toshihiro Shimizu 890ddd
	painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x0 = m_valueAxisX;
Toshihiro Shimizu 890ddd
	int x1 = width();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw curve
Toshihiro Shimizu 890ddd
	int kCount = curve->getKeyframeCount();
Toshihiro Shimizu 890ddd
	if (kCount == 0) {
Toshihiro Shimizu 890ddd
		// no control points
Toshihiro Shimizu 890ddd
		painter.setPen(dashedPen);
Toshihiro Shimizu 890ddd
		painter.drawPath(getSegmentPainterPath(curve, 0, x0, x1));
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		for (int k = -1; k < kCount; k++) {
Toshihiro Shimizu 890ddd
			if (k < 0 || k >= kCount - 1) {
Toshihiro Shimizu 890ddd
				painter.setPen(dashedPen);
Toshihiro Shimizu 890ddd
				painter.drawPath(getSegmentPainterPath(curve, k, x0, x1));
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				TDoubleKeyframe::Type segmentType = curve->getKeyframe(k).m_type;
Toshihiro Shimizu 890ddd
				QColor color = Qt::red;
Toshihiro Shimizu 890ddd
				if (segmentType == TDoubleKeyframe::Expression || segmentType == TDoubleKeyframe::SimilarShape || segmentType == TDoubleKeyframe::File)
Toshihiro Shimizu 890ddd
					color = QColor(185, 0, 0);
Toshihiro Shimizu 890ddd
				if (getSelection()->isSegmentSelected(curve, k))
Toshihiro Shimizu 890ddd
					solidPen.setWidth(2);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					solidPen.setWidth(0);
Toshihiro Shimizu 890ddd
				solidPen.setColor(color);
Toshihiro Shimizu 890ddd
				painter.setPen(solidPen);
Toshihiro Shimizu 890ddd
				painter.drawPath(getSegmentPainterPath(curve, k, x0, x1));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	painter.setPen(QPen(m_textColor, 0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw control points
Toshihiro Shimizu 890ddd
	updateGadgets(curve);
Toshihiro Shimizu 890ddd
	painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
	for (int j = 0; j < (int)m_gadgets.size(); j++) {
Toshihiro Shimizu 890ddd
		const Gadget &g = m_gadgets[j];
Toshihiro Shimizu 890ddd
		if (g.m_handle == SpeedIn || g.m_handle == SpeedOut)
Toshihiro Shimizu 890ddd
			painter.drawLine(g.m_pointPos, g.m_pos);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	solidPen.setWidth(0);
Toshihiro Shimizu 890ddd
	solidPen.setColor(Qt::red);
Toshihiro Shimizu 890ddd
	painter.setPen(solidPen);
Toshihiro Shimizu 890ddd
	for (int j = 0; j < (int)m_gadgets.size() - 1; j++)
Toshihiro Shimizu 890ddd
		if (m_gadgets[j].m_handle == Point && m_gadgets[j + 1].m_handle && m_gadgets[j + 1].m_handle != 100 &&
Toshihiro Shimizu 890ddd
			m_gadgets[j].m_pos.x() == m_gadgets[j + 1].m_pos.x())
Toshihiro Shimizu 890ddd
			painter.drawLine(m_gadgets[j].m_pos, m_gadgets[j + 1].m_pos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setRenderHint(QPainter::Antialiasing, false);
Toshihiro Shimizu 890ddd
	for (int j = 0; j < (int)m_gadgets.size(); j++) {
Toshihiro Shimizu 890ddd
		const Gadget &g = m_gadgets[j];
Toshihiro Shimizu 890ddd
		int i = g.m_kIndex;
Toshihiro Shimizu 890ddd
		int r = 1;
Toshihiro Shimizu 890ddd
		QPointF p = g.m_pos;
Toshihiro Shimizu 890ddd
		double easeDx = 0, easeHeight = 15, easeTick = 2;
Toshihiro Shimizu 890ddd
		bool isSelected = getSelection()->isSelected(curve, i);
Toshihiro Shimizu 890ddd
		bool isHighlighted = m_highlighted.handle == g.m_handle && m_highlighted.gIndex == j;
Toshihiro Shimizu 890ddd
		switch (g.m_handle) {
Toshihiro Shimizu 890ddd
		case Point:
Toshihiro Shimizu 890ddd
			painter.setBrush(isSelected ? QColor(255, 126, 0) : m_subColor);
Toshihiro Shimizu 890ddd
			painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
			r = isHighlighted ? 3 : 2;
Toshihiro Shimizu 890ddd
			drawSquare(painter, p, r);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		case SpeedIn:
Toshihiro Shimizu 890ddd
		case SpeedOut:
Toshihiro Shimizu 890ddd
			painter.setBrush(m_subColor);
Toshihiro Shimizu 890ddd
			painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
			r = isHighlighted ? 3 : 2;
Toshihiro Shimizu 890ddd
			drawRoundedSquare(painter, p, r);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		case EaseIn:
Toshihiro Shimizu 890ddd
		case EaseOut:
Toshihiro Shimizu 890ddd
		case EaseInPercentage:
Toshihiro Shimizu 890ddd
		case EaseOutPercentage:
Toshihiro Shimizu 890ddd
			painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
			painter.setPen(isHighlighted ? QColor(255, 126, 0) : m_textColor);
Toshihiro Shimizu 890ddd
			painter.drawLine(p.x(), p.y() - easeHeight, p.x(), p.y() + easeHeight);
Toshihiro Shimizu 890ddd
			if (g.m_handle == EaseIn || g.m_handle == EaseInPercentage)
Toshihiro Shimizu 890ddd
				easeDx = easeTick;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				easeDx = -easeTick;
Toshihiro Shimizu 890ddd
			painter.drawLine(p.x(), p.y() - easeHeight, p.x() + easeDx, p.y() - easeHeight - easeTick);
Toshihiro Shimizu 890ddd
			painter.drawLine(p.x(), p.y() + easeHeight, p.x() + easeDx, p.y() + easeHeight + easeTick);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			painter.setBrush(Qt::NoBrush); // isSelected ? QColor(255,126,0) : Qt::white);
Toshihiro Shimizu 890ddd
			painter.setPen(isHighlighted ? QColor(255, 126, 0) : m_selectedColor);
Toshihiro Shimizu 890ddd
			painter.drawLine(p.x(), p.y() - 15, p.x(), p.y() + 15);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setRenderHint(QPainter::Antialiasing, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::drawGroupKeyframes(QPainter &painter)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *channel =
Toshihiro Shimizu 890ddd
		m_functionTreeModel ? m_functionTreeModel->getCurrentChannel() : 0;
Toshihiro Shimizu 890ddd
	if (!channel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	QColor color = Qt::red;
Toshihiro Shimizu 890ddd
	QPen solidPen(color);
Toshihiro Shimizu 890ddd
	QPen dashedPen(color);
Toshihiro Shimizu 890ddd
	QVector<qreal> dashes;</qreal>
Toshihiro Shimizu 890ddd
	dashes << 4 << 4;
Toshihiro Shimizu 890ddd
	dashedPen.setDashPattern(dashes);
Toshihiro Shimizu 890ddd
	painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x0 = m_valueAxisX;
Toshihiro Shimizu 890ddd
	int x1 = width();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	solidPen.setWidth(0);
Toshihiro Shimizu 890ddd
	solidPen.setColor(Qt::red);
Toshihiro Shimizu 890ddd
	painter.setPen(solidPen);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::vector<double> keyframes;</double>
Toshihiro Shimizu 890ddd
	int y = 0;
Toshihiro Shimizu 890ddd
	for (int j = 0; j < (int)m_gadgets.size(); j++) {
Toshihiro Shimizu 890ddd
		const Gadget &g = m_gadgets[j];
Toshihiro Shimizu 890ddd
		int i = g.m_kIndex;
Toshihiro Shimizu 890ddd
		int r = 1;
Toshihiro Shimizu 890ddd
		QPointF p = g.m_pos;
Toshihiro Shimizu 890ddd
		double easeDx = 0, easeHeight = 15, easeTick = 2;
Toshihiro Shimizu 890ddd
		bool isSelected = false; // getSelection()->isSelected(curve, i);
Toshihiro Shimizu 890ddd
		bool isHighlighted = m_highlighted.handle == g.m_handle && m_highlighted.gIndex == j;
Toshihiro Shimizu 890ddd
		painter.setBrush(isSelected ? QColor(255, 126, 0) : m_subColor);
Toshihiro Shimizu 890ddd
		painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
		r = isHighlighted ? 3 : 2;
Toshihiro Shimizu 890ddd
		QPainterPath pp;
Toshihiro Shimizu 890ddd
		int d = 2;
Toshihiro Shimizu 890ddd
		int h = 4;
Toshihiro Shimizu 890ddd
		switch (g.m_handle) {
Toshihiro Shimizu 890ddd
		case 100:
Toshihiro Shimizu 890ddd
			drawSquare(painter, p, r);
Toshihiro Shimizu 890ddd
			y = p.y();
Toshihiro Shimizu 890ddd
			keyframes.push_back(p.x());
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 101:
Toshihiro Shimizu 890ddd
			d = -d;
Toshihiro Shimizu 890ddd
		// Note: NO break!
Toshihiro Shimizu 890ddd
		case 102:
Toshihiro Shimizu 890ddd
			painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
			painter.setPen(isHighlighted ? QColor(255, 126, 0) : m_textColor);
Toshihiro Shimizu 890ddd
			pp.moveTo(p + QPointF(d, -h));
Toshihiro Shimizu 890ddd
			pp.lineTo(p + QPointF(0, -h));
Toshihiro Shimizu 890ddd
			pp.lineTo(p + QPointF(0, h));
Toshihiro Shimizu 890ddd
			pp.lineTo(p + QPointF(d, h));
Toshihiro Shimizu 890ddd
			painter.drawPath(pp);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
	for (int i = 0; i + 1 < (int)keyframes.size(); i++) {
Toshihiro Shimizu 890ddd
		painter.drawLine(keyframes[i] + 3, y, keyframes[i + 1] - 3, y);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::paintEvent(QPaintEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_gadgets.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPainter painter(this);
Toshihiro Shimizu 890ddd
	QFont font("Arial", 8);
Toshihiro Shimizu 890ddd
	painter.setFont(font);
Toshihiro Shimizu 890ddd
	QFontMetrics fm(font);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// define ruler sizes
Toshihiro Shimizu 890ddd
	m_valueAxisX = fm.width("-888.88") + 2;
Toshihiro Shimizu 890ddd
	m_frameAxisY = fm.height() + 2;
Toshihiro Shimizu 890ddd
	m_graphViewportY = m_frameAxisY + 12;
Toshihiro Shimizu 890ddd
	int ox = m_valueAxisX;
Toshihiro Shimizu 890ddd
	int oy0 = m_graphViewportY;
Toshihiro Shimizu 890ddd
	int oy1 = m_frameAxisY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// QRect bounds(0,0,width(),height());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw functions background
Toshihiro Shimizu 890ddd
	painter.setBrush(getBGColor());
Toshihiro Shimizu 890ddd
	painter.setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
	painter.drawRect(ox, oy0, width() - ox, height() - oy0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setClipRect(ox, 0, width() - ox, height());
Toshihiro Shimizu 890ddd
	drawCurrentFrame(painter);
Toshihiro Shimizu 890ddd
	drawFrameGrid(painter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setClipRect(0, oy0, width(), height() - oy0);
Toshihiro Shimizu 890ddd
	drawValueGrid(painter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw axes
Toshihiro Shimizu 890ddd
	painter.setClipping(false);
Toshihiro Shimizu 890ddd
	painter.setPen(m_textColor);
Toshihiro Shimizu 890ddd
	painter.drawLine(0, oy0, width(), oy0);
Toshihiro Shimizu 890ddd
	painter.drawLine(ox, oy1, width(), oy1);
Toshihiro Shimizu 890ddd
	painter.drawLine(ox, 0, ox, height());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw curves
Toshihiro Shimizu 890ddd
	painter.setClipRect(ox + 1, oy0 + 1, width() - ox - 1, height() - oy0 - 1);
Toshihiro Shimizu 890ddd
	drawOtherCurves(painter);
Toshihiro Shimizu 890ddd
	drawCurrentCurve(painter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setClipping(false);
Toshihiro Shimizu 890ddd
	painter.setClipRect(ox + 1, oy1 + 1, width() - ox - 1, oy0 - oy1 - 1);
Toshihiro Shimizu 890ddd
	drawGroupKeyframes(painter);
Toshihiro Shimizu 890ddd
	painter.setClipRect(ox + 1, oy0 + 1, width() - ox - 1, height() - oy0 - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// tool
Toshihiro Shimizu 890ddd
	if (m_dragTool)
Toshihiro Shimizu 890ddd
		m_dragTool->draw(painter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// cursor
Toshihiro Shimizu 890ddd
	if (m_cursor.visible) {
Toshihiro Shimizu 890ddd
		painter.setClipRect(ox + 1, oy0 + 1, width() - ox - 1, height() - oy0 - 1);
Toshihiro Shimizu 890ddd
		painter.setPen(getOtherCurvesColor());
Toshihiro Shimizu 890ddd
		int x = frameToX(m_cursor.frame);
Toshihiro Shimizu 890ddd
		painter.drawLine(x, oy0 + 1, x, oy0 + 10);
Toshihiro Shimizu 890ddd
		QString text = QString::number(tround(m_cursor.frame) + 1);
Toshihiro Shimizu 890ddd
		painter.drawText(x - fm.width(text) / 2, oy0 + 10 + fm.height(), text);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDoubleParam *currentCurve = getCurrentCurve();
Toshihiro Shimizu 890ddd
		if (currentCurve) {
Toshihiro Shimizu 890ddd
			const TUnit *unit = 0;
Toshihiro Shimizu 890ddd
			if (currentCurve->getMeasure())
Toshihiro Shimizu 890ddd
				unit = currentCurve->getMeasure()->getCurrentUnit();
Toshihiro Shimizu 890ddd
			double displayValue = m_cursor.value;
Toshihiro Shimizu 890ddd
			if (unit)
Toshihiro Shimizu 890ddd
				displayValue = unit->convertTo(displayValue);
Toshihiro Shimizu 890ddd
			// painter.setClipRect(0,oy0,height(),height()-oy0);
Toshihiro Shimizu 890ddd
			int y = valueToY(currentCurve, m_cursor.value);
Toshihiro Shimizu 890ddd
			painter.drawLine(ox, y, ox + 10, y);
Toshihiro Shimizu 890ddd
			painter.drawText(m_origin.x() + 10, y + 4, QString::number(displayValue));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// curve name
Toshihiro Shimizu 890ddd
	if (m_curveLabel.text != "") {
Toshihiro Shimizu 890ddd
		painter.setClipRect(ox, oy0, width() - ox, height() - oy0);
Toshihiro Shimizu 890ddd
		painter.setPen(m_selectedColor);
Toshihiro Shimizu 890ddd
		painter.drawLine(m_curveLabel.curvePos, m_curveLabel.labelPos);
Toshihiro Shimizu 890ddd
		painter.drawText(m_curveLabel.labelPos, QString::fromStdString(m_curveLabel.text));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//painter.setPen(Qt::black);
Toshihiro Shimizu 890ddd
	//painter.drawText(QPointF(70,70),
Toshihiro Shimizu 890ddd
	//  "f0=" + QString::number(xToFrame(ox)) +
Toshihiro Shimizu 890ddd
	//  " f1=" + QString::number(xToFrame(width())));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//painter.setPen(Qt::black);
Toshihiro Shimizu 890ddd
	//painter.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
	//painter.drawRect(ox+10,oy+10,width()-ox-20,height()-oy-20);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::mousePressEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_cursor.visible = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// m_dragTool should be 0. just in case...
Toshihiro Shimizu 890ddd
	assert(m_dragTool == 0);
Toshihiro Shimizu 890ddd
	m_dragTool = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (e->button() == Qt::MidButton) {
Toshihiro Shimizu 890ddd
		// mid mouse click => panning
Toshihiro Shimizu 890ddd
		bool xLocked = e->pos().x() <= m_valueAxisX;
Toshihiro Shimizu 890ddd
		bool yLocked = e->pos().y() <= m_valueAxisX;
Toshihiro Shimizu 890ddd
		m_dragTool = new PanDragTool(this, xLocked, yLocked);
Toshihiro Shimizu 890ddd
		m_dragTool->click(e);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} else if (e->button() == Qt::RightButton) {
Toshihiro Shimizu 890ddd
		// right mouse click => open context menu
Toshihiro Shimizu 890ddd
		openContextMenu(e);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPoint winPos = e->pos();
Toshihiro Shimizu 890ddd
	Handle handle = None;
Toshihiro Shimizu 890ddd
	const int maxDistance = 20;
Toshihiro Shimizu 890ddd
	int closestGadgetId = findClosestGadget(e->pos(), handle, maxDistance);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (e->pos().x() > m_valueAxisX && e->pos().y() < m_frameAxisY && closestGadgetId < 0 && (e->modifiers() & Qt::ControlModifier) == 0) {
Toshihiro Shimizu 890ddd
		// click on topbar => frame zoom
Toshihiro Shimizu 890ddd
		m_dragTool = new ZoomDragTool(this, ZoomDragTool::FrameZoom);
Toshihiro Shimizu 890ddd
	} else if (e->pos().x() < m_valueAxisX && e->pos().y() > m_graphViewportY) {
Toshihiro Shimizu 890ddd
		// click on topbar => value zoom
Toshihiro Shimizu 890ddd
		m_dragTool = new ZoomDragTool(this, ZoomDragTool::ValueZoom);
Toshihiro Shimizu 890ddd
	} else if (m_currentFrameStatus == 1 && m_frameHandle != 0 && closestGadgetId < 0) {
Toshihiro Shimizu 890ddd
		// click on current frame => move frame
Toshihiro Shimizu 890ddd
		m_currentFrameStatus = 2;
Toshihiro Shimizu 890ddd
		m_dragTool = new MoveFrameDragTool(this, m_frameHandle);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (0 <= closestGadgetId && closestGadgetId < (int)m_gadgets.size()) {
Toshihiro Shimizu 890ddd
		if (handle == 100) // group move gadget
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			MovePointDragTool *dragTool = new MovePointDragTool(this, 0);
Toshihiro Shimizu 890ddd
			dragTool->selectKeyframes(m_gadgets[closestGadgetId].m_keyframePosition);
Toshihiro Shimizu 890ddd
			m_dragTool = dragTool;
Toshihiro Shimizu 890ddd
		} else if (handle == 101 || handle == 102) {
Toshihiro Shimizu 890ddd
			m_dragTool = new MoveGroupHandleDragTool(this, m_gadgets[closestGadgetId].m_keyframePosition, handle);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_dragTool) {
Toshihiro Shimizu 890ddd
		m_dragTool->click(e);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FunctionTreeModel::Channel *currentChannel =
Toshihiro Shimizu 890ddd
		m_functionTreeModel ? m_functionTreeModel->getCurrentChannel() : 0;
Toshihiro Shimizu 890ddd
	if (!currentChannel ||
Toshihiro Shimizu 890ddd
		getCurveDistance(currentChannel->getParam(), winPos) > maxDistance &&
Toshihiro Shimizu 890ddd
			closestGadgetId < 0) {
Toshihiro Shimizu 890ddd
		// if current channel is undefined or its curve is too far from the clicked point
Toshihiro Shimizu 890ddd
		// the user is possibly trying to select a different curve
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *channel = findClosestChannel(winPos, maxDistance);
Toshihiro Shimizu 890ddd
		if (channel) {
Toshihiro Shimizu 890ddd
			channel->setIsCurrent(true);
Toshihiro Shimizu 890ddd
			//Open folder
Toshihiro Shimizu 890ddd
			FunctionTreeModel::ChannelGroup *channelGroup = channel->getChannelGroup();
Toshihiro Shimizu 890ddd
			if (!channelGroup->isOpen())
Toshihiro Shimizu 890ddd
				channelGroup->getModel()->setExpandedItem(channelGroup->createIndex(), true);
Toshihiro Shimizu 890ddd
			currentChannel = channel;
Toshihiro Shimizu 890ddd
			getSelection()->selectNone();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (currentChannel) {
Toshihiro Shimizu 890ddd
		TDoubleParam *currentCurve = currentChannel->getParam();
Toshihiro Shimizu 890ddd
		if (currentCurve) {
Toshihiro Shimizu 890ddd
			int kIndex = closestGadgetId >= 0 ? m_gadgets[closestGadgetId].m_kIndex : -1;
Toshihiro Shimizu 890ddd
			if (kIndex >= 0) {
Toshihiro Shimizu 890ddd
				// keyframe clicked
Toshihiro Shimizu 890ddd
				if (handle == FunctionPanel::Point) {
Toshihiro Shimizu 890ddd
					// select point (if needed)
Toshihiro Shimizu 890ddd
					if (!getSelection()->isSelected(currentCurve, kIndex)) {
Toshihiro Shimizu 890ddd
						// shift-click => add to selection
Toshihiro Shimizu 890ddd
						if (0 == (e->modifiers() & Qt::ShiftModifier))
Toshihiro Shimizu 890ddd
							getSelection()->deselectAllKeyframes();
Toshihiro Shimizu 890ddd
						getSelection()->select(currentCurve, kIndex);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					// move selected point(s)
Toshihiro Shimizu 890ddd
					MovePointDragTool *dragTool = new MovePointDragTool(this, currentCurve);
Toshihiro Shimizu 890ddd
					if (getSelection()->getSelectedSegment().first != 0) {
Toshihiro Shimizu 890ddd
						// if a segment is selected then move only the clicked point
Toshihiro Shimizu 890ddd
						dragTool->addKeyframe2(kIndex);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						dragTool->setSelection(getSelection());
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					m_dragTool = dragTool;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					m_dragTool = new MoveHandleDragTool(this, currentCurve, kIndex, handle);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				// no keyframe clicked
Toshihiro Shimizu 890ddd
				int curveDistance = getCurveDistance(currentChannel->getParam(), winPos);
Toshihiro Shimizu 890ddd
				bool isKeyframeable = true;
Toshihiro Shimizu 890ddd
				bool isGroup = abs(winPos.y() - (m_graphViewportY - 5)) < 5;
Toshihiro Shimizu 890ddd
				if (0 != (e->modifiers() & Qt::ControlModifier) && (curveDistance < maxDistance || isGroup) && isKeyframeable) {
Toshihiro Shimizu 890ddd
					// ctrl-clicked near curve => create a new keyframe
Toshihiro Shimizu 890ddd
					double frame = tround(xToFrame(winPos.x()));
Toshihiro Shimizu 890ddd
					MovePointDragTool *dragTool = new MovePointDragTool(this, isGroup ? 0 : currentCurve);
Toshihiro Shimizu 890ddd
					//          if(curveDistance>=maxDistance)
Toshihiro Shimizu 890ddd
					//            dragTool->m_channelGroup = currentChannel->getChannelGroup();
Toshihiro Shimizu 890ddd
					dragTool->createKeyframe(frame);
Toshihiro Shimizu 890ddd
					dragTool->selectKeyframes(frame);
Toshihiro Shimizu 890ddd
					m_dragTool = dragTool;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					/*
Toshihiro Shimizu 890ddd
          int kIndex = dragTool->createKeyframe(frame);
Toshihiro Shimizu 890ddd
					if(kIndex!=-1)
Toshihiro Shimizu 890ddd
					{
Toshihiro Shimizu 890ddd
						getSelection()->deselectAllKeyframes();
Toshihiro Shimizu 890ddd
						getSelection()->select(currentCurve, kIndex);
Toshihiro Shimizu 890ddd
						m_dragTool = dragTool;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
          */
Toshihiro Shimizu 890ddd
					// assert(0);
Toshihiro Shimizu 890ddd
				} else if (curveDistance < maxDistance) {
Toshihiro Shimizu 890ddd
					// clicked near curve (but far from keyframes)
Toshihiro Shimizu 890ddd
					getSelection()->deselectAllKeyframes();
Toshihiro Shimizu 890ddd
					double frame = xToFrame(winPos.x());
Toshihiro Shimizu 890ddd
					int k0 = currentCurve->getPrevKeyframe(frame);
Toshihiro Shimizu 890ddd
					int k1 = currentCurve->getNextKeyframe(frame);
Toshihiro Shimizu 890ddd
					if (k0 >= 0 && k1 == k0 + 1) {
Toshihiro Shimizu 890ddd
						// select and move the segment
Toshihiro Shimizu 890ddd
						getSelection()->selectSegment(currentCurve, k0);
Toshihiro Shimizu 890ddd
						MovePointDragTool *dragTool = new MovePointDragTool(this, currentCurve);
Toshihiro Shimizu 890ddd
						dragTool->addKeyframe2(k0);
Toshihiro Shimizu 890ddd
						dragTool->addKeyframe2(k1);
Toshihiro Shimizu 890ddd
						m_dragTool = dragTool;
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						// start a rectangular selection
Toshihiro Shimizu 890ddd
						m_dragTool = new RectSelectTool(this, currentCurve);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					// nothing clicked: start a rectangular selection
Toshihiro Shimizu 890ddd
					getSelection()->deselectAllKeyframes();
Toshihiro Shimizu 890ddd
					m_dragTool = new RectSelectTool(this, currentCurve);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_dragTool)
Toshihiro Shimizu 890ddd
		m_dragTool->click(e);
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::mouseReleaseEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_dragTool)
Toshihiro Shimizu 890ddd
		m_dragTool->release(e);
Toshihiro Shimizu 890ddd
	delete m_dragTool;
Toshihiro Shimizu 890ddd
	m_dragTool = 0;
Toshihiro Shimizu 890ddd
	m_cursor.visible = true;
Toshihiro Shimizu 890ddd
	m_currentFrameStatus = 0;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::mouseMoveEvent(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (e->buttons()) {
Toshihiro Shimizu 890ddd
		if (m_dragTool)
Toshihiro Shimizu 890ddd
			m_dragTool->drag(e);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_cursor.frame = xToFrame(e->pos().x());
Toshihiro Shimizu 890ddd
		m_cursor.value = 0;
Toshihiro Shimizu 890ddd
		m_cursor.visible = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TDoubleParam *currentCurve = getCurrentCurve();
Toshihiro Shimizu 890ddd
		if (currentCurve) {
Toshihiro Shimizu 890ddd
			Handle handle = None;
Toshihiro Shimizu 890ddd
			int gIndex = findClosestGadget(e->pos(), handle, 20);
Toshihiro Shimizu 890ddd
			if (m_highlighted.handle != handle || m_highlighted.gIndex != gIndex) {
Toshihiro Shimizu 890ddd
				m_highlighted.handle = handle;
Toshihiro Shimizu 890ddd
				m_highlighted.gIndex = gIndex;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			m_cursor.value = yToValue(currentCurve, e->pos().y());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double currentFrame = m_frameHandle ? m_frameHandle->getFrame() : 0;
MichaƂ Janiszewski 0585ee
		if (m_highlighted.handle == None && std::abs(e->pos().x() - frameToX(currentFrame)) < 5)
Toshihiro Shimizu 890ddd
			m_currentFrameStatus = 1;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			m_currentFrameStatus = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *closestChannel = findClosestChannel(e->pos(), 20);
Toshihiro Shimizu 890ddd
		if (closestChannel && m_highlighted.handle == None) {
Toshihiro Shimizu 890ddd
			TDoubleParam *curve = closestChannel->getParam();
Toshihiro Shimizu 890ddd
			if (m_functionTreeModel->getActiveChannelCount() <= 1)
Toshihiro Shimizu 890ddd
				//|| closestChannel == m_functionTreeModel->getCurrentChannel())
Toshihiro Shimizu 890ddd
				curve = 0;
Toshihiro Shimizu 890ddd
			if (curve && m_curveLabel.curve != curve) {
Toshihiro Shimizu 890ddd
				m_curveLabel.curve = curve;
Toshihiro Shimizu 890ddd
				QString channelName = closestChannel->data(Qt::DisplayRole).toString();
Toshihiro Shimizu 890ddd
				QString parentChannelName = closestChannel->getChannelGroup()->data(Qt::DisplayRole).toString();
Toshihiro Shimizu 890ddd
				QString name = parentChannelName + QString(", ") + channelName;
Toshihiro Shimizu 890ddd
				m_curveLabel.text = name.toStdString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// in order to avoid run off the right-end of visible area
Toshihiro Shimizu 890ddd
				int textWidth = fontMetrics().width(name) + 30;
Toshihiro Shimizu 890ddd
				double frame = xToFrame(width() - textWidth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				m_curveLabel.curvePos = getWinPos(curve, frame).toPoint();
Toshihiro Shimizu 890ddd
				m_curveLabel.labelPos = m_curveLabel.curvePos + QPoint(20, -10);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			m_curveLabel.text = "";
Toshihiro Shimizu 890ddd
			m_curveLabel.curve = 0;
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
Toshihiro Shimizu 890ddd
void FunctionPanel::keyPressEvent(QKeyEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FunctionPanelZoomer(this).exec(e);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::enterEvent(QEvent *)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_cursor.visible = true;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::leaveEvent(QEvent *)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_cursor.visible = false;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::wheelEvent(QWheelEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double factor = exp(0.002 * (double)e->delta());
Toshihiro Shimizu 890ddd
	zoom(factor, factor, e->pos());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::fitGraphToWindow(bool currentCurveOnly)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double f0 = 0, f1 = -1;
Toshihiro Shimizu 890ddd
	double v0 = 0, v1 = -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_functionTreeModel->getActiveChannelCount(); i++) {
Toshihiro Shimizu 890ddd
		FunctionTreeModel::Channel *channel = m_functionTreeModel->getActiveChannel(i);
Toshihiro Shimizu 890ddd
		TDoubleParam *curve = channel->getParam();
Toshihiro Shimizu 890ddd
		if (currentCurveOnly && curve != getCurrentCurve())
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		const TUnit *unit = 0;
Toshihiro Shimizu 890ddd
		if (curve->getMeasure())
Toshihiro Shimizu 890ddd
			unit = curve->getMeasure()->getCurrentUnit();
Toshihiro Shimizu 890ddd
		int n = curve->getKeyframeCount();
Toshihiro Shimizu 890ddd
		if (n == 0) {
Toshihiro Shimizu 890ddd
			double v = curve->getDefaultValue();
Toshihiro Shimizu 890ddd
			if (unit)
Toshihiro Shimizu 890ddd
				v = unit->convertTo(v);
Toshihiro Shimizu 890ddd
			if (v0 > v1)
Toshihiro Shimizu 890ddd
				v0 = v1 = v;
Toshihiro Shimizu 890ddd
			else if (v > v1)
Toshihiro Shimizu 890ddd
				v1 = v;
Toshihiro Shimizu 890ddd
			else if (v < v0)
Toshihiro Shimizu 890ddd
				v0 = v;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TDoubleKeyframe k = curve->getKeyframe(0);
Toshihiro Shimizu 890ddd
			double fa = k.m_frame;
Toshihiro Shimizu 890ddd
			k = curve->getKeyframe(n - 1);
Toshihiro Shimizu 890ddd
			double fb = k.m_frame;
Toshihiro Shimizu 890ddd
			if (f0 > f1) {
Toshihiro Shimizu 890ddd
				f0 = fa;
Toshihiro Shimizu 890ddd
				f1 = fb;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				f0 = qMin(f0, fa);
Toshihiro Shimizu 890ddd
				f1 = qMax(f1, fb);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			double v = curve->getValue(fa);
Toshihiro Shimizu 890ddd
			if (unit)
Toshihiro Shimizu 890ddd
				v = unit->convertTo(v);
Toshihiro Shimizu 890ddd
			if (v0 > v1)
Toshihiro Shimizu 890ddd
				v0 = v1 = v;
Toshihiro Shimizu 890ddd
			int m = 50;
Toshihiro Shimizu 890ddd
			for (int j = 0; j < m; j++) {
Toshihiro Shimizu 890ddd
				double t = (double)j / (double)(m - 1);
Toshihiro Shimizu 890ddd
				double v = curve->getValue((1 - t) * fa + t * fb);
Toshihiro Shimizu 890ddd
				if (unit)
Toshihiro Shimizu 890ddd
					v = unit->convertTo(v);
Toshihiro Shimizu 890ddd
				v0 = qMin(v0, v);
Toshihiro Shimizu 890ddd
				v1 = qMax(v1, v);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (f0 >= f1 || v0 >= v1) {
Toshihiro Shimizu 890ddd
		m_viewTransform = QTransform();
Toshihiro Shimizu 890ddd
		m_viewTransform.translate(m_valueAxisX, 200);
Toshihiro Shimizu 890ddd
		m_viewTransform.scale(5, -1);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		double mx = (width() - m_valueAxisX - 20) / (f1 - f0);
Toshihiro Shimizu 890ddd
		double my = -(height() - m_graphViewportY - 20) / (v1 - v0);
Toshihiro Shimizu 890ddd
		double dx = m_valueAxisX + 10 - f0 * mx;
Toshihiro Shimizu 890ddd
		double dy = m_graphViewportY + 10 - v1 * my;
Toshihiro Shimizu 890ddd
		m_viewTransform = QTransform(mx, 0, 0, my, dx, dy);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::fitSelectedPoints()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	fitGraphToWindow(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::fitCurve()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	fitGraphToWindow();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::fitRegion(double f0, double v0, double f1, double v1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void setSegmentType(
Toshihiro Shimizu 890ddd
	FunctionSelection *selection,
Toshihiro Shimizu 890ddd
	TDoubleParam *curve,
Toshihiro Shimizu 890ddd
	int segmentIndex,
Toshihiro Shimizu 890ddd
	TDoubleKeyframe::Type type)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	selection->selectSegment(curve, segmentIndex);
Toshihiro Shimizu 890ddd
	KeyframeSetter setter(curve, segmentIndex);
Toshihiro Shimizu 890ddd
	setter.setType(type);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::openContextMenu(QMouseEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QAction linkHandlesAction(tr("Link Handles"), 0);
Toshihiro Shimizu 890ddd
	QAction unlinkHandlesAction(tr("Unlink Handles"), 0);
Toshihiro Shimizu 890ddd
	QAction resetHandlesAction(tr("Reset Handles"), 0);
Toshihiro Shimizu 890ddd
	QAction deleteKeyframeAction(tr("Delete"), 0);
Toshihiro Shimizu 890ddd
	QAction insertKeyframeAction(tr("Set Key"), 0);
Toshihiro Shimizu 890ddd
	QAction activateCycleAction(tr("Activate Cycle"), 0);
Toshihiro Shimizu 890ddd
	QAction deactivateCycleAction(tr("Deactivate Cycle"), 0);
Toshihiro Shimizu 890ddd
	QAction setLinearAction(tr("Linear Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setSpeedInOutAction(tr("Speed In / Speed Out Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setEaseInOutAction(tr("Ease In / Ease Out Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setEaseInOut2Action(tr("Ease In / Ease Out (%) Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setExponentialAction(tr("Exponential Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setExpressionAction(tr("Expression Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setFileAction(tr("File Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setConstantAction(tr("Constant Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction setSimilarShapeAction(tr("Similar Shape Interpolation"), 0);
Toshihiro Shimizu 890ddd
	QAction fitSelectedAction(tr("Fit Selection"), 0);
Toshihiro Shimizu 890ddd
	QAction fitAllAction(tr("Fit"), 0);
Toshihiro Shimizu 890ddd
	QAction setStep1Action(tr("Step 1"), 0);
Toshihiro Shimizu 890ddd
	QAction setStep2Action(tr("Step 2"), 0);
Toshihiro Shimizu 890ddd
	QAction setStep3Action(tr("Step 3"), 0);
Toshihiro Shimizu 890ddd
	QAction setStep4Action(tr("Step 4"), 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDoubleParam *curve = getCurrentCurve();
Toshihiro Shimizu 890ddd
	int segmentIndex = -1;
Toshihiro Shimizu 890ddd
	if (!curve)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TDoubleKeyframe kf;
Toshihiro Shimizu 890ddd
	double frame = xToFrame(e->pos().x());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// build menu
Toshihiro Shimizu 890ddd
	QMenu menu(0);
Toshihiro Shimizu 890ddd
	if (m_highlighted.handle == Point &&
Toshihiro Shimizu 890ddd
		m_highlighted.gIndex >= 0 &&
Toshihiro Shimizu 890ddd
		m_gadgets[m_highlighted.gIndex].m_handle != 100) {
Toshihiro Shimizu 890ddd
		kf = curve->getKeyframe(m_gadgets[m_highlighted.gIndex].m_kIndex);
Toshihiro Shimizu 890ddd
		if (kf.m_linkedHandles)
Toshihiro Shimizu 890ddd
			menu.addAction(&unlinkHandlesAction);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			menu.addAction(&linkHandlesAction);
Toshihiro Shimizu 890ddd
		menu.addAction(&resetHandlesAction);
Toshihiro Shimizu 890ddd
		menu.addAction(&deleteKeyframeAction);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		int k0 = curve->getPrevKeyframe(frame);
Toshihiro Shimizu 890ddd
		int k1 = curve->getNextKeyframe(frame);
Toshihiro Shimizu 890ddd
		if (k0 == curve->getKeyframeCount() - 1) // after last keyframe
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			if (curve->isCycleEnabled())
Toshihiro Shimizu 890ddd
				menu.addAction(&deactivateCycleAction);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				menu.addAction(&activateCycleAction);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		menu.addAction(&insertKeyframeAction);
Toshihiro Shimizu 890ddd
		if (k0 >= 0 && k1 >= 0) {
Toshihiro Shimizu 890ddd
			menu.addSeparator();
Toshihiro Shimizu 890ddd
			segmentIndex = k0;
Toshihiro Shimizu 890ddd
			kf = curve->getKeyframe(k0);
Toshihiro Shimizu 890ddd
			menu.addAction(&setLinearAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::Linear)
Toshihiro Shimizu 890ddd
				setLinearAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setSpeedInOutAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::SpeedInOut)
Toshihiro Shimizu 890ddd
				setSpeedInOutAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setEaseInOutAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::EaseInOut)
Toshihiro Shimizu 890ddd
				setEaseInOutAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setEaseInOut2Action);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::EaseInOutPercentage)
Toshihiro Shimizu 890ddd
				setEaseInOut2Action.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setExponentialAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::Exponential)
Toshihiro Shimizu 890ddd
				setExponentialAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setExpressionAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::Expression)
Toshihiro Shimizu 890ddd
				setExpressionAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setSimilarShapeAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::SimilarShape)
Toshihiro Shimizu 890ddd
				setSimilarShapeAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setFileAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::File)
Toshihiro Shimizu 890ddd
				setFileAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addAction(&setConstantAction);
Toshihiro Shimizu 890ddd
			if (kf.m_type == TDoubleKeyframe::Constant)
Toshihiro Shimizu 890ddd
				setConstantAction.setEnabled(false);
Toshihiro Shimizu 890ddd
			menu.addSeparator();
Toshihiro Shimizu 890ddd
			if (kf.m_step != 1)
Toshihiro Shimizu 890ddd
				menu.addAction(&setStep1Action);
Toshihiro Shimizu 890ddd
			if (kf.m_step != 2)
Toshihiro Shimizu 890ddd
				menu.addAction(&setStep2Action);
Toshihiro Shimizu 890ddd
			if (kf.m_step != 3)
Toshihiro Shimizu 890ddd
				menu.addAction(&setStep3Action);
Toshihiro Shimizu 890ddd
			if (kf.m_step != 4)
Toshihiro Shimizu 890ddd
				menu.addAction(&setStep4Action);
Toshihiro Shimizu 890ddd
			menu.addSeparator();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!getSelection()->isEmpty())
Toshihiro Shimizu 890ddd
		menu.addAction(&fitSelectedAction);
Toshihiro Shimizu 890ddd
	menu.addAction(&fitAllAction);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// curve shape type
Toshihiro Shimizu 890ddd
	QAction curveShapeSmoothAction(tr("Smooth"), 0);
Toshihiro Shimizu 890ddd
	QAction curveShapeFrameBasedAction(tr("Frame Based"), 0);
Toshihiro Shimizu 890ddd
	QMenu curveShapeSubmenu(tr("Curve Shape"), 0);
Toshihiro Shimizu 890ddd
	menu.addSeparator();
Toshihiro Shimizu 890ddd
	curveShapeSubmenu.addAction(&curveShapeSmoothAction);
Toshihiro Shimizu 890ddd
	curveShapeSubmenu.addAction(&curveShapeFrameBasedAction);
Toshihiro Shimizu 890ddd
	menu.addMenu(&curveShapeSubmenu);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	curveShapeSmoothAction.setCheckable(true);
Toshihiro Shimizu 890ddd
	curveShapeSmoothAction.setChecked(m_curveShape == SMOOTH);
Toshihiro Shimizu 890ddd
	curveShapeFrameBasedAction.setCheckable(true);
Toshihiro Shimizu 890ddd
	curveShapeFrameBasedAction.setChecked(m_curveShape == FRAME_BASED);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Store m_highlighted due to the following exec()
Toshihiro Shimizu 890ddd
	Highlighted highlighted(m_highlighted);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// execute menu
Toshihiro Shimizu 890ddd
	QAction *action = menu.exec(e->globalPos()); // Will process events, possibly altering m_highlighted (MAC-verified)
Toshihiro Shimizu 890ddd
	if (action == &linkHandlesAction)			 // Let's just *hope* that doesn't happen to m_gadgets though...  :/
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_gadgets[highlighted.gIndex].m_handle != 100)
Toshihiro Shimizu 890ddd
			KeyframeSetter(curve, m_gadgets[highlighted.gIndex].m_kIndex).linkHandles();
Toshihiro Shimizu 890ddd
	} else if (action == &unlinkHandlesAction) {
Toshihiro Shimizu 890ddd
		if (m_gadgets[highlighted.gIndex].m_handle != 100)
Toshihiro Shimizu 890ddd
			KeyframeSetter(curve, m_gadgets[highlighted.gIndex].m_kIndex).unlinkHandles();
Toshihiro Shimizu 890ddd
	} else if (action == &resetHandlesAction) {
Toshihiro Shimizu 890ddd
		kf.m_speedIn = TPointD(-5, 0);
Toshihiro Shimizu 890ddd
		kf.m_speedOut = -kf.m_speedIn;
Toshihiro Shimizu 890ddd
		curve->setKeyframe(kf);
Toshihiro Shimizu 890ddd
	} else if (action == &deleteKeyframeAction) {
Toshihiro Shimizu 890ddd
		KeyframeSetter::removeKeyframeAt(curve, kf.m_frame);
Toshihiro Shimizu 890ddd
	} else if (action == &insertKeyframeAction) {
Toshihiro Shimizu 890ddd
		KeyframeSetter(curve).createKeyframe(tround(frame));
Toshihiro Shimizu 890ddd
	} else if (action == &activateCycleAction) {
Toshihiro Shimizu 890ddd
		KeyframeSetter::enableCycle(curve, true);
Toshihiro Shimizu 890ddd
	} else if (action == &deactivateCycleAction) {
Toshihiro Shimizu 890ddd
		KeyframeSetter::enableCycle(curve, false);
Toshihiro Shimizu 890ddd
	} else if (action == &setLinearAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::Linear);
Toshihiro Shimizu 890ddd
	else if (action == &setSpeedInOutAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::SpeedInOut);
Toshihiro Shimizu 890ddd
	else if (action == &setEaseInOutAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::EaseInOut);
Toshihiro Shimizu 890ddd
	else if (action == &setEaseInOut2Action)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::EaseInOutPercentage);
Toshihiro Shimizu 890ddd
	else if (action == &setExponentialAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::Exponential);
Toshihiro Shimizu 890ddd
	else if (action == &setExpressionAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::Expression);
Toshihiro Shimizu 890ddd
	else if (action == &setSimilarShapeAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::SimilarShape);
Toshihiro Shimizu 890ddd
	else if (action == &setFileAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::File);
Toshihiro Shimizu 890ddd
	else if (action == &setConstantAction)
Toshihiro Shimizu 890ddd
		setSegmentType(getSelection(), curve, segmentIndex, TDoubleKeyframe::Constant);
Toshihiro Shimizu 890ddd
	else if (action == &fitSelectedAction)
Toshihiro Shimizu 890ddd
		fitSelectedPoints();
Toshihiro Shimizu 890ddd
	else if (action == &fitAllAction)
Toshihiro Shimizu 890ddd
		fitCurve();
Toshihiro Shimizu 890ddd
	else if (action == &setStep1Action)
Toshihiro Shimizu 890ddd
		KeyframeSetter(curve, segmentIndex).setStep(1);
Toshihiro Shimizu 890ddd
	else if (action == &setStep2Action)
Toshihiro Shimizu 890ddd
		KeyframeSetter(curve, segmentIndex).setStep(2);
Toshihiro Shimizu 890ddd
	else if (action == &setStep3Action)
Toshihiro Shimizu 890ddd
		KeyframeSetter(curve, segmentIndex).setStep(3);
Toshihiro Shimizu 890ddd
	else if (action == &setStep4Action)
Toshihiro Shimizu 890ddd
		KeyframeSetter(curve, segmentIndex).setStep(4);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	else if (action == &curveShapeSmoothAction)
Toshihiro Shimizu 890ddd
		m_curveShape = SMOOTH;
Toshihiro Shimizu 890ddd
	else if (action == &curveShapeFrameBasedAction)
Toshihiro Shimizu 890ddd
		m_curveShape = FRAME_BASED;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::setFrameHandle(TFrameHandle *frameHandle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_frameHandle == frameHandle)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_frameHandle)
Toshihiro Shimizu 890ddd
		m_frameHandle->disconnect(this);
Toshihiro Shimizu 890ddd
	m_frameHandle = frameHandle;
Toshihiro Shimizu 890ddd
	if (isVisible() && m_frameHandle) {
Toshihiro Shimizu 890ddd
		connect(m_frameHandle, SIGNAL(frameSwitched()), this, SLOT(onFrameSwitched()));
Toshihiro Shimizu 890ddd
		update();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	assert(m_selection);
Toshihiro Shimizu 890ddd
	m_selection->setFrameHandle(frameHandle);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::showEvent(QShowEvent *)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_frameHandle)
Toshihiro Shimizu 890ddd
		connect(m_frameHandle, SIGNAL(frameSwitched()), this, SLOT(onFrameSwitched()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::hideEvent(QHideEvent *)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_frameHandle)
Toshihiro Shimizu 890ddd
		m_frameHandle->disconnect(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FunctionPanel::onFrameSwitched()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}