2012-03-08 15:10:25 +00:00
|
|
|
#include "widgetcurveeditor.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2012-03-29 20:19:50 +00:00
|
|
|
#include <QMouseEvent>
|
2012-06-27 12:49:51 +00:00
|
|
|
#include <qt4/QtGui/qpaintengine.h>
|
2012-03-29 20:19:50 +00:00
|
|
|
#include "../lib_paysages/tools.h"
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
WidgetCurveEditor::WidgetCurveEditor(QWidget *parent, double xmin, double xmax, double ymin, double 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-07-03 09:15:15 +00:00
|
|
|
_xmin = xmin;
|
|
|
|
_xmax = xmax;
|
|
|
|
_ymin = ymin;
|
|
|
|
_ymax = ymax;
|
|
|
|
|
2012-04-14 13:05:03 +00:00
|
|
|
setMinimumSize(300, 300);
|
2012-03-08 15:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WidgetCurveEditor::~WidgetCurveEditor()
|
|
|
|
{
|
|
|
|
curveDelete(_curve);
|
|
|
|
}
|
|
|
|
|
2012-06-27 12:49:51 +00:00
|
|
|
void WidgetCurveEditor::setAxisLabels(QString xlabel, QString ylabel)
|
|
|
|
{
|
|
|
|
_xlabel = xlabel;
|
|
|
|
_ylabel = ylabel;
|
|
|
|
}
|
|
|
|
|
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-17 09:40:40 +00:00
|
|
|
double dwidth, dheight;
|
2012-03-08 15:56:44 +00:00
|
|
|
CurvePoint point;
|
2012-06-17 09:40:40 +00:00
|
|
|
double 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-17 09:40:40 +00:00
|
|
|
dheight = (double)(height - 1);
|
|
|
|
dwidth = (double)(width - 1);
|
2012-04-14 13:05:03 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
QPainter painter(this);
|
2012-06-27 12:49:51 +00:00
|
|
|
painter.fillRect(rect(), Qt::white);
|
|
|
|
|
|
|
|
// Draw grid
|
|
|
|
painter.setPen(QPen(Qt::lightGray));
|
|
|
|
for (int x = 0; x <= 10; x++)
|
|
|
|
{
|
|
|
|
int vx = (x == 10) ? width - 1 : x * width / 10;
|
|
|
|
painter.drawLine(vx, 0, vx, height -1);
|
|
|
|
vx = (x == 10) ? height - 1 : x * height / 10;
|
|
|
|
painter.drawLine(0, vx, width - 1, vx);
|
|
|
|
}
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-06-27 12:49:51 +00:00
|
|
|
// Draw labels
|
|
|
|
painter.setPen(QColor(50, 50, 50));
|
|
|
|
if (not _xlabel.isEmpty())
|
|
|
|
{
|
|
|
|
painter.drawText(rect(), Qt::AlignCenter | Qt::AlignBottom, _xlabel);
|
|
|
|
}
|
|
|
|
if (not _ylabel.isEmpty())
|
|
|
|
{
|
|
|
|
painter.rotate(-90.0);
|
|
|
|
painter.translate(-height, 0.0);
|
|
|
|
painter.drawText(0, 0, height, 20, Qt::AlignCenter | Qt::AlignTop, _ylabel);
|
|
|
|
painter.translate(height, 0.0);
|
|
|
|
painter.rotate(90.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw curve path
|
|
|
|
painter.setPen(_pen);
|
2012-04-14 13:05:03 +00:00
|
|
|
for (int x = 0; x < width; x++)
|
2012-03-08 15:10:25 +00:00
|
|
|
{
|
2012-07-03 09:15:15 +00:00
|
|
|
position = ((double)x / dwidth) * (_xmax - _xmin) + _xmin;
|
2012-06-09 09:49:23 +00:00
|
|
|
|
2012-07-03 09:15:15 +00:00
|
|
|
value = (curveGetValue(_curve, position) - _ymin) / (_ymax - _ymin);
|
|
|
|
prev_value = curveGetValue(_curve, position - (_xmax - _xmin) / dwidth);
|
|
|
|
next_value = curveGetValue(_curve, position + (_xmax - _xmin) / dwidth);
|
2012-06-09 09:49:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2012-06-27 12:49:51 +00:00
|
|
|
// Draw handles
|
2012-03-08 15:56:44 +00:00
|
|
|
n = curveGetPointCount(_curve);
|
2012-06-27 12:49:51 +00:00
|
|
|
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing, true);
|
2012-03-08 15:56:44 +00:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
curveGetPoint(_curve, i, &point);
|
2012-07-03 09:15:15 +00:00
|
|
|
painter.drawEllipse(QPointF((int)((point.position - _xmin) / (_xmax - _xmin) * dwidth), height - 1 - (int)((point.value - _ymin) / (_ymax - _ymin) * 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-07-03 09:15:15 +00:00
|
|
|
screenToCurve(event->x(), event->y(), &point.position, &point.value);
|
2012-03-29 20:19:50 +00:00
|
|
|
|
2012-07-03 09:15:15 +00:00
|
|
|
point.position = (point.position < _xmin) ? _xmin : point.position;
|
|
|
|
point.position = (point.position > _xmax) ? _xmax : point.position;
|
|
|
|
point.value = (point.value < _ymin) ? _ymin : point.value;
|
|
|
|
point.value = (point.value > _ymax) ? _ymax : point.value;
|
2012-03-29 20:19:50 +00:00
|
|
|
|
|
|
|
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-07-03 09:15:15 +00:00
|
|
|
screenToCurve(event->x(), event->y(), &point.position, &point.value);
|
2012-03-31 11:32:03 +00:00
|
|
|
curveAddPoint(_curve, &point);
|
|
|
|
curveValidate(_curve);
|
|
|
|
update();
|
|
|
|
emit liveChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 09:15:15 +00:00
|
|
|
void WidgetCurveEditor::curveToScreen(double curve_x, double curve_y, int* screen_x, int* screen_y)
|
|
|
|
{
|
|
|
|
*screen_x = (int)((curve_x - _xmin) / (_xmax - _xmin) * (double)(width() - 1));
|
|
|
|
*screen_y = (int)((1.0 - (curve_y - _ymin) / (_ymax - _ymin)) * (double)(height() - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetCurveEditor::screenToCurve(int screen_x, int screen_y, double* curve_x, double* curve_y)
|
|
|
|
{
|
|
|
|
*curve_x = ((double)screen_x / (double)(width() - 1)) * (_xmax - _xmin) + _xmin;
|
|
|
|
*curve_y = (1.0 - ((double)screen_y / (double)(height() - 1))) * (_ymax - _ymin) + _ymin;
|
|
|
|
}
|
|
|
|
|
2012-03-31 11:32:03 +00:00
|
|
|
int WidgetCurveEditor::getPointAt(int x, int y)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int nearest;
|
2012-06-17 09:40:40 +00:00
|
|
|
double distance, ndistance;
|
2012-03-31 11:32:03 +00:00
|
|
|
CurvePoint point;
|
2012-07-03 09:15:15 +00:00
|
|
|
int dx, dy;
|
|
|
|
|
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);
|
2012-07-03 09:15:15 +00:00
|
|
|
curveToScreen(point.position, point.value, &dx, &dy);
|
|
|
|
ndistance = toolsGetDistance2D((double)x, (double)y, (double)dx, (double)dy);
|
2012-03-31 11:32:03 +00:00
|
|
|
if (nearest < 0 || ndistance < distance)
|
|
|
|
{
|
|
|
|
distance = ndistance;
|
|
|
|
nearest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 09:15:15 +00:00
|
|
|
if (nearest >= 0 && distance < 5.0)
|
2012-03-31 11:32:03 +00:00
|
|
|
{
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|