KChart

KChartMeasure.cpp
1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartMeasure.h"
21 
22 #include <KChartAbstractArea.h>
23 #include <KChartCartesianCoordinatePlane.h>
24 #include <KChartTextAttributes.h>
25 #include <KChartFrameAttributes.h>
26 #include <KChartBackgroundAttributes.h>
27 #include "KChartMath_p.h"
28 
29 #include <QWidget>
30 
31 namespace KChart {
32 
33 
34 Measure::Measure()
35  : mValue( 0.0 ),
36  mMode( KChartEnums::MeasureCalculationModeAuto ),
37  mArea( nullptr ),
38  mOrientation( KChartEnums::MeasureOrientationAuto )
39 {
40  // this bloc left empty intentionally
41 }
42 
43 Measure::Measure( qreal value,
46  : mValue( value ),
47  mMode( mode ),
48  mArea( nullptr ),
49  mOrientation( orientation )
50 {
51  // this bloc left empty intentionally
52 }
53 
54 Measure::Measure( const Measure& r )
55  : mValue( r.value() ),
56  mMode( r.calculationMode() ),
57  mArea( r.referenceArea() ),
58  mOrientation( r.referenceOrientation() )
59 {
60  // this bloc left empty intentionally
61 }
62 
63 Measure & Measure::operator=( const Measure& r )
64 {
65  if ( this != &r ) {
66  mValue = r.value();
67  mMode = r.calculationMode();
68  mArea = r.referenceArea();
69  mOrientation = r.referenceOrientation();
70  }
71 
72  return *this;
73 }
74 
75 
76 qreal Measure::calculatedValue( const QSizeF& autoSize,
77  KChartEnums::MeasureOrientation autoOrientation) const
78 {
79  if ( mMode == KChartEnums::MeasureCalculationModeAbsolute ) {
80  return mValue;
81  } else {
82  qreal value = 0.0;
83  const QObject theAutoArea;
84  const QObject* autoArea = &theAutoArea;
85  const QObject* area = mArea ? mArea : autoArea;
86  KChartEnums::MeasureOrientation orientation = mOrientation;
87  switch ( mMode ) {
88  case KChartEnums::MeasureCalculationModeAuto:
89  area = autoArea;
90  orientation = autoOrientation;
91  break;
92  case KChartEnums::MeasureCalculationModeAutoArea:
93  area = autoArea;
94  break;
95  case KChartEnums::MeasureCalculationModeAutoOrientation:
96  orientation = autoOrientation;
97  break;
98  case KChartEnums::MeasureCalculationModeAbsolute: // fall through intended
99  case KChartEnums::MeasureCalculationModeRelative:
100  break;
101  }
102  if ( area ) {
103  QSizeF size;
104  if ( area == autoArea )
105  size = autoSize;
106  else
107  size = sizeOfArea( area );
108  //qDebug() << ( area == autoArea ) << "size" << size;
109  qreal referenceValue = 0;
110  switch ( orientation ) {
111  case KChartEnums::MeasureOrientationAuto: // fall through intended
112  case KChartEnums::MeasureOrientationMinimum:
113  referenceValue = qMin( size.width(), size.height() );
114  break;
115  case KChartEnums::MeasureOrientationMaximum:
116  referenceValue = qMax( size.width(), size.height() );
117  break;
118  case KChartEnums::MeasureOrientationHorizontal:
119  referenceValue = size.width();
120  break;
121  case KChartEnums::MeasureOrientationVertical:
122  referenceValue = size.height();
123  break;
124  }
125  value = mValue / 1000.0 * referenceValue;
126  }
127  return value;
128  }
129 }
130 
131 
132 qreal Measure::calculatedValue( const QObject* autoArea,
133  KChartEnums::MeasureOrientation autoOrientation) const
134 {
135  return calculatedValue( sizeOfArea( autoArea ), autoOrientation);
136 }
137 
138 
139 const QSizeF Measure::sizeOfArea( const QObject* area ) const
140 {
141  QSizeF size;
142  const CartesianCoordinatePlane* plane = dynamic_cast<const CartesianCoordinatePlane*>( area );
143  if ( false ) {
144  size = plane->visibleDiagramArea().size();
145  } else {
146  const AbstractArea* kdcArea = dynamic_cast<const AbstractArea*>(area);
147  if ( kdcArea ) {
148  size = kdcArea->geometry().size();
149  //qDebug() << "Measure::sizeOfArea() found kdcArea with size" << size;
150  } else {
151  const QWidget* widget = dynamic_cast<const QWidget*>(area);
152  if ( widget ) {
153  /* ATTENTION: Using the layout does not work: The Legend will never get the right size then!
154  const QLayout * layout = widget->layout();
155  if ( layout ) {
156  size = layout->geometry().size();
157  //qDebug() << "Measure::sizeOfArea() found widget with layout size" << size;
158  } else*/
159  {
160  size = widget->geometry().size();
161  //qDebug() << "Measure::sizeOfArea() found widget with size" << size;
162  }
163  } else if ( mMode != KChartEnums::MeasureCalculationModeAbsolute ) {
164  size = QSizeF(1.0, 1.0);
165  //qDebug("Measure::sizeOfArea() got no valid area.");
166  }
167  }
168  }
169  const QPair< qreal, qreal > factors
170  = GlobalMeasureScaling::instance()->currentFactors();
171  return QSizeF(size.width() * factors.first, size.height() * factors.second);
172 }
173 
174 
175 bool Measure::operator==( const Measure& r ) const
176 {
177  return( mValue == r.value() &&
178  mMode == r.calculationMode() &&
179  mArea == r.referenceArea() &&
180  mOrientation == r.referenceOrientation() );
181 }
182 
183 GlobalMeasureScaling::GlobalMeasureScaling() :
184  m_paintDevice( nullptr )
185 {
186  mFactors.push( qMakePair(qreal(1.0), qreal(1.0)) );
187 }
188 
189 GlobalMeasureScaling::~GlobalMeasureScaling()
190 {
191  // this space left empty intentionally
192 }
193 
194 GlobalMeasureScaling* GlobalMeasureScaling::instance()
195 {
196  static GlobalMeasureScaling instance;
197  return &instance;
198 }
199 
200 void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
201 {
202  instance()->mFactors.push( qMakePair(factorX, factorY) );
203 }
204 
206 {
207  // never remove the initial (1.0. 1.0) setting
208  if ( instance()->mFactors.count() > 1 )
209  instance()->mFactors.pop();
210 }
211 
213 {
214  return instance()->mFactors.top();
215 }
216 
218 {
219  instance()->m_paintDevice = paintDevice;
220 }
221 
223 {
224  return instance()->m_paintDevice;
225 }
226 
227 }
228 
229 #if !defined(QT_NO_DEBUG_STREAM)
231 {
232  dbg << "KChart::Measure("
233  << "value="<<m.value()
234  << "calculationmode="<<m.calculationMode()
235  << "referencearea="<<m.referenceArea()
236  << "referenceorientation="<<m.referenceOrientation()
237  << ")";
238  return dbg;
239 }
240 #endif /* QT_NO_DEBUG_STREAM */
Class only listed here to document inheritance of some KChart classes.
QSize size() const const
QSizeF size() const const
static const QPair< qreal, qreal > currentFactors()
Return the currently active factors.
Auxiliary class used by the KChart::Measure and KChart::Chart class.
MeasureCalculationMode
Measure calculation mode: the way how the absolute value of a KChart::Measure is determined during KC...
Definition: KChartEnums.h:219
qreal calculatedValue(const QObject *autoArea, KChartEnums::MeasureOrientation autoOrientation) const
The reference area must either be derived from AbstractArea or from QWidget, so it can also be derive...
Project global class providing some enums needed both by KChartParams and by KChartCustomBox.
Definition: KChartEnums.h:37
QRectF visibleDiagramArea() const
Returns the visible part of the diagram area, i.e.
Declaring the class KChart::Measure.
const QObject * referenceArea() const
The returned reference area will be derived from AbstractArea or QWidget or both. ...
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:287
static void setPaintDevice(QPaintDevice *paintDevice)
Set the paint device to use for calculating font metrics.
An area in the chart with a background, a frame, etc.
static void resetFactors()
Restore factors to the values before the previous call to setFactors.
static QPaintDevice * paintDevice()
Return the paint device to use for calculating font metrics.
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
virtual QRect geometry() const const =0
qreal height() const const
Global namespace.
Measure is used to specify relative and absolute sizes in KChart, e.g.
Definition: KChartMeasure.h:48
qreal width() const const
static void setFactors(qreal factorX, qreal factorY)
Set new factors to be used by all Measure objects from now on.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Sep 29 2020 22:42:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.