From 11fdbd1b159c13ef6da94a94d32e19bd61361480 Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig Date: Mon, 13 Sep 2010 16:10:24 +0200 Subject: Add color property --- src/canvas.cpp | 33 +++++++++++++++++++++++++++------ src/canvas.h | 9 +++++++++ src/context2d.cpp | 5 +++-- src/context2d.h | 2 +- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/canvas.cpp b/src/canvas.cpp index 0c91929..9be8b67 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -44,10 +44,11 @@ Canvas::Canvas(QDeclarativeItem *parent) : QDeclarativeItem(parent), - m_context(new Context2D(this)), - m_canvasWidth(0), - m_canvasHeight(0), - m_fillMode(Canvas::Stretch) + m_context(new Context2D(this)), + m_canvasWidth(0), + m_canvasHeight(0), + m_fillMode(Canvas::Stretch), + m_color(Qt::white) { setFlag(QGraphicsItem::ItemHasNoContents, false); setCacheMode(QGraphicsItem::DeviceCoordinateCache); @@ -75,7 +76,9 @@ void Canvas::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget if (smooth()) painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, smooth()); - if (width() != m_context->pixmap().width() || height() != m_context->pixmap().height()) { + if (m_context->pixmap().isNull()) { + painter->fillRect(0, 0, width(), height(), m_color); + } else 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()); @@ -158,7 +161,7 @@ void Canvas::requestPaint() void Canvas::geometryChanged(const QRectF &newGeometry, const QRectF &) { if (m_canvasWidth == 0 && m_canvasHeight == 0 - && newGeometry.width() > 0 && newGeometry.height() > 0) { + && newGeometry.width() > 0 && newGeometry.height() > 0) { m_context->setSize(width(), height()); } } @@ -191,7 +194,25 @@ void Canvas::setFillMode(FillMode mode) emit fillModeChanged(); } +QColor Canvas::color() +{ + return m_color; +} + +void Canvas::setColor(const QColor &color) +{ + if (m_color !=color) { + m_color = color; + colorChanged(); + } +} + Canvas::FillMode Canvas::fillMode() const { return m_fillMode; } + +bool Canvas::save(const QString &filename) const +{ + return m_context->pixmap().save(filename); +} diff --git a/src/canvas.h b/src/canvas.h index f3bfd02..149c1a4 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -48,6 +48,7 @@ class Canvas : public QDeclarativeItem Q_OBJECT Q_ENUMS(FillMode) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged); Q_PROPERTY(int canvasWidth READ canvasWidth WRITE setCanvasWidth NOTIFY canvasWidthChanged); Q_PROPERTY(int canvasHeight READ canvasHeight WRITE setCanvasHeight NOTIFY canvasHeightChanged); Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged) @@ -67,6 +68,7 @@ public: void componentComplete(); + public Q_SLOTS: Context2D *getContext(); void requestPaint(); @@ -74,10 +76,16 @@ public Q_SLOTS: FillMode fillMode() const; void setFillMode(FillMode); + QColor color(); + void setColor(const QColor &); + + bool save(const QString& filename) const; + Q_SIGNALS: void fillModeChanged(); void canvasWidthChanged(); void canvasHeightChanged(); + void colorChanged(); void init(); void paint(); @@ -86,6 +94,7 @@ private: int m_canvasWidth; int m_canvasHeight; FillMode m_fillMode; + QColor m_color; }; #endif diff --git a/src/context2d.cpp b/src/context2d.cpp index 7c3c4a0..3f1c2ef 100644 --- a/src/context2d.cpp +++ b/src/context2d.cpp @@ -479,7 +479,8 @@ void Context2D::clearRect(qreal x, qreal y, qreal w, qreal h) m_painter.save(); m_painter.setMatrix(m_state.matrix, false); m_painter.setCompositionMode(QPainter::CompositionMode_Source); - m_painter.fillRect(QRectF(x, y, w, h), QColor(0, 0, 0, 0)); + QColor fillColor = parent()->property("color").value(); + m_painter.fillRect(QRectF(x, y, w, h), fillColor); m_painter.restore(); scheduleChange(); } @@ -678,7 +679,7 @@ void Context2D::beginPainting() if (m_pixmap.width() != m_width || m_pixmap.height() != m_height) { m_pixmap = QPixmap(m_width, m_height); - m_pixmap.fill(Qt::transparent); + m_pixmap.fill(parent()->property("color").value()); } if (!m_painter.isActive()) { diff --git a/src/context2d.h b/src/context2d.h index 4bbaeeb..354943f 100644 --- a/src/context2d.h +++ b/src/context2d.h @@ -131,7 +131,7 @@ public: void clear(); void reset(); - QPixmap pixmap() { return m_pixmap; } + QPixmap pixmap() { endPainting(); return m_pixmap; } // compositing qreal globalAlpha() const; // (default 1.0) -- cgit v1.2.3