KWordWrap

KDE 4.9 PyKDE API Reference

- KDE's Python API

# KWordWrap Class Reference

`from PyKDE4.kdeui import *`

## 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>

## Enumerations
<anonymous> | { FadeOut, Truncate }
## Methods
| __init__ (self, KWordWrap a0)
| __init__ (self, QRect r)
QRect | boundingRect (self)
| drawText (self, QPainter painter, int x, int y, int flags=Qt.AlignLeft)
QString | truncatedString (self, bool dots=1)
QString | wrappedString (self)
## Static Methods
| drawFadeoutText (QPainter p, int x, int y, int maxW, QString t)
| drawTruncateText (QPainter p, int x, int y, int maxW, QString t)
KWordWrap | formatText (QFontMetrics fm, QRect r, int flags, QString str, int len=-1)

## Method Documentation

__init__ | ( | self, |
|  | KWordWrap | a0
| ) |  |  |

__init__ | ( | self, |
|  | QRect | r
| ) |  |  |

QRect boundingRect | ( |  | self ) |

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.

drawText | ( | self, |
|  | QPainter | painter,
|  | int | x,
|  | int | y,
|  | int | flags=Qt.AlignLeft
| ) |  |  |

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)

QString truncatedString | ( | self, |
|  | bool | dots=1
| ) |  |  |

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.

QString wrappedString | ( |  | self ) |

Returns:
  the original string, with '\n' inserted where the text is broken by the wordwrap algorithm.

## Static Method Documentation

drawFadeoutText | ( | QPainter | p,
|  | int | x,
|  | int | y,
|  | int | maxW,
|  | QString | t
| ) |  |  |

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

drawTruncateText | ( | QPainter | p,
|  | int | x,
|  | int | y,
|  | int | maxW,
|  | QString | t
| ) |  |  |

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

KWordWrap formatText | ( | QFontMetrics | fm,
|  | QRect | r,
|  | int | flags,
|  | QString | str,
|  | int | len=-1
| ) |  |  |

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.

## Enumeration Documentation

anonymous

Use this flag in drawText() if you want to fade out the text if it does not fit into the constraining rectangle.

Enumerator:

  FadeOut |
  Truncate |