From 0148243a77a42de1ba6170a1da3977b3026c1927 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Wed, 13 Feb 2019 14:08:49 +0100 Subject: Canvas: Handle switching between object and string based colors When switching between the two it should respect whatever the color is regardless of whether it is a color value or a string based color. This also accounts for "invalid" colors as this should set the stroke-style to be #000000 to be inline with the default stroke-style indicated in the specification. Change-Id: I00bee6c9a85787762271882838510b4187798ee0 Fixes: QTBUG-42155 Fixes: QTBUG-52959 Reviewed-by: Mitch Curtis --- src/quick/items/context2d/qquickcontext2d.cpp | 9 ++++-- .../qquickcanvasitem/data/tst_strokeStyle.qml | 35 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp index 66727e7845..57e697bdd1 100644 --- a/src/quick/items/context2d/qquickcontext2d.cpp +++ b/src/quick/items/context2d/qquickcontext2d.cpp @@ -1548,7 +1548,7 @@ QV4::ReturnedValue QQuickJSContext2D::method_set_strokeStyle(const QV4::Function if (value->as()) { QColor color = scope.engine->toVariant(value, qMetaTypeId()).value(); if (color.isValid()) { - r->d()->context()->state.fillStyle = color; + r->d()->context()->state.strokeStyle = color; r->d()->context()->buffer()->setStrokeStyle(color); r->d()->context()->m_strokeStyle.set(scope.engine, value); } else { @@ -1559,7 +1559,12 @@ QV4::ReturnedValue QQuickJSContext2D::method_set_strokeStyle(const QV4::Function r->d()->context()->m_strokeStyle.set(scope.engine, value); r->d()->context()->state.strokePatternRepeatX = style->d()->patternRepeatX; r->d()->context()->state.strokePatternRepeatY = style->d()->patternRepeatY; - + } else if (!style && r->d()->context()->state.strokeStyle != QBrush(QColor())) { + // If there is no style object, then ensure that the strokeStyle is at least + // QColor in case it was previously set + r->d()->context()->state.strokeStyle = QBrush(QColor()); + r->d()->context()->buffer()->setStrokeStyle(r->d()->context()->state.strokeStyle); + r->d()->context()->m_strokeStyle.set(scope.engine, value); } } } else if (value->isString()) { diff --git a/tests/auto/quick/qquickcanvasitem/data/tst_strokeStyle.qml b/tests/auto/quick/qquickcanvasitem/data/tst_strokeStyle.qml index 22803a19ce..a3f1ab0a9b 100644 --- a/tests/auto/quick/qquickcanvasitem/data/tst_strokeStyle.qml +++ b/tests/auto/quick/qquickcanvasitem/data/tst_strokeStyle.qml @@ -3,6 +3,8 @@ import QtQuick 2.0 CanvasTestCase { id:testCase name: "strokeStyle" + property color anotherColor: "#0000ff" + property color emptyColor function init_data() { return testData("2d"); } function test_default(row) { var canvas = createCanvasObject(row); @@ -46,4 +48,37 @@ CanvasTestCase { comparePixel(ctx,0,0,255,255,255,255); canvas.destroy() } + function test_colorFromObjectToString(row) { + var canvas = createCanvasObject(row); + var ctx = canvas.getContext('2d'); + + ctx.reset(); + ctx.strokeStyle = anotherColor + ctx.strokeStyle = "red"; + compare(ctx.strokeStyle, "#ff0000"); + + ctx.strokeStyle = anotherColor + ctx.strokeStyle = "black"; + compare(ctx.strokeStyle, "#000000"); + + ctx.strokeStyle = "white"; + ctx.strokeStyle = anotherColor + compare(ctx.strokeStyle, "#0000ff"); + canvas.destroy() + } + function test_withInvalidColor(row) { + var canvas = createCanvasObject(row); + var ctx = canvas.getContext('2d'); + + ctx.reset(); + ctx.strokeStyle = emptyColor + compare(ctx.strokeStyle, "#000000"); + ctx.strokeStyle = "red"; + compare(ctx.strokeStyle, "#ff0000"); + ctx.strokeStyle = emptyColor + compare(ctx.strokeStyle, "#000000"); + ctx.strokeStyle = anotherColor; + compare(ctx.strokeStyle, "#0000ff"); + canvas.destroy() + } } -- cgit v1.2.3