KD Chart 2  [rev.2.7]
KDChartRulerAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartRulerAttributes.h"
24 
25 #include <limits>
26 
27 #include <QPen>
28 #include <QDebug>
29 
30 #include <KDABLibFakes>
31 
32 #define d d_func()
33 
34 using namespace KDChart;
35 
36 class RulerAttributes::Private
37 {
38  friend class RulerAttributes;
39 public:
40  Private();
41 private:
42  QPen tickMarkPen;
43  QPen majorTickMarkPen;
44  QPen minorTickMarkPen;
45 
46  bool majorTickMarkPenIsSet : 1;
47  bool minorTickMarkPenIsSet : 1;
48 
49  bool showMajorTickMarks : 1;
50  bool showMinorTickMarks : 1;
51 
52  bool showRulerLine : 1;
53 
54  bool majorTickLengthIsSet : 1;
55  bool minorTickLengthIsSet : 1;
56 
57  bool showFirstTick : 1;
58 
59  int labelMargin;
60  int majorTickLength;
61  int minorTickLength;
62 
63  RulerAttributes::TickMarkerPensMap customTickMarkPens;
64 };
65 
66 RulerAttributes::Private::Private()
67  : tickMarkPen( Qt::black )
68  , majorTickMarkPen( Qt::black )
69  , minorTickMarkPen( Qt::black )
70  , majorTickMarkPenIsSet( false )
71  , minorTickMarkPenIsSet( false )
72  , showMajorTickMarks( true )
73  , showMinorTickMarks( true )
74  , showRulerLine( false )
75  , majorTickLengthIsSet( false )
76  , minorTickLengthIsSet( false )
77  , showFirstTick( true )
78  , labelMargin( -1 )
79  , majorTickLength( 3 )
80  , minorTickLength( 2 )
81 {
82  tickMarkPen.setCapStyle( Qt::FlatCap );
83  majorTickMarkPen.setCapStyle( Qt::FlatCap );
84  minorTickMarkPen.setCapStyle( Qt::FlatCap );
85 }
86 
88  : _d( new Private() )
89 {
90  // this bloc left empty intentionally
91 }
92 
94  : _d( new Private( *r.d ) )
95 {
96 }
97 
98 void RulerAttributes::setTickMarkPen( const QPen& pen )
99 {
100  d->tickMarkPen = pen;
101 }
102 
104 {
105  return d->tickMarkPen;
106 }
107 
109 {
110  d->majorTickMarkPen = pen;
111  d->majorTickMarkPenIsSet = true;
112 }
113 
115 {
116  return d->majorTickMarkPenIsSet;
117 }
118 
120 {
121  return d->majorTickMarkPenIsSet ? d->majorTickMarkPen : d->tickMarkPen;
122 }
123 
125 {
126  d->minorTickMarkPen = pen;
127  d->minorTickMarkPenIsSet = true;
128 }
129 
131 {
132  return d->minorTickMarkPenIsSet;
133 }
134 
136 {
137  return d->minorTickMarkPenIsSet ? d->minorTickMarkPen : d->tickMarkPen;
138 }
139 
140 void RulerAttributes::setTickMarkPen( qreal value, const QPen& pen )
141 {
142  if ( !d->customTickMarkPens.contains( value ) )
143  d->customTickMarkPens.insert( value, pen );
144 }
145 
146 QPen RulerAttributes::tickMarkPen( qreal value ) const
147 {
148  QMapIterator<qreal, QPen> it( d->customTickMarkPens );
149  while ( it.hasNext() ) {
150  it.next();
151  if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
152  return it.value();
153  }
154  return d->tickMarkPen;
155 }
156 
158 {
159  return d->customTickMarkPens;
160 }
161 
162 bool RulerAttributes::hasTickMarkPenAt( qreal value ) const
163 {
164  QMapIterator<qreal, QPen> it( d->customTickMarkPens );
165  while ( it.hasNext() ) {
166  it.next();
167  if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
168  return true;
169  }
170  return false;
171 }
172 
173 void RulerAttributes::setTickMarkColor( const QColor& color )
174 {
175  d->tickMarkPen.setColor( color );
176 }
177 
179 {
180  return d->tickMarkPen.color();
181 }
182 
184 {
185  d->showMajorTickMarks = show;
186 }
187 
189 {
190  return d->showMajorTickMarks;
191 }
192 
194 {
195  d->showMinorTickMarks = show;
196 }
197 
199 {
200  return d->showMinorTickMarks;
201 }
202 
204 {
205  d->labelMargin = margin;
206 }
207 
209 {
210  return d->labelMargin;
211 }
212 
214 {
215  d->majorTickLength = length;
216  d->majorTickLengthIsSet = true;
217 }
218 
220 {
221  return d->majorTickLength;
222 }
223 
225 {
226  return d->majorTickLengthIsSet;
227 }
228 
230 {
231  d->minorTickLength = length;
232  d->minorTickLengthIsSet = true;
233 }
234 
236 {
237  return d->minorTickLength;
238 }
239 
241 {
242  return d->minorTickLengthIsSet;
243 }
244 
246 {
247  d->showFirstTick = show;
248 }
249 
251 {
252  return d->showFirstTick;
253 }
254 
256 {
257  if ( this == &r )
258  return *this;
259 
260  *d = *r.d;
261 
262  return *this;
263 }
264 
266 {
267  delete _d; _d = 0;
268 }
269 
271 {
272  bool isEqual = tickMarkPen() == r.tickMarkPen() &&
279  showRulerLine() == r.showRulerLine() &&
282  showFirstTick() == r.showFirstTick() &&
283  d->customTickMarkPens.size() == r.d->customTickMarkPens.size();
284  if ( !isEqual ) {
285  return false;
286  }
287  QMap< qreal, QPen >::ConstIterator it = d->customTickMarkPens.constBegin();
288  QMap< qreal, QPen >::ConstIterator it2 = r.d->customTickMarkPens.constBegin();
289  for ( ; it != d->customTickMarkPens.constEnd(); ++it, ++it2 ) {
290  if ( it.key() != it2.key() || it.value() != it2.value() ) {
291  return false;
292  }
293  }
294  return true;
295 }
296 
298 {
299  d->showRulerLine = show;
300 }
301 
303 {
304  return d->showRulerLine;
305 }
306 
307 #if !defined( QT_NO_DEBUG_STREAM )
308 QDebug operator << ( QDebug dbg, const KDChart::RulerAttributes& a )
309 {
310  dbg << "KDChart::RulerAttributes("
311  << "tickMarkPen=" << a.tickMarkPen()
312  << "majorTickMarkPen=" << a.majorTickMarkPen()
313  << "minorTickMarkPen=" << a.minorTickMarkPen();
315  QMapIterator<qreal, QPen> it( pens );
316  while ( it.hasNext() ) {
317  it.next();
318  dbg << "customTickMarkPen=(" << it.value() << " : " << it.key() << ")";
319  }
320  dbg << ")";
321  return dbg;
322 }
323 #endif /* QT_NO_DEBUG_STREAM */
324 
void setMajorTickMarkLength(int length)
Sets the length of major tick marks.
void setShowMinorTickMarks(bool show)
Shows or hides minor tick marks.
void setShowFirstTick(bool show)
Shows or hides the first tick.
void setTickMarkColor(const QColor &color)
Color setter method provided for convenience.
A set of attributes controlling the appearance of axis rulers.
#define d
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.
bool hasTickMarkPenAt(qreal value) const
void setMinorTickMarkPen(const QPen &pen)
Sets the pen used to draw minor tick marks.
bool operator==(const RulerAttributes &) const
void setShowMajorTickMarks(bool show)
Shows or hides major tick marks.
RulerAttributes & operator=(const RulerAttributes &)
void setLabelMargin(int margin)
Set margin that should be used between the labels and the ticks.
QDebug operator<<(QDebug stream, const DataDimension &r)
void setTickMarkPen(const QPen &pen)
Sets the pen used to draw the tick marks.
TickMarkerPensMap tickMarkPens() const

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/