KD Chart 2  [rev.2.7]
KDChartMeasure.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 "KDChartMeasure.h"
24 
25 #include <QWidget>
26 
27 #include <KDChartAbstractArea.h>
29 #include <KDChartTextAttributes.h>
30 #include <KDChartFrameAttributes.h>
32 
33 #include <KDABLibFakes>
34 
35 
36 namespace KDChart {
37 
38 
40  : mValue( 0.0 ),
41  mMode( KDChartEnums::MeasureCalculationModeAuto ),
42  mArea( 0 ),
43  mOrientation( KDChartEnums::MeasureOrientationAuto )
44 {
45  // this bloc left empty intentionally
46 }
47 
51  : mValue( value ),
52  mMode( mode ),
53  mArea( 0 ),
54  mOrientation( orientation )
55 {
56  // this bloc left empty intentionally
57 }
58 
60  : mValue( r.value() ),
61  mMode( r.calculationMode() ),
62  mArea( r.referenceArea() ),
63  mOrientation( r.referenceOrientation() )
64 {
65  // this bloc left empty intentionally
66 }
67 
69 {
70  if ( this != &r ) {
71  mValue = r.value();
72  mMode = r.calculationMode();
73  mArea = r.referenceArea();
74  mOrientation = r.referenceOrientation();
75  }
76 
77  return *this;
78 }
79 
80 
81 qreal Measure::calculatedValue( const QSizeF& autoSize,
82  KDChartEnums::MeasureOrientation autoOrientation) const
83 {
85  return mValue;
86  } else {
87  qreal value = 0.0;
88  const QObject theAutoArea;
89  const QObject* autoArea = &theAutoArea;
90  const QObject* area = mArea ? mArea : autoArea;
91  KDChartEnums::MeasureOrientation orientation = mOrientation;
92  switch ( mMode ) {
94  area = autoArea;
95  orientation = autoOrientation;
96  break;
98  area = autoArea;
99  break;
101  orientation = autoOrientation;
102  break;
103  case KDChartEnums::MeasureCalculationModeAbsolute: // fall through intended
105  break;
106  }
107  if ( area ) {
108  QSizeF size;
109  if ( area == autoArea )
110  size = autoSize;
111  else
112  size = sizeOfArea( area );
113  //qDebug() << ( area == autoArea ) << "size" << size;
114  qreal referenceValue = 0;
115  switch ( orientation ) {
116  case KDChartEnums::MeasureOrientationAuto: // fall through intended
118  referenceValue = qMin( size.width(), size.height() );
119  break;
121  referenceValue = qMax( size.width(), size.height() );
122  break;
124  referenceValue = size.width();
125  break;
127  referenceValue = size.height();
128  break;
129  }
130  value = mValue / 1000.0 * referenceValue;
131  }
132  return value;
133  }
134 }
135 
136 
137 qreal Measure::calculatedValue( const QObject* autoArea,
138  KDChartEnums::MeasureOrientation autoOrientation) const
139 {
140  return calculatedValue( sizeOfArea( autoArea ), autoOrientation);
141 }
142 
143 
144 const QSizeF Measure::sizeOfArea( const QObject* area ) const
145 {
146  QSizeF size;
147  const CartesianCoordinatePlane* plane = dynamic_cast<const CartesianCoordinatePlane*>( area );
148  if ( false ) {
149  size = plane->visibleDiagramArea().size();
150  } else {
151  const AbstractArea* kdcArea = dynamic_cast<const AbstractArea*>(area);
152  if ( kdcArea ) {
153  size = kdcArea->geometry().size();
154  //qDebug() << "Measure::sizeOfArea() found kdcArea with size" << size;
155  } else {
156  const QWidget* widget = dynamic_cast<const QWidget*>(area);
157  if ( widget ) {
158  /* ATTENTION: Using the layout does not work: The Legend will never get the right size then!
159  const QLayout * layout = widget->layout();
160  if ( layout ) {
161  size = layout->geometry().size();
162  //qDebug() << "Measure::sizeOfArea() found widget with layout size" << size;
163  } else*/
164  {
165  size = widget->geometry().size();
166  //qDebug() << "Measure::sizeOfArea() found widget with size" << size;
167  }
168  } else if ( mMode != KDChartEnums::MeasureCalculationModeAbsolute ) {
169  size = QSizeF(1.0, 1.0);
170  //qDebug("Measure::sizeOfArea() got no valid area.");
171  }
172  }
173  }
174  const QPair< qreal, qreal > factors
176  return QSizeF(size.width() * factors.first, size.height() * factors.second);
177 }
178 
179 
180 bool Measure::operator==( const Measure& r ) const
181 {
182  return( mValue == r.value() &&
183  mMode == r.calculationMode() &&
184  mArea == r.referenceArea() &&
185  mOrientation == r.referenceOrientation() );
186 }
187 
189  m_paintDevice( 0 )
190 {
191  mFactors.push( qMakePair(qreal(1.0), qreal(1.0)) );
192 }
193 
195 {
196  // this space left empty intentionally
197 }
198 
200 {
202  return &instance;
203 }
204 
205 void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
206 {
207  instance()->mFactors.push( qMakePair(factorX, factorY) );
208 }
209 
211 {
212  // never remove the initial (1.0. 1.0) setting
213  if ( instance()->mFactors.count() > 1 )
214  instance()->mFactors.pop();
215 }
216 
218 {
219  return instance()->mFactors.top();
220 }
221 
223 {
224  instance()->m_paintDevice = paintDevice;
225 }
226 
228 {
229  return instance()->m_paintDevice;
230 }
231 
232 }
233 
234 #if !defined(QT_NO_DEBUG_STREAM)
235 QDebug operator<<(QDebug dbg, const KDChart::Measure& m)
236 {
237  dbg << "KDChart::Measure("
238  << "value="<<m.value()
239  << "calculationmode="<<m.calculationMode()
240  << "referencearea="<<m.referenceArea()
241  << "referenceorientation="<<m.referenceOrientation()
242  << ")";
243  return dbg;
244 }
245 #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
Project global class providing some enums needed both by KDChartParams and by KDChartCustomBox.
Definition: KDChartEnums.h:40
An area in the chart with a background, a frame, etc.
static const QPair< qreal, qreal > currentFactors()
Return the currently active factors.
Auxiliary class used by the KDChart::Measure and KDChart::Chart class.
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...
Measure & operator=(const Measure &)
static void setPaintDevice(QPaintDevice *paintDevice)
Set the paint device to use for calculating font metrics.
KDChartEnums::MeasureOrientation referenceOrientation() const
bool operator==(const Measure &) const
const QObject * referenceArea() const
The returned reference area will be derived from AbstractArea or QWidget or both. ...
static QPaintDevice * paintDevice()
Return the paint device to use for calculating font metrics.
QRectF visibleDiagramArea() const
Returns the visible part of the diagram area, i.e.
static GlobalMeasureScaling * instance()
Declaring the class KDChart::Measure.
static void resetFactors()
Restore factors to the values before the previous call to setFactors.
MeasureCalculationMode
Measure calculation mode: the way how the absolute value of a KDChart::Measure is determined during K...
Definition: KDChartEnums.h:222
const QSizeF sizeOfArea(const QObject *area) const
KDChartEnums::MeasureCalculationMode calculationMode() const
Measure is used to specify relative and absolute sizes in KDChart, e.g.
Class only listed here to document inheritance of some KDChart classes.
QDebug operator<<(QDebug stream, const DataDimension &r)
Class only listed here to document inheritance of some KDChart classes.
qreal value() const
static void setFactors(qreal factorX, qreal factorY)
Set new factors to be used by all Measure objects from now on.

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/