23 #include "textutils.h"
25 #include <QtGui/QTextBlock>
26 #include <QtGui/QTextCharFormat>
27 #include <QtGui/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();
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() )
133 int newLine = wrappedText.indexOf( QLatin1Char(
'\n' ) );
134 if( newLine > 0 && newLine <= maxLength ) {
135 result += indent + wrappedText.left( newLine + 1 );
136 wrappedText = wrappedText.mid( newLine + 1 );
143 if ( wrappedText.length() > maxLength ) {
144 breakPosition = maxLength;
145 while ( ( breakPosition >= 0 ) && ( wrappedText[breakPosition] != QLatin1Char(
' ' ) ) ) {
148 if ( breakPosition <= 0 ) {
150 breakPosition = maxLength;
153 breakPosition = wrappedText.length();
156 QString line = wrappedText.left( breakPosition );
157 if ( breakPosition < wrappedText.length() ) {
158 wrappedText = wrappedText.mid( breakPosition );
164 if ( !result.isEmpty() && line.startsWith( QLatin1Char(
' ' ) ) ) {
165 line = line.mid( 1 );
168 result += indent + line + QLatin1Char(
'\n' );
171 return result.left( result.length() - 1 );