- // TODO: cleanup
- m_spline.fromString(spline.toString());
-}
-
-void BezierSplineWidget::paintEvent(QPaintEvent* event)
-{
- Q_UNUSED(event);
-
- QPainter p(this);
-
- p.fillRect(rect(), palette().background());
-
- int wWidth = width() - 1;
- int wHeight = height() - 1;
-
- /*
- * Spline
- */
- double prevY = wHeight - m_spline.value(0.) * wHeight;
- double prevX = 0.;
- double curY;
- double normalizedX = -1;
- int x;
-
- p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
- for (x = 0 ; x < wWidth ; ++x) {
- normalizedX = x / (double)wWidth;
- curY = wHeight - m_spline.value(normalizedX, true) * wHeight;
-
- /*
- * Keep in mind that QLineF rounds doubles
- * to ints mathematically, not just rounds down
- * like in C
- */
- p.drawLine(QLineF(prevX, prevY,
- x, curY));
- prevX = x;
- prevY = curY;
- }
- p.drawLine(QLineF(prevX, prevY ,
- x, wHeight - m_spline.value(1.0, true) * wHeight));
-
- /*
- * Points + Handles
- */
- p.setPen(QPen(Qt::red, 1, Qt::SolidLine));
- BPoint point;
- QPolygon handle(4);
- handle.setPoints(4,
- 1, -2,
- 4, 1,
- 1, 4,
- -2, 1);
- for (int i = 0; i < m_spline.points().count(); ++i) {
- point = m_spline.points().at(i);
- if (i == m_currentPointIndex)
- p.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
-
- p.drawConvexPolygon(handle.translated(point.h1.x() * wWidth, wHeight - point.h1.y() * wHeight));
- p.drawEllipse(QRectF(point.p.x() * wWidth - 3,
- wHeight - 3 - point.p.y() * wHeight, 6, 6));
- p.drawConvexPolygon(handle.translated(point.h2.x() * wWidth, wHeight - point.h2.y() * wHeight));
-
- if ( i == m_currentPointIndex)
- p.setBrush(QBrush(Qt::NoBrush));
- }
-}
-
-void BezierSplineWidget::resizeEvent(QResizeEvent* event)
-{
- m_spline.setPrecision(width());
- QWidget::resizeEvent(event);
-}
-
-void BezierSplineWidget::mousePressEvent(QMouseEvent* event)
-{
- double x = event->pos().x() / (double)(width() - 1);
- double y = 1.0 - event->pos().y() / (double)(height() - 1);
-
- point_types selectedPoint;
- int closestPointIndex = nearestPointInRange(QPointF(x, y), width(), height(), &selectedPoint);
-
- if (event->button() == Qt::RightButton && closestPointIndex > 0 && closestPointIndex < m_spline.points().count() - 1 && selectedPoint == PTypeP) {
- m_spline.removePoint(closestPointIndex);
- setCursor(Qt::ArrowCursor);
- m_mode = ModeNormal;
- if (closestPointIndex < m_currentPointIndex)
- --m_currentPointIndex;
- update();
- emit modified();
- return;
- } else if (event->button() != Qt::LeftButton) {
- return;
- }
-
- if (closestPointIndex < 0) {
- BPoint po;
- po.p = QPointF(x, y);
- po.h1 = QPointF(x-0.05, y-0.05);
- po.h2 = QPointF(x+0.05, y+0.05);
- m_currentPointIndex = m_spline.addPoint(po);
- m_currentPointType = PTypeP;
- /*if (!d->jumpOverExistingPoints(newPoint, -1)) return;*/