aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2019-02-13 14:08:49 +0100
committerAndy Shaw <andy.shaw@qt.io>2019-02-14 06:26:36 +0000
commit0148243a77a42de1ba6170a1da3977b3026c1927 (patch)
tree663b864b4bec3a7a454cf0dce9ca0b888e470f9d
parent68d0377736a541fa29d1aaba6b7aa4a4cf8781ef (diff)
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 <mitch.curtis@qt.io>
-rw-r--r--src/quick/items/context2d/qquickcontext2d.cpp9
-rw-r--r--tests/auto/quick/qquickcanvasitem/data/tst_strokeStyle.qml35
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<Object>()) {
QColor color = scope.engine->toVariant(value, qMetaTypeId<QColor>()).value<QColor>();
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()
+ }
}