00001 /**************************************************************************** 00002 ** Copyright (C) 2006 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 /* 00098 qDebug() << "\n" << "TextAttributes::operator== :" << ( isVisible() == r.isVisible()) 00099 << (font() == r.font()) 00100 << (fontSize() == r.fontSize()) 00101 << (minimalFontSize() == r.minimalFontSize()) 00102 << (autoRotate() == r.autoRotate()) 00103 << (autoShrink() == r.autoShrink()) 00104 << (rotation() == rotation()) 00105 << (pen() == r.pen()); 00106 */ 00107 return ( isVisible() == r.isVisible() && 00108 font() == r.font() && 00109 fontSize() == r.fontSize() && 00110 minimalFontSize() == r.minimalFontSize() && 00111 autoRotate() == r.autoRotate() && 00112 autoShrink() == r.autoShrink() && 00113 rotation() == rotation() && 00114 pen() == r.pen() ); 00115 } 00116 00117 00118 void TextAttributes::setVisible( bool visible ) 00119 { 00120 d->visible = visible; 00121 } 00122 00123 bool TextAttributes::isVisible() const 00124 { 00125 return d->visible; 00126 } 00127 00128 void TextAttributes::setFont( const QFont& font ) 00129 { 00130 d->font = font; 00131 d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont() 00132 d->cachedFontSize = -1.0; 00133 } 00134 00135 QFont TextAttributes::font() const 00136 { 00137 return d->font; 00138 } 00139 00140 void TextAttributes::setFontSize( const Measure & measure ) 00141 { 00142 d->fontSize = measure; 00143 } 00144 00145 Measure TextAttributes::fontSize() const 00146 { 00147 return d->fontSize; 00148 } 00149 00150 void TextAttributes::setMinimalFontSize( const Measure & measure ) 00151 { 00152 d->minimalFontSize = measure; 00153 } 00154 00155 Measure TextAttributes::minimalFontSize() const 00156 { 00157 return d->minimalFontSize; 00158 } 00159 00160 bool TextAttributes::hasAbsoluteFontSize() const 00161 { 00162 return d->fontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute 00163 && d->minimalFontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute; 00164 } 00165 00166 00167 const qreal TextAttributes::calculatedFontSize( 00168 const QObject* autoReferenceArea, 00169 KDChartEnums::MeasureOrientation autoReferenceOrientation ) const 00170 { 00171 const qreal normalSize = fontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation ); 00172 const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation ); 00173 //qDebug() << "TextAttributes::calculatedFontSize() finds" << normalSize << "and" << minimalSize; 00174 return qMax( normalSize, minimalSize ); 00175 } 00176 00177 00178 const QFont TextAttributes::calculatedFont( 00179 const QObject* autoReferenceArea, 00180 KDChartEnums::MeasureOrientation autoReferenceOrientation ) const 00181 { 00182 const qreal size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation ); 00183 //qDebug() << "TextAttributes::calculatedFont() has d->cachedFontSize" << d->cachedFontSize << " calculatedFontSize" << calculatedFontSize; 00184 if( size > 0.0 && d->cachedFontSize != size ){ 00185 d->cachedFontSize = size; 00186 d->cachedFont.setPointSizeF( d->cachedFontSize ); 00187 } 00188 return d->cachedFont; 00189 } 00190 00191 00192 void TextAttributes::setAutoRotate( bool autoRotate ) 00193 { 00194 d->autoRotate = autoRotate; 00195 } 00196 00197 bool TextAttributes::autoRotate() const 00198 { 00199 return d->autoRotate; 00200 } 00201 00202 void TextAttributes::setAutoShrink( bool autoShrink ) 00203 { 00204 d->autoShrink = autoShrink; 00205 } 00206 00207 bool TextAttributes::autoShrink() const 00208 { 00209 return d->autoShrink; 00210 } 00211 00212 void TextAttributes::setRotation( int rotation ) 00213 { 00214 d->rotation = rotation; 00215 } 00216 00217 int TextAttributes::rotation() const 00218 { 00219 return d->rotation; 00220 } 00221 00222 void TextAttributes::setPen( const QPen& pen ) 00223 { 00224 d->pen = pen; 00225 } 00226 00227 QPen TextAttributes::pen() const 00228 { 00229 return d->pen; 00230 } 00231 00232 #if !defined(QT_NO_DEBUG_STREAM) 00233 QDebug operator<<(QDebug dbg, const KDChart::TextAttributes& ta) 00234 { 00235 dbg << "KDChart::TextAttributes(" 00236 << "visible="<<ta.isVisible() 00237 << "font="<<ta.font().toString() /* What? No QDebug for QFont? */ 00238 << "fontsize="<<ta.fontSize() 00239 << "minimalfontsize="<<ta.minimalFontSize() 00240 << "autorotate="<<ta.autoRotate() 00241 << "autoshrink="<<ta.autoShrink() 00242 << "rotation="<<ta.rotation() 00243 << "pen="<<ta.pen() 00244 << ")"; 00245 return dbg; 00246 } 00247 #endif /* QT_NO_DEBUG_STREAM */