00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "textutils.h"
00024
00025 #include <QtGui/QTextBlock>
00026 #include <QtGui/QTextCharFormat>
00027 #include <QtGui/QTextDocument>
00028 #include <KDE/KDebug>
00029
00030 using namespace KPIMTextEdit;
00031
00032 static bool isCharFormatFormatted( const QTextCharFormat &format, const QFont &defaultFont,
00033 const QTextCharFormat &defaultBlockFormat )
00034 {
00035 if ( !format.anchorHref().isEmpty() ||
00036 format.font() != defaultFont ||
00037 format.isAnchor() ||
00038 format.verticalAlignment() != defaultBlockFormat.verticalAlignment() ||
00039 format.layoutDirection() != defaultBlockFormat.layoutDirection() ||
00040 format.underlineStyle() != defaultBlockFormat.underlineStyle() ||
00041 format.foreground().color() != defaultBlockFormat.foreground().color() ||
00042 format.background().color() != defaultBlockFormat.background().color() )
00043 return true;
00044
00045 return false;
00046 }
00047
00048 static bool isBlockFormatFormatted( const QTextBlockFormat &format,
00049 const QTextBlockFormat &defaultFormat )
00050 {
00051 if ( format.alignment() != defaultFormat.alignment() ||
00052 format.layoutDirection() != defaultFormat.layoutDirection() ||
00053 format.indent() != defaultFormat.indent() ||
00054 format.textIndent() != defaultFormat.textIndent() )
00055 return true;
00056
00057 return false;
00058 }
00059
00061 static bool isSpecial( const QTextFormat &charFormat )
00062 {
00063 return charFormat.isFrameFormat() || charFormat.isImageFormat() ||
00064 charFormat.isListFormat() || charFormat.isTableFormat();
00065 }
00066
00067 bool TextUtils::containsFormatting( const QTextDocument *document )
00068 {
00069 if ( !document )
00070 return false;
00071
00072 QTextDocument defaultTextDocument;
00073 const QTextCharFormat defaultCharFormat = defaultTextDocument.begin().charFormat();
00074 const QTextBlockFormat defaultBlockFormat = defaultTextDocument.begin().blockFormat();
00075 const QFont defaultFont = defaultTextDocument.defaultFont();
00076
00077 QTextBlock block = document->firstBlock();
00078 while ( block.isValid() ) {
00079
00080 if ( isBlockFormatFormatted( block.blockFormat(), defaultBlockFormat ) ) {
00081 return true;
00082 }
00083
00084 if ( isSpecial( block.charFormat() ) || isSpecial( block.blockFormat() ) ||
00085 block.textList() ) {
00086 return true;
00087 }
00088
00089 QTextBlock::iterator it = block.begin();
00090 while ( !it.atEnd() ) {
00091 const QTextFragment fragment = it.fragment();
00092 const QTextCharFormat charFormat = fragment.charFormat();
00093 if ( isSpecial( charFormat ) ) {
00094 return true;
00095 }
00096 if ( isCharFormatFormatted( fragment.charFormat(), defaultFont, defaultCharFormat ) ) {
00097 return true;
00098 }
00099
00100 it++;
00101 }
00102
00103 block = block.next();
00104 }
00105
00106 if ( document->toHtml().contains( QLatin1String( "<hr />" ) ) )
00107 return true;
00108
00109 return false;
00110 }
00111
00112 QString TextUtils::flowText( QString &wrappedText, const QString& indent, int maxLength )
00113 {
00114 if ( wrappedText.isEmpty() ) {
00115 return indent + QLatin1String( "\n" );
00116 }
00117
00118 maxLength -= indent.length();
00119 QString result;
00120 while ( !wrappedText.isEmpty() )
00121 {
00122
00123 int newLine = wrappedText.indexOf( QLatin1Char( '\n' ) );
00124 if( newLine > 0 && newLine < maxLength ) {
00125 result += indent + wrappedText.left( newLine + 1 );
00126 wrappedText = wrappedText.mid( newLine + 1 );
00127 continue;
00128 }
00129
00130
00131 int breakPosition;
00132 if ( wrappedText.length() > maxLength )
00133 {
00134 breakPosition = maxLength;
00135 while( ( breakPosition >= 0 ) && ( wrappedText[breakPosition] != QLatin1Char( ' ' ) ) )
00136 breakPosition--;
00137 if ( breakPosition <= 0 ) {
00138
00139 breakPosition = maxLength;
00140 }
00141 }
00142 else {
00143 breakPosition = wrappedText.length();
00144 }
00145
00146 QString line = wrappedText.left( breakPosition );
00147 if ( breakPosition < wrappedText.length() )
00148 wrappedText = wrappedText.mid( breakPosition );
00149 else
00150 wrappedText.clear();
00151
00152
00153 if ( !result.isEmpty() && line.startsWith( QLatin1Char( ' ' ) ) )
00154 line = line.mid( 1 );
00155
00156 result += indent + line + QLatin1Char( '\n' );
00157 }
00158
00159 return result;
00160 }