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