fix(strokecanvasitem): use qreal instead of float to suppress -Wdouble-promotion
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 16s
Python / lint-format (pull_request) Successful in 23s
Python / test (pull_request) Successful in 48s
C++ / build (pull_request) Successful in 2m20s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m59s

This commit is contained in:
2026-06-30 01:22:40 +02:00
parent 3eecbc16bc
commit 4a18edfc78
5 changed files with 184 additions and 188 deletions
+2 -2
View File
@@ -11,9 +11,9 @@ struct Stroke {
QVector<QPointF> points; QVector<QPointF> points;
QCanvasPath path; QCanvasPath path;
QColor color; QColor color;
float width; qreal width;
int groupId = -1; int groupId = -1;
bool isSinglePoint = false; bool isSinglePoint = false;
}; };
}; }; // namespace ZShell::internal
+30 -38
View File
@@ -6,7 +6,8 @@
namespace ZShell::internal { namespace ZShell::internal {
StrokeCanvasItem::StrokeCanvasItem(QQuickItem *parent) : QCanvasPainterItem(parent) { StrokeCanvasItem::StrokeCanvasItem(QQuickItem* parent)
: QCanvasPainterItem(parent) {
setFillColor(Qt::transparent); setFillColor(Qt::transparent);
setAlphaBlending(true); setAlphaBlending(true);
} }
@@ -16,38 +17,34 @@ QCanvasPainterItemRenderer *StrokeCanvasItem::createItemRenderer() const {
} }
static bool shouldAddPoint( static bool shouldAddPoint(
const QVector<QPointF> &points, const QVector<QPointF>& points, const QPointF& p, qreal minDistance) {
const QPointF &p, if (points.isEmpty()) return true;
qreal minDistance)
{
if (points.isEmpty())
return true;
const QPointF delta = p - points.last(); const QPointF delta = p - points.last();
return QPointF::dotProduct(delta, delta) return QPointF::dotProduct(delta, delta) >= minDistance * minDistance;
>= minDistance * minDistance;
} }
static QCanvasPath buildStrokePath(const QVector<QPointF> &points, float width) { static QCanvasPath buildStrokePath(const QVector<QPointF>& points, qreal width) {
QCanvasPath path; QCanvasPath path;
if (points.size() == 1) { if (points.size() == 1) {
path.circle(points[0], width * 0.5f); path.circle(points[0], width * 0.5);
return path; return path;
} }
auto catmullToBezier = []( auto catmullToBezier = [](const QPointF& p0,
const QPointF &p0, const QPointF &p1, const QPointF& p1,
const QPointF &p2, const QPointF &p3, const QPointF& p2,
float tension, const QPointF& p3,
QPointF &cp1, QPointF &cp2) qreal tension,
{ QPointF& cp1,
cp1 = p1 + (p2 - p0) * tension / 3.0f; QPointF& cp2) {
cp2 = p2 - (p3 - p1) * tension / 3.0f; cp1 = p1 + (p2 - p0) * tension / 3.0;
cp2 = p2 - (p3 - p1) * tension / 3.0;
}; };
const float tension = 0.5f; const qreal tension = 0.5;
path.moveTo(points[0]); path.moveTo(points[0]);
for (int i = 0; i < points.size() - 1; ++i) { for (int i = 0; i < points.size() - 1; ++i) {
@@ -65,8 +62,7 @@ static QCanvasPath buildStrokePath(const QVector<QPointF> &points, float width)
} }
void StrokeCanvasItem::setPenColor(const QColor& color) { void StrokeCanvasItem::setPenColor(const QColor& color) {
if (m_penColor == color) if (m_penColor == color) return;
return;
m_penColor = color; m_penColor = color;
update(); update();
@@ -75,8 +71,7 @@ void StrokeCanvasItem::setPenColor(const QColor &color) {
} }
void StrokeCanvasItem::setHoverVisible(bool visible) { void StrokeCanvasItem::setHoverVisible(bool visible) {
if (m_hoverVisible == visible) if (m_hoverVisible == visible) return;
return;
m_hoverVisible = visible; m_hoverVisible = visible;
update(); update();
@@ -85,8 +80,7 @@ void StrokeCanvasItem::setHoverVisible(bool visible) {
} }
void StrokeCanvasItem::setHoverPoint(const QPointF& point) { void StrokeCanvasItem::setHoverPoint(const QPointF& point) {
if (m_hoverPoint == point) if (m_hoverPoint == point) return;
return;
m_hoverPoint = point; m_hoverPoint = point;
update(); update();
@@ -110,8 +104,7 @@ void StrokeCanvasItem::showHover(qreal x, qreal y) {
} }
void StrokeCanvasItem::hideHover() { void StrokeCanvasItem::hideHover() {
if (!m_hoverVisible) if (!m_hoverVisible) return;
return;
m_hoverVisible = false; m_hoverVisible = false;
update(); update();
@@ -119,9 +112,8 @@ void StrokeCanvasItem::hideHover() {
emit hoverVisibleChanged(); emit hoverVisibleChanged();
} }
void StrokeCanvasItem::setPenWidth(float width) { void StrokeCanvasItem::setPenWidth(qreal width) {
if (qFuzzyCompare(m_penWidth, width)) if (qFuzzyCompare(m_penWidth, width)) return;
return;
m_penWidth = width; m_penWidth = width;
update(); update();
@@ -143,8 +135,7 @@ void StrokeCanvasItem::beginStroke(qreal x, qreal y) {
void StrokeCanvasItem::appendPoint(qreal x, qreal y) { void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
const QPointF incoming{x, y}; const QPointF incoming{x, y};
if (!shouldAddPoint(m_currentStroke.points, incoming, 2.0)) if (!shouldAddPoint(m_currentStroke.points, incoming, 2.0)) return;
return;
QPointF smoothed; QPointF smoothed;
if (m_currentStroke.points.isEmpty()) { if (m_currentStroke.points.isEmpty()) {
@@ -159,7 +150,8 @@ void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
constexpr qreal minAlpha = 0.1; constexpr qreal minAlpha = 0.1;
constexpr qreal maxAlpha = 0.3; constexpr qreal maxAlpha = 0.3;
const qreal t = std::clamp((dist - minDist) / (maxDist - minDist), 0.0, 1.0); const qreal t =
std::clamp((dist - minDist) / (maxDist - minDist), 0.0, 1.0);
const qreal alpha = minAlpha + t * (maxAlpha - minAlpha); const qreal alpha = minAlpha + t * (maxAlpha - minAlpha);
smoothed = last * (1.0 - alpha) + incoming * alpha; smoothed = last * (1.0 - alpha) + incoming * alpha;
@@ -171,11 +163,11 @@ void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
void StrokeCanvasItem::endStroke() { void StrokeCanvasItem::endStroke() {
m_isDrawing = false; m_isDrawing = false;
if (m_currentStroke.points.isEmpty()) if (m_currentStroke.points.isEmpty()) return;
return;
m_currentStroke.isSinglePoint = (m_currentStroke.points.size() == 1); m_currentStroke.isSinglePoint = (m_currentStroke.points.size() == 1);
m_currentStroke.path = buildStrokePath(m_currentStroke.points, m_currentStroke.width); m_currentStroke.path =
buildStrokePath(m_currentStroke.points, m_currentStroke.width);
m_currentStroke.groupId = m_nextGroupId++; m_currentStroke.groupId = m_nextGroupId++;
m_currentStroke.points.clear(); m_currentStroke.points.clear();
m_strokes.append(m_currentStroke); m_strokes.append(m_currentStroke);
@@ -190,4 +182,4 @@ void StrokeCanvasItem::clear() {
update(); update();
} }
}; }; // namespace ZShell::internal
+19 -20
View File
@@ -17,20 +17,22 @@ Q_OBJECT
QML_NAMED_ELEMENT(StrokeCanvas) QML_NAMED_ELEMENT(StrokeCanvas)
Q_PROPERTY(QColor penColor READ penColor WRITE setPenColor NOTIFY penColorChanged) Q_PROPERTY(
Q_PROPERTY(bool hoverVisible READ hoverVisible WRITE setHoverVisible NOTIFY hoverVisibleChanged) QColor penColor READ penColor WRITE setPenColor NOTIFY penColorChanged)
Q_PROPERTY(QPointF hoverPoint READ hoverPoint WRITE setHoverPoint NOTIFY hoverPointChanged) Q_PROPERTY(
Q_PROPERTY(qreal penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged) bool hoverVisible READ hoverVisible WRITE setHoverVisible NOTIFY
hoverVisibleChanged)
Q_PROPERTY(
QPointF hoverPoint READ hoverPoint WRITE setHoverPoint NOTIFY
hoverPointChanged)
Q_PROPERTY(
qreal penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged)
public: public:
explicit StrokeCanvasItem(QQuickItem* parent = nullptr); explicit StrokeCanvasItem(QQuickItem* parent = nullptr);
[[nodiscard]] bool hoverVisible() const { [[nodiscard]] bool hoverVisible() const { return m_hoverVisible; }
return m_hoverVisible; [[nodiscard]] QPointF hoverPoint() const { return m_hoverPoint; }
}
[[nodiscard]] QPointF hoverPoint() const {
return m_hoverPoint;
}
void setHoverVisible(bool visible); void setHoverVisible(bool visible);
void setHoverPoint(const QPointF& point); void setHoverPoint(const QPointF& point);
@@ -38,15 +40,11 @@ void setHoverPoint(const QPointF &point);
Q_INVOKABLE void showHover(qreal x, qreal y); Q_INVOKABLE void showHover(qreal x, qreal y);
Q_INVOKABLE void hideHover(); Q_INVOKABLE void hideHover();
[[nodiscard]] QColor penColor() const { [[nodiscard]] QColor penColor() const { return m_penColor; }
return m_penColor; [[nodiscard]] qreal penWidth() const { return m_penWidth; }
}
[[nodiscard]] float penWidth() const {
return m_penWidth;
}
void setPenColor(const QColor& color); void setPenColor(const QColor& color);
void setPenWidth(float width); void setPenWidth(qreal width);
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();
@@ -61,7 +59,8 @@ void hoverVisibleChanged();
void hoverPointChanged(); void hoverPointChanged();
protected: protected:
[[nodiscard]] QCanvasPainterItemRenderer *createItemRenderer() const override; [[nodiscard]] QCanvasPainterItemRenderer* createItemRenderer()
const override;
private: private:
friend class StrokeCanvasRenderer; friend class StrokeCanvasRenderer;
@@ -69,7 +68,7 @@ friend class StrokeCanvasRenderer;
bool m_hoverVisible = false; bool m_hoverVisible = false;
QPointF m_hoverPoint; QPointF m_hoverPoint;
QColor m_penColor = Qt::white; QColor m_penColor = Qt::white;
float m_penWidth = 4.f; qreal m_penWidth = 4.0;
bool m_isDrawing = false; bool m_isDrawing = false;
int m_nextGroupId = 0; int m_nextGroupId = 0;
@@ -77,4 +76,4 @@ QVector<Stroke> m_strokes;
Stroke m_currentStroke; Stroke m_currentStroke;
}; };
}; }; // namespace ZShell::internal
@@ -8,10 +8,8 @@ static void drawStroke(
QCanvasPainter* painter, QCanvasPainter* painter,
const QVector<QPointF>& points, const QVector<QPointF>& points,
const QColor& color, const QColor& color,
float width) { qreal width) {
if (points.isEmpty()) return;
if (points.isEmpty())
return;
painter->setStrokeStyle(color); painter->setStrokeStyle(color);
painter->setFillStyle(color); painter->setFillStyle(color);
@@ -26,17 +24,18 @@ static void drawStroke(
return; return;
} }
auto catmullToBezier = []( auto catmullToBezier = [](const QPointF& p0,
const QPointF &p0, const QPointF &p1, const QPointF& p1,
const QPointF &p2, const QPointF &p3, const QPointF& p2,
float tension, const QPointF& p3,
QPointF &cp1, QPointF &cp2) qreal tension,
{ QPointF& cp1,
cp1 = p1 + (p2 - p0) * tension / 3.0f; QPointF& cp2) {
cp2 = p2 - (p3 - p1) * tension / 3.0f; cp1 = p1 + (p2 - p0) * tension / 3.0;
cp2 = p2 - (p3 - p1) * tension / 3.0;
}; };
const float tension = 0.5f; // increase toward 1.0 for tighter curves const qreal tension = 0.5; // increase toward 1.0 for tighter curves
painter->beginPath(); painter->beginPath();
painter->moveTo(points[0]); painter->moveTo(points[0]);
@@ -58,11 +57,10 @@ static void drawStroke(
static void drawHoverCursor( static void drawHoverCursor(
QCanvasPainter* painter, QCanvasPainter* painter,
const QPointF& point, const QPointF& point,
float penWidth, qreal penWidth,
QColor penColor, QColor penColor,
bool isDrawing) bool isDrawing) {
{ const qreal radius = penWidth * 0.5;
const float radius = penWidth * 0.5f;
if (isDrawing) { if (isDrawing) {
painter->setFillStyle(penColor); painter->setFillStyle(penColor);
@@ -73,9 +71,9 @@ static void drawHoverCursor(
const float lineWidth = 1.5f; const float lineWidth = 1.5f;
const float crosshairSize = 6.0f; const float crosshairSize = 6.0f;
const bool useDashes = penWidth > 10.0f; const bool useDashes = penWidth > 10.0;
auto drawOutline = [&](const QColor &color, float width) { auto drawOutline = [&](const QColor& color, qreal width) {
painter->setStrokeStyle(color); painter->setStrokeStyle(color);
painter->setLineWidth(width); painter->setLineWidth(width);
painter->setLineCap(QCanvasPainter::LineCap::Round); painter->setLineCap(QCanvasPainter::LineCap::Round);
@@ -90,7 +88,11 @@ static void drawHoverCursor(
float angle = 0.0f; float angle = 0.0f;
for (int i = 0; i < dashCount; ++i) { for (int i = 0; i < dashCount; ++i) {
painter->beginPath(); painter->beginPath();
painter->arc(point, radius, angle, angle + dashAngle, painter->arc(
point,
radius,
angle,
angle + dashAngle,
QCanvasPainter::PathWinding::ClockWise, QCanvasPainter::PathWinding::ClockWise,
QCanvasPainter::PathConnection::NotConnected); QCanvasPainter::PathConnection::NotConnected);
painter->stroke(); painter->stroke();
@@ -103,7 +105,7 @@ static void drawHoverCursor(
} }
}; };
auto drawCrosshair = [&](const QColor &color, float width) { auto drawCrosshair = [&](const QColor& color, qreal width) {
painter->setStrokeStyle(color); painter->setStrokeStyle(color);
painter->setLineWidth(width); painter->setLineWidth(width);
painter->setLineCap(QCanvasPainter::LineCap::Round); painter->setLineCap(QCanvasPainter::LineCap::Round);
@@ -174,10 +176,15 @@ void StrokeCanvasRenderer::paint(QCanvasPainter *painter) {
} }
} }
drawStroke(painter, m_currentStroke.points, m_currentStroke.color, m_currentStroke.width); drawStroke(
painter,
m_currentStroke.points,
m_currentStroke.color,
m_currentStroke.width);
if (m_hoverVisible) if (m_hoverVisible)
drawHoverCursor(painter, m_hoverPoint, m_penWidth, m_penColor, m_isDrawing); drawHoverCursor(
painter, m_hoverPoint, m_penWidth, m_penColor, m_isDrawing);
} }
}; }; // namespace ZShell::internal
@@ -7,14 +7,13 @@
namespace ZShell::internal { namespace ZShell::internal {
class StrokeCanvasRenderer final : public QCanvasPainterItemRenderer { class StrokeCanvasRenderer final : public QCanvasPainterItemRenderer {
public: public:
void synchronizeData(QCanvasPainterItem* item) override; void synchronizeData(QCanvasPainterItem* item) override;
void paint(QCanvasPainter* painter) override; void paint(QCanvasPainter* painter) override;
private: private:
QColor m_penColor; QColor m_penColor;
float m_penWidth = 4.f; qreal m_penWidth = 4.0;
bool m_hoverVisible = false; bool m_hoverVisible = false;
QPointF m_hoverPoint; QPointF m_hoverPoint;
bool m_isDrawing = false; bool m_isDrawing = false;
@@ -22,7 +21,6 @@ bool m_isDrawing = false;
QVector<Stroke> m_strokes; QVector<Stroke> m_strokes;
Stroke m_currentStroke; Stroke m_currentStroke;
QVector<int> m_pendingGroupRemovals; QVector<int> m_pendingGroupRemovals;
}; };
}; }; // namespace ZShell::internal