Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_abstract_scale_draw.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_abstract_scale_draw.h"
11#include "qwt_text.h"
12#include "qwt_painter.h"
13#include "qwt_scale_map.h"
14#include "qwt_math.h"
15
16#include <qpainter.h>
17#include <qpalette.h>
18#include <qmap.h>
19#include <qlist.h>
20#include <qlocale.h>
21
22 class QwtAbstractScaleDraw::PrivateData
23{
24 public:
25 PrivateData():
26 spacing( 4.0 ),
27 penWidthF( 0.0 ),
28 minExtent( 0.0 )
29 {
30 components = QwtAbstractScaleDraw::Backbone
31 | QwtAbstractScaleDraw::Ticks
32 | QwtAbstractScaleDraw::Labels;
33
34 tickLength[QwtScaleDiv::MinorTick] = 4.0;
35 tickLength[QwtScaleDiv::MediumTick] = 6.0;
36 tickLength[QwtScaleDiv::MajorTick] = 8.0;
37 }
38
39 ScaleComponents components;
40
41 QwtScaleMap map;
42 QwtScaleDiv scaleDiv;
43
44 double spacing;
45 double tickLength[QwtScaleDiv::NTickTypes];
46 qreal penWidthF;
47
48 double minExtent;
49
50 QMap< double, QwtText > labelCache;
51};
52
60 QwtAbstractScaleDraw::QwtAbstractScaleDraw()
61{
62 m_data = new QwtAbstractScaleDraw::PrivateData;
63}
64
66 QwtAbstractScaleDraw::~QwtAbstractScaleDraw()
67{
68 delete m_data;
69}
70
79 void QwtAbstractScaleDraw::enableComponent(
80 ScaleComponent component, bool enable )
81{
82 if ( enable )
83 m_data->components |= component;
84 else
85 m_data->components &= ~component;
86}
87
95 bool QwtAbstractScaleDraw::hasComponent( ScaleComponent component ) const
96{
97 return ( m_data->components & component );
98}
99
104 void QwtAbstractScaleDraw::setScaleDiv( const QwtScaleDiv& scaleDiv )
105{
106 m_data->scaleDiv = scaleDiv;
107 m_data->map.setScaleInterval( scaleDiv.lowerBound(), scaleDiv.upperBound() );
108 m_data->labelCache.clear();
109}
110
115 void QwtAbstractScaleDraw::setTransformation( QwtTransform* transformation )
116{
117 m_data->map.setTransformation( transformation );
118}
119
121 const QwtScaleMap& QwtAbstractScaleDraw::scaleMap() const
122{
123 return m_data->map;
124}
125
127 QwtScaleMap& QwtAbstractScaleDraw::scaleMap()
128{
129 return m_data->map;
130}
131
133 const QwtScaleDiv& QwtAbstractScaleDraw::scaleDiv() const
134{
135 return m_data->scaleDiv;
136}
137
144 void QwtAbstractScaleDraw::setPenWidthF( qreal width )
145{
146 if ( width < 0.0 )
147 width = 0.0;
148
149 m_data->penWidthF = width;
150}
151
156qreal QwtAbstractScaleDraw::penWidthF() const
157{
158 return m_data->penWidthF;
159}
160
169 void QwtAbstractScaleDraw::draw( QPainter* painter,
170 const QPalette& palette ) const
171{
172 painter->save();
173
174 QPen pen = painter->pen();
175 pen.setWidthF( m_data->penWidthF );
176
177 painter->setPen( pen );
178
179 if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
180 {
181 painter->save();
182 painter->setPen( palette.color( QPalette::Text ) ); // ignore pen style
183
184 const QList< double >& majorTicks =
185 m_data->scaleDiv.ticks( QwtScaleDiv::MajorTick );
186
187 for ( int i = 0; i < majorTicks.count(); i++ )
188 {
189 const double v = majorTicks[i];
190 if ( m_data->scaleDiv.contains( v ) )
191 drawLabel( painter, v );
192 }
193
194 painter->restore();
195 }
196
197 if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
198 {
199 painter->save();
200
201 pen = painter->pen();
202 pen.setColor( palette.color( QPalette::WindowText ) );
203 pen.setCapStyle( Qt::FlatCap );
204
205 painter->setPen( pen );
206
207 for ( int tickType = QwtScaleDiv::MinorTick;
208 tickType < QwtScaleDiv::NTickTypes; tickType++ )
209 {
210 const double tickLen = m_data->tickLength[tickType];
211 if ( tickLen <= 0.0 )
212 continue;
213
214 const QList< double >& ticks = m_data->scaleDiv.ticks( tickType );
215 for ( int i = 0; i < ticks.count(); i++ )
216 {
217 const double v = ticks[i];
218 if ( m_data->scaleDiv.contains( v ) )
219 drawTick( painter, v, tickLen );
220 }
221 }
222
223 painter->restore();
224 }
225
226 if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
227 {
228 painter->save();
229
230 pen = painter->pen();
231 pen.setColor( palette.color( QPalette::WindowText ) );
232 pen.setCapStyle( Qt::FlatCap );
233
234 painter->setPen( pen );
235
236 drawBackbone( painter );
237
238 painter->restore();
239 }
240
241 painter->restore();
242}
243
254 void QwtAbstractScaleDraw::setSpacing( double spacing )
255{
256 if ( spacing < 0 )
257 spacing = 0;
258
259 m_data->spacing = spacing;
260}
261
271 double QwtAbstractScaleDraw::spacing() const
272{
273 return m_data->spacing;
274}
275
289 void QwtAbstractScaleDraw::setMinimumExtent( double minExtent )
290{
291 if ( minExtent < 0.0 )
292 minExtent = 0.0;
293
294 m_data->minExtent = minExtent;
295}
296
302 double QwtAbstractScaleDraw::minimumExtent() const
303{
304 return m_data->minExtent;
305}
306
315 void QwtAbstractScaleDraw::setTickLength(
316 QwtScaleDiv::TickType tickType, double length )
317{
318 if ( tickType < QwtScaleDiv::MinorTick ||
319 tickType > QwtScaleDiv::MajorTick )
320 {
321 return;
322 }
323
324 if ( length < 0.0 )
325 length = 0.0;
326
327 const double maxTickLen = 1000.0;
328 if ( length > maxTickLen )
329 length = maxTickLen;
330
331 m_data->tickLength[tickType] = length;
332}
333
338 double QwtAbstractScaleDraw::tickLength( QwtScaleDiv::TickType tickType ) const
339{
340 if ( tickType < QwtScaleDiv::MinorTick ||
341 tickType > QwtScaleDiv::MajorTick )
342 {
343 return 0;
344 }
345
346 return m_data->tickLength[tickType];
347}
348
355 double QwtAbstractScaleDraw::maxTickLength() const
356{
357 double length = 0.0;
358 for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
359 length = qwtMaxF( length, m_data->tickLength[i] );
360
361 return length;
362}
363
375 QwtText QwtAbstractScaleDraw::label( double value ) const
376{
377 return QLocale().toString( value );
378}
379
393 const QwtText& QwtAbstractScaleDraw::tickLabel(
394 const QFont& font, double value ) const
395{
396 QMap< double, QwtText >::const_iterator it1 = m_data->labelCache.constFind( value );
397 if ( it1 != m_data->labelCache.constEnd() )
398 return *it1;
399
400 QwtText lbl = label( value );
401 lbl.setRenderFlags( 0 );
402 lbl.setLayoutAttribute( QwtText::MinimumLayout );
403
404 ( void )lbl.textSize( font ); // initialize the internal cache
405
406 QMap< double, QwtText >::iterator it2 = m_data->labelCache.insert( value, lbl );
407 return *it2;
408}
409
417 void QwtAbstractScaleDraw::invalidateCache()
418{
419 m_data->labelCache.clear();
420}
QList< double >
QMap< double, QwtText >
QwtAbstractScaleDraw::setSpacing
void setSpacing(double)
Set the spacing between tick and labels.
Definition qwt_abstract_scale_draw.cpp:254
QwtAbstractScaleDraw::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Definition qwt_abstract_scale_draw.cpp:104
QwtAbstractScaleDraw::drawBackbone
virtual void drawBackbone(QPainter *painter) const =0
QwtAbstractScaleDraw::QwtAbstractScaleDraw
QwtAbstractScaleDraw()
Constructor.
Definition qwt_abstract_scale_draw.cpp:60
QwtAbstractScaleDraw::scaleMap
const QwtScaleMap & scaleMap() const
Definition qwt_abstract_scale_draw.cpp:121
QwtAbstractScaleDraw::ScaleComponent
ScaleComponent
Definition qwt_abstract_scale_draw.h:40
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition qwt_abstract_scale_draw.h:45
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition qwt_abstract_scale_draw.h:42
QwtAbstractScaleDraw::Labels
@ Labels
Labels.
Definition qwt_abstract_scale_draw.h:48
QwtAbstractScaleDraw::drawTick
virtual void drawTick(QPainter *painter, double value, double len) const =0
QwtAbstractScaleDraw::invalidateCache
void invalidateCache()
Definition qwt_abstract_scale_draw.cpp:417
QwtAbstractScaleDraw::tickLength
double tickLength(QwtScaleDiv::TickType) const
Definition qwt_abstract_scale_draw.cpp:338
QwtAbstractScaleDraw::setPenWidthF
void setPenWidthF(qreal width)
Specify the width of the scale pen.
Definition qwt_abstract_scale_draw.cpp:144
QwtAbstractScaleDraw::setTransformation
void setTransformation(QwtTransform *)
Definition qwt_abstract_scale_draw.cpp:115
QwtAbstractScaleDraw::setTickLength
void setTickLength(QwtScaleDiv::TickType, double length)
Definition qwt_abstract_scale_draw.cpp:315
QwtAbstractScaleDraw::hasComponent
bool hasComponent(ScaleComponent) const
Definition qwt_abstract_scale_draw.cpp:95
QwtAbstractScaleDraw::tickLabel
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
Definition qwt_abstract_scale_draw.cpp:393
QwtAbstractScaleDraw::drawLabel
virtual void drawLabel(QPainter *painter, double value) const =0
QwtAbstractScaleDraw::maxTickLength
double maxTickLength() const
Definition qwt_abstract_scale_draw.cpp:355
QwtAbstractScaleDraw::minimumExtent
double minimumExtent() const
Definition qwt_abstract_scale_draw.cpp:302
QwtAbstractScaleDraw::setMinimumExtent
void setMinimumExtent(double)
Set a minimum for the extent.
Definition qwt_abstract_scale_draw.cpp:289
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition qwt_abstract_scale_draw.cpp:375
QwtAbstractScaleDraw::draw
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
Definition qwt_abstract_scale_draw.cpp:169
QwtAbstractScaleDraw::penWidthF
qreal penWidthF() const
Definition qwt_abstract_scale_draw.cpp:156
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition qwt_abstract_scale_draw.cpp:133
QwtAbstractScaleDraw::spacing
double spacing() const
Get the spacing.
Definition qwt_abstract_scale_draw.cpp:271
QwtAbstractScaleDraw::~QwtAbstractScaleDraw
virtual ~QwtAbstractScaleDraw()
Destructor.
Definition qwt_abstract_scale_draw.cpp:66
QwtAbstractScaleDraw::ScaleComponents
QFlags< ScaleComponent > ScaleComponents
Definition qwt_abstract_scale_draw.h:51
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition qwt_abstract_scale_draw.cpp:79
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::lowerBound
double lowerBound() const
Definition qwt_scale_div.cpp:131
QwtScaleDiv::upperBound
double upperBound() const
Definition qwt_scale_div.cpp:151
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition qwt_scale_div.cpp:309
QwtScaleDiv::contains
bool contains(double value) const
Definition qwt_scale_div.cpp:212
QwtScaleDiv::TickType
TickType
Scale tick types.
Definition qwt_scale_div.h:38
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition qwt_scale_div.h:46
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition qwt_scale_div.h:43
QwtScaleDiv::NTickTypes
@ NTickTypes
Number of valid tick types.
Definition qwt_scale_div.h:52
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition qwt_scale_div.h:49
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::setScaleInterval
void setScaleInterval(double s1, double s2)
Specify the borders of the scale interval.
Definition qwt_scale_map.cpp:100
QwtScaleMap::setTransformation
void setTransformation(QwtTransform *)
Definition qwt_scale_map.cpp:76
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::MinimumLayout
@ MinimumLayout
Definition qwt_text.h:140
QwtText::setLayoutAttribute
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition qwt_text.cpp:494
QwtText::textSize
QSizeF textSize() const
Definition qwt_text.cpp:570
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition qwt_text.cpp:304
QwtTransform
A transformation between coordinate systems.
Definition qwt_transform.h:36