File indexing completed on 2024-05-12 04:20:37

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "KChartTextAttributes.h"
0010 
0011 #include <KChartCartesianCoordinatePlane.h>
0012 #include <KChartCartesianCoordinatePlane_p.h>
0013 #include "KChartMath_p.h"
0014 
0015 #include <QFont>
0016 #include <QPen>
0017 #include <qglobal.h>
0018 #include <QApplication>
0019 #include <QSharedPointer>
0020 #include <QTextDocument>
0021 
0022 #define d d_func()
0023 
0024 using namespace KChart;
0025 
0026 class Q_DECL_HIDDEN TextAttributes::Private
0027 {
0028     friend class TextAttributes;
0029 public:
0030      Private();
0031 private:
0032     bool visible;
0033     QFont font;
0034     mutable QFont cachedFont;
0035     mutable qreal cachedFontSize;
0036     Measure fontSize;
0037     Measure minimalFontSize;
0038     bool autoRotate;
0039     bool autoShrink;
0040     bool hasRotation;
0041     int rotation;
0042     QPen pen;
0043     QSharedPointer<QTextDocument> document;
0044 };
0045 
0046 TextAttributes::Private::Private()
0047     : visible( true ),
0048       font( QApplication::font() ),
0049       cachedFontSize( -1.0 ),
0050       autoRotate( false ),
0051       autoShrink( false ),
0052       hasRotation( false ),
0053       rotation( 0 ),
0054       pen( Qt::black )
0055 {
0056 }
0057 
0058 TextAttributes::TextAttributes()
0059     : _d( new Private() )
0060 {
0061 }
0062 
0063 TextAttributes::TextAttributes( const TextAttributes& r )
0064     : _d( new Private( *r.d ) )
0065 {
0066 
0067 }
0068 
0069 TextAttributes & TextAttributes::operator=( const TextAttributes& r )
0070 {
0071     if ( this == &r )
0072         return *this;
0073 
0074     *d = *r.d;
0075 
0076     return *this;
0077 }
0078 
0079 TextAttributes::~TextAttributes()
0080 {
0081     delete _d; _d = nullptr;
0082 }
0083 
0084 
0085 bool TextAttributes::operator==( const TextAttributes& r ) const
0086 {
0087     // the following works around a bug in gcc 4.3.2
0088     // causing StyleHint to be set to Zero when copying a QFont
0089     const QFont myFont( font() );
0090     QFont r_font( r.font() );
0091     r_font.setStyleHint( myFont.styleHint(), myFont.styleStrategy() );
0092     return ( isVisible() == r.isVisible() &&
0093              myFont == r_font &&
0094              fontSize() == r.fontSize() &&
0095              minimalFontSize() == r.minimalFontSize() &&
0096              autoRotate() == r.autoRotate() &&
0097              autoShrink() == r.autoShrink() &&
0098              rotation() == r.rotation() &&
0099              pen() == r.pen() &&
0100              textDocument() == r.textDocument() );
0101 }
0102 
0103 
0104 void TextAttributes::setVisible( bool visible )
0105 {
0106     d->visible = visible;
0107 }
0108 
0109 bool TextAttributes::isVisible() const
0110 {
0111     return d->visible;
0112 }
0113 
0114 void TextAttributes::setFont( const QFont& font )
0115 {
0116     d->font       = font;
0117     d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont()
0118     d->cachedFontSize = -1.0;
0119 }
0120 
0121 QFont TextAttributes::font() const
0122 {
0123     return d->font;
0124 }
0125 
0126 void TextAttributes::setFontSize( const Measure & measure )
0127 {
0128     d->fontSize = measure;
0129 }
0130 
0131 Measure TextAttributes::fontSize() const
0132 {
0133     return d->fontSize;
0134 }
0135 
0136 void TextAttributes::setMinimalFontSize( const Measure & measure )
0137 {
0138     d->minimalFontSize = measure;
0139 }
0140 
0141 Measure TextAttributes::minimalFontSize() const
0142 {
0143     return d->minimalFontSize;
0144 }
0145 
0146 bool TextAttributes::hasAbsoluteFontSize() const
0147 {
0148     return d->fontSize.calculationMode() == KChartEnums::MeasureCalculationModeAbsolute
0149         && d->minimalFontSize.calculationMode() == KChartEnums::MeasureCalculationModeAbsolute;
0150 }
0151 
0152 qreal TextAttributes::calculatedFontSize( const QSizeF &referenceSize,
0153                                           KChartEnums::MeasureOrientation autoReferenceOrientation ) const
0154 {
0155     const qreal normalSize  = fontSize().calculatedValue( referenceSize, autoReferenceOrientation );
0156     const qreal minimalSize = minimalFontSize().calculatedValue( referenceSize, autoReferenceOrientation );
0157     return qMax( normalSize, minimalSize );
0158 }
0159 
0160 #if defined(Q_COMPILER_MANGLES_RETURN_TYPE)
0161 const
0162 #endif
0163 qreal TextAttributes::calculatedFontSize( const QObject* autoReferenceArea,
0164                                           KChartEnums::MeasureOrientation autoReferenceOrientation ) const
0165 {
0166     const qreal normalSize  = fontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
0167     const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
0168     return qMax( normalSize, minimalSize );
0169 }
0170 
0171 const QFont TextAttributes::calculatedFont( const QObject* autoReferenceArea,
0172                                             KChartEnums::MeasureOrientation autoReferenceOrientation ) const
0173 {
0174     qreal size = NaN;
0175 
0176     const CartesianCoordinatePlane* plane = qobject_cast< const CartesianCoordinatePlane* >( autoReferenceArea );
0177     if ( plane  && plane->hasFixedDataCoordinateSpaceRelation() ) {
0178         // HACK
0179         // if hasFixedDataCoordinateSpaceRelation, we use a zoom trick to keep the diagram at a constant size
0180         // even when the plane size changes. calculatedFontSize() usually uses the plane size, not the diagram
0181         // size, to determine the font size. here we need to give it the diagram size in order to make the font
0182         // size constant, too. see KDCHDEV-219.
0183         CartesianCoordinatePlane::Private *priv
0184             = CartesianCoordinatePlane::Private::get( const_cast< CartesianCoordinatePlane * >( plane ) );
0185         size = calculatedFontSize( priv->fixedDataCoordinateSpaceRelationPinnedSize,
0186                                    autoReferenceOrientation );
0187     } else {
0188         size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
0189     }
0190 
0191     if ( size > 0.0 && d->cachedFontSize != size ) {
0192         d->cachedFontSize = size;
0193         d->cachedFont.setPointSizeF( d->cachedFontSize );
0194     }
0195 
0196     return d->cachedFont;
0197 }
0198 
0199 
0200 void TextAttributes::setAutoRotate( bool autoRotate )
0201 {
0202     d->autoRotate = autoRotate;
0203 }
0204 
0205 bool TextAttributes::autoRotate() const
0206 {
0207     return d->autoRotate;
0208 }
0209 
0210 void TextAttributes::setAutoShrink( bool autoShrink )
0211 {
0212     d->autoShrink = autoShrink;
0213 }
0214 
0215 bool TextAttributes::autoShrink() const
0216 {
0217     return d->autoShrink;
0218 }
0219 
0220 void TextAttributes::setRotation( int rotation )
0221 {
0222     d->hasRotation = true;
0223     d->rotation = rotation;
0224 }
0225 
0226 int TextAttributes::rotation() const
0227 {
0228     return d->rotation;
0229 }
0230 
0231 void TextAttributes::resetRotation()
0232 {
0233     d->hasRotation = false;
0234     d->rotation = 0;
0235 }
0236 
0237 bool TextAttributes::hasRotation() const
0238 {
0239     return d->hasRotation;
0240 }
0241 
0242 void TextAttributes::setPen( const QPen& pen )
0243 {
0244     d->pen = pen;
0245 }
0246 
0247 QPen TextAttributes::pen() const
0248 {
0249     return d->pen;
0250 }
0251 
0252 QTextDocument* TextAttributes::textDocument() const
0253 {
0254     return d->document.data();
0255 }
0256 
0257 void TextAttributes::setTextDocument(QTextDocument* document)
0258 {
0259     d->document = QSharedPointer<QTextDocument>(document);
0260 }
0261 
0262 #if !defined(QT_NO_DEBUG_STREAM)
0263 QDebug operator<<(QDebug dbg, const KChart::TextAttributes& ta)
0264 {
0265     dbg << "KChart::TextAttributes("
0266     << "visible=" << ta.isVisible()
0267     << "font=" << ta.font().toString() /* What? No QDebug for QFont? */
0268     << "fontsize=" << ta.fontSize()
0269     << "minimalfontsize=" << ta.minimalFontSize()
0270     << "autorotate=" << ta.autoRotate()
0271     << "autoshrink=" << ta.autoShrink()
0272     << "rotation=" << ta.rotation()
0273     << "pen=" << ta.pen()
0274     << ")";
0275     return dbg;
0276 }
0277 #endif /* QT_NO_DEBUG_STREAM */