kchart

KDChartTextPiece.cpp

00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KDChart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KDChart licenses may use this file in
00016  ** accordance with the KDChart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
00023  **   information about KDChart Commercial License Agreements.
00024  **
00025  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00026  ** licensing are not clear to you.
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     // These three are needed for both
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     // These three are needed for both
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         //qDebug( "richtext width %s", QString::number(_richText->width()).latin1());
00080     //qDebug( "richtext height %s", QString::number(_richText->height()).latin1());
00081         _richText->adjustSize();
00082         //qDebug( "richtext width %s", QString::number(_richText->width()).latin1());
00083     //qDebug( "richtext height %s", QString::number(_richText->height()).latin1());
00084         
00085     } else {
00086         _isRichText = false;
00087         _richText = 0;
00088     }
00089 
00090     // These three are needed for both
00091     _dirtyMetrics = (p == 0);
00092     if( _dirtyMetrics ) {
00093         _metrics = new QFontMetrics( font );
00094         //qDebug("dirty metrics text: %s", text.latin1());
00095     }
00096     else{
00097         p->save();
00098         p->setFont( font );
00099         _metrics = new QFontMetrics( p->fontMetrics() );
00100         //qDebug ( "drawing metrics text: %s", text.latin1() );
00101         //p->drawRect( _metrics->boundingRect( text) );
00102     //p->drawText( _metrics->boundingRect(text).bottomRight(), text);
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     // used for both
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     //qDebug ("_richText height %s", QString::number(_richText->height()).latin1()); 
00164         return _richText->height();
00165   }
00166   else {
00167   
00168     //qDebug ("_metrics height %s", QString::number(_metrics->height()).latin1()); 
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     // Pending Michel make sure the fonts are not too large
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     //calculate the text area before drawing
00209     QRect txtArea = rect( p,clipRect); 
00210     QRect rect;
00211     cg.setColor( QColorGroup::Text, color );
00212     // adjust the vertical position of the text within the area - we send a null rectangle to avoid clipping
00213     //PENDING: Michel - TODO - Let the user set or unset the adjustment factor by himself
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     //dont clip to avoid truncated text 
00222     //p->setClipRect( txtArea );
00223     if( _dirtyMetrics ){
00224       if( _metrics )
00225     delete _metrics;
00226       KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
00227       //KDChartTextPiece* meNotConst(const_cast<KDChartTextPiece*>(this));
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             // this line does not compile with MSVC++:
00262             // KDChartTextPiece* meNotConst( const_cast<KDChartTextPiece*>(this) );
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"
KDE Home | KDE Accessibility Home | Description of Access Keys