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 QColorGroup cg;
00208
00209 QRect txtArea = rect( p,clipRect);
00210 QRect rect;
00211 cg.setColor( QColorGroup::Text, color );
00212
00213
00214 _richText->draw( p, txtArea.x(), txtArea.y() + (int)txtArea.height()/10 , rect, cg, paper );
00215 } else {
00216 p->save();
00217 p->setFont( _font );
00218 if( paper )
00219 p->setBrush( *paper );
00220 p->setPen( color );
00221
00222
00223 if( _dirtyMetrics ){
00224 if( _metrics )
00225 delete _metrics;
00226 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
00227
00228 meNotConst->_metrics = new QFontMetrics( p->fontMetrics() );
00229 meNotConst->_dirtyMetrics = false;
00230 }
00231
00232 p->drawText( x, y + _metrics->ascent(), _text );
00233 p->restore();
00234 }
00235 }
00236
00237
00238 void KDChartTextPiece::draw( QPainter *p, int x, int y,
00239 const QRegion& clipRegion,
00240 const QColor& color,
00241 const QBrush* paper ) const
00242 {
00243 if( _isRichText ) {
00244 QColorGroup cg;
00245 cg.setColor( QColorGroup::Text, color );
00246 _richText->setDefaultFont( _font );
00247 _richText->setWidth( p, clipRegion.boundingRect().width() );
00248 _richText->draw( p, x, y, clipRegion, cg, paper );
00249 } else {
00250 p->save();
00251 p->setFont( _font );
00252 if( paper )
00253 p->setBrush( *paper );
00254 p->setPen( color );
00255 p->setClipRegion( clipRegion );
00256
00257 if( _dirtyMetrics ){
00258 if( _metrics )
00259 delete _metrics;
00260
00261
00262
00263 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
00264
00265 meNotConst->_metrics = new QFontMetrics( p->fontMetrics() );
00266 meNotConst->_dirtyMetrics = false;
00267 }
00268
00269 p->drawText( x, y + _metrics->ascent(), _text );
00270 p->restore();
00271 }
00272 }
00273
00274
00275 QString KDChartTextPiece::text() const
00276 {
00277 return _text;
00278 }
00279
00280
00281 QFont KDChartTextPiece::font() const
00282 {
00283 return _font;
00284 }
00285
00286
00287 bool KDChartTextPiece::isRichText() const
00288 {
00289 return _isRichText;
00290 }
00291
00292
00293
00294 #include "KDChartTextPiece.moc"