| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# KWordWrap Class Reference

Word-wrap algorithm that takes into account beautifulness ;). More...

`#include <kwordwrap.h>`

Collaboration diagram for KWordWrap:

[legend]
List of all members.

## Public Types
enum | { FadeOut = 0x10000000, Truncate = 0x20000000 }
## Public Member Functions
QRect | boundingRect () const
QString | wrappedString () const
QString | truncatedString (bool dots=true) const
void | drawText (QPainter *painter, int x, int y, int flags=Qt::AlignAuto) const
| ~KWordWrap ()
## Static Public Member Functions
static KWordWrap * | formatText (QFontMetrics &fm, const QRect &r, int flags, const QString &str, int len=-1)
static void | drawFadeoutText (QPainter *p, int x, int y, int maxW, const QString &t)
static void | drawTruncateText (QPainter *p, int x, int y, int maxW, const QString &t)

## Detailed Description

Word-wrap algorithm that takes into account beautifulness ;).

That means:

- not letting a letter alone on the last line,
- breaking at punctuation signs (not only at spaces)
- improved handling of (), [] and {}
- improved handling of '/' (e.g. for paths)

Usage: call the static method, formatText, with the text to wrap and the constraining rectangle etc., it will return an instance of KWordWrap containing internal data, result of the word-wrapping. From that instance you can retrieve the boundingRect, and invoke drawing.

This design allows to call the word-wrap algorithm only when the text changes and not every time we want to know the bounding rect or draw the text.

Author:
  David Faure <faure@kde.org>

Definition at line 48 of file kwordwrap.h.

## Member Enumeration Documentation

anonymous enum

| Use this flag in drawText() if you want to fade out the text if it does not fit into the constraining rectangle. Since: 3.2 Definition at line 56 of file kwordwrap.h.

## Constructor & Destructor Documentation

KWordWrap::~KWordWrap | ( |  | ) |

| Destructor. Definition at line 144 of file kwordwrap.cpp.

## Member Function Documentation

KWordWrap * KWordWrap::formatText | ( | QFontMetrics & | fm,
|  | const QRect & | r,
|  | int | flags,
|  | const QString & | str,
|  | int | len = `-1`
| ) | `[static]`

  | Main method for wrapping text. Parameters:
  | fm | Font metrics, for the chosen font. Better cache it, creating a QFontMetrics is expensive.
  | r | Constraining rectangle. Only the width and height matter. With negative height the complete text will be rendered
  | flags | currently unused
  | str | The text to be wrapped.
  | len | Length of text to wrap (default is -1 for all).
Returns: a KWordWrap instance. The caller is responsible for storing and deleting the result. Definition at line 34 of file kwordwrap.cpp. References QValueList::append(), QChar::isPunct(), QChar::isSpace(), QChar::isSymbol(), QString::left(), QString::length(), m_boundingRect, m_breakPositions, m_lineWidths, m_text, QString::remove(), and QRect::setRect().

QRect KWordWrap::boundingRect | ( |  | ) | const`[inline]`

| Returns: the bounding rect, calculated by formatText. The width is the width of the widest text line, and never wider than the rectangle given to formatText. The height is the text block. X and Y are always 0. Definition at line 77 of file kwordwrap.h.

QString KWordWrap::wrappedString | ( |  | ) | const

| Returns: the original string, with ' ' inserted where the text is broken by the wordwrap algorithm. Definition at line 148 of file kwordwrap.cpp. References QValueList::begin(), QValueList::end(), and QString::mid().

QString KWordWrap::truncatedString | ( | bool | dots = `true` | ) | const

| Returns: the original string, truncated to the first line. If `dots` was set, '...' is appended in case the string was truncated. Bug: Note that the '...' come out of the bounding rect. Definition at line 164 of file kwordwrap.cpp. References QValueList::first(), QValueList::isEmpty(), and QString::left().

void KWordWrap::drawText | ( | QPainter * | painter,
|  | int | x,
|  | int | y,
|  | int | flags = `Qt::AlignAuto`
| ) | const

  | Draw the text that has been previously wrapped, at position x,y. Flags are for alignment, e.g. Qt::AlignHCenter. Default is Qt::AlignAuto. Parameters:
  | painter | the QPainter to use.
  | x | the horizontal position of the text
  | y | the vertical position of the text
  | flags | the ORed text alignment flags from the Qt namespace, ORed with FadeOut if you want the text to fade out if it does not fit (the `painter's` background must be set accordingly)
Definition at line 219 of file kwordwrap.cpp. References QFontMetrics::ascent(), QValueList::begin(), drawFadeoutText(), drawTruncateText(), QValueList::end(), QFontMetrics::height(), QString::mid(), and QRect::width().

void KWordWrap::drawFadeoutText | ( | QPainter * | p,
|  | int | x,
|  | int | y,
|  | int | maxW,
|  | const QString & | t
| ) | `[static]`

  | Draws the string `t` at the given coordinates, if it does not `fit` into `maxW` the text will be faded out. Parameters:
  | p | the painter to use. Must have set the pen for the text color and the background for the color to fade out
  | x | the horizontal position of the text
  | y | the vertical position of the text
  | maxW | the maximum width of the text (including the fade-out effect)
  | t | the text to draw
Since: 3.2 Definition at line 181 of file kwordwrap.cpp. References QFontMetrics::boundingRect(), QFontMetrics::charWidth(), and QFontMetrics::width(). Referenced by drawText().

void KWordWrap::drawTruncateText | ( | QPainter * | p,
|  | int | x,
|  | int | y,
|  | int | maxW,
|  | const QString & | t
| ) | `[static]`

  | Draws the string `t` at the given coordinates, if it does not `fit` into `maxW` the text will be truncated. Parameters:
  | p | the painter to use
  | x | the horizontal position of the text
  | y | the vertical position of the text
  | maxW | the maximum width of the text (including the '...')
  | t | the text to draw
Since: 3.3 Definition at line 213 of file kwordwrap.cpp. References KStringHandler::rPixelSqueeze(). Referenced by drawText().

The documentation for this class was generated from the following files:

- kwordwrap.h
- kwordwrap.cpp

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:22:42 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003