KPIMTextedit Library
textutils.cpp
00001 /* 00002 This file is part of KDE. 00003 00004 Copyright (c) 2009 Thomas McGuire <mcguire@kde.org> 00005 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com> 00006 00007 This library is free software; you can redistribute it and/or modify it 00008 under the terms of the GNU Library General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or (at your 00010 option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, but WITHOUT 00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00015 License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to the 00019 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00020 02110-1301, USA. 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 00046 return false; 00047 } 00048 00049 static bool isBlockFormatFormatted( const QTextBlockFormat &format, 00050 const QTextBlockFormat &defaultFormat ) 00051 { 00052 if ( format.alignment() != defaultFormat.alignment() || 00053 format.layoutDirection() != defaultFormat.layoutDirection() || 00054 format.indent() != defaultFormat.indent() || 00055 format.textIndent() != defaultFormat.textIndent() ) { 00056 return true; 00057 } 00058 00059 return false; 00060 } 00061 00063 static bool isSpecial( const QTextFormat &charFormat ) 00064 { 00065 return charFormat.isFrameFormat() || charFormat.isImageFormat() || 00066 charFormat.isListFormat() || charFormat.isTableFormat(); 00067 } 00068 00069 bool TextUtils::containsFormatting( const QTextDocument *document ) 00070 { 00071 if ( !document ) { 00072 return false; 00073 } 00074 00075 QTextDocument defaultTextDocument; 00076 const QTextCharFormat defaultCharFormat = defaultTextDocument.begin().charFormat(); 00077 const QTextBlockFormat defaultBlockFormat = defaultTextDocument.begin().blockFormat(); 00078 const QFont defaultFont = defaultTextDocument.defaultFont(); 00079 00080 QTextBlock block = document->firstBlock(); 00081 while ( block.isValid() ) { 00082 00083 if ( isBlockFormatFormatted( block.blockFormat(), defaultBlockFormat ) ) { 00084 return true; 00085 } 00086 00087 if ( isSpecial( block.charFormat() ) || isSpecial( block.blockFormat() ) || 00088 block.textList() ) { 00089 return true; 00090 } 00091 00092 QTextBlock::iterator it = block.begin(); 00093 while ( !it.atEnd() ) { 00094 const QTextFragment fragment = it.fragment(); 00095 const QTextCharFormat charFormat = fragment.charFormat(); 00096 if ( isSpecial( charFormat ) ) { 00097 return true; 00098 } 00099 if ( isCharFormatFormatted( fragment.charFormat(), defaultFont, defaultCharFormat ) ) { 00100 return true; 00101 } 00102 00103 it++; 00104 } 00105 00106 block = block.next(); 00107 } 00108 00109 if ( document->toHtml().contains( QLatin1String( "<hr />" ) ) ) { 00110 return true; 00111 } 00112 00113 return false; 00114 } 00115 00116 QString TextUtils::flowText( QString &wrappedText, const QString &indent, int maxLength ) 00117 { 00118 if ( wrappedText.isEmpty() ) { 00119 return indent; 00120 } 00121 00122 if ( maxLength <= indent.length() ) { 00123 kWarning() << "indent was set to a string that is longer or the same length " 00124 << "as maxLength, setting maxLength to indent.length() + 1"; 00125 maxLength = indent.length() + 1; 00126 } 00127 00128 maxLength -= indent.length(); // take into account indent 00129 QString result; 00130 while ( !wrappedText.isEmpty() ) 00131 { 00132 // first check for the next newline. if it's before maxLength, break there, and continue 00133 int newLine = wrappedText.indexOf( QLatin1Char( '\n' ) ); 00134 if( newLine > 0 && newLine <= maxLength ) { 00135 result += indent + wrappedText.left( newLine + 1 ); 00136 wrappedText = wrappedText.mid( newLine + 1 ); 00137 continue; 00138 } 00139 // Find the next point in the wrappedText where we have to do a line break. 00140 // Start searching at maxLength position and then walk backwards looking 00141 // for a space. 00142 int breakPosition; 00143 if ( wrappedText.length() > maxLength ) { 00144 breakPosition = maxLength; 00145 while ( ( breakPosition >= 0 ) && ( wrappedText[breakPosition] != QLatin1Char( ' ' ) ) ) { 00146 breakPosition--; 00147 } 00148 if ( breakPosition <= 0 ) { 00149 // Couldn't break before maxLength. 00150 breakPosition = maxLength; 00151 } 00152 } else { 00153 breakPosition = wrappedText.length(); 00154 } 00155 00156 QString line = wrappedText.left( breakPosition ); 00157 if ( breakPosition < wrappedText.length() ) { 00158 wrappedText = wrappedText.mid( breakPosition ); 00159 } else { 00160 wrappedText.clear(); 00161 } 00162 00163 // Strip leading whitespace of new lines, since that looks strange 00164 if ( !result.isEmpty() && line.startsWith( QLatin1Char( ' ' ) ) ) { 00165 line = line.mid( 1 ); 00166 } 00167 00168 result += indent + line + QLatin1Char( '\n' ); 00169 } 00170 00171 return result.left( result.length() - 1 ); 00172 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:44:16 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:44:16 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.