fix(strokecanvasitem): use qreal instead of float to suppress -Wdouble-promotion
Python / lint-format (pull_request) Successful in 26s
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 38s
Python / test (pull_request) Successful in 52s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m38s
C++ / build (pull_request) Successful in 2m26s
Python / lint-format (pull_request) Successful in 26s
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 38s
Python / test (pull_request) Successful in 52s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m38s
C++ / build (pull_request) Successful in 2m26s
This commit is contained in:
@@ -6,55 +6,52 @@
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
QCanvasPainterItemRenderer *StrokeCanvasItem::createItemRenderer() const {
|
QCanvasPainterItemRenderer* StrokeCanvasItem::createItemRenderer() const {
|
||||||
return new StrokeCanvasRenderer;
|
return new StrokeCanvasRenderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
const QPointF &p0 = points[qMax(i - 1, 0)];
|
const QPointF& p0 = points[qMax(i - 1, 0)];
|
||||||
const QPointF &p1 = points[i];
|
const QPointF& p1 = points[i];
|
||||||
const QPointF &p2 = points[i + 1];
|
const QPointF& p2 = points[i + 1];
|
||||||
const QPointF &p3 = points[qMin(i + 2, points.size() - 1)];
|
const QPointF& p3 = points[qMin(i + 2, points.size() - 1)];
|
||||||
|
|
||||||
QPointF cp1, cp2;
|
QPointF cp1, cp2;
|
||||||
catmullToBezier(p0, p1, p2, p3, tension, cp1, cp2);
|
catmullToBezier(p0, p1, p2, p3, tension, cp1, cp2);
|
||||||
@@ -64,9 +61,8 @@ static QCanvasPath buildStrokePath(const QVector<QPointF> &points, float width)
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
@@ -84,9 +79,8 @@ void StrokeCanvasItem::setHoverVisible(bool visible) {
|
|||||||
emit hoverVisibleChanged();
|
emit hoverVisibleChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
@@ -120,8 +113,7 @@ void StrokeCanvasItem::hideHover() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void StrokeCanvasItem::setPenWidth(float width) {
|
void StrokeCanvasItem::setPenWidth(float 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
|
||||||
|
|||||||
Reference in New Issue
Block a user