23 #include "textutils.h"
26 #include <QTextCharFormat>
27 #include <QTextDocument>
30 using namespace KPIMTextEdit;
32 static bool isCharFormatFormatted(
const QTextCharFormat &format,
const QFont &defaultFont,
33 const QTextCharFormat &defaultBlockFormat )
35 if ( !format.anchorHref().isEmpty() ||
36 format.font() != defaultFont ||
38 format.verticalAlignment() != defaultBlockFormat.verticalAlignment() ||
39 format.layoutDirection() != defaultBlockFormat.layoutDirection() ||
40 format.underlineStyle() != defaultBlockFormat.underlineStyle() ||
41 format.foreground().color() != defaultBlockFormat.foreground().color() ||
42 format.background().color() != defaultBlockFormat.background().color() ) {
49 static bool isBlockFormatFormatted(
const QTextBlockFormat &format,
50 const QTextBlockFormat &defaultFormat )
52 if ( format.alignment() != defaultFormat.alignment() ||
53 format.layoutDirection() != defaultFormat.layoutDirection() ||
54 format.indent() != defaultFormat.indent() ||
55 format.textIndent() != defaultFormat.textIndent() ) {
63 static bool isSpecial(
const QTextFormat &charFormat )
65 return charFormat.isFrameFormat() || charFormat.isImageFormat() ||
66 charFormat.isListFormat() || charFormat.isTableFormat() || charFormat.isTableCellFormat();
75 QTextDocument defaultTextDocument;
76 const QTextCharFormat defaultCharFormat = defaultTextDocument.begin().charFormat();
77 const QTextBlockFormat defaultBlockFormat = defaultTextDocument.begin().blockFormat();
78 const QFont defaultFont = defaultTextDocument.defaultFont();
80 QTextBlock block = document->firstBlock();
81 while ( block.isValid() ) {
83 if ( isBlockFormatFormatted( block.blockFormat(), defaultBlockFormat ) ) {
87 if ( isSpecial( block.charFormat() ) || isSpecial( block.blockFormat() ) ||
92 QTextBlock::iterator it = block.begin();
93 while ( !it.atEnd() ) {
94 const QTextFragment fragment = it.fragment();
95 const QTextCharFormat charFormat = fragment.charFormat();
96 if ( isSpecial( charFormat ) ) {
99 if ( isCharFormatFormatted( fragment.charFormat(), defaultFont, defaultCharFormat ) ) {
106 block = block.next();
109 if ( document->toHtml().contains( QLatin1String(
"<hr />" ) ) ) {
118 if ( wrappedText.isEmpty() ) {
122 if ( maxLength <= indent.length() ) {
123 kWarning() <<
"indent was set to a string that is longer or the same length "
124 <<
"as maxLength, setting maxLength to indent.length() + 1";
125 maxLength = indent.length() + 1;
128 maxLength -= indent.length();
130 while ( !wrappedText.isEmpty() ) {
132 int newLine = wrappedText.indexOf( QLatin1Char(
'\n' ) );
133 if ( newLine > 0 && newLine <= maxLength ) {
134 result += indent + wrappedText.left( newLine + 1 );
135 wrappedText = wrappedText.mid( newLine + 1 );
142 if ( wrappedText.length() > maxLength ) {
143 breakPosition = maxLength;
144 while ( ( breakPosition >= 0 ) && ( wrappedText[breakPosition] != QLatin1Char(
' ' ) ) ) {
147 if ( breakPosition <= 0 ) {
149 breakPosition = maxLength;
152 breakPosition = wrappedText.length();
155 QString line = wrappedText.left( breakPosition );
156 if ( breakPosition < wrappedText.length() ) {
157 wrappedText = wrappedText.mid( breakPosition );
163 if ( !result.isEmpty() && line.startsWith( QLatin1Char(
' ' ) ) ) {
164 line = line.mid( 1 );
167 result += indent + line + QLatin1Char(
'\n' );
170 return result.left( result.length() - 1 );
KPIMTEXTEDIT_EXPORT QString flowText(QString &text, const QString &indent, int maxLength)
Changes the given text so that each line of it fits into the given maximal length.
KPIMTEXTEDIT_EXPORT bool containsFormatting(const QTextDocument *document)
Returns whether the QTextDocument document contains rich text formatting.