2012-03-08 15:10:25 +00:00
|
|
|
#include "widgetcurveeditor.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2012-03-29 20:19:50 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include "../lib_paysages/tools.h"
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
WidgetCurveEditor::WidgetCurveEditor(QWidget *parent, float xmin, float xmax, float ymin, float ymax) : QWidget(parent)
|
2012-03-08 15:10:25 +00:00
|
|
|
{
|
|
|
|
_curve = curveCreate();
|
2012-03-29 20:19:50 +00:00
|
|
|
_dragged = -1;
|
2012-03-31 11:32:03 +00:00
|
|
|
_pen = QColor(0, 0, 0);
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-04-14 13:05:03 +00:00
|
|
|
setMinimumSize(300, 300);
|
2012-03-08 15:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WidgetCurveEditor::~WidgetCurveEditor()
|
|
|
|
{
|
|
|
|
curveDelete(_curve);
|
|
|
|
}
|
|
|
|
|
2012-03-08 15:56:44 +00:00
|
|
|
void WidgetCurveEditor::setCurve(Curve* curve)
|
|
|
|
{
|
|
|
|
curveCopy(curve, _curve);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetCurveEditor::getCurve(Curve* curve)
|
|
|
|
{
|
|
|
|
curveCopy(_curve, curve);
|
|
|
|
}
|
|
|
|
|
2012-03-31 11:32:03 +00:00
|
|
|
void WidgetCurveEditor::setPenColor(QColor color)
|
|
|
|
{
|
|
|
|
_pen = color;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void WidgetCurveEditor::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
2012-03-08 15:56:44 +00:00
|
|
|
int i, n;
|
2012-04-14 13:05:03 +00:00
|
|
|
int width, height;
|
2012-06-15 09:31:11 +00:00
|
|
|
float dwidth, dheight;
|
2012-03-08 15:56:44 +00:00
|
|
|
CurvePoint point;
|
2012-06-15 09:31:11 +00:00
|
|
|
float position, value, prev_value, next_value;
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-04-14 13:05:03 +00:00
|
|
|
width = this->width();
|
|
|
|
height = this->height();
|
2012-06-15 09:31:11 +00:00
|
|
|
dheight = (float)(height - 1);
|
|
|
|
dwidth = (float)(width - 1);
|
2012-04-14 13:05:03 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
QPainter painter(this);
|
2012-04-14 13:05:03 +00:00
|
|
|
painter.fillRect(0, 0, width, height, QColor(255, 255, 255));
|
2012-03-31 11:32:03 +00:00
|
|
|
painter.setPen(_pen);
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-04-14 13:05:03 +00:00
|
|
|
for (int x = 0; x < width; x++)
|
2012-03-08 15:10:25 +00:00
|
|
|
{
|
2012-06-15 09:31:11 +00:00
|
|
|
position = ((float)x) / dwidth;
|
2012-06-09 09:49:23 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
value = curveGetValue(_curve, position);
|
2012-06-09 09:49:23 +00:00
|
|
|
prev_value = curveGetValue(_curve, position - 1.0 / dwidth);
|
|
|
|
next_value = curveGetValue(_curve, position + 1.0 / dwidth);
|
|
|
|
|
|
|
|
painter.drawLine(x, height - 1 - (int)((value + (prev_value - value) / 2.0) * dheight), x, height - 1 - (int)((value + (next_value - value) / 2.0) * dheight));
|
2012-04-14 13:05:03 +00:00
|
|
|
painter.drawPoint(x, height - 1 - (int)(value * dheight));
|
2012-03-08 15:10:25 +00:00
|
|
|
}
|
2012-03-08 15:56:44 +00:00
|
|
|
|
|
|
|
n = curveGetPointCount(_curve);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
curveGetPoint(_curve, i, &point);
|
2012-04-14 13:05:03 +00:00
|
|
|
painter.drawEllipse(QPointF((int)(point.position * dwidth), height - 1 - (int)(point.value * dheight)), 4.0, 4.0);
|
2012-03-08 15:56:44 +00:00
|
|
|
}
|
2012-03-08 15:10:25 +00:00
|
|
|
}
|
2012-03-29 20:19:50 +00:00
|
|
|
|
|
|
|
void WidgetCurveEditor::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
2012-03-31 11:32:03 +00:00
|
|
|
if (event->button() == Qt::LeftButton && _dragged < 0)
|
2012-03-29 20:19:50 +00:00
|
|
|
{
|
2012-03-31 11:32:03 +00:00
|
|
|
_dragged = getPointAt(event->x(), event->y());
|
2012-03-29 20:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetCurveEditor::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
CurvePoint point;
|
|
|
|
|
2012-03-31 11:32:03 +00:00
|
|
|
if (_dragged >= 0 && (event->buttons() & Qt::LeftButton))
|
2012-03-29 20:19:50 +00:00
|
|
|
{
|
2012-06-15 09:31:11 +00:00
|
|
|
point.position = ((float)event->x()) / (float)(width() - 1);
|
|
|
|
point.value = 1.0 - ((float)event->y()) / (float)(height() - 1);
|
2012-03-29 20:19:50 +00:00
|
|
|
|
|
|
|
point.position = (point.position < 0.0) ? 0.0 : point.position;
|
|
|
|
point.position = (point.position > 1.0) ? 1.0 : point.position;
|
|
|
|
point.value = (point.value < 0.0) ? 0.0 : point.value;
|
|
|
|
point.value = (point.value > 1.0) ? 1.0 : point.value;
|
|
|
|
|
|
|
|
curveSetPoint(_curve, _dragged, &point);
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
emit liveChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetCurveEditor::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
2012-03-31 11:32:03 +00:00
|
|
|
int clicked;
|
|
|
|
|
|
|
|
if (event->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
clicked = getPointAt(event->x(), event->y());
|
|
|
|
if (clicked >= 0)
|
|
|
|
{
|
|
|
|
curveRemovePoint(_curve, clicked);
|
|
|
|
update();
|
|
|
|
emit liveChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event->button() == Qt::LeftButton && _dragged >= 0)
|
2012-03-29 20:19:50 +00:00
|
|
|
{
|
|
|
|
_dragged = -1;
|
|
|
|
curveValidate(_curve);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
2012-03-31 11:32:03 +00:00
|
|
|
|
|
|
|
void WidgetCurveEditor::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
CurvePoint point;
|
|
|
|
|
|
|
|
if (event->button() == Qt::LeftButton && _dragged < 0)
|
|
|
|
{
|
|
|
|
if (getPointAt(event->x(), event->y()) < 0)
|
|
|
|
{
|
2012-06-15 09:31:11 +00:00
|
|
|
point.position = ((float)event->x()) / (float)(width() - 1);
|
|
|
|
point.value = 1.0 - ((float)event->y()) / (float)(height() - 1);
|
2012-03-31 11:32:03 +00:00
|
|
|
|
|
|
|
curveAddPoint(_curve, &point);
|
|
|
|
curveValidate(_curve);
|
|
|
|
update();
|
|
|
|
emit liveChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int WidgetCurveEditor::getPointAt(int x, int y)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int nearest;
|
2012-06-15 09:31:11 +00:00
|
|
|
float distance, ndistance;
|
2012-03-31 11:32:03 +00:00
|
|
|
CurvePoint point;
|
2012-06-15 09:31:11 +00:00
|
|
|
float dx = ((float)x) / (float)(width() - 1);
|
|
|
|
float dy = 1.0 - ((float)y) / (float)(height() - 1);
|
2012-03-31 11:32:03 +00:00
|
|
|
|
|
|
|
n = curveGetPointCount(_curve);
|
|
|
|
if (n < 1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find nearest point
|
|
|
|
nearest = -1;
|
2012-04-14 15:52:16 +00:00
|
|
|
distance = 0.0;
|
2012-03-31 11:32:03 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
curveGetPoint(_curve, i, &point);
|
|
|
|
ndistance = toolsGetDistance2D(point.position, point.value, dx, dy);
|
|
|
|
if (nearest < 0 || ndistance < distance)
|
|
|
|
{
|
|
|
|
distance = ndistance;
|
|
|
|
nearest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nearest >= 0 && distance < 0.015)
|
|
|
|
{
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|