42 #include "private/qdeclarativetext_p.h" 43 #include "private/qdeclarativetext_p_p.h" 49 #include <QTextLayout> 51 #include <QTextDocument> 52 #include <QGraphicsSceneMouseEvent> 54 #include <QAbstractTextDocumentLayout> 94 maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false),
95 cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true),
96 rightToLeftText(false), layoutTextElided(false), naturalWidth(0), doc(0)
152 #ifndef QT_NO_TEXTHTMLPARSER 170 #ifndef QT_NO_TEXTHTMLPARSER 185 if (!requireImplicitWidth) {
192 return mImplicitWidth;
198 if (!q->isComponentComplete()) {
199 updateOnComponentComplete =
true;
203 layoutTextElided =
false;
216 layoutTextElided =
true;
219 emit q->truncatedChanged();
237 if (!q->isComponentComplete()) {
238 updateOnComponentComplete =
true;
242 if (!requireImplicitWidth) {
243 emit q->implicitWidthChanged();
245 if (requireImplicitWidth)
249 invalidateImageCache();
252 if (
text.isEmpty()) {
253 q->setImplicitWidth(0);
254 q->setImplicitHeight(fm.
height());
256 emit q->paintedSizeChanged();
261 int dy = q->height();
267 if (layedOutTextRect.size() != textRect.
size())
268 q->prepareGeometryChange();
269 layedOutTextRect = textRect;
270 size = textRect.
size();
275 doc->setDefaultFont(font);
278 if (rightToLeftText) {
285 option.
setAlignment((Qt::Alignment)
int(horizontalAlignment | vAlign));
287 doc->setDefaultTextOption(option);
288 if (requireImplicitWidth && q->widthValid()) {
289 doc->setTextWidth(-1);
290 naturalWidth = doc->idealWidth();
293 doc->setTextWidth(q->width());
295 doc->setTextWidth(doc->idealWidth());
296 dy -= (int)doc->size().height();
297 QSize dsize = doc->size().toSize();
298 if (dsize != layedOutTextRect.size()) {
299 q->prepareGeometryChange();
302 size =
QSize(
int(doc->idealWidth()),dsize.
height());
306 if (q->heightValid()) {
312 q->setBaselineOffset(fm.
ascent() + yoff);
315 internalWidthUpdate =
true;
316 if (!q->widthValid())
317 q->setImplicitWidth(size.
width());
318 else if (requireImplicitWidth)
319 q->setImplicitWidth(naturalWidth);
320 internalWidthUpdate =
false;
321 q->setImplicitHeight(size.
height());
322 if (paintedSize != size) {
324 emit q->paintedSizeChanged();
340 layout.setCacheEnabled(
true);
343 int visibleCount = 0;
347 lineWidth = q->width();
350 textOption.
setAlignment(Qt::Alignment(q->effectiveHAlign()));
352 layout.setTextOption(textOption);
354 bool elideText =
false;
355 bool truncate =
false;
360 if (requireImplicitWidth && q->widthValid()) {
363 if (layoutTextElided) {
365 elidedText =
layout.text();
376 for (
int i = 0; i <
layout.lineCount(); ++i) {
380 naturalWidth = br.
width();
381 if (layoutTextElided)
382 layout.setText(elidedText);
385 if (maximumLineCountValid) {
389 int linesLeft = maximumLineCount;
390 int visibleTextLength = 0;
391 while (linesLeft > 0) {
401 if (--linesLeft == 0) {
402 if (visibleTextLength <
text.length()) {
405 qreal elideWidth = fm.width(elideChar);
422 if (truncated != truncate) {
423 truncated = truncate;
424 emit q->truncatedChanged();
441 for (
int i = 0; i <
layout.lineCount(); ++i) {
445 if (elideText && i ==
layout.lineCount()-1) {
446 elidePos.setY(height + fm.ascent());
454 if (!q->widthValid())
455 naturalWidth = br.
width();
460 emit q->lineCountChanged();
486 drawTextLayout(&p,
QPointF(-layedOutTextRect.x(),0), drawStyle);
498 painter->
setPen(styleColor);
502 layout.draw(painter, pos);
503 if (!elidePos.isNull())
504 painter->
drawText(pos + elidePos, elideChar);
534 doc->setDefaultTextOption(colorOption);
538 doc->documentLayout()->draw(&p, context);
540 doc->setDefaultTextOption(oldOption);
555 imageCacheDirty =
true;
558 if (q->isComponentComplete())
567 if (!imageCacheDirty)
570 if (
text.isEmpty()) {
580 textImage = textDocumentImage(
false);
582 styledImage = textDocumentImage(
true);
584 textImage = textLayoutImage(
false);
586 styledImage = textLayoutImage(
true);
591 imageCache = drawOutline(textImage, styledImage);
594 imageCache = drawOutline(textImage, styledImage, -1);
597 imageCache = drawOutline(textImage, styledImage, 1);
600 imageCache = textImage;
606 imageCacheDirty =
false;
617 doc->setDocumentMargin(0);
909 return d->sourceFont;
915 if (
d->sourceFont == font)
918 d->sourceFont =
font;
921 if (
d->font.pointSizeF() != -1) {
924 d->font.setPointSizeF(size/2.0);
927 if (oldFont !=
d->font)
962 d->rightToLeftText =
d->doc->toPlainText().isRightToLeft();
964 d->rightToLeftText =
d->text.isRightToLeft();
966 d->determineHorizontalAlignment();
1006 if (
d->color == color)
1010 d->invalidateImageCache();
1050 if (
d->style == style)
1057 d->invalidateImageCache();
1082 return d->styleColor;
1088 if (
d->styleColor == color)
1092 d->invalidateImageCache();
1132 bool forceAlign =
d->hAlignImplicit &&
d->effectiveLayoutMirror;
1133 d->hAlignImplicit =
false;
1141 d->hAlignImplicit =
true;
1150 if (!
d->hAlignImplicit &&
d->effectiveLayoutMirror) {
1151 switch (
d->hAlign) {
1162 return effectiveAlignment;
1168 if (
hAlign != alignment || forceAlign) {
1179 if (hAlignImplicit && q->isComponentComplete()) {
1189 if (q->isComponentComplete()) {
1210 if (
d->vAlign == align)
1244 if (mode ==
d->wrapMode)
1269 return d->lineCount;
1289 return d->truncated;
1310 return d->maximumLineCount;
1317 d->maximumLineCountValid = lines==
INT_MAX ? false :
true;
1318 if (
d->maximumLineCount != lines) {
1319 d->maximumLineCount = lines;
1330 if (
d->truncated !=
false) {
1331 d->truncated =
false;
1402 if (format ==
d->format)
1405 bool wasRich =
d->richText;
1410 d->doc->setText(
d->text);
1449 return d->elideMode;
1455 if (mode ==
d->elideMode)
1458 d->elideMode = mode;
1469 QRect rect =
d->layedOutTextRect;
1471 rect.
adjust(-1, 0, 1, 2);
1476 switch (
d->vAlign) {
1494 if ((!
d->internalWidthUpdate && newGeometry.
width() != oldGeometry.
width())
1522 return d->paintedSize.width();
1537 return d->paintedSize.height();
1556 return d->lineHeight;
1563 if ((
d->lineHeight == lineHeight) || (lineHeight < 0.0))
1589 return d->lineHeightMode;
1595 if (mode ==
d->lineHeightMode)
1598 d->lineHeightMode = mode;
1610 return d->doc ?
d->doc->resourcesLoading() : 0;
1618 if (
d->cacheAllTextAsImage ||
d->style !=
Normal) {
1619 d->checkImageCache();
1620 if (
d->imageCache.isNull())
1630 bool needClip =
clip() && (
d->imageCache.width() >
width() ||
1631 d->imageCache.height() >
height());
1645 bool needClip =
clip() && (
d->layedOutTextRect.width() >
width() ||
1646 d->layedOutTextRect.height() >
height());
1656 d->doc->documentLayout()->draw(p, context);
1659 d->drawTextLayout(p,
QPointF(0, bounds.
y()),
false);
1673 if (
d->updateOnComponentComplete) {
1674 d->updateOnComponentComplete =
false;
1677 d->doc->setText(
d->text);
1678 d->rightToLeftText =
d->doc->toPlainText().isRightToLeft();
1680 d->rightToLeftText =
d->text.isRightToLeft();
1682 d->determineHorizontalAlignment();
1692 if (!
d->richText || !
d->doc ||
d->doc->documentLayout()->anchorAt(event->
pos()).
isEmpty()) {
1693 event->setAccepted(
false);
1694 d->activeLink.clear();
1696 d->activeLink =
d->doc->documentLayout()->anchorAt(event->
pos());
1712 if (
d->richText &&
d->doc &&
d->activeLink ==
d->doc->documentLayout()->anchorAt(event->
pos()))
1715 event->setAccepted(
false);
1723 #include "qdeclarativetext.moc"
The QVariant class acts like a union for the most common Qt data types.
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
The QPainter class performs low-level painting on widgets and other paint devices.
void setColor(const QColor &c)
The QColor class provides colors based on RGB, HSV or CMYK values.
TextFormat textFormat() const
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
qreal y() const
Returns the y-coordinate of the rectangle's top edge.
bool determineHorizontalAlignment()
qreal right() const
Returns the x-coordinate of the rectangle's right edge.
QPixmap textLayoutImage(bool drawStyle)
Returns a painted version of the QDeclarativeTextPrivate::layout QTextLayout.
void setHeight(qreal h)
Sets the height of the rectangle to the given height.
The QFontMetrics class provides font metrics information.
#define QT_END_NAMESPACE
This macro expands to.
static void parse(const QString &string, QTextLayout &layout)
int lineCount() const
Returns the number of lines of this document (if the layout supports this).
void setPosition(const QPointF &pos)
Moves the line to position pos.
int width() const
Returns the width of the pixmap.
QDeclarativeParserStatus ** d
void clear()
Removes all items from the hash.
TextElideMode elideMode() const
QHash< QUrl, QDeclarativePixmap * > m_resources
void colorChanged(const QColor &color)
void setText(const QString &)
Q_GUI_EXPORT bool qt_applefontsmoothing_enabled
LineHeightMode lineHeightMode() const
bool widthValid() const
Returns whether the width property has been set explicitly.
QSize toSize() const
Returns an integer based copy of this size.
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
QRectF boundingRect() const
DEFINE_BOOL_CONFIG_OPTION(enableImageCache, QML_ENABLE_TEXT_IMAGE_CACHE)
void setElideMode(TextElideMode)
virtual bool event(QEvent *)
void setWrapMode(WrapMode w)
QString & replace(int i, int len, QChar after)
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
The QPointF class defines a point in the plane using floating point precision.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
qreal left() const
Returns the x-coordinate of the rectangle's left edge.
The QTextLine class represents a line of text inside a QTextLayout.
void restore()
Restores the current painter state (pops a saved state off the stack).
WrapMode
This enum describes how text is wrapped in a document.
int textLength() const
Returns the length of the text in the line.
QUrl resolvedUrl(const QUrl &)
Resolves the URL src relative to the URL of the containing component.
Q_GUI_EXPORT bool mightBeRichText(const QString &)
Returns true if the string text is likely to be rich text; otherwise returns false.
void styleChanged(TextStyle style)
void lineHeightChanged(qreal lineHeight)
void fontChanged(const QFont &font)
qreal paintedWidth() const
void setLineWidth(qreal width)
Lays out the line with the given width.
void setFlags(Flags flags)
Sets the flags associated with the option to the given flags.
static QRectF setupTextLayout(QTextLayout *layout)
bool testRenderHint(RenderHint hint) const
Returns true if hint is set; otherwise returns false.
WrapMode wrapMode() const
int height() const
Returns the height of the rectangle.
The QUrl class provides a convenient interface for working with URLs.
The QString class provides a Unicode character string.
VAlignment vAlign() const
The QHash class is a template class that provides a hash-table-based dictionary.
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
This event handler, for event event, can be reimplemented to receive mouse press events for this item...
qreal paintedHeight() const
void setTextFormat(TextFormat format)
QPalette palette
the default color that is used for the text, when no color is specified.
void textFormatChanged(TextFormat textFormat)
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
void invalidateImageCache()
Mark the image cache as dirty.
bool isValid() const
Returns true if this text line is valid; otherwise returns false.
The QSizeF class defines the size of a two-dimensional object using floating point precision...
void resetMaximumLineCount()
void save()
Saves the current painter state (pushes the state onto a stack).
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags=0) const
If the string text is wider than width, returns an elided version of the string (i.
qreal x() const
Returns the x-coordinate of this point.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
quint32 acceptedMouseButtons
static QPixmap drawOutline(const QPixmap &source, const QPixmap &styleSource)
Draw styleSource as an outline around source and return the new image.
int ascent() const
Returns the ascent of the font.
void setPlainText(const QString &text)
Replaces the entire contents of the document with the given plain text.
int width() const
Returns the width.
void setAlignment(Qt::Alignment alignment)
Sets the option's text alignment to the specified alignment.
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position...
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint...
#define QT_BEGIN_NAMESPACE
This macro expands to.
QDeclarativeTextPrivate()
The QRectF class defines a rectangle in the plane using floating point precision. ...
const QPixmap & pixmap() const
static bool isEmpty(const char *str)
The QDeclarativeItem class provides the most basic of all visual items in QML.
bool contains(const T &value) const
QTextDocumentWithImageResources(QDeclarativeText *parent)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
void checkImageCache()
Tests if the image cache is dirty, and repaints it if it is.
void linkActivated(const QString &link)
qreal height() const
Returns the height of the rectangle.
QSize size() const
Returns the size of the rectangle.
QPixmap textDocumentImage(bool drawStyle)
Returns a painted version of the QDeclarativeTextPrivate::doc QTextDocument.
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
virtual QVariant loadResource(int type, const QUrl &name)
Loads data of the specified type from the resource with the given name.
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
const_iterator insert(const T &value)
QColor styleColor() const
qreal width() const
Returns the width of the rectangle.
virtual void componentComplete()
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void setHtml(const QString &html)
Replaces the entire contents of the document with the given HTML-formatted text in the html string...
void elideModeChanged(TextElideMode mode)
virtual ~QTextDocumentWithImageResources()
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
This function is called to handle this item's changes in geometry from oldGeometry to newGeometry...
QRect toRect() const
Returns a QRect based on the values of this rectangle.
void mousePressEvent(QGraphicsSceneMouseEvent *event)
void setMaximumLineCount(int lines)
void setLineHeightMode(LineHeightMode)
void setHAlign(HAlignment align)
void setText(const QString &)
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
QPointF position() const
Returns the line's position relative to the text layout's position.
void setRenderHints(RenderHints hints, bool on=true)
Sets the given render hints on the painter if on is true; otherwise clears the render hints...
QTextDocument * textDocument()
QVariant loadResource(int type, const QUrl &name)
Loads data of the specified type from the resource with the given name.
void setStyle(TextStyle style)
int resourcesLoading() const
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
void prepareGeometryChange()
Prepares the item for a geometry change.
bool connectFinished(QObject *, const char *)
The QFont class specifies a font used for drawing text.
int y() const
Returns the y-coordinate of the rectangle's top edge.
void drawTextLayout(QPainter *p, const QPointF &pos, bool drawStyle)
Paints the QDeclarativeTextPrivate::layout QTextLayout into painter at pos.
qreal x() const
Returns the x-coordinate of the rectangle's left edge.
bool requireImplicitWidth
HAlignment effectiveHAlign() const
int x() const
Returns the x-coordinate of the rectangle's left edge.
QObject * parent() const
Returns a pointer to the parent object.
virtual void componentComplete()
The QPoint class defines a point in the plane using integer precision.
void setColor(ColorGroup cg, ColorRole cr, const QColor &color)
Sets the color in the specified color group, used for the given color role, to the specified solid co...
QRectF united(const QRectF &other) const
Returns the bounding rectangle of this rectangle and the given rectangle.
~QDeclarativeTextPrivate()
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
The QAbstractTextDocumentLayout::PaintContext class is a convenience class defining the parameters us...
void setPen(const QColor &color)
Sets the painter's pen to have style Qt::SolidLine, width 0 and the specified color.
static QSet< QUrl > errors
int height() const
Returns the height.
The QRect class defines a rectangle in the plane using integer precision.
bool setHAlign(QDeclarativeText::HAlignment, bool forceAlign=false)
The QTextOption class provides a description of general rich text properties.
The QDeclarativeContext class defines a context within a QML engine.
qreal y() const
Returns the y-coordinate of this point.
The QPixmap class is an off-screen image representation that can be used as a paint device...
QDeclarativeEngine * engine() const
Return the context's QDeclarativeEngine, or 0 if the context has no QDeclarativeEngine or the QDeclar...
virtual qreal implicitWidth() const
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
QPointF pos() const
Returns the mouse cursor position in item coordinates.
void setFont(const QFont &font)
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
int height() const
Returns the height of the pixmap.
static QDeclarativeTextPrivate * get(QDeclarativeText *t)
HAlignment hAlign() const
void setWrapMode(WrapMode wrap)
Sets the option's text wrap mode to the given mode.
The QSize class defines the size of a two-dimensional object using integer point precision.
void setFont(const QFont &f)
Sets the painter's font to the given font.
int resourcesLoading() const
Returns the number of resources (images) that are being loaded asynchronously.
void setVAlign(VAlignment align)
QDeclarativeText(QDeclarativeItem *parent=0)
void setLineHeight(qreal lineHeight)
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
void textChanged(const QString &text)
void maximumLineCountChanged()
void moveTop(int pos)
Moves the rectangle vertically, leaving the rectangle's top edge at the given y coordinate.
void lineHeightModeChanged(LineHeightMode mode)
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
void verticalAlignmentChanged(VAlignment alignment)
QDeclarativeInfo qmlInfo(const QObject *me)
static Qt::LayoutDirection keyboardInputDirection()
Returns the current keyboard input direction.
int height() const
Returns the height of the font.
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
int maximumLineCount() const
void styleColorChanged(const QColor &color)
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
void ensureDoc()
Ensures the QDeclarativeTextPrivate::doc variable is set to a valid text document.
qreal height() const
Returns the line's height.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
This event handler, for event event, can be reimplemented to receive mouse release events for this it...
Q_DECL_CONSTEXPR int qRound(qreal d)
int textStart() const
Returns the start of the line from the beginning of the string passed to the QTextLayout.
void setStyleColor(const QColor &c)
QRectF naturalTextRect() const
Returns the rectangle covered by the line.
QRect setupTextLayout()
Lays out the QDeclarativeTextPrivate::layout QTextLayout in the constraints of the QDeclarativeText...
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
#define forever
This macro is provided for convenience for writing infinite loops.