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 #ifndef KCHARTMEASURE_H
21 #define KCHARTMEASURE_H
22 
23 #include <QDebug>
24 #include <Qt>
25 #include <QStack>
26 #include "KChartGlobal.h"
27 #include "KChartEnums.h"
28 
29 /** \file KChartMeasure.h
30  *  \brief Declaring the class KChart::Measure.
31  *
32  *
33  */
34 
35 QT_BEGIN_NAMESPACE
36 class QObject;
37 class QPaintDevice;
38 QT_END_NAMESPACE
39 
40 namespace KChart {
41 
42 /**
43   * \class Measure KChartMeasure.h KChartMeasure
44   * \brief Measure is used to specify relative and absolute sizes in KChart, e.g. font sizes.
45   *
46   */
47 
48 class KCHART_EXPORT Measure
49 {
50 public:
51     Measure();
52     /*implicit*/ Measure( qreal value,
53                           KChartEnums::MeasureCalculationMode mode = KChartEnums::MeasureCalculationModeAuto,
54                           KChartEnums::MeasureOrientation orientation = KChartEnums::MeasureOrientationAuto );
55     Measure( const Measure& );
56     Measure &operator= ( const Measure& );
57 
setValue(qreal val)58     void setValue( qreal val ) { mValue = val; }
value()59     qreal value() const { return mValue; }
60 
setCalculationMode(KChartEnums::MeasureCalculationMode mode)61     void setCalculationMode( KChartEnums::MeasureCalculationMode mode ) { mMode = mode; }
calculationMode()62     KChartEnums::MeasureCalculationMode calculationMode() const { return mMode; }
63 
64     /**
65       * The reference area must either be derived from AbstractArea
66       * or from QWidget, so it can also be derived from AbstractAreaWidget.
67       */
setRelativeMode(const QObject * area,KChartEnums::MeasureOrientation orientation)68     void setRelativeMode( const QObject * area,
69                           KChartEnums::MeasureOrientation orientation )
70     {
71         mMode = KChartEnums::MeasureCalculationModeRelative;
72         mArea = area;
73         mOrientation = orientation;
74     }
75 
76     /**
77      * \brief This is a convenience method for specifying a value,
78      *  implicitly setting the calculation mode to MeasureCalculationModeAbsolute.
79      *
80      * Calling setAbsoluteValue( value ) is the same as calling
81 \verbatim
82     setValue( value );
83     setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
84 \endverbatim
85      */
setAbsoluteValue(qreal val)86     void setAbsoluteValue( qreal val )
87     {
88         mMode = KChartEnums::MeasureCalculationModeAbsolute;
89         mValue = val;
90     }
91 
92     /**
93       * The reference area must either be derived from AbstractArea
94       * or from QWidget, so it can also be derived from AbstractAreaWidget.
95       */
setReferenceArea(const QObject * area)96     void setReferenceArea( const QObject * area ) { mArea = area; }
97     /**
98       * The returned reference area will be derived from AbstractArea
99       * or QWidget or both.
100       */
referenceArea()101     const QObject * referenceArea() const { return mArea; }
102 
setReferenceOrientation(KChartEnums::MeasureOrientation orientation)103     void setReferenceOrientation( KChartEnums::MeasureOrientation orientation ) { mOrientation = orientation; }
referenceOrientation()104     KChartEnums::MeasureOrientation referenceOrientation() const { return mOrientation; }
105 
106     /**
107       * The reference area must either be derived from AbstractArea
108       * or from QWidget, so it can also be derived from AbstractAreaWidget.
109       */
110     qreal calculatedValue( const QObject * autoArea, KChartEnums::MeasureOrientation autoOrientation ) const;
111     qreal calculatedValue( const QSizeF& autoSize, KChartEnums::MeasureOrientation autoOrientation ) const;
112     const QSizeF sizeOfArea( const QObject* area ) const;
113 
114     bool operator==( const Measure& ) const;
115     bool operator!=( const Measure& other ) const { return !operator==(other); }
116 
117 private:
118     qreal mValue;
119     KChartEnums::MeasureCalculationMode mMode;
120     const QObject* mArea;
121     KChartEnums::MeasureOrientation mOrientation;
122 }; // End of class Measure
123 
124 
125 
126 /**
127  * Auxiliary class used by the KChart::Measure and KChart::Chart class.
128  *
129  * Normally there should be no need to call any of these methods yourself.
130  *
131  * They are used by KChart::Chart::paint( QPainter*, const QRect& )
132  * to adjust all of the relative Measures according to the target
133  * rectangle's size.
134  *
135  * Default factors are (1.0, 1.0)
136  */
137 class GlobalMeasureScaling
138 {
139 public:
140     static GlobalMeasureScaling* instance();
141 
142     GlobalMeasureScaling();
143     virtual ~GlobalMeasureScaling();
144 
145 public:
146     /**
147      * Set new factors to be used by all Measure objects from now on.
148      * Previous values will be saved on a stack internally.
149      */
150     static void setFactors(qreal factorX, qreal factorY);
151 
152     /**
153      * Restore factors to the values before the previous call to
154      * setFactors. The current values are popped off a stack internally.
155      */
156     static void resetFactors();
157 
158     /**
159      * Return the currently active factors.
160      */
161     static const QPair< qreal, qreal > currentFactors();
162 
163     /**
164      * Set the paint device to use for calculating font metrics.
165      */
166     static void setPaintDevice( QPaintDevice* paintDevice );
167 
168     /**
169      * Return the paint device to use for calculating font metrics.
170      */
171     static QPaintDevice* paintDevice();
172 
173 private:
174     QStack< QPair< qreal, qreal > > mFactors;
175     QPaintDevice* m_paintDevice;
176 };
177 
178 }
179 
180 #if !defined(QT_NO_DEBUG_STREAM)
181 KCHART_EXPORT QDebug operator<<(QDebug, const KChart::Measure& );
182 #endif /* QT_NO_DEBUG_STREAM */
183 
184 #endif // KCHARTMEASURE_H
185