qtextlayout.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 #ifndef QTEXTLAYOUT_H
00042 #define QTEXTLAYOUT_H
00043 
00044 #include <QtCore/qstring.h>
00045 #include <QtCore/qnamespace.h>
00046 #include <QtCore/qrect.h>
00047 #include <QtCore/qvector.h>
00048 #include <QtGui/qcolor.h>
00049 #include <QtCore/qobject.h>
00050 #include <QtGui/qevent.h>
00051 #include <QtGui/qtextformat.h>
00052 
00053 QT_BEGIN_HEADER
00054 
00055 QT_BEGIN_NAMESPACE
00056 
00057 QT_MODULE(Gui)
00058 
00059 class QTextEngine;
00060 class QFont;
00061 class QRect;
00062 class QRegion;
00063 class QTextFormat;
00064 class QPalette;
00065 class QPainter;
00066 
00067 class Q_GUI_EXPORT QTextInlineObject
00068 {
00069 public:
00070     QTextInlineObject(int i, QTextEngine *e) : itm(i), eng(e) {}
00071     inline QTextInlineObject() : itm(0), eng(0) {}
00072     inline bool isValid() const { return eng; }
00073 
00074     QRectF rect() const;
00075     qreal width() const;
00076     qreal ascent() const;
00077     qreal descent() const;
00078     qreal height() const;
00079 
00080     Qt::LayoutDirection textDirection() const;
00081 
00082     void setWidth(qreal w);
00083     void setAscent(qreal a);
00084     void setDescent(qreal d);
00085 
00086     int textPosition() const;
00087 
00088     int formatIndex() const;
00089     QTextFormat format() const;
00090 
00091 private:
00092     friend class QTextLayout;
00093     int itm;
00094     QTextEngine *eng;
00095 };
00096 
00097 class QPaintDevice;
00098 class QTextFormat;
00099 class QTextLine;
00100 class QTextBlock;
00101 class QTextOption;
00102 
00103 class Q_GUI_EXPORT QTextLayout
00104 {
00105 public:
00106     // does itemization
00107     QTextLayout();
00108     QTextLayout(const QString& text);
00109     QTextLayout(const QString& text, const QFont &font, QPaintDevice *paintdevice = 0);
00110     QTextLayout(const QTextBlock &b);
00111     ~QTextLayout();
00112 
00113     void setFont(const QFont &f);
00114     QFont font() const;
00115 
00116     void setText(const QString& string);
00117     QString text() const;
00118 
00119     void setTextOption(const QTextOption &option);
00120     QTextOption textOption() const;
00121 
00122     void setPreeditArea(int position, const QString &text);
00123     int preeditAreaPosition() const;
00124     QString preeditAreaText() const;
00125 
00126     struct FormatRange {
00127         int start;
00128         int length;
00129         QTextCharFormat format;
00130     };
00131     void setAdditionalFormats(const QList<FormatRange> &overrides);
00132     QList<FormatRange> additionalFormats() const;
00133     void clearAdditionalFormats();
00134 
00135     void setCacheEnabled(bool enable);
00136     bool cacheEnabled() const;
00137 
00138     void beginLayout();
00139     void endLayout();
00140     void clearLayout();
00141 
00142     QTextLine createLine();
00143 
00144     int lineCount() const;
00145     QTextLine lineAt(int i) const;
00146     QTextLine lineForTextPosition(int pos) const;
00147 
00148     enum CursorMode {
00149         SkipCharacters,
00150         SkipWords
00151     };
00152     bool isValidCursorPosition(int pos) const;
00153     int nextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
00154     int previousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const;
00155 
00156     void draw(QPainter *p, const QPointF &pos, const QVector<FormatRange> &selections = QVector<FormatRange>(),
00157               const QRectF &clip = QRectF()) const;
00158     void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition) const;
00159     void drawCursor(QPainter *p, const QPointF &pos, int cursorPosition, int width) const;
00160 
00161     QPointF position() const;
00162     void setPosition(const QPointF &p);
00163 
00164     QRectF boundingRect() const;
00165 
00166     qreal minimumWidth() const;
00167     qreal maximumWidth() const;
00168 
00169     QTextEngine *engine() const { return d; }
00170     void setFlags(int flags);
00171 private:
00172     QTextLayout(QTextEngine *e) : d(e) {}
00173     Q_DISABLE_COPY(QTextLayout)
00174 
00175     friend class QPainter;
00176     friend class QPSPrinter;
00177     friend class QGraphicsSimpleTextItemPrivate;
00178     friend class QGraphicsSimpleTextItem;
00179     friend void qt_format_text(const QFont &font, const QRectF &_r, int tf, const QTextOption *, const QString& str,
00180                                QRectF *brect, int tabstops, int* tabarray, int tabarraylen,
00181                                QPainter *painter);
00182     QTextEngine *d;
00183 };
00184 
00185 
00186 class Q_GUI_EXPORT QTextLine
00187 {
00188 public:
00189     inline QTextLine() : i(0), eng(0) {}
00190     inline bool isValid() const { return eng; }
00191 
00192     QRectF rect() const;
00193     qreal x() const;
00194     qreal y() const;
00195     qreal width() const;
00196     qreal ascent() const;
00197     qreal descent() const;
00198     qreal height() const;
00199     qreal leading() const;
00200 
00201     void setLeadingIncluded(bool included);
00202     bool leadingIncluded() const;
00203 
00204     qreal naturalTextWidth() const;
00205     qreal horizontalAdvance() const;
00206     QRectF naturalTextRect() const;
00207 
00208     enum Edge {
00209         Leading,
00210         Trailing
00211     };
00212     enum CursorPosition {
00213         CursorBetweenCharacters,
00214         CursorOnCharacter
00215     };
00216 
00217     /* cursorPos gets set to the valid position */
00218     qreal cursorToX(int *cursorPos, Edge edge = Leading) const;
00219     inline qreal cursorToX(int cursorPos, Edge edge = Leading) const { return cursorToX(&cursorPos, edge); }
00220     int xToCursor(qreal x, CursorPosition = CursorBetweenCharacters) const;
00221 
00222     void setLineWidth(qreal width);
00223     void setNumColumns(int columns);
00224     void setNumColumns(int columns, qreal alignmentWidth);
00225 
00226     void setPosition(const QPointF &pos);
00227     QPointF position() const;
00228 
00229     int textStart() const;
00230     int textLength() const;
00231 
00232     int lineNumber() const { return i; }
00233 
00234     void draw(QPainter *p, const QPointF &point, const QTextLayout::FormatRange *selection = 0) const;
00235 
00236 private:
00237     QTextLine(int line, QTextEngine *e) : i(line), eng(e) {}
00238     void layout_helper(int numGlyphs);
00239     friend class QTextLayout;
00240     int i;
00241     QTextEngine *eng;
00242 };
00243 
00244 QT_END_NAMESPACE
00245 
00246 QT_END_HEADER
00247 
00248 #endif // QTEXTLAYOUT_H