• Skip to content
  • Skip to link menu
KDE 4.6 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 + QLatin1String( "\n" );
00116   }
00117 
00118   maxLength -= indent.length(); // take into account indent
00119   QString result;
00120   while ( !wrappedText.isEmpty() )
00121   {
00122     // first check for the next newline. if it's before maxLength, break there, and continue
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     // Find the next point in the wrappedText where we have to do a line break. Start searching
00130     // at maxLength position and then walk backwards looking for a space
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         // Couldn't break before maxLength.
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     // Strip leading whitespace of new lines, since that looks strange
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 }

KPIMTextedit Library

Skip menu "KPIMTextedit Library"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • 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.3
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