summaryrefslogtreecommitdiffstats
path: root/src/canvas.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/canvas.cpp')
-rw-r--r--src/canvas.cpp68
1 files changed, 59 insertions, 9 deletions
diff --git a/src/canvas.cpp b/src/canvas.cpp
index a657948..05cde71 100644
--- a/src/canvas.cpp
+++ b/src/canvas.cpp
@@ -38,15 +38,40 @@
****************************************************************************/
#include "canvas.h"
+#include "context2d.h"
#include <QPainter>
Canvas::Canvas(QDeclarativeItem *parent)
- : QDeclarativeItem(parent)
+ : QDeclarativeItem(parent),
+ m_context(new Context2D(this)),
+ m_canvasWidth(0),
+ m_canvasHeight(0)
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
- setCacheMode(QGraphicsItem::ItemCoordinateCache);
- setSmooth(true);
+}
+
+
+void Canvas::componentComplete()
+{
+ if (m_canvasWidth == 0 && m_canvasHeight == 0)
+ m_context->setSize(width(), height());
+ else
+ m_context->setSize(m_canvasWidth, m_canvasHeight);
+
+ connect(m_context, SIGNAL(changed()), this, SLOT(updateCanvas()));
+}
+
+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());
+}
+
+Context2D *Canvas::getContext()
+{
+ return m_context;
}
void Canvas::updateCanvas()
@@ -54,12 +79,37 @@ void Canvas::updateCanvas()
update();
}
-void Canvas::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
+void Canvas::geometryChanged(const QRectF &newGeometry, const QRectF &)
+{
+ if (m_canvasWidth == 0 && m_canvasHeight == 0
+ && newGeometry.width() > 0 && newGeometry.height() > 0) {
+ m_context->setSize(width(), height());
+ }
+}
+
+void Canvas::setCanvasWidth(int newWidth)
{
- painter->save();
- painter->setRenderHint(QPainter::Antialiasing, smooth());
- Context2D context(this, painter);
- emit paint(&context);
- painter->restore();
+ if (m_canvasWidth != newWidth) {
+ m_canvasWidth = newWidth;
+ m_context->setSize(m_canvasWidth, m_canvasHeight);
+ emit canvasWidthChanged();
+ }
}
+void Canvas::setCanvasHeight(int newHeight)
+{
+ if (m_canvasHeight != newHeight) {
+ m_canvasHeight = newHeight;
+ m_context->setSize(m_canvasWidth, m_canvasHeight);
+ emit canvasHeightChanged();
+ }
+}
+
+void Canvas::setFillMode(FillMode mode)
+{
+ if (m_fillMode == mode)
+ return;
+ m_fillMode = mode;
+ update();
+ emit fillModeChanged();
+}