KD Chart 2
[rev.2.5]
|
00001 /**************************************************************************** 00002 ** Copyright (C) 2001-2012 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.txt 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 00025 #include <KDChartCartesianCoordinatePlane.h> 00026 00027 #include <QFont> 00028 #include <QPen> 00029 #include <qglobal.h> 00030 #include <QApplication> 00031 #include <QSharedPointer> 00032 #include <QTextDocument> 00033 #include <KDABLibFakes> 00034 00035 #define d d_func() 00036 00037 using namespace KDChart; 00038 00039 class TextAttributes::Private 00040 { 00041 friend class TextAttributes; 00042 public: 00043 Private(); 00044 private: 00045 bool visible; 00046 QFont font; 00047 mutable QFont cachedFont; 00048 mutable qreal cachedFontSize; 00049 Measure fontSize; 00050 Measure minimalFontSize; 00051 bool autoRotate; 00052 bool autoShrink; 00053 bool hasRotation; 00054 int rotation; 00055 QPen pen; 00056 QSharedPointer<QTextDocument> document; 00057 }; 00058 00059 TextAttributes::Private::Private() 00060 : visible( true ), 00061 font( QApplication::font() ), 00062 cachedFontSize( -1.0 ), 00063 autoRotate( false ), 00064 autoShrink( false ), 00065 hasRotation( false ), 00066 rotation( 0 ), 00067 pen( Qt::black ) 00068 { 00069 } 00070 00071 TextAttributes::TextAttributes() 00072 : _d( new Private() ) 00073 { 00074 } 00075 00076 TextAttributes::TextAttributes( const TextAttributes& r ) 00077 : _d( new Private( *r.d ) ) 00078 { 00079 00080 } 00081 00082 TextAttributes & TextAttributes::operator=( const TextAttributes& r ) 00083 { 00084 if( this == &r ) 00085 return *this; 00086 00087 *d = *r.d; 00088 00089 return *this; 00090 } 00091 00092 TextAttributes::~TextAttributes() 00093 { 00094 delete _d; _d = 0; 00095 } 00096 00097 00098 bool TextAttributes::operator==( const TextAttributes& r ) const 00099 { 00100 // the following works around a bug in gcc 4.3.2 00101 // causing StyleHint to be set to Zero when copying a QFont 00102 const QFont myFont( font() ); 00103 QFont r_font( r.font() ); 00104 r_font.setStyleHint( myFont.styleHint(), myFont.styleStrategy() ); 00105 return ( isVisible() == r.isVisible() && 00106 myFont == r_font && 00107 fontSize() == r.fontSize() && 00108 minimalFontSize() == r.minimalFontSize() && 00109 autoRotate() == r.autoRotate() && 00110 autoShrink() == r.autoShrink() && 00111 rotation() == r.rotation() && 00112 pen() == r.pen() && 00113 textDocument() == r.textDocument() ); 00114 } 00115 00116 00117 void TextAttributes::setVisible( bool visible ) 00118 { 00119 d->visible = visible; 00120 } 00121 00122 bool TextAttributes::isVisible() const 00123 { 00124 return d->visible; 00125 } 00126 00127 void TextAttributes::setFont( const QFont& font ) 00128 { 00129 d->font = font; 00130 d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont() 00131 d->cachedFontSize = -1.0; 00132 } 00133 00134 QFont TextAttributes::font() const 00135 { 00136 return d->font; 00137 } 00138 00139 void TextAttributes::setFontSize( const Measure & measure ) 00140 { 00141 d->fontSize = measure; 00142 } 00143 00144 Measure TextAttributes::fontSize() const 00145 { 00146 return d->fontSize; 00147 } 00148 00149 void TextAttributes::setMinimalFontSize( const Measure & measure ) 00150 { 00151 d->minimalFontSize = measure; 00152 } 00153 00154 Measure TextAttributes::minimalFontSize() const 00155 { 00156 return d->minimalFontSize; 00157 } 00158 00159 bool TextAttributes::hasAbsoluteFontSize() const 00160 { 00161 return d->fontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute 00162 && d->minimalFontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute; 00163 } 00164 00165 00166 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE) 00167 const 00168 #endif 00169 qreal TextAttributes::calculatedFontSize( 00170 const QObject* autoReferenceArea, 00171 KDChartEnums::MeasureOrientation autoReferenceOrientation ) const 00172 { 00173 const qreal normalSize = fontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation ); 00174 const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation ); 00175 return qMax( normalSize, minimalSize ); 00176 } 00177 00178 00179 const QFont TextAttributes::calculatedFont( const QObject* autoReferenceArea, 00180 KDChartEnums::MeasureOrientation autoReferenceOrientation ) const 00181 { 00182 qreal size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation ); 00183 00184 const CartesianCoordinatePlane* plane = qobject_cast< const CartesianCoordinatePlane* >( autoReferenceArea ); 00185 if ( plane && plane->hasFixedDataCoordinateSpaceRelation() ) { 00186 // FIXME (KDCH-514) make sure to return the same size as last time for this plane 00187 // note that a proper fix might have to be implemented somewhere else, not here 00188 } 00189 00190 if ( size > 0.0 && d->cachedFontSize != size ) { 00191 d->cachedFontSize = size; 00192 d->cachedFont.setPointSizeF( d->cachedFontSize ); 00193 } 00194 00195 return d->cachedFont; 00196 } 00197 00198 00199 void TextAttributes::setAutoRotate( bool autoRotate ) 00200 { 00201 d->autoRotate = autoRotate; 00202 } 00203 00204 bool TextAttributes::autoRotate() const 00205 { 00206 return d->autoRotate; 00207 } 00208 00209 void TextAttributes::setAutoShrink( bool autoShrink ) 00210 { 00211 d->autoShrink = autoShrink; 00212 } 00213 00214 bool TextAttributes::autoShrink() const 00215 { 00216 return d->autoShrink; 00217 } 00218 00219 void TextAttributes::setRotation( int rotation ) 00220 { 00221 d->hasRotation = true; 00222 d->rotation = rotation; 00223 } 00224 00225 int TextAttributes::rotation() const 00226 { 00227 return d->rotation; 00228 } 00229 00230 void TextAttributes::resetRotation() 00231 { 00232 d->hasRotation = false; 00233 d->rotation = 0; 00234 } 00235 00236 bool TextAttributes::hasRotation() const 00237 { 00238 return d->hasRotation; 00239 } 00240 00241 void TextAttributes::setPen( const QPen& pen ) 00242 { 00243 d->pen = pen; 00244 } 00245 00246 QPen TextAttributes::pen() const 00247 { 00248 return d->pen; 00249 } 00250 00251 QTextDocument* TextAttributes::textDocument() const 00252 { 00253 return d->document.data(); 00254 } 00255 00256 void TextAttributes::setTextDocument(QTextDocument* document) 00257 { 00258 d->document = QSharedPointer<QTextDocument>(document); 00259 } 00260 00261 #if !defined(QT_NO_DEBUG_STREAM) 00262 QDebug operator<<(QDebug dbg, const KDChart::TextAttributes& ta) 00263 { 00264 dbg << "KDChart::TextAttributes(" 00265 << "visible=" << ta.isVisible() 00266 << "font=" << ta.font().toString() /* What? No QDebug for QFont? */ 00267 << "fontsize=" << ta.fontSize() 00268 << "minimalfontsize=" << ta.minimalFontSize() 00269 << "autorotate=" << ta.autoRotate() 00270 << "autoshrink=" << ta.autoShrink() 00271 << "rotation=" << ta.rotation() 00272 << "pen=" << ta.pen() 00273 << ")"; 00274 return dbg; 00275 } 00276 #endif /* QT_NO_DEBUG_STREAM */