paysages : Fixed curve editor display range and selection.

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@371 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-07-03 09:15:15 +00:00 committed by ThunderK
parent b15b6c7d80
commit b80c703f36
2 changed files with 39 additions and 19 deletions

View file

@ -11,6 +11,11 @@ WidgetCurveEditor::WidgetCurveEditor(QWidget *parent, double xmin, double xmax,
_dragged = -1; _dragged = -1;
_pen = QColor(0, 0, 0); _pen = QColor(0, 0, 0);
_xmin = xmin;
_xmax = xmax;
_ymin = ymin;
_ymax = ymax;
setMinimumSize(300, 300); setMinimumSize(300, 300);
} }
@ -87,11 +92,11 @@ void WidgetCurveEditor::paintEvent(QPaintEvent* event)
painter.setPen(_pen); painter.setPen(_pen);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
position = ((double)x) / dwidth; position = ((double)x / dwidth) * (_xmax - _xmin) + _xmin;
value = curveGetValue(_curve, position); value = (curveGetValue(_curve, position) - _ymin) / (_ymax - _ymin);
prev_value = curveGetValue(_curve, position - 1.0 / dwidth); prev_value = curveGetValue(_curve, position - (_xmax - _xmin) / dwidth);
next_value = curveGetValue(_curve, position + 1.0 / dwidth); next_value = curveGetValue(_curve, position + (_xmax - _xmin) / 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)); painter.drawLine(x, height - 1 - (int)((value + (prev_value - value) / 2.0) * dheight), x, height - 1 - (int)((value + (next_value - value) / 2.0) * dheight));
painter.drawPoint(x, height - 1 - (int)(value * dheight)); painter.drawPoint(x, height - 1 - (int)(value * dheight));
@ -103,7 +108,7 @@ void WidgetCurveEditor::paintEvent(QPaintEvent* event)
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
curveGetPoint(_curve, i, &point); curveGetPoint(_curve, i, &point);
painter.drawEllipse(QPointF((int)(point.position * dwidth), height - 1 - (int)(point.value * dheight)), 4.0, 4.0); painter.drawEllipse(QPointF((int)((point.position - _xmin) / (_xmax - _xmin) * dwidth), height - 1 - (int)((point.value - _ymin) / (_ymax - _ymin) * dheight)), 4.0, 4.0);
} }
} }
@ -123,13 +128,12 @@ void WidgetCurveEditor::mouseMoveEvent(QMouseEvent* event)
if (_dragged >= 0 && (event->buttons() & Qt::LeftButton)) if (_dragged >= 0 && (event->buttons() & Qt::LeftButton))
{ {
point.position = ((double)event->x()) / (double)(width() - 1); screenToCurve(event->x(), event->y(), &point.position, &point.value);
point.value = 1.0 - ((double)event->y()) / (double)(height() - 1);
point.position = (point.position < 0.0) ? 0.0 : point.position; point.position = (point.position < _xmin) ? _xmin : point.position;
point.position = (point.position > 1.0) ? 1.0 : point.position; point.position = (point.position > _xmax) ? _xmax : point.position;
point.value = (point.value < 0.0) ? 0.0 : point.value; point.value = (point.value < _ymin) ? _ymin : point.value;
point.value = (point.value > 1.0) ? 1.0 : point.value; point.value = (point.value > _ymax) ? _ymax : point.value;
curveSetPoint(_curve, _dragged, &point); curveSetPoint(_curve, _dragged, &point);
@ -173,9 +177,7 @@ void WidgetCurveEditor::mouseDoubleClickEvent(QMouseEvent* event)
{ {
if (getPointAt(event->x(), event->y()) < 0) if (getPointAt(event->x(), event->y()) < 0)
{ {
point.position = ((double)event->x()) / (double)(width() - 1); screenToCurve(event->x(), event->y(), &point.position, &point.value);
point.value = 1.0 - ((double)event->y()) / (double)(height() - 1);
curveAddPoint(_curve, &point); curveAddPoint(_curve, &point);
curveValidate(_curve); curveValidate(_curve);
update(); update();
@ -184,15 +186,26 @@ void WidgetCurveEditor::mouseDoubleClickEvent(QMouseEvent* event)
} }
} }
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;
}
int WidgetCurveEditor::getPointAt(int x, int y) int WidgetCurveEditor::getPointAt(int x, int y)
{ {
int n; int n;
int nearest; int nearest;
double distance, ndistance; double distance, ndistance;
CurvePoint point; CurvePoint point;
double dx = ((double)x) / (double)(width() - 1); int dx, dy;
double dy = 1.0 - ((double)y) / (double)(height() - 1);
n = curveGetPointCount(_curve); n = curveGetPointCount(_curve);
if (n < 1) if (n < 1)
{ {
@ -205,7 +218,8 @@ int WidgetCurveEditor::getPointAt(int x, int y)
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
curveGetPoint(_curve, i, &point); curveGetPoint(_curve, i, &point);
ndistance = toolsGetDistance2D(point.position, point.value, dx, dy); curveToScreen(point.position, point.value, &dx, &dy);
ndistance = toolsGetDistance2D((double)x, (double)y, (double)dx, (double)dy);
if (nearest < 0 || ndistance < distance) if (nearest < 0 || ndistance < distance)
{ {
distance = ndistance; distance = ndistance;
@ -213,7 +227,7 @@ int WidgetCurveEditor::getPointAt(int x, int y)
} }
} }
if (nearest >= 0 && distance < 0.015) if (nearest >= 0 && distance < 5.0)
{ {
return nearest; return nearest;
} }

View file

@ -31,8 +31,14 @@ protected:
void mouseDoubleClickEvent(QMouseEvent* event); void mouseDoubleClickEvent(QMouseEvent* event);
private: private:
void curveToScreen(double curve_x, double curve_y, int* screen_x, int* screen_y);
void screenToCurve(int screen_x, int screen_y, double* curve_x, double* curve_y);
int getPointAt(int x, int y); int getPointAt(int x, int y);
double _xmin;
double _xmax;
double _ymin;
double _ymax;
Curve* _curve; Curve* _curve;
int _dragged; int _dragged;
QColor _pen; QColor _pen;