KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartTextAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 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 
72 TextAttributes::TextAttributes()
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 
214 void TextAttributes::setAutoRotate( bool autoRotate )
215 {
216  d->autoRotate = autoRotate;
217 }
218 
220 {
221  return d->autoRotate;
222 }
223 
224 void TextAttributes::setAutoShrink( bool autoShrink )
225 {
226  d->autoShrink = autoShrink;
227 }
228 
230 {
231  return d->autoShrink;
232 }
233 
234 void TextAttributes::setRotation( int rotation )
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 */

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