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

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.4
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