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 "KChartDataValueAttributes.h"
21 
22 #include <QVariant>
23 #include <QDebug>
24 #include "KChartRelativePosition.h"
25 #include "KChartPosition.h"
26 #include "KChartMath_p.h"
27 #include <KChartTextAttributes.h>
28 #include <KChartFrameAttributes.h>
29 #include <KChartBackgroundAttributes.h>
30 #include <KChartMarkerAttributes.h>
31 
32 // FIXME till
33 #define KCHART_DATA_VALUE_AUTO_DIGITS 4
34 
35 
36 #define d d_func()
37 
38 using namespace KChart;
39 
40 class Q_DECL_HIDDEN DataValueAttributes::Private
41 {
42     friend class DataValueAttributes;
43 public:
44     Private();
45 private:
46     TextAttributes textAttributes;
47     FrameAttributes frameAttributes;
48     BackgroundAttributes backgroundAttributes;
49     MarkerAttributes markerAttributes;
50     QString prefix;
51     QString suffix;
52     QString dataLabel;
53     RelativePosition negativeRelPos;
54     RelativePosition positiveRelPos;
55     qint16 decimalDigits;
56     qint16 powerOfTenDivisor;
57     bool visible : 1;
58     bool showInfinite : 1;
59     bool showRepetitiveDataLabels : 1;
60     bool showOverlappingDataLabels : 1;
61     bool usePercentage : 1;
62     bool mirrorNegativeValueTextRotation : 1;
63 };
64 
Private()65 DataValueAttributes::Private::Private() :
66     decimalDigits( KCHART_DATA_VALUE_AUTO_DIGITS ),
67     powerOfTenDivisor( 0 ),
68     visible( false ),
69     showInfinite( true )
70 {
71     Measure me( 20.0, KChartEnums::MeasureCalculationModeAuto, KChartEnums::MeasureOrientationAuto );
72     textAttributes.setFontSize( me );
73     me.setValue( 8.0 );
74     me.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
75     textAttributes.setMinimalFontSize( me );
76     textAttributes.setRotation( -45 );
77 
78     // we set the Position to unknown: so the diagrams can take their own decisions
79     positiveRelPos.setReferencePosition( Position::Unknown );
80     negativeRelPos.setReferencePosition( Position::Unknown );
81 
82     positiveRelPos.setAlignment( Qt::AlignTop | Qt::AlignRight );
83     negativeRelPos.setAlignment( Qt::AlignBottom | Qt::AlignRight );
84 
85     showRepetitiveDataLabels = false;
86     showOverlappingDataLabels = false;
87 
88     usePercentage = false;
89     mirrorNegativeValueTextRotation = false;
90 }
91 
92 
DataValueAttributes()93 DataValueAttributes::DataValueAttributes()
94     : _d( new Private() )
95 {
96 }
97 
DataValueAttributes(const DataValueAttributes & r)98 DataValueAttributes::DataValueAttributes( const DataValueAttributes& r )
99     : _d( new Private( *r.d ) )
100 {
101 }
102 
operator =(const DataValueAttributes & r)103 DataValueAttributes & DataValueAttributes::operator=( const DataValueAttributes& r )
104 {
105     if ( this == &r )
106         return *this;
107 
108     *d = *r.d;
109 
110     return *this;
111 }
112 
~DataValueAttributes()113 DataValueAttributes::~DataValueAttributes()
114 {
115     delete _d; _d = nullptr;
116 }
117 
118 
operator ==(const DataValueAttributes & r) const119 bool DataValueAttributes::operator==( const DataValueAttributes& r ) const
120 {
121     return  isVisible() == r.isVisible() &&
122             textAttributes() == r.textAttributes() &&
123             frameAttributes() == r.frameAttributes() &&
124             backgroundAttributes() == r.backgroundAttributes() &&
125             markerAttributes() == r.markerAttributes() &&
126             decimalDigits() == r.decimalDigits() &&
127             prefix() == r.prefix() &&
128             suffix() == r.suffix() &&
129             dataLabel() == r.dataLabel() &&
130             powerOfTenDivisor() == r.powerOfTenDivisor() &&
131             showInfinite() == r.showInfinite() &&
132             negativePosition() == r.negativePosition() &&
133             positivePosition() == r.positivePosition() &&
134             showRepetitiveDataLabels() == r.showRepetitiveDataLabels() &&
135             showOverlappingDataLabels() == r.showOverlappingDataLabels() &&
136             usePercentage() == r.usePercentage() &&
137             mirrorNegativeValueTextRotation() == r.mirrorNegativeValueTextRotation();
138 }
139 
140 /*static*/
defaultAttributes()141 const DataValueAttributes& DataValueAttributes::defaultAttributes()
142 {
143     static const DataValueAttributes theDefaultDataValueAttributes;
144     return theDefaultDataValueAttributes;
145 }
146 
147 /*static*/
defaultAttributesAsVariant()148 const QVariant& DataValueAttributes::defaultAttributesAsVariant()
149 {
150     static const QVariant theDefaultDataValueAttributesVariant = QVariant::fromValue(defaultAttributes());
151     return theDefaultDataValueAttributesVariant;
152 }
153 
154 
setVisible(bool visible)155 void DataValueAttributes::setVisible( bool visible )
156 {
157     d->visible = visible;
158 }
159 
isVisible() const160 bool DataValueAttributes::isVisible() const
161 {
162     return d->visible;
163 }
164 
setTextAttributes(const TextAttributes & a)165 void DataValueAttributes::setTextAttributes( const TextAttributes &a )
166 {
167     d->textAttributes = a;
168 }
169 
textAttributes() const170 TextAttributes DataValueAttributes::textAttributes() const
171 {
172     return d->textAttributes;
173 }
174 
setFrameAttributes(const FrameAttributes & a)175 void DataValueAttributes::setFrameAttributes( const FrameAttributes &a )
176 {
177     d->frameAttributes = a;
178 }
179 
frameAttributes() const180 FrameAttributes DataValueAttributes::frameAttributes() const
181 {
182     return d->frameAttributes;
183 }
184 
setBackgroundAttributes(const BackgroundAttributes & a)185 void DataValueAttributes::setBackgroundAttributes( const BackgroundAttributes &a )
186 {
187     d->backgroundAttributes = a;
188 }
189 
backgroundAttributes() const190 BackgroundAttributes DataValueAttributes::backgroundAttributes() const
191 {
192     return d->backgroundAttributes;
193 }
194 
setMarkerAttributes(const MarkerAttributes & a)195 void DataValueAttributes::setMarkerAttributes( const MarkerAttributes &a )
196 {
197     d->markerAttributes = a;
198 }
199 
markerAttributes() const200 MarkerAttributes DataValueAttributes::markerAttributes() const
201 {
202     return d->markerAttributes;
203 }
204 
setMirrorNegativeValueTextRotation(bool enable)205 void DataValueAttributes::setMirrorNegativeValueTextRotation( bool enable )
206 {
207     d->mirrorNegativeValueTextRotation = enable;
208 }
209 
mirrorNegativeValueTextRotation() const210 bool DataValueAttributes::mirrorNegativeValueTextRotation() const
211 {
212     return d->mirrorNegativeValueTextRotation;
213 }
214 
setUsePercentage(bool enable)215 void DataValueAttributes::setUsePercentage( bool enable )
216 {
217     d->usePercentage = enable;
218 }
219 
usePercentage() const220 bool DataValueAttributes::usePercentage() const
221 {
222     return d->usePercentage;
223 }
224 
setDecimalDigits(int digits)225 void DataValueAttributes::setDecimalDigits( int digits )
226 {
227     d->decimalDigits = digits;
228 }
229 
decimalDigits() const230 int DataValueAttributes::decimalDigits() const
231 {
232     return d->decimalDigits;
233 }
234 
setPrefix(const QString prefixString)235 void DataValueAttributes::setPrefix( const QString prefixString )
236 {
237     d->prefix = prefixString;
238 }
239 
prefix() const240 QString DataValueAttributes::prefix() const
241 {
242     return d->prefix;
243 }
244 
setSuffix(const QString suffixString)245 void DataValueAttributes::setSuffix( const QString suffixString )
246 {
247     d->suffix  = suffixString;
248 }
249 
suffix() const250 QString DataValueAttributes::suffix() const
251 {
252     return d->suffix;
253 }
254 
setDataLabel(const QString label)255 void DataValueAttributes::setDataLabel( const QString label )
256 {
257     d->dataLabel =  label;
258 }
259 
dataLabel() const260 QString DataValueAttributes::dataLabel() const
261 {
262     return d->dataLabel;
263 }
264 
showRepetitiveDataLabels() const265 bool DataValueAttributes::showRepetitiveDataLabels() const
266 {
267     return d->showRepetitiveDataLabels;
268 }
269 
setShowRepetitiveDataLabels(bool showRepetitiveDataLabels)270 void DataValueAttributes::setShowRepetitiveDataLabels( bool showRepetitiveDataLabels )
271 {
272     d->showRepetitiveDataLabels = showRepetitiveDataLabels;
273 }
274 
showOverlappingDataLabels() const275 bool DataValueAttributes::showOverlappingDataLabels() const
276 {
277     return d->showOverlappingDataLabels;
278 }
279 
setShowOverlappingDataLabels(bool showOverlappingDataLabels)280 void DataValueAttributes::setShowOverlappingDataLabels( bool showOverlappingDataLabels )
281 {
282     d->showOverlappingDataLabels = showOverlappingDataLabels;
283 }
284 
setPowerOfTenDivisor(int powerOfTenDivisor)285 void DataValueAttributes::setPowerOfTenDivisor( int powerOfTenDivisor )
286 {
287     d->powerOfTenDivisor = powerOfTenDivisor;
288 }
289 
powerOfTenDivisor() const290 int DataValueAttributes::powerOfTenDivisor() const
291 {
292     return d->powerOfTenDivisor;
293 }
294 
setShowInfinite(bool infinite)295 void DataValueAttributes::setShowInfinite( bool infinite )
296 {
297     d->showInfinite = infinite;
298 }
299 
showInfinite() const300 bool DataValueAttributes::showInfinite() const
301 {
302     return d->showInfinite;
303 }
304 
setNegativePosition(const RelativePosition & relPosition)305 void DataValueAttributes::setNegativePosition( const RelativePosition& relPosition )
306 {
307     d->negativeRelPos = relPosition;
308 }
309 
negativePosition() const310 const RelativePosition DataValueAttributes::negativePosition() const
311 {
312     return d->negativeRelPos;
313 }
314 
setPositivePosition(const RelativePosition & relPosition)315 void DataValueAttributes::setPositivePosition( const RelativePosition& relPosition )
316 {
317     d->positiveRelPos = relPosition;
318 }
319 
positivePosition() const320 const RelativePosition DataValueAttributes::positivePosition() const
321 {
322     return d->positiveRelPos;
323 }
324 
325 #if !defined(QT_NO_DEBUG_STREAM)
operator <<(QDebug dbg,const KChart::DataValueAttributes & val)326 QDebug operator<<(QDebug dbg, const KChart::DataValueAttributes& val )
327 {
328     dbg << "RelativePosition DataValueAttributes("
329 	<< "visible="<<val.isVisible()
330 	<< "textattributes="<<val.textAttributes()
331 	<< "frameattributes="<<val.frameAttributes()
332 	<< "backgroundattributes="<<val.backgroundAttributes()
333 	<< "decimaldigits="<<val.decimalDigits()
334 	<< "poweroftendivisor="<<val.powerOfTenDivisor()
335 	<< "showinfinite="<<val.showInfinite()
336 	<< "negativerelativeposition="<<val.negativePosition()
337 	<< "positiverelativeposition="<<val.positivePosition()
338     << "showRepetitiveDataLabels="<<val.showRepetitiveDataLabels()
339     << "showOverlappingDataLabels="<<val.showOverlappingDataLabels()
340     <<")";
341     return dbg;
342 }
343 #endif /* QT_NO_DEBUG_STREAM */
344