KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartMeasure.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 "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 
48 Measure::Measure( qreal value,
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 
188 
189 
191 {
192  mFactors.push( qMakePair(qreal(1.0), qreal(1.0)) );
193 }
194 
196 {
197  // this space left empty intentionally
198 }
199 
201 {
203  return &instance;
204 }
205 
206 void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
207 {
208  instance()->mFactors.push( qMakePair(factorX, factorY) );
209 }
210 
212 {
213  // never remove the initial (1.0. 1.0) setting
214  if ( instance()->mFactors.count() > 1 )
215  instance()->mFactors.pop();
216 }
217 
219 {
220  return instance()->mFactors.top();
221 }
222 
224 {
225  instance()->m_paintDevice = paintDevice;
226 }
227 
229 {
230  return instance()->m_paintDevice;
231 }
232 
233 }
234 
235 #if !defined(QT_NO_DEBUG_STREAM)
236 QDebug operator<<(QDebug dbg, const KDChart::Measure& m)
237 {
238  dbg << "KDChart::Measure("
239  << "value="<<m.value()
240  << "calculationmode="<<m.calculationMode()
241  << "referencearea="<<m.referenceArea()
242  << "referenceorientation="<<m.referenceOrientation()
243  << ")";
244  return dbg;
245 }
246 #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/