summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Bache-Wiig <jbache@trolltech.com>2010-09-19 13:29:18 +0200
committerJens Bache-Wiig <jbache@trolltech.com>2010-09-19 13:29:18 +0200
commit4775aea557293693cd6aab52193cf7a66e1ff5e4 (patch)
tree047870e8e6258550c5a7ae693fe0ea31cf122cb0
parent2c75fcdebe986ba1c193d319b3eb47cf34261b1e (diff)
Add basic text support
-rw-r--r--examples/painting/Drawing.qml6
-rw-r--r--src/context2d.cpp40
-rw-r--r--src/context2d.h7
3 files changed, 52 insertions, 1 deletions
diff --git a/examples/painting/Drawing.qml b/examples/painting/Drawing.qml
index f0f39a2..6c8b82f 100644
--- a/examples/painting/Drawing.qml
+++ b/examples/painting/Drawing.qml
@@ -38,7 +38,11 @@ Canvas {
function drawPoint() {
ctx.lineWidth = lineWidth
ctx.fillStyle = drawColor
- ctx.fillRect(mousearea.mouseX, mousearea.mouseY, 2, 2);
+ ctx.shadowOffsetX = 1;
+ ctx.shadowOffsetY = 1;
+ ctx.shadowBlur = 64;
+ ctx.shadowColor = "black";
+ ctx.fillRect(mousearea.mouseX, mousearea.mouseY, 20, 20);
}
function clear() {
diff --git a/src/context2d.cpp b/src/context2d.cpp
index 69cee66..6bea78b 100644
--- a/src/context2d.cpp
+++ b/src/context2d.cpp
@@ -474,6 +474,32 @@ void Context2D::setShadowColor(const QString &str)
m_state.flags |= DirtyShadowColor;
}
+void Context2D::setFont(const QString &fontString)
+{
+ QFont font;
+ // ### this is simplified and incomplete
+ QStringList tokens = fontString.split(" ");
+ foreach (const QString &token, tokens) {
+ if (token == "italic")
+ font.setItalic(true);
+ else if (token == "bold")
+ font.setBold(true);
+ else if (token.endsWith("px")) {
+ QString number = token;
+ number.remove("px");
+ font.setPointSizeF(number.trimmed().toFloat());
+ } else
+ font.setFamily(token);
+ }
+ m_state.font = font;
+ m_state.flags |= DirtyFont;
+}
+
+QString Context2D::font()
+{
+ return m_state.font.toString();
+}
+
qreal Context2D::shadowOffsetX() const
{
return m_state.shadowOffsetX;
@@ -520,6 +546,20 @@ void Context2D::fillRect(qreal x, qreal y, qreal w, qreal h)
scheduleChange();
}
+void Context2D::fillText(const QString &text, qreal x, qreal y)
+{
+ beginPainting();
+ m_painter.save();
+ m_painter.setPen(QPen(m_state.fillStyle,0));
+ m_painter.setMatrix(m_state.matrix, false);
+ QFont font;
+ font.setBold(true);
+ m_painter.setFont(m_state.font);
+ m_painter.drawText(x, y, text);
+ m_painter.restore();
+ scheduleChange();
+}
+
void Context2D::strokeRect(qreal x, qreal y, qreal w, qreal h)
{
QPainterPath path;
diff --git a/src/context2d.h b/src/context2d.h
index 5a8cdaf..dfc3c10 100644
--- a/src/context2d.h
+++ b/src/context2d.h
@@ -121,6 +121,8 @@ class Context2D : public QObject
Q_PROPERTY(qreal shadowOffsetY READ shadowOffsetY WRITE setShadowOffsetY)
Q_PROPERTY(qreal shadowBlur READ shadowBlur WRITE setShadowBlur)
Q_PROPERTY(QString shadowColor READ shadowColor WRITE setShadowColor)
+ // fonts
+ Q_PROPERTY(QString font READ font WRITE setFont)
public:
Context2D(QObject *parent = 0);
@@ -158,6 +160,9 @@ public:
void setLineJoin(const QString &s);
void setMiterLimit(qreal m);
+ void setFont(const QString &font);
+ QString font();
+
// shadows
qreal shadowOffsetX() const; // (default 0)
qreal shadowOffsetY() const; // (default 0)
@@ -173,6 +178,8 @@ public slots:
void save(); // push state on state stack
void restore(); // pop state stack and restore state
+ void fillText(const QString &text, qreal x, qreal y);
+
void setInPaint(bool val){m_inPaint = val;}
void scale(qreal x, qreal y);
void rotate(qreal angle);