1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2001 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef kwordwrap_h
8#define kwordwrap_h
9
10#include <kguiaddons_export.h>
11
12#include <QSharedDataPointer>
13#include <qnamespace.h>
14
15class QFontMetrics;
16class QRect;
17class QString;
18class QPainter;
19class KWordWrapPrivate;
20
21/**
22 * @class KWordWrap kwordwrap.h KWordWrap
23 *
24 * Word-wrap algorithm that takes into account beautifulness ;)
25 *
26 * That means:
27 * @li not letting a letter alone on the last line,
28 * @li breaking at punctuation signs (not only at spaces)
29 * @li improved handling of (), [] and {}
30 * @li improved handling of '/' (e.g. for paths)
31 *
32 * Usage: call the static method, formatText, with the text to
33 * wrap and the constraining rectangle etc., it will return an instance of KWordWrap
34 * containing internal data, result of the word-wrapping.
35 * From that instance you can retrieve the boundingRect, and invoke drawing.
36 *
37 * This design allows to call the word-wrap algorithm only when the text changes
38 * and not every time we want to know the bounding rect or draw the text.
39 *
40 * @author David Faure <faure@kde.org>
41 */
42class KGUIADDONS_EXPORT KWordWrap
43{
44public:
45 /**
46 * Use this flag in drawText() if you want to fade out the text if it does
47 * not fit into the constraining rectangle.
48 */
49 enum {
50 FadeOut = 0x10000000,
51 Truncate = 0x20000000,
52 };
53
54 /**
55 * Main method for wrapping text.
56 *
57 * @param fm Font metrics, for the chosen font. Better cache it, creating a QFontMetrics is expensive.
58 * @param r Constraining rectangle. Only the width and height matter. With
59 * negative height the complete text will be rendered
60 * @param flags currently unused
61 * @param str The text to be wrapped.
62 * @param len Length of text to wrap (default is -1 for all).
63 * @return a KWordWrap instance. The caller is responsible for storing and deleting the result.
64 */
65 static KWordWrap formatText(QFontMetrics &fm, const QRect &r, int flags, const QString &str, int len = -1);
66
67 /**
68 * @return the bounding rect, calculated by formatText. The width is the
69 * width of the widest text line, and never wider than
70 * the rectangle given to formatText. The height is the
71 * text block. X and Y are always 0.
72 */
73 QRect boundingRect() const;
74
75 /**
76 * @return the original string, with '\n' inserted where
77 * the text is broken by the wordwrap algorithm.
78 */
79 QString wrappedString() const; // gift for Dirk :)
80
81 /**
82 * @return the original string, truncated to the first line.
83 * If @p dots was set, '...' is appended in case the string was truncated.
84 * Bug: Note that the '...' come out of the bounding rect.
85 */
86 QString truncatedString(bool dots = true) const;
87
88 /**
89 * Draw the text that has been previously wrapped, at position x,y.
90 * Flags are for alignment, e.g. Qt::AlignHCenter. Default is
91 * Qt::AlignAuto.
92 * @param painter the QPainter to use.
93 * @param x the horizontal position of the text
94 * @param y the vertical position of the text
95 * @param flags the ORed text alignment flags from the Qt namespace,
96 * ORed with FadeOut if you want the text to fade out if it
97 * does not fit (the @p painter's background must be set
98 * accordingly)
99 */
100 void drawText(QPainter *painter, int x, int y, int flags = Qt::AlignLeft) const;
101
102 /**
103 * Destructor.
104 */
105 ~KWordWrap();
106
107 /**
108 * Copy constructor
109 */
110 KWordWrap(const KWordWrap &other);
111 /**
112 * Assignment operator
113 */
114 KWordWrap &operator=(const KWordWrap &other);
115
116 /**
117 * Draws the string @p t at the given coordinates, if it does not
118 * @p fit into @p maxW the text will be faded out.
119 * @param p the painter to use. Must have set the pen for the text
120 * color and the background for the color to fade out
121 * @param x the horizontal position of the text
122 * @param y the vertical position of the text
123 * @param maxW the maximum width of the text (including the fade-out
124 * effect)
125 * @param t the text to draw
126 */
127 static void drawFadeoutText(QPainter *p, int x, int y, int maxW, const QString &t);
128
129 /**
130 * Draws the string @p t at the given coordinates, if it does not
131 * @p fit into @p maxW the text will be truncated.
132 * @param p the painter to use
133 * @param x the horizontal position of the text
134 * @param y the vertical position of the text
135 * @param maxW the maximum width of the text (including the '...')
136 * @param t the text to draw
137 */
138 static void drawTruncateText(QPainter *p, int x, int y, int maxW, const QString &t);
139
140private:
141 KGUIADDONS_NO_EXPORT explicit KWordWrap(const QRect &r);
142
143 QExplicitlySharedDataPointer<KWordWrapPrivate> d;
144};
145
146#endif
147

source code of kguiaddons/src/text/kwordwrap.h