KDChartTextAttributes.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "KDChartTextAttributes.h"
00024 #include <QFont>
00025 #include <QPen>
00026 #include <qglobal.h>
00027 #include <QApplication>
00028 
00029 #include <KDABLibFakes>
00030 #include <KDChartCartesianCoordinatePlane>
00031 
00032 #define d d_func()
00033 
00034 using namespace KDChart;
00035 
00036 class TextAttributes::Private
00037 {
00038     friend class TextAttributes;
00039 public:
00040     Private();
00041 private:
00042     bool visible;
00043     QFont font;
00044     mutable QFont cachedFont;
00045     mutable qreal cachedFontSize;
00046     Measure fontSize;
00047     Measure minimalFontSize;
00048     bool autoRotate;
00049     bool autoShrink;
00050     int rotation;
00051     QPen pen;
00052 };
00053 
00054 TextAttributes::Private::Private()
00055 {
00056     cachedFontSize = -1.0;
00057 }
00058 
00059 
00060 TextAttributes::TextAttributes()
00061     : _d( new Private() )
00062 {
00063     setVisible( true );
00064     setFont( QApplication::font() );
00065     setAutoRotate( false );
00066     setAutoShrink( false );
00067     setRotation( 0 );
00068     setPen( QPen( Qt::black ) );
00069 }
00070 
00071 TextAttributes::TextAttributes( const TextAttributes& r )
00072     : _d( new Private( *r.d ) )
00073 {
00074 
00075 }
00076 
00077 TextAttributes & TextAttributes::operator=( const TextAttributes& r )
00078 {
00079     if( this == &r )
00080         return *this;
00081 
00082     *d = *r.d;
00083 
00084     return *this;
00085 }
00086 
00087 TextAttributes::~TextAttributes()
00088 {
00089     delete _d; _d = 0;
00090 }
00091 
00092 
00093 bool TextAttributes::operator==( const TextAttributes& r ) const
00094 {
00095     // the following works around a bug in gcc 4.3.2
00096     // causing StyleHint to be set to Zero when copying a QFont
00097     const QFont myFont( font() );
00098     QFont r_font( r.font() );
00099     r_font.setStyleHint( myFont.styleHint(), myFont.styleStrategy() );
00100     /*
00101     qDebug() << "\nTextAttributes::operator== :" << ( isVisible() == r.isVisible())
00102             << " font:"<<(myFont == r_font)
00103             << " fontSize:"<<(fontSize() == r.fontSize())
00104             << " minimalFontSize:"<<(minimalFontSize() == r.minimalFontSize())
00105             << (autoRotate() == r.autoRotate())
00106             << (autoShrink() == r.autoShrink())
00107             << (rotation() == rotation())
00108             << (pen() == r.pen());
00109     */
00110     return ( isVisible() == r.isVisible() &&
00111             myFont == r_font &&
00112             fontSize() == r.fontSize() &&
00113             minimalFontSize() == r.minimalFontSize() &&
00114             autoRotate() == r.autoRotate() &&
00115             autoShrink() == r.autoShrink() &&
00116             rotation() == r.rotation() &&
00117             pen() == r.pen() );
00118 }
00119 
00120 
00121 void TextAttributes::setVisible( bool visible )
00122 {
00123     d->visible = visible;
00124 }
00125 
00126 bool TextAttributes::isVisible() const
00127 {
00128     return d->visible;
00129 }
00130 
00131 void TextAttributes::setFont( const QFont& font )
00132 {
00133     d->font       = font;
00134     d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont()
00135     //qDebug() << "resetting cached font size";
00136     d->cachedFontSize = -1.0;
00137 }
00138 
00139 QFont TextAttributes::font() const
00140 {
00141     return d->font;
00142 }
00143 
00144 void TextAttributes::setFontSize( const Measure & measure )
00145 {
00146     d->fontSize = measure;
00147 }
00148 
00149 Measure TextAttributes::fontSize() const
00150 {
00151     return d->fontSize;
00152 }
00153 
00154 void TextAttributes::setMinimalFontSize( const Measure & measure )
00155 {
00156     d->minimalFontSize = measure;
00157 }
00158 
00159 Measure TextAttributes::minimalFontSize() const
00160 {
00161     return d->minimalFontSize;
00162 }
00163 
00164 bool TextAttributes::hasAbsoluteFontSize() const
00165 {
00166     return d->fontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute
00167         && d->minimalFontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute;
00168 }
00169 
00170 
00171 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
00172 const
00173 #endif
00174 qreal TextAttributes::calculatedFontSize(
00175         const QObject*                   autoReferenceArea,
00176         KDChartEnums::MeasureOrientation autoReferenceOrientation ) const
00177 {
00178     const qreal normalSize  = fontSize().calculatedValue(        autoReferenceArea, autoReferenceOrientation );
00179     const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
00180     //qDebug() << "TextAttributes::calculatedFontSize() finds" << normalSize << "and" << minimalSize;
00181     return qMax( normalSize, minimalSize );
00182 }
00183 
00184 
00185 const QFont TextAttributes::calculatedFont(
00186     const QObject*                   autoReferenceArea,
00187     KDChartEnums::MeasureOrientation autoReferenceOrientation ) const
00188 {
00189     const CartesianCoordinatePlane* plane = dynamic_cast<const CartesianCoordinatePlane*>( autoReferenceArea );
00190 
00191     static qreal size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
00192     if ( plane )
00193     {
00194         if(!plane->hasFixedDataCoordinateSpaceRelation())
00195             size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
00196     }
00197     else
00198         size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
00199 
00200     if( size > 0.0 && d->cachedFontSize != size ){
00201         //qDebug() << "new into the cache:" << size;
00202         d->cachedFontSize = size;
00203         d->cachedFont.setPointSizeF( d->cachedFontSize );
00204     }
00205 
00206     return d->cachedFont;
00207 }
00208 
00209 
00210 void TextAttributes::setAutoRotate( bool autoRotate )
00211 {
00212     d->autoRotate = autoRotate;
00213 }
00214 
00215 bool TextAttributes::autoRotate() const
00216 {
00217     return d->autoRotate;
00218 }
00219 
00220 void TextAttributes::setAutoShrink( bool autoShrink )
00221 {
00222     d->autoShrink = autoShrink;
00223 }
00224 
00225 bool TextAttributes::autoShrink() const
00226 {
00227     return d->autoShrink;
00228 }
00229 
00230 void TextAttributes::setRotation( int rotation )
00231 {
00232     d->rotation = rotation;
00233 }
00234 
00235 int TextAttributes::rotation() const
00236 {
00237     return d->rotation;
00238 }
00239 
00240 void TextAttributes::setPen( const QPen& pen )
00241 {
00242     d->pen = pen;
00243 }
00244 
00245 QPen TextAttributes::pen() const
00246 {
00247     return d->pen;
00248 }
00249 
00250 #if !defined(QT_NO_DEBUG_STREAM)
00251 QDebug operator<<(QDebug dbg, const KDChart::TextAttributes& ta)
00252 {
00253     dbg << "KDChart::TextAttributes("
00254     << "visible="<<ta.isVisible()
00255     << "font="<<ta.font().toString() /* What? No QDebug for QFont? */
00256     << "fontsize="<<ta.fontSize()
00257     << "minimalfontsize="<<ta.minimalFontSize()
00258     << "autorotate="<<ta.autoRotate()
00259     << "autoshrink="<<ta.autoShrink()
00260     << "rotation="<<ta.rotation()
00261     << "pen="<<ta.pen()
00262     << ")";
00263     return dbg;
00264 }
00265 #endif /* QT_NO_DEBUG_STREAM */