From f909983982d412fb3ea3b221dd31b23e40db748f Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Tue, 4 Aug 2009 14:13:31 +1000 Subject: Paint Rect smoothly Still some artifacts when painting rounded rects whith smooth = true. --- src/declarative/fx/qfxrect.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/declarative/fx/qfxrect.cpp b/src/declarative/fx/qfxrect.cpp index 0722d26..2237e34 100644 --- a/src/declarative/fx/qfxrect.cpp +++ b/src/declarative/fx/qfxrect.cpp @@ -475,10 +475,17 @@ void QFxRect::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) { Q_D(QFxRect); if (d->radius > 0 || (d->pen && d->pen->isValid()) - || (d->gradient && d->gradient->gradient()) ) + || (d->gradient && d->gradient->gradient()) ) { drawRect(*p); - else + } + else { + bool oldAA = p->testRenderHint(QPainter::Antialiasing); + if (d->smooth) + p->setRenderHints(QPainter::Antialiasing, true); p->fillRect(QRect(0, 0, width(), height()), d->getColor()); + if (d->smooth) + p->setRenderHint(QPainter::Antialiasing, oldAA); + } } void QFxRect::drawRect(QPainter &p) @@ -502,6 +509,11 @@ void QFxRect::drawRect(QPainter &p) p.drawRect(0, 0, width(), height()); p.setRenderHint(QPainter::Antialiasing, oldAA); } else { + bool oldAA = p.testRenderHint(QPainter::Antialiasing); + bool oldSmooth = p.testRenderHint(QPainter::SmoothPixmapTransform); + if (d->smooth) + p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth); + int offset = 0; const int pw = d->pen && d->pen->isValid() ? (d->pen->width()+1)/2*2 : 0; @@ -569,6 +581,11 @@ void QFxRect::drawRect(QPainter &p) // Lower Right p.drawPixmap(QPoint(width()-xOffset+pw/2, height() - yOffset+pw/2), d->rectImage, QRect(d->rectImage.width()-xOffset, d->rectImage.height() - yOffset, xOffset, yOffset)); + + if (d->smooth) { + p.setRenderHint(QPainter::Antialiasing, oldAA); + p.setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth); + } } } -- cgit v0.12 From c77a478f5dad857074a79cf1b6fd2a76972bce1a Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Tue, 4 Aug 2009 14:18:34 +1000 Subject: Example of smooth rect painting --- examples/declarative/smooth/GradientRect.qml | 25 +++++++++++ examples/declarative/smooth/MyRect.qml | 21 +++++++++ examples/declarative/smooth/rect-painting.qml | 64 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 examples/declarative/smooth/GradientRect.qml create mode 100644 examples/declarative/smooth/MyRect.qml create mode 100644 examples/declarative/smooth/rect-painting.qml diff --git a/examples/declarative/smooth/GradientRect.qml b/examples/declarative/smooth/GradientRect.qml new file mode 100644 index 0000000..267a487 --- /dev/null +++ b/examples/declarative/smooth/GradientRect.qml @@ -0,0 +1,25 @@ +import Qt 4.6 + +Item { + id: MyRect + property string color + property string border : "" + property int rotation + property int radius + property int borderWidth + property bool smooth: false + + width: 80; height: 80 + Item { + anchors.centerIn: parent; rotation: MyRect.rotation; + Rect { + anchors.centerIn: parent; width: 80; height: 80 + border.color: MyRect.border; border.width: MyRect.border != "" ? 2 : 0 + radius: MyRect.radius; smooth: MyRect.smooth + gradient: Gradient { + GradientStop { position: 0.0; color: MyRect.color } + GradientStop { position: 1.0; color: "white" } + } + } + } +} diff --git a/examples/declarative/smooth/MyRect.qml b/examples/declarative/smooth/MyRect.qml new file mode 100644 index 0000000..7791e27 --- /dev/null +++ b/examples/declarative/smooth/MyRect.qml @@ -0,0 +1,21 @@ +import Qt 4.6 + +Item { + id: MyRect + property string color + property string border : "" + property int rotation + property int radius + property int borderWidth + property bool smooth: false + + width: 80; height: 80 + Item { + anchors.centerIn: parent; rotation: MyRect.rotation; + Rect { + anchors.centerIn: parent; width: 80; height: 80 + color: MyRect.color; border.color: MyRect.border; border.width: MyRect.border != "" ? 2 : 0 + radius: MyRect.radius; smooth: MyRect.smooth + } + } +} diff --git a/examples/declarative/smooth/rect-painting.qml b/examples/declarative/smooth/rect-painting.qml new file mode 100644 index 0000000..2f01e4b --- /dev/null +++ b/examples/declarative/smooth/rect-painting.qml @@ -0,0 +1,64 @@ +import Qt 4.6 + +Rect { + width: 900; height: 500 + color: "white" + + Rect { + anchors.top: parent.verticalCenter + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + color: "#eeeeee" + } + + GridLayout { + anchors.centerIn: parent + columns: 8; rows:4; spacing: 30 + + MyRect { color: "lightsteelblue" } + MyRect { color: "lightsteelblue"; border: "gray" } + MyRect { color: "lightsteelblue"; radius: 10 } + MyRect { color: "lightsteelblue"; radius: 10; border: "gray" } + GradientRect { color: "lightsteelblue" } + GradientRect { color: "lightsteelblue"; border: "gray" } + GradientRect { color: "lightsteelblue"; radius: 10 } + GradientRect { color: "lightsteelblue"; radius: 10; border: "gray" } + + MyRect { color: "thistle"; rotation: 10 } + MyRect { color: "thistle"; border: "gray"; rotation: 10 } + MyRect { color: "thistle"; radius: 10; rotation: 10 } + MyRect { color: "thistle"; radius: 10; border: "gray"; rotation: 10 } + GradientRect { color: "thistle"; rotation: 10 } + GradientRect { color: "thistle"; border: "gray"; rotation: 10 } + GradientRect { color: "thistle"; radius: 10; rotation: 10 } + GradientRect { color: "thistle"; radius: 10; border: "gray"; rotation: 10 } + + MyRect { color: "lightsteelblue"; smooth: true } + MyRect { color: "lightsteelblue"; border: "gray"; smooth: true } + MyRect { color: "lightsteelblue"; radius: 10; smooth: true } + MyRect { color: "lightsteelblue"; radius: 10; border: "gray"; smooth: true } + GradientRect { color: "lightsteelblue"; smooth: true } + GradientRect { color: "lightsteelblue"; border: "gray"; smooth: true } + GradientRect { color: "lightsteelblue"; radius: 10; smooth: true } + GradientRect { color: "lightsteelblue"; radius: 10; border: "gray"; smooth: true } + + MyRect { color: "thistle"; rotation: 10; smooth: true } + MyRect { color: "thistle"; border: "gray"; rotation: 10; smooth: true } + MyRect { color: "thistle"; radius: 10; rotation: 10; smooth: true } + MyRect { color: "thistle"; radius: 10; border: "gray"; rotation: 10; smooth: true } + GradientRect { color: "thistle"; rotation: 10; smooth: true } + GradientRect { color: "thistle"; border: "gray"; rotation: 10; smooth: true } + GradientRect { color: "thistle"; radius: 10; rotation: 10; smooth: true } + GradientRect { color: "thistle"; radius: 10; border: "gray"; rotation: 10; smooth: true } + } + + Text { + text: "smooth: false"; font.bold: true + anchors.horizontalCenter: parent.horizontalCenter; anchors.top: parent.top + } + Text { + text: "smooth: true"; font.bold: true + anchors.horizontalCenter: parent.horizontalCenter; anchors.bottom: parent.bottom + } +} -- cgit v0.12 From 8b4d6c9c173fdd3ec50d73bf23e2cef434e8cb1d Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Tue, 4 Aug 2009 15:15:23 +1000 Subject: Do not use antialiasing with gradient rounded rect if smooth = false --- src/declarative/fx/qfxrect.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/declarative/fx/qfxrect.cpp b/src/declarative/fx/qfxrect.cpp index 2237e34..29321b8 100644 --- a/src/declarative/fx/qfxrect.cpp +++ b/src/declarative/fx/qfxrect.cpp @@ -495,7 +495,8 @@ void QFxRect::drawRect(QPainter &p) // XXX This path is still slower than the image path // Image path won't work for gradients though bool oldAA = p.testRenderHint(QPainter::Antialiasing); - p.setRenderHint(QPainter::Antialiasing); + if (d->smooth) + p.setRenderHint(QPainter::Antialiasing); if (d->pen && d->pen->isValid()) { QPen pn(QColor(d->pen->color()), d->pen->width()); p.setPen(pn); @@ -507,7 +508,8 @@ void QFxRect::drawRect(QPainter &p) p.drawRoundedRect(0, 0, width(), height(), d->radius, d->radius); else p.drawRect(0, 0, width(), height()); - p.setRenderHint(QPainter::Antialiasing, oldAA); + if (d->smooth) + p.setRenderHint(QPainter::Antialiasing, oldAA); } else { bool oldAA = p.testRenderHint(QPainter::Antialiasing); bool oldSmooth = p.testRenderHint(QPainter::SmoothPixmapTransform); -- cgit v0.12