KD Chart API Documentation  3.1
KDChartTextAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDChartTextAttributes.h"
12 
14 #include <KDChartCartesianCoordinatePlane_p.h>
15 
16 #include <KDABLibFakes>
17 #include <QApplication>
18 #include <QFont>
19 #include <QPen>
20 #include <QSharedPointer>
21 #include <QTextDocument>
22 #include <qglobal.h>
23 
24 #define d d_func()
25 
26 using namespace KDChart;
27 
28 class TextAttributes::Private
29 {
30  friend class TextAttributes;
31 
32 public:
33  Private();
34 
35 private:
36  bool visible = true;
37  QFont font;
38  mutable QFont cachedFont;
39  mutable qreal cachedFontSize = -1.0;
42  bool autoRotate = false;
43  bool autoShrink = false;
44  bool hasRotation = false;
45  int rotation = 0;
46  QPen pen;
47  QSharedPointer<QTextDocument> document;
48 };
49 
50 TextAttributes::Private::Private()
51  : font(QApplication::font())
52  , pen(Qt::black)
53 {
54 }
55 
56 TextAttributes::TextAttributes()
57  : _d(new Private())
58 {
59 }
60 
62  : _d(new Private(*r.d))
63 {
64 }
65 
67 {
68  if (this == &r)
69  return *this;
70 
71  *d = *r.d;
72 
73  return *this;
74 }
75 
77 {
78  delete _d;
79  _d = nullptr;
80 }
81 
83 {
84  // the following works around a bug in gcc 4.3.2
85  // causing StyleHint to be set to Zero when copying a QFont
86  const QFont myFont(font());
87  QFont r_font(r.font());
88  r_font.setStyleHint(myFont.styleHint(), myFont.styleStrategy());
89  return (isVisible() == r.isVisible() && myFont == r_font && fontSize() == r.fontSize() && minimalFontSize() == r.minimalFontSize() && autoRotate() == r.autoRotate() && autoShrink() == r.autoShrink() && rotation() == r.rotation() && pen() == r.pen() && textDocument() == r.textDocument());
90 }
91 
92 void TextAttributes::setVisible(bool visible)
93 {
94  d->visible = visible;
95 }
96 
98 {
99  return d->visible;
100 }
101 
102 void TextAttributes::setFont(const QFont &font)
103 {
104  d->font = font;
105  d->cachedFont = font; // note: we do not set the font's size here, but in calculatedFont()
106  d->cachedFontSize = -1.0;
107 }
108 
109 QFont TextAttributes::font() const
110 {
111  return d->font;
112 }
113 
115 {
116  d->fontSize = measure;
117 }
118 
120 {
121  return d->fontSize;
122 }
123 
125 {
126  d->minimalFontSize = measure;
127 }
128 
130 {
131  return d->minimalFontSize;
132 }
133 
135 {
136  return d->fontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute
137  && d->minimalFontSize.calculationMode() == KDChartEnums::MeasureCalculationModeAbsolute;
138 }
139 
140 qreal TextAttributes::calculatedFontSize(const QSizeF &referenceSize,
141  KDChartEnums::MeasureOrientation autoReferenceOrientation) const
142 {
143  const qreal normalSize = fontSize().calculatedValue(referenceSize, autoReferenceOrientation);
144  const qreal minimalSize = minimalFontSize().calculatedValue(referenceSize, autoReferenceOrientation);
145  return qMax(normalSize, minimalSize);
146 }
147 
148 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && defined(Q_COMPILER_MANGLES_RETURN_TYPE)
149 const
150 #endif
151  qreal
152  TextAttributes::calculatedFontSize(const QObject *autoReferenceArea,
153  KDChartEnums::MeasureOrientation autoReferenceOrientation) const
154 {
155  const qreal normalSize = fontSize().calculatedValue(autoReferenceArea, autoReferenceOrientation);
156  const qreal minimalSize = minimalFontSize().calculatedValue(autoReferenceArea, autoReferenceOrientation);
157  return qMax(normalSize, minimalSize);
158 }
159 
160 const QFont TextAttributes::calculatedFont(const QObject *autoReferenceArea,
161  KDChartEnums::MeasureOrientation autoReferenceOrientation) const
162 {
163  qreal size = NaN;
164 
165  const auto *plane = qobject_cast<const CartesianCoordinatePlane *>(autoReferenceArea);
166  if (plane && plane->hasFixedDataCoordinateSpaceRelation()) {
167  // HACK
168  // if hasFixedDataCoordinateSpaceRelation, we use a zoom trick to keep the diagram at a constant size
169  // even when the plane size changes. calculatedFontSize() usually uses the plane size, not the diagram
170  // size, to determine the font size. here we need to give it the diagram size in order to make the font
171  // size constant, too. see KDCHDEV-219.
172  CartesianCoordinatePlane::Private *priv = CartesianCoordinatePlane::Private::get(const_cast<CartesianCoordinatePlane *>(plane));
173  size = calculatedFontSize(priv->fixedDataCoordinateSpaceRelationPinnedSize,
174  autoReferenceOrientation);
175  } else {
176  size = calculatedFontSize(autoReferenceArea, autoReferenceOrientation);
177  }
178 
179  if (size > 0.0 && d->cachedFontSize != size) {
180  d->cachedFontSize = size;
181  d->cachedFont.setPointSizeF(d->cachedFontSize);
182  }
183 
184  return d->cachedFont;
185 }
186 
187 void TextAttributes::setAutoRotate(bool autoRotate)
188 {
189  d->autoRotate = autoRotate;
190 }
191 
193 {
194  return d->autoRotate;
195 }
196 
197 void TextAttributes::setAutoShrink(bool autoShrink)
198 {
199  d->autoShrink = autoShrink;
200 }
201 
203 {
204  return d->autoShrink;
205 }
206 
207 void TextAttributes::setRotation(int rotation)
208 {
209  d->hasRotation = true;
210  d->rotation = rotation;
211 }
212 
214 {
215  return d->rotation;
216 }
217 
219 {
220  d->hasRotation = false;
221  d->rotation = 0;
222 }
223 
225 {
226  return d->hasRotation;
227 }
228 
229 void TextAttributes::setPen(const QPen &pen)
230 {
231  d->pen = pen;
232 }
233 
235 {
236  return d->pen;
237 }
238 
239 QTextDocument *TextAttributes::textDocument() const
240 {
241  return d->document.data();
242 }
243 
244 void TextAttributes::setTextDocument(QTextDocument *document)
245 {
246  d->document = QSharedPointer<QTextDocument>(document);
247 }
248 
249 #if !defined(QT_NO_DEBUG_STREAM)
250 QDebug operator<<(QDebug dbg, const KDChart::TextAttributes &ta)
251 {
252  dbg << "KDChart::TextAttributes("
253  << "visible=" << ta.isVisible()
254  << "font=" << ta.font().toString() /* What? No QDebug for QFont? */
255  << "fontsize=" << ta.fontSize()
256  << "minimalfontsize=" << ta.minimalFontSize()
257  << "autorotate=" << ta.autoRotate()
258  << "autoshrink=" << ta.autoShrink()
259  << "rotation=" << ta.rotation()
260  << "pen=" << ta.pen()
261  << ")";
262  return dbg;
263 }
264 #endif /* QT_NO_DEBUG_STREAM */
#define d
@ MeasureCalculationModeAbsolute
Definition: KDChartEnums.h:215
Measure is used to specify relative and absolute sizes in KDChart, e.g. font sizes.
qreal calculatedValue(const QObject *autoArea, KDChartEnums::MeasureOrientation autoOrientation) const
A set of text attributes.
void setFontSize(const Measure &measure)
void setAutoRotate(bool autoRotate)
const QFont calculatedFont(const QObject *autoReferenceArea, KDChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font in the size that is used at drawing time.
bool operator==(const TextAttributes &) const
void setMinimalFontSize(const Measure &measure)
void setPen(const QPen &pen)
void setTextDocument(QTextDocument *layout)
TextAttributes & operator=(const TextAttributes &)
void setFont(const QFont &font)
QTextDocument * textDocument() const
void setAutoShrink(bool autoShrink)
qreal calculatedFontSize(const QSizeF &referenceSize, KDChartEnums::MeasureOrientation autoReferenceOrientation) const
Returns the font size that is used at drawing time.
QDebug operator<<(QDebug stream, const DataDimension &r)

© 2001 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-chart/
Generated by doxygen 1.9.1