KChart

KChartMeasure.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartMeasure.h"
10
11#include <KChartAbstractArea.h>
12#include <KChartCartesianCoordinatePlane.h>
13#include <KChartTextAttributes.h>
14#include <KChartFrameAttributes.h>
15#include <KChartBackgroundAttributes.h>
16#include "KChartMath_p.h"
17
18#include <QWidget>
19
20namespace KChart {
21
22
23Measure::Measure()
24 : mValue( 0.0 ),
25 mMode( KChartEnums::MeasureCalculationModeAuto ),
26 mArea( nullptr ),
27 mOrientation( KChartEnums::MeasureOrientationAuto )
28{
29 // this block left empty intentionally
30}
31
32Measure::Measure( qreal value,
35 : mValue( value ),
36 mMode( mode ),
37 mArea( nullptr ),
38 mOrientation( orientation )
39{
40 // this block left empty intentionally
41}
42
43Measure::Measure( const Measure& r )
44 : mValue( r.value() ),
45 mMode( r.calculationMode() ),
46 mArea( r.referenceArea() ),
47 mOrientation( r.referenceOrientation() )
48{
49 // this block left empty intentionally
50}
51
52Measure & Measure::operator=( const Measure& r )
53{
54 if ( this != &r ) {
55 mValue = r.value();
56 mMode = r.calculationMode();
57 mArea = r.referenceArea();
58 mOrientation = r.referenceOrientation();
59 }
60
61 return *this;
62}
63
64
65qreal Measure::calculatedValue( const QSizeF& autoSize,
66 KChartEnums::MeasureOrientation autoOrientation) const
67{
68 if ( mMode == KChartEnums::MeasureCalculationModeAbsolute ) {
69 return mValue;
70 } else {
71 qreal value = 0.0;
72 const QObject theAutoArea;
73 const QObject* autoArea = &theAutoArea;
74 const QObject* area = mArea ? mArea : autoArea;
75 KChartEnums::MeasureOrientation orientation = mOrientation;
76 switch ( mMode ) {
77 case KChartEnums::MeasureCalculationModeAuto:
78 area = autoArea;
79 orientation = autoOrientation;
80 break;
81 case KChartEnums::MeasureCalculationModeAutoArea:
82 area = autoArea;
83 break;
84 case KChartEnums::MeasureCalculationModeAutoOrientation:
85 orientation = autoOrientation;
86 break;
87 case KChartEnums::MeasureCalculationModeAbsolute: // fall through intended
88 case KChartEnums::MeasureCalculationModeRelative:
89 break;
90 }
91 if ( area ) {
92 QSizeF size;
93 if ( area == autoArea )
94 size = autoSize;
95 else
96 size = sizeOfArea( area );
97 //qDebug() << ( area == autoArea ) << "size" << size;
98 qreal referenceValue = 0;
99 switch ( orientation ) {
100 case KChartEnums::MeasureOrientationAuto: // fall through intended
101 case KChartEnums::MeasureOrientationMinimum:
102 referenceValue = qMin( size.width(), size.height() );
103 break;
104 case KChartEnums::MeasureOrientationMaximum:
105 referenceValue = qMax( size.width(), size.height() );
106 break;
107 case KChartEnums::MeasureOrientationHorizontal:
108 referenceValue = size.width();
109 break;
110 case KChartEnums::MeasureOrientationVertical:
111 referenceValue = size.height();
112 break;
113 }
114 value = mValue / 1000.0 * referenceValue;
115 }
116 return value;
117 }
118}
119
120
121qreal Measure::calculatedValue( const QObject* autoArea,
122 KChartEnums::MeasureOrientation autoOrientation) const
123{
124 return calculatedValue( sizeOfArea( autoArea ), autoOrientation);
125}
126
127
128const QSizeF Measure::sizeOfArea( const QObject* area ) const
129{
130 QSizeF size;
131 const CartesianCoordinatePlane* plane = dynamic_cast<const CartesianCoordinatePlane*>( area );
132 if ( false ) {
133 size = plane->visibleDiagramArea().size();
134 } else {
135 const AbstractArea* kdcArea = dynamic_cast<const AbstractArea*>(area);
136 if ( kdcArea ) {
137 size = kdcArea->geometry().size();
138 //qDebug() << "Measure::sizeOfArea() found kdcArea with size" << size;
139 } else {
140 const QWidget* widget = dynamic_cast<const QWidget*>(area);
141 if ( widget ) {
142 /* ATTENTION: Using the layout does not work: The Legend will never get the right size then!
143 const QLayout * layout = widget->layout();
144 if ( layout ) {
145 size = layout->geometry().size();
146 //qDebug() << "Measure::sizeOfArea() found widget with layout size" << size;
147 } else*/
148 {
149 size = widget->geometry().size();
150 //qDebug() << "Measure::sizeOfArea() found widget with size" << size;
151 }
152 } else if ( mMode != KChartEnums::MeasureCalculationModeAbsolute ) {
153 size = QSizeF(1.0, 1.0);
154 //qDebug("Measure::sizeOfArea() got no valid area.");
155 }
156 }
157 }
158 const QPair< qreal, qreal > factors
159 = GlobalMeasureScaling::instance()->currentFactors();
160 return QSizeF(size.width() * factors.first, size.height() * factors.second);
161}
162
163
164bool Measure::operator==( const Measure& r ) const
165{
166 return( mValue == r.value() &&
167 mMode == r.calculationMode() &&
168 mArea == r.referenceArea() &&
169 mOrientation == r.referenceOrientation() );
170}
171
172GlobalMeasureScaling::GlobalMeasureScaling() :
173 m_paintDevice( nullptr )
174{
175 mFactors.push( qMakePair(qreal(1.0), qreal(1.0)) );
176}
177
178GlobalMeasureScaling::~GlobalMeasureScaling()
179{
180 // this space left empty intentionally
181}
182
183GlobalMeasureScaling* GlobalMeasureScaling::instance()
184{
185 static GlobalMeasureScaling instance;
186 return &instance;
187}
188
189void GlobalMeasureScaling::setFactors(qreal factorX, qreal factorY)
190{
191 instance()->mFactors.push( qMakePair(factorX, factorY) );
192}
193
195{
196 // never remove the initial (1.0. 1.0) setting
197 if ( instance()->mFactors.count() > 1 )
198 instance()->mFactors.pop();
199}
200
201const QPair< qreal, qreal > GlobalMeasureScaling::currentFactors()
202{
203 return instance()->mFactors.top();
204}
205
207{
208 instance()->m_paintDevice = paintDevice;
209}
210
212{
213 return instance()->m_paintDevice;
214}
215
216}
217
218#if !defined(QT_NO_DEBUG_STREAM)
219QDebug operator<<(QDebug dbg, const KChart::Measure& m)
220{
221 dbg << "KChart::Measure("
222 << "value="<<m.value()
223 << "calculationmode="<<m.calculationMode()
224 << "referencearea="<<m.referenceArea()
225 << "referenceorientation="<<m.referenceOrientation()
226 << ")";
227 return dbg;
228}
229#endif /* QT_NO_DEBUG_STREAM */
Declaring the class KChart::Measure.
Project global class providing some enums needed both by KChartParams and by KChartCustomBox.
Definition KChartEnums.h:27
MeasureCalculationMode
Measure calculation mode: the way how the absolute value of a KChart::Measure is determined during KC...
MeasureOrientation
Measure orientation mode: the way how the absolute value of a KChart::Measure is determined during KC...
QRectF visibleDiagramArea() const
Returns the visible part of the diagram area, i.e.
static const QPair< qreal, qreal > currentFactors()
Return the currently active factors.
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.
static void setPaintDevice(QPaintDevice *paintDevice)
Set the paint device to use for calculating font metrics.
static void setFactors(qreal factorX, qreal factorY)
Set new factors to be used by all Measure objects from now on.
Measure is used to specify relative and absolute sizes in KChart, e.g.
const QObject * referenceArea() const
The returned reference area will be derived from AbstractArea or QWidget or both.
QSizeF size() const const
qreal height() const const
qreal width() const const
void push(const T &t)
T & top()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:03:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.