diff options
author | Jens Bache-Wiig <jens.bache-wiig@nokia.com> | 2010-09-03 11:02:32 +0200 |
---|---|---|
committer | Jens Bache-Wiig <jens.bache-wiig@nokia.com> | 2010-09-03 11:02:32 +0200 |
commit | 404aafe2f2f82440643be555b5952091177ea791 (patch) | |
tree | d487b409082cd31f233e6ec45dda403d853a6392 | |
parent | 99e845301a6e5a3b560e90835238553c140959ab (diff) |
Add fillmodes as for the Image element
-rw-r--r-- | examples/graph/graph.qml | 3 | ||||
-rw-r--r-- | src/canvas.cpp | 82 | ||||
-rw-r--r-- | src/canvas.h | 1 |
3 files changed, 80 insertions, 6 deletions
diff --git a/examples/graph/graph.qml b/examples/graph/graph.qml index 86cd167..d6ec44b 100644 --- a/examples/graph/graph.qml +++ b/examples/graph/graph.qml @@ -103,7 +103,4 @@ Canvas { ctx.fill(); ctx.restore(); } - Component.onCompleted: {draw()} } - - diff --git a/src/canvas.cpp b/src/canvas.cpp index 05cde71..2900416 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -60,13 +60,88 @@ void Canvas::componentComplete() m_context->setSize(m_canvasWidth, m_canvasHeight); connect(m_context, SIGNAL(changed()), this, SLOT(updateCanvas())); + emit init(); } void Canvas::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { - painter->setRenderHint(QPainter::SmoothPixmapTransform, smooth()); - if (!m_context->pixmap().isNull()) - painter->drawPixmap(0, 0, width(), height(), m_context->pixmap()); + + if (m_context->pixmap().isNull()) + return; + + bool oldAA = painter->testRenderHint(QPainter::Antialiasing); + bool oldSmooth = painter->testRenderHint(QPainter::SmoothPixmapTransform); + if (smooth()) + painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, smooth()); + + + if (width() != m_context->pixmap().width() || height() != m_context->pixmap().height()) { + if (m_fillMode>= Tile) { + if (m_fillMode== Tile) { + painter->drawTiledPixmap(QRectF(0,0,width(),height()), m_context->pixmap()); + } else { + qreal widthScale = width() / qreal(m_context->pixmap().width()); + qreal heightScale = height() / qreal(m_context->pixmap().height()); + + QTransform scale; + if (m_fillMode== TileVertically) { + scale.scale(widthScale, 1.0); + QTransform old = painter->transform(); + painter->setWorldTransform(scale * old); + painter->drawTiledPixmap(QRectF(0,0,m_context->pixmap().width(),height()), m_context->pixmap()); + painter->setWorldTransform(old); + } else { + scale.scale(1.0, heightScale); + QTransform old = painter->transform(); + painter->setWorldTransform(scale * old); + painter->drawTiledPixmap(QRectF(0,0,width(),m_context->pixmap().height()), m_context->pixmap()); + painter->setWorldTransform(old); + } + } + } else { + qreal widthScale = width() / qreal(m_context->pixmap().width()); + qreal heightScale = height() / qreal(m_context->pixmap().height()); + + QTransform scale; + + if (m_fillMode== PreserveAspectFit) { + if (widthScale <= heightScale) { + heightScale = widthScale; + scale.translate(0, (height() - heightScale * m_context->pixmap().height()) / 2); + } else if(heightScale < widthScale) { + widthScale = heightScale; + scale.translate((width() - widthScale * m_context->pixmap().width()) / 2, 0); + } + } else if (m_fillMode== PreserveAspectCrop) { + if (widthScale < heightScale) { + widthScale = heightScale; + scale.translate((width() - widthScale * m_context->pixmap().width()) / 2, 0); + } else if(heightScale < widthScale) { + heightScale = widthScale; + scale.translate(0, (height() - heightScale * m_context->pixmap().height()) / 2); + } + } + if (clip()) { + painter->save(); + painter->setClipRect(boundingRect(), Qt::IntersectClip); + } + scale.scale(widthScale, heightScale); + QTransform old = painter->transform(); + painter->setWorldTransform(scale * old); + painter->drawPixmap(0, 0, m_context->pixmap()); + painter->setWorldTransform(old); + if (clip()) { + painter->restore(); + } + } + } else { + painter->drawPixmap(0, 0, m_context->pixmap()); + } + + if (smooth()) { + painter->setRenderHint(QPainter::Antialiasing, oldAA); + painter->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth); + } } Context2D *Canvas::getContext() @@ -109,6 +184,7 @@ void Canvas::setFillMode(FillMode mode) { if (m_fillMode == mode) return; + m_fillMode = mode; update(); emit fillModeChanged(); diff --git a/src/canvas.h b/src/canvas.h index b492f80..3d5013f 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -78,6 +78,7 @@ Q_SIGNALS: void fillModeChanged(); void canvasWidthChanged(); void canvasHeightChanged(); + void init(); private: Context2D *m_context; |