• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

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   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;
00116   }
00117 
00118   if ( maxLength <= indent.length() ) {
00119     kWarning() << "indent was set to a string that is longer or the same length as maxLength, setting maxLength to indent.length() + 1";
00120     maxLength = indent.length() + 1;
00121   }
00122 
00123   maxLength -= indent.length(); // take into account indent
00124   QString result;
00125   while ( !wrappedText.isEmpty() )
00126   {
00127     // first check for the next newline. if it's before maxLength, break there, and continue
00128     int newLine = wrappedText.indexOf( QLatin1Char( '\n' ) );
00129     if( newLine > 0 && newLine <= maxLength ) {
00130       result += indent + wrappedText.left( newLine + 1  );
00131       wrappedText = wrappedText.mid( newLine + 1 );
00132       continue;
00133     }
00134     // Find the next point in the wrappedText where we have to do a line break. Start searching
00135     // at maxLength position and then walk backwards looking for a space
00136     int breakPosition;
00137     if ( wrappedText.length() > maxLength )
00138     {
00139       breakPosition = maxLength;
00140       while( ( breakPosition >= 0 ) && ( wrappedText[breakPosition] != QLatin1Char( ' ' ) ) )
00141         breakPosition--;
00142       if ( breakPosition <= 0 ) {
00143         // Couldn't break before maxLength.
00144         breakPosition = maxLength;
00145       }
00146     }
00147     else {
00148       breakPosition = wrappedText.length();
00149     }
00150     
00151     QString line = wrappedText.left( breakPosition );
00152     if ( breakPosition < wrappedText.length() )
00153       wrappedText = wrappedText.mid( breakPosition );
00154     else
00155       wrappedText.clear();
00156     
00157     // Strip leading whitespace of new lines, since that looks strange
00158       if ( !result.isEmpty() && line.startsWith( QLatin1Char( ' ' ) ) )
00159         line = line.mid( 1 );
00160 
00161       result += indent + line + QLatin1Char( '\n' );
00162   }
00163 
00164   return result.left( result.length() - 1 );
00165 }

KPIMTextedit Library

Skip menu "KPIMTextedit Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal