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 "KChartRulerAttributes.h"
21 
22 #include "KChartMath_p.h"
23 
24 #include <QPen>
25 #include <QDebug>
26 
27 #define d d_func()
28 
29 using namespace KChart;
30 
31 class Q_DECL_HIDDEN RulerAttributes::Private
32 {
33     friend class RulerAttributes;
34 public:
35     Private();
36 private:
37     QPen tickMarkPen;
38     QPen majorTickMarkPen;
39     QPen minorTickMarkPen;
40     QPen rulerLinePen;
41 
42     bool majorTickMarkPenIsSet : 1;
43     bool minorTickMarkPenIsSet : 1;
44 
45     bool showMajorTickMarks : 1;
46     bool showMinorTickMarks : 1;
47 
48     bool showRulerLine : 1;
49 
50     bool majorTickLengthIsSet : 1;
51     bool minorTickLengthIsSet : 1;
52 
53     bool showFirstTick : 1;
54 
55     int labelMargin;
56     int majorTickLength;
57     int minorTickLength;
58 
59     RulerAttributes::TickMarkerPensMap customTickMarkPens;
60 };
61 
Private()62 RulerAttributes::Private::Private()
63     : tickMarkPen( Qt::black )
64     , majorTickMarkPen( Qt::black )
65     , minorTickMarkPen( Qt::black )
66     , majorTickMarkPenIsSet( false )
67     , minorTickMarkPenIsSet( false )
68     , showMajorTickMarks( true )
69     , showMinorTickMarks( true )
70     , showRulerLine( false )
71     , majorTickLengthIsSet( false )
72     , minorTickLengthIsSet( false )
73     , showFirstTick( true )
74     , labelMargin( -1 )
75     , majorTickLength( 3 )
76     , minorTickLength( 2 )
77 {
78     tickMarkPen.setCapStyle( Qt::FlatCap );
79     majorTickMarkPen.setCapStyle( Qt::FlatCap );
80     minorTickMarkPen.setCapStyle( Qt::FlatCap );
81 }
82 
RulerAttributes()83 RulerAttributes::RulerAttributes()
84     : _d( new Private() )
85 {
86     // this bloc left empty intentionally
87 }
88 
RulerAttributes(const RulerAttributes & r)89 RulerAttributes::RulerAttributes( const RulerAttributes& r )
90     : _d( new Private( *r.d ) )
91 {
92 }
93 
setTickMarkPen(const QPen & pen)94 void RulerAttributes::setTickMarkPen( const QPen& pen )
95 {
96 	d->tickMarkPen = pen;
97 }
98 
tickMarkPen() const99 QPen RulerAttributes::tickMarkPen() const
100 {
101 	return d->tickMarkPen;
102 }
103 
setMajorTickMarkPen(const QPen & pen)104 void RulerAttributes::setMajorTickMarkPen( const QPen& pen )
105 {
106 	d->majorTickMarkPen = pen;
107 	d->majorTickMarkPenIsSet = true;
108 }
109 
majorTickMarkPenIsSet() const110 bool RulerAttributes::majorTickMarkPenIsSet() const
111 {
112     return d->majorTickMarkPenIsSet;
113 }
114 
majorTickMarkPen() const115 QPen RulerAttributes::majorTickMarkPen() const
116 {
117 	return d->majorTickMarkPenIsSet ? d->majorTickMarkPen : d->tickMarkPen;
118 }
119 
setMinorTickMarkPen(const QPen & pen)120 void RulerAttributes::setMinorTickMarkPen( const QPen& pen )
121 {
122 	d->minorTickMarkPen = pen;
123 	d->minorTickMarkPenIsSet = true;
124 }
125 
minorTickMarkPenIsSet() const126 bool RulerAttributes::minorTickMarkPenIsSet() const
127 {
128     return d->minorTickMarkPenIsSet;
129 }
130 
minorTickMarkPen() const131 QPen RulerAttributes::minorTickMarkPen() const
132 {
133 	return d->minorTickMarkPenIsSet ? d->minorTickMarkPen : d->tickMarkPen;
134 }
135 
setTickMarkPen(qreal value,const QPen & pen)136 void RulerAttributes::setTickMarkPen( qreal value, const QPen& pen )
137 {
138 	if ( !d->customTickMarkPens.contains( value ) )
139 		d->customTickMarkPens.insert( value, pen );
140 }
141 
tickMarkPen(qreal value) const142 QPen RulerAttributes::tickMarkPen( qreal value ) const
143 {
144 	QMapIterator<qreal, QPen> it( d->customTickMarkPens );
145 	while ( it.hasNext() ) {
146 		it.next();
147 		if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
148 			return it.value();
149 	}
150 	return d->tickMarkPen;
151 }
152 
tickMarkPens() const153 RulerAttributes::TickMarkerPensMap RulerAttributes::tickMarkPens() const
154 {
155     return d->customTickMarkPens;
156 }
157 
hasTickMarkPenAt(qreal value) const158 bool RulerAttributes::hasTickMarkPenAt( qreal value ) const
159 {
160 	QMapIterator<qreal, QPen> it( d->customTickMarkPens );
161 	while ( it.hasNext() ) {
162 		it.next();
163 		if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
164 			return true;
165 	}
166 	return false;
167 }
168 
setTickMarkColor(const QColor & color)169 void RulerAttributes::setTickMarkColor( const QColor& color )
170 {
171 	d->tickMarkPen.setColor( color );
172 }
173 
tickMarkColor() const174 QColor RulerAttributes::tickMarkColor() const
175 {
176 	return d->tickMarkPen.color();
177 }
178 
setShowMajorTickMarks(bool show)179 void RulerAttributes::setShowMajorTickMarks( bool show )
180 {
181     d->showMajorTickMarks = show;
182 }
183 
showMajorTickMarks() const184 bool RulerAttributes::showMajorTickMarks() const
185 {
186     return d->showMajorTickMarks;
187 }
188 
setShowMinorTickMarks(bool show)189 void RulerAttributes::setShowMinorTickMarks( bool show )
190 {
191     d->showMinorTickMarks = show;
192 }
193 
showMinorTickMarks() const194 bool RulerAttributes::showMinorTickMarks() const
195 {
196     return d->showMinorTickMarks;
197 }
198 
setLabelMargin(int margin)199 void RulerAttributes::setLabelMargin(int margin)
200 {
201     d->labelMargin = margin;
202 }
203 
labelMargin() const204 int RulerAttributes::labelMargin() const
205 {
206     return d->labelMargin;
207 }
208 
setMajorTickMarkLength(int length)209 void RulerAttributes::setMajorTickMarkLength( int length )
210 {
211     d->majorTickLength = length;
212     d->majorTickLengthIsSet = true;
213 }
214 
majorTickMarkLength() const215 int RulerAttributes::majorTickMarkLength() const
216 {
217     return d->majorTickLength;
218 }
219 
majorTickMarkLengthIsSet() const220 bool RulerAttributes::majorTickMarkLengthIsSet() const
221 {
222     return d->majorTickLengthIsSet;
223 }
224 
setMinorTickMarkLength(int length)225 void RulerAttributes::setMinorTickMarkLength( int length )
226 {
227     d->minorTickLength = length;
228     d->minorTickLengthIsSet = true;
229 }
230 
minorTickMarkLength() const231 int RulerAttributes::minorTickMarkLength() const
232 {
233     return d->minorTickLength;
234 }
235 
minorTickMarkLengthIsSet() const236 bool RulerAttributes::minorTickMarkLengthIsSet() const
237 {
238     return d->minorTickLengthIsSet;
239 }
240 
setShowFirstTick(bool show)241 void RulerAttributes::setShowFirstTick( bool show )
242 {
243     d->showFirstTick = show;
244 }
245 
showFirstTick() const246 bool RulerAttributes::showFirstTick() const
247 {
248     return d->showFirstTick;
249 }
250 
operator =(const RulerAttributes & r)251 RulerAttributes & RulerAttributes::operator=( const RulerAttributes& r )
252 {
253     if ( this == &r )
254         return *this;
255 
256     *d = *r.d;
257 
258     return *this;
259 }
260 
~RulerAttributes()261 RulerAttributes::~RulerAttributes()
262 {
263     delete _d; _d = nullptr;
264 }
265 
operator ==(const RulerAttributes & r) const266 bool RulerAttributes::operator==( const RulerAttributes& r ) const
267 {
268     bool isEqual = tickMarkPen() == r.tickMarkPen() &&
269                    majorTickMarkPen() == r.majorTickMarkPen() &&
270                    minorTickMarkPen() == r.minorTickMarkPen() &&
271                    majorTickMarkPenIsSet() == r.majorTickMarkPenIsSet() &&
272                    minorTickMarkPenIsSet() == r.minorTickMarkPenIsSet() &&
273                    showMajorTickMarks() == r.showMajorTickMarks() &&
274                    showMinorTickMarks() == r.showMinorTickMarks() &&
275                    rulerLinePen() == r.rulerLinePen() &&
276                    showRulerLine() == r.showRulerLine() &&
277                    majorTickMarkLengthIsSet() == r.majorTickMarkLengthIsSet() &&
278                    minorTickMarkLengthIsSet() == r.minorTickMarkLengthIsSet() &&
279                    showFirstTick() == r.showFirstTick() &&
280                    d->customTickMarkPens.size() == r.d->customTickMarkPens.size();
281     if ( !isEqual ) {
282         return false;
283     }
284     QMap< qreal, QPen >::ConstIterator it = d->customTickMarkPens.constBegin();
285     QMap< qreal, QPen >::ConstIterator it2 = r.d->customTickMarkPens.constBegin();
286     for ( ; it != d->customTickMarkPens.constEnd(); ++it, ++it2 ) {
287         if ( it.key() != it2.key() ||  it.value() != it2.value() ) {
288             return false;
289         }
290     }
291     return true;
292 }
293 
setRulerLinePen(const QPen & pen)294 void RulerAttributes::setRulerLinePen(const QPen &pen)
295 {
296     d->rulerLinePen = pen;
297 }
298 
rulerLinePen() const299 QPen RulerAttributes::rulerLinePen() const
300 {
301     return d->rulerLinePen;
302 }
303 
setShowRulerLine(bool show)304 void RulerAttributes::setShowRulerLine( bool show )
305 {
306     d->showRulerLine = show;
307 }
308 
showRulerLine() const309 bool RulerAttributes::showRulerLine() const
310 {
311     return d->showRulerLine;
312 }
313 
314 #if !defined( QT_NO_DEBUG_STREAM )
operator <<(QDebug dbg,const KChart::RulerAttributes & a)315 QDebug operator << ( QDebug dbg, const KChart::RulerAttributes& a )
316 {
317     dbg << "KChart::RulerAttributes("
318             << "tickMarkPen=" << a.tickMarkPen()
319             << "majorTickMarkPen=" << a.majorTickMarkPen()
320             << "minorTickMarkPen=" << a.minorTickMarkPen();
321     const RulerAttributes::TickMarkerPensMap pens( a.tickMarkPens() );
322     QMapIterator<qreal, QPen> it( pens );
323     while ( it.hasNext() ) {
324         it.next();
325         dbg << "customTickMarkPen=(" << it.value() << " : " << it.key() << ")";
326     }
327     dbg << ")";
328     return dbg;
329 }
330 #endif /* QT_NO_DEBUG_STREAM */
331 
332