summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-22 14:23:08 (GMT)
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2010-01-14 12:46:29 (GMT)
commit596d92d6ca25465594e5b852e99d73dda25494fd (patch)
tree0aabae97679cf55c25b9583d79b1f088aa1217b4
parenta25dd3694d752cdf104f0a9064f47f001082edad (diff)
downloadQt-596d92d6ca25465594e5b852e99d73dda25494fd.zip
Qt-596d92d6ca25465594e5b852e99d73dda25494fd.tar.gz
Qt-596d92d6ca25465594e5b852e99d73dda25494fd.tar.bz2
Fix formatting/clipping rectangle in QPainter::drawStaticText()
The rectangle should clip the text vertically and break lines horizontally, to mirror behavior of QPainter::drawText().
-rw-r--r--src/gui/painting/qpainter.cpp18
-rw-r--r--src/gui/text/qstatictext.cpp26
-rw-r--r--src/gui/text/qstatictext.h1
-rw-r--r--src/gui/text/qstatictext_p.h59
4 files changed, 95 insertions, 9 deletions
diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp
index a890d63..00b462b 100644
--- a/src/gui/painting/qpainter.cpp
+++ b/src/gui/painting/qpainter.cpp
@@ -5709,7 +5709,23 @@ void QPainter::drawStaticText(const QPointF &p, const QStaticText &staticText)
const QStaticTextPrivate *staticText_d = QStaticTextPrivate::get(&staticText);
QTextLayout *textLayout = staticText_d->textLayout;
- textLayout->draw(this, p);
+ QSizeF size = staticText_d->size;
+
+ QRectF clipRect = size.isValid() ? QRectF(p, staticText_d->size) : QRectF();
+ QPainterPath oldClipPath;
+ if (clipRect.isValid()) {
+ oldClipPath = clipPath();
+
+ QPainterPath clipPath;
+ clipPath.addRect(clipRect);
+
+ setClipPath(clipPath, Qt::IntersectClip);
+ }
+
+ textLayout->draw(this, p, QVector<QTextLayout::FormatRange>(), clipRect);
+
+ if (clipRect.isValid())
+ setClipPath(oldClipPath);
}
/*!
diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp
index 9ddcf44..44cb799 100644
--- a/src/gui/text/qstatictext.cpp
+++ b/src/gui/text/qstatictext.cpp
@@ -44,11 +44,17 @@
QT_BEGIN_NAMESPACE
-QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &size)
+QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &sz)
: d_ptr(new QStaticTextPrivate)
{
Q_D(QStaticText);
- d->init(text, font, size.isValid() ? size.width() : -1.0);
+ d->init(text, font, sz);
+}
+
+QStaticText::~QStaticText()
+{
+ Q_D(QStaticText);
+ delete d;
}
QStaticTextPrivate::QStaticTextPrivate()
@@ -56,24 +62,32 @@ QStaticTextPrivate::QStaticTextPrivate()
{
}
+QStaticTextPrivate::~QStaticTextPrivate()
+{
+ delete textLayout;
+}
+
QStaticTextPrivate *QStaticTextPrivate::get(const QStaticText *q)
{
return q->d_ptr;
}
-void QStaticTextPrivate::init(const QString &text, const QFont &font, qreal width)
+void QStaticTextPrivate::init(const QString &text, const QFont &font, const QSizeF &sz)
{
Q_ASSERT(textLayout == 0);
+ size = sz;
+
textLayout = new QTextLayout(text, font);
textLayout->setCacheEnabled(true);
QFontMetrics fontMetrics(font);
textLayout->beginLayout();
- int h = width >= 0.0 ? 0 : -fontMetrics.ascent();
+ int h = size.isValid() ? 0 : -fontMetrics.ascent();
+
QTextLine line;
- if ((line = textLayout->createLine()).isValid()) {
- line.setLineWidth(width >= 0.0 ? width : fontMetrics.width(text));
+ while ((line = textLayout->createLine()).isValid()) {
+ line.setLineWidth(size.isValid() ? size.width() : fontMetrics.width(text));
line.setPosition(QPointF(0, h));
h += line.height();
}
diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h
index 1855114..fc10712 100644
--- a/src/gui/text/qstatictext.h
+++ b/src/gui/text/qstatictext.h
@@ -57,6 +57,7 @@ class Q_GUI_EXPORT QStaticText
Q_DECLARE_PRIVATE(QStaticText);
public:
QStaticText(const QString &text, const QFont &font, const QSizeF &maximumSize = QSizeF());
+ ~QStaticText();
private:
QStaticTextPrivate *d_ptr;
diff --git a/src/gui/text/qstatictext_p.h b/src/gui/text/qstatictext_p.h
index d0e0c2a..b2f29f7 100644
--- a/src/gui/text/qstatictext_p.h
+++ b/src/gui/text/qstatictext_p.h
@@ -1,18 +1,73 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the QtGui module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
#ifndef QSTATICTEXT_P_H
#define QSTATICTEXT_P_H
-QT_BEGIN_NAMESPACE
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of internal files. This header file may change from version to version
+// without notice, or even be removed.
+//
+// We mean it.
+//
#include <private/qtextengine_p.h>
+QT_BEGIN_NAMESPACE
+
class QStaticText;
class QStaticTextPrivate
{
public:
QStaticTextPrivate();
- void init(const QString &text, const QFont &font, qreal width);
+ ~QStaticTextPrivate();
+
+ void init(const QString &text, const QFont &font, const QSizeF &size);
QTextLayout *textLayout;
+ QSizeF size;
static QStaticTextPrivate *get(const QStaticText *q);
};