KChart

KChartRulerAttributes.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 "KChartRulerAttributes.h"
10 
11 #include "KChartMath_p.h"
12 
13 #include <QPen>
14 #include <QDebug>
15 
16 #define d d_func()
17 
18 using namespace KChart;
19 
20 class Q_DECL_HIDDEN RulerAttributes::Private
21 {
22  friend class RulerAttributes;
23 public:
24  Private();
25 private:
26  QPen tickMarkPen;
27  QPen majorTickMarkPen;
28  QPen minorTickMarkPen;
29  QPen rulerLinePen;
30 
31  bool majorTickMarkPenIsSet : 1;
32  bool minorTickMarkPenIsSet : 1;
33 
34  bool showMajorTickMarks : 1;
35  bool showMinorTickMarks : 1;
36 
37  bool showRulerLine : 1;
38 
39  bool majorTickLengthIsSet : 1;
40  bool minorTickLengthIsSet : 1;
41 
42  bool showFirstTick : 1;
43 
44  int labelMargin;
45  int majorTickLength;
46  int minorTickLength;
47 
48  RulerAttributes::TickMarkerPensMap customTickMarkPens;
49 };
50 
51 RulerAttributes::Private::Private()
52  : tickMarkPen( Qt::black )
53  , majorTickMarkPen( Qt::black )
54  , minorTickMarkPen( Qt::black )
55  , majorTickMarkPenIsSet( false )
56  , minorTickMarkPenIsSet( false )
57  , showMajorTickMarks( true )
58  , showMinorTickMarks( true )
59  , showRulerLine( false )
60  , majorTickLengthIsSet( false )
61  , minorTickLengthIsSet( false )
62  , showFirstTick( true )
63  , labelMargin( -1 )
64  , majorTickLength( 3 )
65  , minorTickLength( 2 )
66 {
67  tickMarkPen.setCapStyle( Qt::FlatCap );
68  majorTickMarkPen.setCapStyle( Qt::FlatCap );
69  minorTickMarkPen.setCapStyle( Qt::FlatCap );
70 }
71 
72 RulerAttributes::RulerAttributes()
73  : _d( new Private() )
74 {
75  // this block left empty intentionally
76 }
77 
78 RulerAttributes::RulerAttributes( const RulerAttributes& r )
79  : _d( new Private( *r.d ) )
80 {
81 }
82 
84 {
85  d->tickMarkPen = pen;
86 }
87 
88 QPen RulerAttributes::tickMarkPen() const
89 {
90  return d->tickMarkPen;
91 }
92 
94 {
95  d->majorTickMarkPen = pen;
96  d->majorTickMarkPenIsSet = true;
97 }
98 
99 bool RulerAttributes::majorTickMarkPenIsSet() const
100 {
101  return d->majorTickMarkPenIsSet;
102 }
103 
104 QPen RulerAttributes::majorTickMarkPen() const
105 {
106  return d->majorTickMarkPenIsSet ? d->majorTickMarkPen : d->tickMarkPen;
107 }
108 
110 {
111  d->minorTickMarkPen = pen;
112  d->minorTickMarkPenIsSet = true;
113 }
114 
115 bool RulerAttributes::minorTickMarkPenIsSet() const
116 {
117  return d->minorTickMarkPenIsSet;
118 }
119 
120 QPen RulerAttributes::minorTickMarkPen() const
121 {
122  return d->minorTickMarkPenIsSet ? d->minorTickMarkPen : d->tickMarkPen;
123 }
124 
125 void RulerAttributes::setTickMarkPen( qreal value, const QPen& pen )
126 {
127  if ( !d->customTickMarkPens.contains( value ) )
128  d->customTickMarkPens.insert( value, pen );
129 }
130 
131 QPen RulerAttributes::tickMarkPen( qreal value ) const
132 {
133  QMapIterator<qreal, QPen> it( d->customTickMarkPens );
134  while ( it.hasNext() ) {
135  it.next();
136  if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
137  return it.value();
138  }
139  return d->tickMarkPen;
140 }
141 
142 RulerAttributes::TickMarkerPensMap RulerAttributes::tickMarkPens() const
143 {
144  return d->customTickMarkPens;
145 }
146 
147 bool RulerAttributes::hasTickMarkPenAt( qreal value ) const
148 {
149  QMapIterator<qreal, QPen> it( d->customTickMarkPens );
150  while ( it.hasNext() ) {
151  it.next();
152  if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
153  return true;
154  }
155  return false;
156 }
157 
159 {
160  d->tickMarkPen.setColor( color );
161 }
162 
163 QColor RulerAttributes::tickMarkColor() const
164 {
165  return d->tickMarkPen.color();
166 }
167 
169 {
170  d->showMajorTickMarks = show;
171 }
172 
173 bool RulerAttributes::showMajorTickMarks() const
174 {
175  return d->showMajorTickMarks;
176 }
177 
179 {
180  d->showMinorTickMarks = show;
181 }
182 
183 bool RulerAttributes::showMinorTickMarks() const
184 {
185  return d->showMinorTickMarks;
186 }
187 
189 {
190  d->labelMargin = margin;
191 }
192 
193 int RulerAttributes::labelMargin() const
194 {
195  return d->labelMargin;
196 }
197 
199 {
200  d->majorTickLength = length;
201  d->majorTickLengthIsSet = true;
202 }
203 
204 int RulerAttributes::majorTickMarkLength() const
205 {
206  return d->majorTickLength;
207 }
208 
209 bool RulerAttributes::majorTickMarkLengthIsSet() const
210 {
211  return d->majorTickLengthIsSet;
212 }
213 
215 {
216  d->minorTickLength = length;
217  d->minorTickLengthIsSet = true;
218 }
219 
220 int RulerAttributes::minorTickMarkLength() const
221 {
222  return d->minorTickLength;
223 }
224 
225 bool RulerAttributes::minorTickMarkLengthIsSet() const
226 {
227  return d->minorTickLengthIsSet;
228 }
229 
231 {
232  d->showFirstTick = show;
233 }
234 
235 bool RulerAttributes::showFirstTick() const
236 {
237  return d->showFirstTick;
238 }
239 
240 RulerAttributes & RulerAttributes::operator=( const RulerAttributes& r )
241 {
242  if ( this == &r )
243  return *this;
244 
245  *d = *r.d;
246 
247  return *this;
248 }
249 
250 RulerAttributes::~RulerAttributes()
251 {
252  delete _d; _d = nullptr;
253 }
254 
255 bool RulerAttributes::operator==( const RulerAttributes& r ) const
256 {
257  bool isEqual = tickMarkPen() == r.tickMarkPen() &&
258  majorTickMarkPen() == r.majorTickMarkPen() &&
259  minorTickMarkPen() == r.minorTickMarkPen() &&
260  majorTickMarkPenIsSet() == r.majorTickMarkPenIsSet() &&
261  minorTickMarkPenIsSet() == r.minorTickMarkPenIsSet() &&
262  showMajorTickMarks() == r.showMajorTickMarks() &&
263  showMinorTickMarks() == r.showMinorTickMarks() &&
264  rulerLinePen() == r.rulerLinePen() &&
265  showRulerLine() == r.showRulerLine() &&
266  majorTickMarkLengthIsSet() == r.majorTickMarkLengthIsSet() &&
267  minorTickMarkLengthIsSet() == r.minorTickMarkLengthIsSet() &&
268  showFirstTick() == r.showFirstTick() &&
269  d->customTickMarkPens.size() == r.d->customTickMarkPens.size();
270  if ( !isEqual ) {
271  return false;
272  }
273  QMap< qreal, QPen >::ConstIterator it = d->customTickMarkPens.constBegin();
274  QMap< qreal, QPen >::ConstIterator it2 = r.d->customTickMarkPens.constBegin();
275  for ( ; it != d->customTickMarkPens.constEnd(); ++it, ++it2 ) {
276  if ( it.key() != it2.key() || it.value() != it2.value() ) {
277  return false;
278  }
279  }
280  return true;
281 }
282 
284 {
285  d->rulerLinePen = pen;
286 }
287 
289 {
290  return d->rulerLinePen;
291 }
292 
293 void RulerAttributes::setShowRulerLine( bool show )
294 {
295  d->showRulerLine = show;
296 }
297 
298 bool RulerAttributes::showRulerLine() const
299 {
300  return d->showRulerLine;
301 }
302 
303 #if !defined( QT_NO_DEBUG_STREAM )
304 QDebug operator << ( QDebug dbg, const KChart::RulerAttributes& a )
305 {
306  dbg << "KChart::RulerAttributes("
307  << "tickMarkPen=" << a.tickMarkPen()
308  << "majorTickMarkPen=" << a.majorTickMarkPen()
309  << "minorTickMarkPen=" << a.minorTickMarkPen();
310  const RulerAttributes::TickMarkerPensMap pens( a.tickMarkPens() );
311  QMapIterator<qreal, QPen> it( pens );
312  while ( it.hasNext() ) {
313  it.next();
314  dbg << "customTickMarkPen=(" << it.value() << " : " << it.key() << ")";
315  }
316  dbg << ")";
317  return dbg;
318 }
319 #endif /* QT_NO_DEBUG_STREAM */
320 
void setLabelMargin(int margin)
Set margin that should be used between the labels and the ticks.
void setShowFirstTick(bool show)
Shows or hides the first tick.
void setMinorTickMarkPen(const QPen &pen)
Sets the pen used to draw minor tick marks.
const T value(const Key &key, const T &defaultValue) const const
A set of attributes controlling the appearance of axis rulers.
void setShowMajorTickMarks(bool show)
Shows or hides major tick marks.
void setRulerLinePen(const QPen &pen)
Set the ruler line pen to pen.
void setMajorTickMarkLength(int length)
Sets the length of major tick marks.
const Key key(const T &value, const Key &defaultKey) const const
void setMinorTickMarkLength(int length)
Sets the length of minor tick marks.
void setShowMinorTickMarks(bool show)
Shows or hides minor tick marks.
void setTickMarkColor(const QColor &color)
Color setter method provided for convenience.
void setMajorTickMarkPen(const QPen &pen)
Sets the pen used to draw major tick marks.
void setTickMarkPen(const QPen &pen)
Sets the pen used to draw the tick marks.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Jul 1 2022 05:09:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.