KChart

KChartRulerAttributes.cpp
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 
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 
83 RulerAttributes::RulerAttributes()
84  : _d( new Private() )
85 {
86  // this bloc left empty intentionally
87 }
88 
89 RulerAttributes::RulerAttributes( const RulerAttributes& r )
90  : _d( new Private( *r.d ) )
91 {
92 }
93 
95 {
96  d->tickMarkPen = pen;
97 }
98 
99 QPen RulerAttributes::tickMarkPen() const
100 {
101  return d->tickMarkPen;
102 }
103 
105 {
106  d->majorTickMarkPen = pen;
107  d->majorTickMarkPenIsSet = true;
108 }
109 
110 bool RulerAttributes::majorTickMarkPenIsSet() const
111 {
112  return d->majorTickMarkPenIsSet;
113 }
114 
115 QPen RulerAttributes::majorTickMarkPen() const
116 {
117  return d->majorTickMarkPenIsSet ? d->majorTickMarkPen : d->tickMarkPen;
118 }
119 
121 {
122  d->minorTickMarkPen = pen;
123  d->minorTickMarkPenIsSet = true;
124 }
125 
126 bool RulerAttributes::minorTickMarkPenIsSet() const
127 {
128  return d->minorTickMarkPenIsSet;
129 }
130 
131 QPen RulerAttributes::minorTickMarkPen() const
132 {
133  return d->minorTickMarkPenIsSet ? d->minorTickMarkPen : d->tickMarkPen;
134 }
135 
136 void RulerAttributes::setTickMarkPen( qreal value, const QPen& pen )
137 {
138  if ( !d->customTickMarkPens.contains( value ) )
139  d->customTickMarkPens.insert( value, pen );
140 }
141 
142 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 
153 RulerAttributes::TickMarkerPensMap RulerAttributes::tickMarkPens() const
154 {
155  return d->customTickMarkPens;
156 }
157 
158 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 
170 {
171  d->tickMarkPen.setColor( color );
172 }
173 
174 QColor RulerAttributes::tickMarkColor() const
175 {
176  return d->tickMarkPen.color();
177 }
178 
180 {
181  d->showMajorTickMarks = show;
182 }
183 
184 bool RulerAttributes::showMajorTickMarks() const
185 {
186  return d->showMajorTickMarks;
187 }
188 
190 {
191  d->showMinorTickMarks = show;
192 }
193 
194 bool RulerAttributes::showMinorTickMarks() const
195 {
196  return d->showMinorTickMarks;
197 }
198 
200 {
201  d->labelMargin = margin;
202 }
203 
204 int RulerAttributes::labelMargin() const
205 {
206  return d->labelMargin;
207 }
208 
210 {
211  d->majorTickLength = length;
212  d->majorTickLengthIsSet = true;
213 }
214 
215 int RulerAttributes::majorTickMarkLength() const
216 {
217  return d->majorTickLength;
218 }
219 
220 bool RulerAttributes::majorTickMarkLengthIsSet() const
221 {
222  return d->majorTickLengthIsSet;
223 }
224 
226 {
227  d->minorTickLength = length;
228  d->minorTickLengthIsSet = true;
229 }
230 
231 int RulerAttributes::minorTickMarkLength() const
232 {
233  return d->minorTickLength;
234 }
235 
236 bool RulerAttributes::minorTickMarkLengthIsSet() const
237 {
238  return d->minorTickLengthIsSet;
239 }
240 
242 {
243  d->showFirstTick = show;
244 }
245 
246 bool RulerAttributes::showFirstTick() const
247 {
248  return d->showFirstTick;
249 }
250 
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 
261 RulerAttributes::~RulerAttributes()
262 {
263  delete _d; _d = nullptr;
264 }
265 
266 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 
295 {
296  d->rulerLinePen = pen;
297 }
298 
300 {
301  return d->rulerLinePen;
302 }
303 
304 void RulerAttributes::setShowRulerLine( bool show )
305 {
306  d->showRulerLine = show;
307 }
308 
309 bool RulerAttributes::showRulerLine() const
310 {
311  return d->showRulerLine;
312 }
313 
314 #if !defined( QT_NO_DEBUG_STREAM )
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 
void setTickMarkPen(const QPen &pen)
Sets the pen used to draw the tick marks.
void setMajorTickMarkPen(const QPen &pen)
Sets the pen used to draw major tick marks.
void setMinorTickMarkLength(int length)
Sets the length of minor tick marks.
void setShowFirstTick(bool show)
Shows or hides the first tick.
QMapIterator::Item next()
void setShowMinorTickMarks(bool show)
Shows or hides minor tick marks.
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
const Key & key() const const
const T & value() const const
void setMinorTickMarkPen(const QPen &pen)
Sets the pen used to draw minor tick marks.
void setShowMajorTickMarks(bool show)
Shows or hides major tick marks.
const Key key(const T &value, const Key &defaultKey) const const
void setLabelMargin(int margin)
Set margin that should be used between the labels and the ticks.
void setTickMarkColor(const QColor &color)
Color setter method provided for convenience.
void setRulerLinePen(const QPen &pen)
Set the ruler line pen to pen.
void show()
void setMajorTickMarkLength(int length)
Sets the length of major tick marks.
Global namespace.
A set of attributes controlling the appearance of axis rulers.
bool hasNext() const const
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Sep 29 2020 22:42:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.