kwordwrap.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kwordwrap.h"
00020 #include <qpainter.h>
00021
00022 KWordWrap* KWordWrap::formatText( QFontMetrics &fm, const QRect & r, int , const QString & str, int len )
00023 {
00024
00025
00026
00027
00028 KWordWrap* kw = new KWordWrap;
00029 if ( len == -1 )
00030 kw->m_text = str;
00031 else
00032 kw->m_text = str.left( len );
00033 int height = fm.height();
00034 if ( len == -1 )
00035 len = str.length();
00036 int lastBreak = -1;
00037 int lineWidth = 0;
00038 int x = 0;
00039 int y = 0;
00040 int w = r.width();
00041 int textwidth = 0;
00042 for ( int i = 0 ; i < len; ++i )
00043 {
00044 QChar c = str[i];
00045 int ww = fm.charWidth( str, i );
00046
00047 bool isBreakable = ( c.isSpace() || c.isPunct() || c.isSymbol() )
00048 && ( c != '(' && c != '[' && c != '{' );
00049 if ( !isBreakable && i < len-1 ) {
00050 QChar nextc = str[i+1];
00051 isBreakable = ( nextc == '(' || nextc == '[' || nextc == '{' );
00052 }
00053
00054
00055
00056 int breakAt = -1;
00057 if ( x + ww > w && lastBreak != -1 )
00058 breakAt = lastBreak;
00059 if ( x + ww > w - 4 && lastBreak == -1 )
00060 breakAt = i;
00061 if ( i == len - 2 && x + ww + fm.charWidth( str, i+1 ) > w )
00062 breakAt = lastBreak == -1 ? i - 1 : lastBreak;
00063 if ( breakAt != -1 )
00064 {
00065
00066 kw->m_breakPositions.append( breakAt );
00067 int thisLineWidth = lastBreak == -1 ? x + ww : lineWidth;
00068 kw->m_lineWidths.append( thisLineWidth );
00069 textwidth = QMAX( textwidth, thisLineWidth );
00070 x = 0;
00071 y += height;
00072 if ( lastBreak != -1 )
00073 {
00074
00075 i = lastBreak;
00076 lastBreak = -1;
00077 continue;
00078 }
00079 } else if ( isBreakable )
00080 {
00081 lastBreak = i;
00082 lineWidth = x + ww;
00083 }
00084 x += ww;
00085 }
00086 textwidth = QMAX( textwidth, x );
00087 kw->m_lineWidths.append( x );
00088 y += height;
00089
00090 kw->m_boundingRect.setRect( 0, 0, textwidth, y );
00091 return kw;
00092 }
00093
00094 QString KWordWrap::wrappedString() const
00095 {
00096
00097 QString ws;
00098 int start = 0;
00099 QValueList<int>::ConstIterator it = m_breakPositions.begin();
00100 for ( ; it != m_breakPositions.end() ; ++it )
00101 {
00102 int end = (*it);
00103 ws += m_text.mid( start, end - start + 1 ) + '\n';
00104 start = end + 1;
00105 }
00106 ws += m_text.mid( start );
00107 return ws;
00108 }
00109
00110 QString KWordWrap::truncatedString( bool dots ) const
00111 {
00112 QString ts;
00113 QValueList<int>::ConstIterator it = m_breakPositions.begin();
00114 if ( it != m_breakPositions.end() )
00115 {
00116 ts = m_text.left( (*it) + 1 );
00117 if ( dots )
00118 ts += "...";
00119 }
00120 else
00121 ts = m_text;
00122 return ts;
00123 }
00124
00125 void KWordWrap::drawText( QPainter *painter, int textX, int textY, int flags ) const
00126 {
00127
00128
00129 int start = 0;
00130 int y = 0;
00131 QFontMetrics fm = painter->fontMetrics();
00132 int height = fm.height();
00133 int ascent = fm.ascent();
00134 int maxwidth = m_boundingRect.width();
00135 QValueList<int>::ConstIterator it = m_breakPositions.begin();
00136 QValueList<int>::ConstIterator itw = m_lineWidths.begin();
00137 for ( ; it != m_breakPositions.end() ; ++it, ++itw )
00138 {
00139 int end = (*it);
00140 int x = textX;
00141 if ( flags & Qt::AlignHCenter )
00142 x += ( maxwidth - *itw ) / 2;
00143 else if ( flags & Qt::AlignRight )
00144 x += maxwidth - *itw;
00145 painter->drawText( x, textY + y + ascent, m_text.mid( start, end - start + 1 ) );
00146 y += height;
00147 start = end + 1;
00148 }
00149
00150 int x = textX;
00151 if ( flags & Qt::AlignHCenter )
00152 x += ( maxwidth - *itw ) / 2;
00153 else if ( flags & Qt::AlignRight )
00154 x += maxwidth - *itw;
00155 painter->drawText( x, textY + y + ascent, m_text.mid( start ) );
00156 }
00157
This file is part of the documentation for kdelibs Version 3.1.4.