00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "KDChartTextPiece.h"
00030
00031 #include <qstylesheet.h>
00032 #include <qsimplerichtext.h>
00033 #include <qfontmetrics.h>
00034 #include <qpainter.h>
00035 #include <qapplication.h>
00036 #include <qrect.h>
00037
00038 KDChartTextPiece::KDChartTextPiece()
00039 :QObject(0)
00040 {
00041 _isRichText = false;
00042 _richText = 0;
00043
00044 _font = QApplication::font();
00045
00046 _metrics = new QFontMetrics( _font );
00047 _dirtyMetrics = true;
00048 _text = QString("");
00049 }
00050
00051
00052 KDChartTextPiece::KDChartTextPiece( const QString& text, const QFont& font )
00053 :QObject(0)
00054 {
00055 if( QStyleSheet::mightBeRichText( text ) ) {
00056 _isRichText = true;
00057 _richText = new QSimpleRichText( text, font );
00058 _richText->adjustSize();
00059 } else {
00060 _isRichText = false;
00061 _richText = 0;
00062 }
00063
00064
00065 _metrics = new QFontMetrics( font );
00066 _dirtyMetrics = true;
00067 _text = text;
00068 _font = font;
00069 }
00070
00071
00072 KDChartTextPiece::KDChartTextPiece( QPainter *p, const QString& text, const QFont& font )
00073 :QObject(0)
00074 {
00075
00076 if( QStyleSheet::mightBeRichText( text ) ) {
00077 _isRichText = true;
00078 _richText = new QSimpleRichText( text, font );
00079
00080
00081 _richText->adjustSize();
00082
00083
00084
00085 } else {
00086 _isRichText = false;
00087 _richText = 0;
00088 }
00089
00090
00091 _dirtyMetrics = (p == 0);
00092 if( _dirtyMetrics ) {
00093 _metrics = new QFontMetrics( font );
00094
00095 }
00096 else{
00097 p->save();
00098 p->setFont( font );
00099 _metrics = new QFontMetrics( p->fontMetrics() );
00100
00101
00102
00103 p->restore();
00104 }
00105 _text = text;
00106 _font = font;
00107 }
00108
00109
00110 void KDChartTextPiece::deepCopy( const KDChartTextPiece* source )
00111 {
00112 if( !source || this == source )
00113 return;
00114 if( _richText )
00115 delete _richText;
00116 _isRichText = source->_isRichText;
00117 if( source->_richText ) {
00118 _richText = new QSimpleRichText( source->_text, source->_font );
00119 _richText->adjustSize();
00120 }
00121 else
00122 _richText = 0;
00123
00124
00125 if( _metrics )
00126 delete _metrics;
00127 _metrics = new QFontMetrics( *source->_metrics );
00128 _dirtyMetrics = source->_dirtyMetrics;
00129 _text = source->_text;
00130 _font = source->_font;
00131 }
00132
00133 const KDChartTextPiece* KDChartTextPiece::clone() const
00134 {
00135 KDChartTextPiece* newPiece = new KDChartTextPiece();
00136 newPiece->deepCopy( this );
00137 return newPiece;
00138 }
00139
00140
00141 KDChartTextPiece::~KDChartTextPiece()
00142 {
00143 if( _richText )
00144 delete _richText;
00145 if( _metrics )
00146 delete _metrics;
00147 }
00148
00149
00150 int KDChartTextPiece::width() const
00151 {
00152 if( _isRichText )
00153 return _richText->widthUsed();
00154 else
00155 return _metrics->width( _text );
00156 }
00157
00158
00159 int KDChartTextPiece::height() const
00160 {
00161
00162 if( _isRichText ) {
00163
00164 return _richText->height();
00165 }
00166 else {
00167
00168
00169 return _metrics->height();
00170 }
00171 }
00172
00173
00174 int KDChartTextPiece::fontLeading() const
00175 {
00176 return _metrics->leading();
00177 }
00178
00179 QRect KDChartTextPiece::rect( QPainter *p, const QRect& clipRect) const
00180 {
00181 QRect rect( clipRect );
00182 QFont font( _font );
00183
00184 if( _isRichText ) {
00185
00186
00187 if ( _richText->height() > clipRect.height() || _richText->width() > clipRect.width() )
00188 font.setPixelSize( QMIN( (int)clipRect.width(),(int)clipRect.height() ) );
00189
00190 _richText->setDefaultFont( font );
00191 _richText->setWidth( p, clipRect.width() );
00192 rect.setWidth( _richText->width() );
00193 rect.setHeight( _richText->height() );
00194 } else
00195 rect = clipRect;
00196
00197 return rect;
00198 }
00199
00200 void KDChartTextPiece::draw( QPainter *p, int x, int y,
00201 const QRect& clipRect,
00202 const QColor& color,
00203 const QBrush* paper ) const
00204 {
00205
00206 if( _isRichText ) {
00207
00208 QColorGroup cg;
00209
00210 QRect txtArea = rect( p,clipRect);
00211 cg.setColor( QColorGroup::Text, color );
00212 _richText->draw( p, txtArea.x(), txtArea.y(),txtArea , cg, paper );
00213 } else {
00214 p->save();
00215 p->setFont( _font );
00216 if( paper )
00217 p->setBrush( *paper );
00218 p->setPen( color );
00219
00220
00221 if( _dirtyMetrics ){
00222 if( _metrics )
00223 delete _metrics;
00224 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
00225
00226 meNotConst->_metrics = new QFontMetrics( p->fontMetrics() );
00227 meNotConst->_dirtyMetrics = false;
00228 }
00229
00230 p->drawText( x, y + _metrics->ascent(), _text );
00231 p->restore();
00232 }
00233 }
00234
00235
00236 void KDChartTextPiece::draw( QPainter *p, int x, int y,
00237 const QRegion& clipRegion,
00238 const QColor& color,
00239 const QBrush* paper ) const
00240 {
00241 if( _isRichText ) {
00242 QColorGroup cg;
00243 cg.setColor( QColorGroup::Text, color );
00244 _richText->setDefaultFont( _font );
00245 _richText->setWidth( p, clipRegion.boundingRect().width() );
00246 _richText->draw( p, x, y, clipRegion, cg, paper );
00247 } else {
00248 p->save();
00249 p->setFont( _font );
00250 if( paper )
00251 p->setBrush( *paper );
00252 p->setPen( color );
00253 p->setClipRegion( clipRegion );
00254
00255 if( _dirtyMetrics ){
00256 if( _metrics )
00257 delete _metrics;
00258
00259
00260
00261 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
00262
00263 meNotConst->_metrics = new QFontMetrics( p->fontMetrics() );
00264 meNotConst->_dirtyMetrics = false;
00265 }
00266
00267 p->drawText( x, y + _metrics->ascent(), _text );
00268 p->restore();
00269 }
00270 }
00271
00272
00273 QString KDChartTextPiece::text() const
00274 {
00275 return _text;
00276 }
00277
00278
00279 QFont KDChartTextPiece::font() const
00280 {
00281 return _font;
00282 }
00283
00284
00285 bool KDChartTextPiece::isRichText() const
00286 {
00287 return _isRichText;
00288 }
00289
00290
00291
00292 #include "KDChartTextPiece.moc"