diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 465b67ef..718505d5 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -335,6 +335,7 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent, bool enableUndoRedoSta , mZLayerController(new UBZLayerController(this)) , mpLastPolygon(NULL) , mCurrentPolygon(0) + , mTempPolygon(NULL) , mSelectionFrame(0) { UBCoreGraphicsScene::setObjectName("BoardScene"); @@ -561,6 +562,23 @@ bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pres if (newPoints.length() > 1) { drawCurve(newPoints, mPreviousWidth, width); } + + if (interpolator == UBInterpolator::Bezier) { + // Bezier curves aren't drawn all the way to the scenePos (they stop halfway between the previous and + // current scenePos), so we add a line from the last drawn position in the stroke and the + // scenePos, to make the drawing feel more responsive. This line is then deleted if a new segment is + // added to the stroke. (Or it is added to the stroke when we stop drawing) + + if (mTempPolygon) { + removeItem(mTempPolygon); + mTempPolygon = NULL; + } + + QPointF lastDrawnPoint = newPoints.last(); + + mTempPolygon = lineToPolygonItem(QLineF(lastDrawnPoint, scenePos), mPreviousWidth, width); + addItem(mTempPolygon); + } } } else if (currentTool == UBStylusTool::Eraser) @@ -629,6 +647,13 @@ bool UBGraphicsScene::inputDeviceRelease() mDrawWithCompass = false; } else if (mCurrentStroke){ + if (mTempPolygon) { + UBGraphicsPolygonItem * poly = dynamic_cast(mTempPolygon->deepCopy()); + removeItem(mTempPolygon); + mTempPolygon = NULL; + addPolygonItemToCurrentStroke(poly); + } + UBGraphicsStrokesGroup* pStrokes = new UBGraphicsStrokesGroup(); // Remove the strokes that were just drawn here and replace them by a stroke item diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index a62d66e7..ea04ce9b 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -442,6 +442,7 @@ public slots: UBZLayerController *mZLayerController; UBGraphicsPolygonItem* mpLastPolygon; + UBGraphicsPolygonItem* mTempPolygon; bool mDrawWithCompass; UBGraphicsPolygonItem *mCurrentPolygon; diff --git a/src/domain/UBGraphicsStroke.cpp b/src/domain/UBGraphicsStroke.cpp index 40516174..df02796b 100644 --- a/src/domain/UBGraphicsStroke.cpp +++ b/src/domain/UBGraphicsStroke.cpp @@ -70,18 +70,18 @@ QList UBGraphicsStroke::polygons() const */ QList UBGraphicsStroke::addPoint(const QPointF& point, UBInterpolator::InterpolationMethod interpolationMethod) { - int n = mDrawnPoints.size(); + int n = mReceivedPoints.size(); if (n == 0) { + mReceivedPoints << point; mDrawnPoints << point; - mAllPoints << point; return QList(); } if (interpolationMethod == UBInterpolator::NoInterpolation) { - QPointF lastPoint = mDrawnPoints.last(); + QPointF lastPoint = mReceivedPoints.last(); + mReceivedPoints << point; mDrawnPoints << point; - mAllPoints << point; return QList() << lastPoint << point; } @@ -91,23 +91,24 @@ QList UBGraphicsStroke::addPoint(const QPointF& point, UBInterpolator:: // Don't draw segments smaller than a certain length. This can help with performance // (less polygons in a stroke) but mostly with keeping the curve smooth. - qreal MIN_DISTANCE = 3*mAntiScaleRatio; - qreal distance = QLineF(mDrawnPoints.last(), point).length(); + qreal MIN_DISTANCE = 5*mAntiScaleRatio; + qreal distance = QLineF(mReceivedPoints.last(), point).length(); if (distance < MIN_DISTANCE) { - return QList(); + return QList() << mDrawnPoints.last(); } // The first segment is just a straight line to the first midway point if (n == 1) { - QPointF lastPoint = mDrawnPoints[0]; - mDrawnPoints << point; + QPointF lastPoint = mReceivedPoints[0]; + mReceivedPoints << point; + mDrawnPoints << QPointF((lastPoint + point)/2.0); return QList() << lastPoint << ((lastPoint + point)/2.0); } - QPointF p0 = mDrawnPoints[mDrawnPoints.size() - 2]; - QPointF p1 = mDrawnPoints[mDrawnPoints.size() - 1]; + QPointF p0 = mReceivedPoints[mReceivedPoints.size() - 2]; + QPointF p1 = mReceivedPoints[mReceivedPoints.size() - 1]; QPointF p2 = point; UBQuadraticBezier bz; @@ -119,11 +120,15 @@ QList UBGraphicsStroke::addPoint(const QPointF& point, UBInterpolator:: QList newPoints = bz.getPoints(10); + // avoid adding duplicates + if (newPoints.first() == mDrawnPoints.last()) + mDrawnPoints.removeLast(); + foreach(QPointF p, newPoints) { - mAllPoints << p; + mDrawnPoints << p; } - mDrawnPoints << point; + mReceivedPoints << point; return newPoints; } diff --git a/src/domain/UBGraphicsStroke.h b/src/domain/UBGraphicsStroke.h index 5701eda2..e5f6d38e 100644 --- a/src/domain/UBGraphicsStroke.h +++ b/src/domain/UBGraphicsStroke.h @@ -59,7 +59,7 @@ class UBGraphicsStroke QList addPoint(const QPointF& point, UBInterpolator::InterpolationMethod interpolationMethod = UBInterpolator::NoInterpolation); - const QList& points() { return mAllPoints; } + const QList& points() { return mDrawnPoints; } protected: void addPolygon(UBGraphicsPolygonItem* pol); @@ -68,11 +68,11 @@ class UBGraphicsStroke QList mPolygons; - /// Points that were drawn by the user (actually received through input device) - QList mDrawnPoints; + /// Points that were drawn by the user (i.e, actually received through input device) + QList mReceivedPoints; /// All the points (including interpolated) that are used to draw the stroke - QList mAllPoints; + QList mDrawnPoints; qreal mAntiScaleRatio; };