KChart

KChartTextAttributes.cpp
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "KChartTextAttributes.h"
10 
11 #include <KChartCartesianCoordinatePlane.h>
12 #include <KChartCartesianCoordinatePlane_p.h>
13 #include "KChartMath_p.h"
14 
15 #include <QFont>
16 #include <QPen>
17 #include <qglobal.h>
18 #include <QApplication>
19 #include <QSharedPointer>
20 #include <QTextDocument>
21 
22 #define d d_func()
23 
24 using namespace KChart;
25 
26 class Q_DECL_HIDDEN TextAttributes::Private
27 {
28  friend class TextAttributes;
29 public:
30  Private();
31 private:
32  bool visible;
33  QFont font;
34  mutable QFont cachedFont;
35  mutable qreal cachedFontSize;
36  Measure fontSize;
37  Measure minimalFontSize;
38  bool autoRotate;
39  bool autoShrink;
40  bool hasRotation;
41  int rotation;
42  QPen pen;
44 };
45 
46 TextAttributes::Private::Private()
47  : visible( true ),
48  font( QApplication::font() ),
49  cachedFontSize( -1.0 ),
50  autoRotate( false ),
51  autoShrink( false ),
52  hasRotation( false ),
53  rotation( 0 ),
54  pen( Qt::black )
55 {
56 }
57 
58 TextAttributes::TextAttributes()
59  : _d( new Private() )
60 {
61 }
62 
63 TextAttributes::TextAttributes( const TextAttributes& r )
64  : _d( new Private( *r.d ) )
65 {
66 
67 }
68 
69 TextAttributes & TextAttributes::operator=( const TextAttributes& r )
70 {
71  if ( this == &r )
72  return *this;
73 
74  *d = *r.d;
75 
76  return *this;
77 }
78 
79 TextAttributes::~TextAttributes()
80 {
81  delete _d; _d = nullptr;
82 }
83 
84 
85 bool TextAttributes::operator==( const TextAttributes& r ) const
86 {
87  // the following works around a bug in gcc 4.3.2
88  // causing StyleHint to be set to Zero when copying a QFont
89  const QFont myFont( font() );
90  QFont r_font( r.font() );
91  r_font.setStyleHint( myFont.styleHint(), myFont.styleStrategy() );
92  return ( isVisible() == r.isVisible() &&
93  myFont == r_font &&
94  fontSize() == r.fontSize() &&
95  minimalFontSize() == r.minimalFontSize() &&
96  autoRotate() == r.autoRotate() &&
97  autoShrink() == r.autoShrink() &&
98  rotation() == r.rotation() &&
99  pen() == r.pen() &&
100  textDocument() == r.textDocument() );
101 }
102 
103 
104 void TextAttributes::setVisible( bool visible )
105 {
106  d->visible = visible;
107 }
108 
110 {
111  return d->visible;
112 }
113 
114 void TextAttributes::setFont( const QFont& font )
115 {
116  d->font = font;
117  d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont()
118  d->cachedFontSize = -1.0;
119 }
120 
122 {
123  return d->font;
124 }
125 
126 void TextAttributes::setFontSize( const Measure & measure )
127 {
128  d->fontSize = measure;
129 }
130 
132 {
133  return d->fontSize;
134 }
135 
137 {
138  d->minimalFontSize = measure;
139 }
140 
142 {
143  return d->minimalFontSize;
144 }
145 
147 {
148  return d->fontSize.calculationMode() == KChartEnums::MeasureCalculationModeAbsolute
149  && d->minimalFontSize.calculationMode() == KChartEnums::MeasureCalculationModeAbsolute;
150 }
151 
152 qreal TextAttributes::calculatedFontSize( const QSizeF &referenceSize,
153  KChartEnums::MeasureOrientation autoReferenceOrientation ) const
154 {
155  const qreal normalSize = fontSize().calculatedValue( referenceSize, autoReferenceOrientation );
156  const qreal minimalSize = minimalFontSize().calculatedValue( referenceSize, autoReferenceOrientation );
157  return qMax( normalSize, minimalSize );
158 }
159 
160 #if defined(Q_COMPILER_MANGLES_RETURN_TYPE)
161 const
162 #endif
163 qreal TextAttributes::calculatedFontSize( const QObject* autoReferenceArea,
164  KChartEnums::MeasureOrientation autoReferenceOrientation ) const
165 {
166  const qreal normalSize = fontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
167  const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
168  return qMax( normalSize, minimalSize );
169 }
170 
171 const QFont TextAttributes::calculatedFont( const QObject* autoReferenceArea,
172  KChartEnums::MeasureOrientation autoReferenceOrientation ) const
173 {
174  qreal size = NaN;
175 
176  const CartesianCoordinatePlane* plane = qobject_cast< const CartesianCoordinatePlane* >( autoReferenceArea );
177  if ( plane && plane->hasFixedDataCoordinateSpaceRelation() ) {
178  // HACK
179  // if hasFixedDataCoordinateSpaceRelation, we use a zoom trick to keep the diagram at a constant size
180  // even when the plane size changes. calculatedFontSize() usually uses the plane size, not the diagram
181  // size, to determine the font size. here we need to give it the diagram size in order to make the font
182  // size constant, too. see KDCHDEV-219.
183  CartesianCoordinatePlane::Private *priv
184  = CartesianCoordinatePlane::Private::get( const_cast< CartesianCoordinatePlane * >( plane ) );
185  size = calculatedFontSize( priv->fixedDataCoordinateSpaceRelationPinnedSize,
186  autoReferenceOrientation );
187  } else {
188  size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
189  }
190 
191  if ( size > 0.0 && d->cachedFontSize != size ) {
192  d->cachedFontSize = size;
193  d->cachedFont.setPointSizeF( d->cachedFontSize );
194  }
195 
196  return d->cachedFont;
197 }
198 
199 
200 void TextAttributes::setAutoRotate( bool autoRotate )
201 {
202  d->autoRotate = autoRotate;
203 }
204 
206 {
207  return d->autoRotate;
208 }
209 
210 void TextAttributes::setAutoShrink( bool autoShrink )
211 {
212  d->autoShrink = autoShrink;
213 }
214 
216 {
217  return d->autoShrink;
218 }
219 
220 void TextAttributes::setRotation( int rotation )
221 {
222  d->hasRotation = true;
223  d->rotation = rotation;
224 }
225 
227 {
228  return d->rotation;
229 }
230 
231 void TextAttributes::resetRotation()
232 {
233  d->hasRotation = false;
234  d->rotation = 0;
235 }
236 
237 bool TextAttributes::hasRotation() const
238 {
239  return d->hasRotation;
240 }
241 
242 void TextAttributes::setPen( const QPen& pen )
243 {
244  d->pen = pen;
245 }
246 
248 {
249  return d->pen;
250 }
251 
253 {
254  return d->document.data();
255 }
256 
258 {
259  d->document = QSharedPointer<QTextDocument>(document);
260 }
261 
262 #if !defined(QT_NO_DEBUG_STREAM)
264 {
265  dbg << "KChart::TextAttributes("
266  << "visible=" << ta.isVisible()
267  << "font=" << ta.font().toString() /* What? No QDebug for QFont? */
268  << "fontsize=" << ta.fontSize()
269  << "minimalfontsize=" << ta.minimalFontSize()
270  << "autorotate=" << ta.autoRotate()
271  << "autoshrink=" << ta.autoShrink()
272  << "rotation=" << ta.rotation()
273  << "pen=" << ta.pen()
274  << ")";
275  return dbg;
276 }
277 #endif /* QT_NO_DEBUG_STREAM */
qreal calculatedFontSize(const QSizeF &referenceSize, KChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font size that is used at drawing time.
void setPen(const QPen &pen)
Set the pen to use for rendering the text.
void setFont(const QFont &font)
Set the font to be used for rendering the text.
const QFont calculatedFont(const QObject *autoReferenceArea, KChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font in the size that is used at drawing time.
Class only listed here to document inheritance of some KChart classes.
MeasureOrientation
Measure orientation mode: the way how the absolute value of a KChart::Measure is determined during KC...
Definition: KChartEnums.h:282
QTextDocument * textDocument() const
void setStyleHint(QFont::StyleHint hint, QFont::StyleStrategy strategy)
void setRotation(int rotation)
Set the rotation angle to use for the text.
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
QString toString() const const
Class only listed here to document inheritance of some KChart classes.
void setFontSize(const Measure &measure)
Set the size of the font used for rendering text.
void setTextDocument(QTextDocument *layout)
Sets the document to use for the text.
A set of text attributes.
void setAutoRotate(bool autoRotate)
Set whether the text should be automatically rotated as needed when space is tight.
void setVisible(bool visible)
Set whether the text is to be rendered at all.
void setAutoShrink(bool autoShrink)
Set whether the text should automatically be shrunk if space is tight.
Global namespace.
void setMinimalFontSize(const Measure &measure)
Set the minimal size of the font used for rendering text.
T qobject_cast(QObject *object)
Measure is used to specify relative and absolute sizes in KChart, e.g.
Definition: KChartMeasure.h:37
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Sep 23 2021 22:37:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.