KD Chart 2  [rev.2.7]
KDChartTextAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartTextAttributes.h"
24 
26 #include <KDChartCartesianCoordinatePlane_p.h>
27 
28 #include <QFont>
29 #include <QPen>
30 #include <qglobal.h>
31 #include <QApplication>
32 #include <QSharedPointer>
33 #include <QTextDocument>
34 #include <KDABLibFakes>
35 
36 #define d d_func()
37 
38 using namespace KDChart;
39 
40 class TextAttributes::Private
41 {
42  friend class TextAttributes;
43 public:
44  Private();
45 private:
46  bool visible;
47  QFont font;
48  mutable QFont cachedFont;
49  mutable qreal cachedFontSize;
52  bool autoRotate;
53  bool autoShrink;
54  bool hasRotation;
55  int rotation;
56  QPen pen;
57  QSharedPointer<QTextDocument> document;
58 };
59 
60 TextAttributes::Private::Private()
61  : visible( true ),
62  font( QApplication::font() ),
63  cachedFontSize( -1.0 ),
64  autoRotate( false ),
65  autoShrink( false ),
66  hasRotation( false ),
67  rotation( 0 ),
68  pen( Qt::black )
69 {
70 }
71 
73  : _d( new Private() )
74 {
75 }
76 
78  : _d( new Private( *r.d ) )
79 {
80 
81 }
82 
84 {
85  if ( this == &r )
86  return *this;
87 
88  *d = *r.d;
89 
90  return *this;
91 }
92 
94 {
95  delete _d; _d = 0;
96 }
97 
98 
100 {
101  // the following works around a bug in gcc 4.3.2
102  // causing StyleHint to be set to Zero when copying a QFont
103  const QFont myFont( font() );
104  QFont r_font( r.font() );
105  r_font.setStyleHint( myFont.styleHint(), myFont.styleStrategy() );
106  return ( isVisible() == r.isVisible() &&
107  myFont == r_font &&
108  fontSize() == r.fontSize() &&
109  minimalFontSize() == r.minimalFontSize() &&
110  autoRotate() == r.autoRotate() &&
111  autoShrink() == r.autoShrink() &&
112  rotation() == r.rotation() &&
113  pen() == r.pen() &&
114  textDocument() == r.textDocument() );
115 }
116 
117 
118 void TextAttributes::setVisible( bool visible )
119 {
120  d->visible = visible;
121 }
122 
124 {
125  return d->visible;
126 }
127 
128 void TextAttributes::setFont( const QFont& font )
129 {
130  d->font = font;
131  d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont()
132  d->cachedFontSize = -1.0;
133 }
134 
135 QFont TextAttributes::font() const
136 {
137  return d->font;
138 }
139 
140 void TextAttributes::setFontSize( const Measure & measure )
141 {
142  d->fontSize = measure;
143 }
144 
146 {
147  return d->fontSize;
148 }
149 
151 {
152  d->minimalFontSize = measure;
153 }
154 
156 {
157  return d->minimalFontSize;
158 }
159 
161 {
162  return d->fontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute
163  && d->minimalFontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute;
164 }
165 
166 qreal TextAttributes::calculatedFontSize( const QSizeF &referenceSize,
167  KDChartEnums::MeasureOrientation autoReferenceOrientation ) const
168 {
169  const qreal normalSize = fontSize().calculatedValue( referenceSize, autoReferenceOrientation );
170  const qreal minimalSize = minimalFontSize().calculatedValue( referenceSize, autoReferenceOrientation );
171  return qMax( normalSize, minimalSize );
172 }
173 
174 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
175 const
176 #endif
177 qreal TextAttributes::calculatedFontSize( const QObject* autoReferenceArea,
178  KDChartEnums::MeasureOrientation autoReferenceOrientation ) const
179 {
180  const qreal normalSize = fontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
181  const qreal minimalSize = minimalFontSize().calculatedValue( autoReferenceArea, autoReferenceOrientation );
182  return qMax( normalSize, minimalSize );
183 }
184 
185 const QFont TextAttributes::calculatedFont( const QObject* autoReferenceArea,
186  KDChartEnums::MeasureOrientation autoReferenceOrientation ) const
187 {
188  qreal size = NaN;
189 
190  const CartesianCoordinatePlane* plane = qobject_cast< const CartesianCoordinatePlane* >( autoReferenceArea );
191  if ( plane && plane->hasFixedDataCoordinateSpaceRelation() ) {
192  // HACK
193  // if hasFixedDataCoordinateSpaceRelation, we use a zoom trick to keep the diagram at a constant size
194  // even when the plane size changes. calculatedFontSize() usually uses the plane size, not the diagram
195  // size, to determine the font size. here we need to give it the diagram size in order to make the font
196  // size constant, too. see KDCHDEV-219.
197  CartesianCoordinatePlane::Private *priv
198  = CartesianCoordinatePlane::Private::get( const_cast< CartesianCoordinatePlane * >( plane ) );
199  size = calculatedFontSize( priv->fixedDataCoordinateSpaceRelationPinnedSize,
200  autoReferenceOrientation );
201  } else {
202  size = calculatedFontSize( autoReferenceArea, autoReferenceOrientation );
203  }
204 
205  if ( size > 0.0 && d->cachedFontSize != size ) {
206  d->cachedFontSize = size;
207  d->cachedFont.setPointSizeF( d->cachedFontSize );
208  }
209 
210  return d->cachedFont;
211 }
212 
213 
215 {
216  d->autoRotate = autoRotate;
217 }
218 
220 {
221  return d->autoRotate;
222 }
223 
225 {
226  d->autoShrink = autoShrink;
227 }
228 
230 {
231  return d->autoShrink;
232 }
233 
235 {
236  d->hasRotation = true;
237  d->rotation = rotation;
238 }
239 
241 {
242  return d->rotation;
243 }
244 
246 {
247  d->hasRotation = false;
248  d->rotation = 0;
249 }
250 
252 {
253  return d->hasRotation;
254 }
255 
256 void TextAttributes::setPen( const QPen& pen )
257 {
258  d->pen = pen;
259 }
260 
262 {
263  return d->pen;
264 }
265 
267 {
268  return d->document.data();
269 }
270 
272 {
273  d->document = QSharedPointer<QTextDocument>(document);
274 }
275 
276 #if !defined(QT_NO_DEBUG_STREAM)
277 QDebug operator<<(QDebug dbg, const KDChart::TextAttributes& ta)
278 {
279  dbg << "KDChart::TextAttributes("
280  << "visible=" << ta.isVisible()
281  << "font=" << ta.font().toString() /* What? No QDebug for QFont? */
282  << "fontsize=" << ta.fontSize()
283  << "minimalfontsize=" << ta.minimalFontSize()
284  << "autorotate=" << ta.autoRotate()
285  << "autoshrink=" << ta.autoShrink()
286  << "rotation=" << ta.rotation()
287  << "pen=" << ta.pen()
288  << ")";
289  return dbg;
290 }
291 #endif /* QT_NO_DEBUG_STREAM */
MeasureOrientation
Measure orientation mode: the way how the absolute value of a KDChart::Measure is determined during K...
Definition: KDChartEnums.h:290
bool operator==(const TextAttributes &) const
qreal calculatedValue(const QObject *autoArea, KDChartEnums::MeasureOrientation autoOrientation) const
The reference area must either be derived from AbstractArea or from QWidget, so it can also be derive...
void setPen(const QPen &pen)
Set the pen to use for rendering the text.
QTextDocument * textDocument() const
void setAutoShrink(bool autoShrink)
Set whether the text should automatically be shrunk if space is tight.
void setRotation(int rotation)
Set the rotation angle to use for the text.
void setFontSize(const Measure &measure)
Set the size of the font used for rendering text.
#define d
void setFont(const QFont &font)
Set the font to be used for rendering the text.
const QFont calculatedFont(const QObject *autoReferenceArea, KDChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font in the size that is used at drawing time.
Class only listed here to document inheritance of some KDChart classes.
TextAttributes & operator=(const TextAttributes &)
Measure is used to specify relative and absolute sizes in KDChart, e.g.
Class only listed here to document inheritance of some KDChart classes.
void setMinimalFontSize(const Measure &measure)
Set the minimal size of the font used for rendering text.
QDebug operator<<(QDebug stream, const DataDimension &r)
void setVisible(bool visible)
Set whether the text is to be rendered at all.
void setTextDocument(QTextDocument *layout)
Sets the document to use for the text.
qreal calculatedFontSize(const QSizeF &referenceSize, KDChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font size that is used at drawing time.
A set of text attributes.
void setAutoRotate(bool autoRotate)
Set whether the text should be automatically rotated as needed when space is tight.

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/