preset color buttons rework + curve smoothing
This commit is contained in:
@@ -13,6 +13,20 @@ QCanvasPainterItemRenderer *StrokeCanvasItem::createItemRenderer() const {
|
||||
return new StrokeCanvasRenderer;
|
||||
}
|
||||
|
||||
static bool shouldAddPoint(
|
||||
const QVector<QPointF> &points,
|
||||
const QPointF &p,
|
||||
qreal minDistance)
|
||||
{
|
||||
if (points.isEmpty())
|
||||
return true;
|
||||
|
||||
const QPointF delta = p - points.last();
|
||||
|
||||
return QPointF::dotProduct(delta, delta)
|
||||
>= minDistance * minDistance;
|
||||
}
|
||||
|
||||
void StrokeCanvasItem::setPenColor(const QColor &color) {
|
||||
if (m_penColor == color)
|
||||
return;
|
||||
@@ -44,7 +58,9 @@ void StrokeCanvasItem::beginStroke(qreal x, qreal y) {
|
||||
}
|
||||
|
||||
void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
|
||||
m_currentStroke.points.append({x, y});
|
||||
if (shouldAddPoint(m_currentStroke.points, {x, y}, 2.0))
|
||||
m_currentStroke.points.append({x, y});
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,11 @@ static void drawStroke(
|
||||
painter->beginPath();
|
||||
painter->moveTo(points[0]);
|
||||
|
||||
for (qsizetype i = 1; i < points.size(); ++i)
|
||||
painter->lineTo(points[i]);
|
||||
for (int i = 1; i < points.size() - 1; ++i) {
|
||||
QPointF mid = (points[i] + points [i + 1]) / 2;
|
||||
painter->quadraticCurveTo(points[i], mid);
|
||||
}
|
||||
painter->lineTo(points.last());
|
||||
|
||||
painter->stroke();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user