Qwt User's Guide  6.2.0
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  {
33 
37  }
38 
39  ScaleComponents components;
40 
41  QwtScaleMap map;
43 
44  double spacing;
46  qreal penWidthF;
47 
48  double minExtent;
49 
50  QMap< double, QwtText > labelCache;
51 };
52 
61 {
62  m_data = new QwtAbstractScaleDraw::PrivateData;
63 }
64 
67 {
68  delete m_data;
69 }
70 
80  ScaleComponent component, bool enable )
81 {
82  if ( enable )
83  m_data->components |= component;
84  else
85  m_data->components &= ~component;
86 }
87 
96 {
97  return ( m_data->components & component );
98 }
99 
105 {
106  m_data->scaleDiv = scaleDiv;
107  m_data->map.setScaleInterval( scaleDiv.lowerBound(), scaleDiv.upperBound() );
108  m_data->labelCache.clear();
109 }
110 
116 {
117  m_data->map.setTransformation( transformation );
118 }
119 
122 {
123  return m_data->map;
124 }
125 
128 {
129  return m_data->map;
130 }
131 
134 {
135  return m_data->scaleDiv;
136 }
137 
145 {
146  if ( width < 0.0 )
147  width = 0.0;
148 
149  m_data->penWidthF = width;
150 }
151 
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 
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 
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 
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 
272 {
273  return m_data->spacing;
274 }
275 
290 {
291  if ( minExtent < 0.0 )
292  minExtent = 0.0;
293 
294  m_data->minExtent = minExtent;
295 }
296 
303 {
304  return m_data->minExtent;
305 }
306 
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 
339 {
340  if ( tickType < QwtScaleDiv::MinorTick ||
341  tickType > QwtScaleDiv::MajorTick )
342  {
343  return 0;
344  }
345 
346  return m_data->tickLength[tickType];
347 }
348 
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 
376 {
377  return QLocale().toString( value );
378 }
379 
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 );
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 
418 {
419  m_data->labelCache.clear();
420 }
void setSpacing(double)
Set the spacing between tick and labels.
void setScaleDiv(const QwtScaleDiv &)
virtual void drawBackbone(QPainter *painter) const =0
const QwtScaleMap & scaleMap() const
@ Backbone
Backbone = the line where the ticks are located.
virtual void drawTick(QPainter *painter, double value, double len) const =0
double tickLength(QwtScaleDiv::TickType) const
void setPenWidthF(qreal width)
Specify the width of the scale pen.
void setTransformation(QwtTransform *)
void setTickLength(QwtScaleDiv::TickType, double length)
bool hasComponent(ScaleComponent) const
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
virtual void drawLabel(QPainter *painter, double value) const =0
void setMinimumExtent(double)
Set a minimum for the extent.
virtual QwtText label(double) const
Convert a value into its representing label.
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
const QwtScaleDiv & scaleDiv() const
double spacing() const
Get the spacing.
virtual ~QwtAbstractScaleDraw()
Destructor.
QFlags< ScaleComponent > ScaleComponents
void enableComponent(ScaleComponent, bool enable=true)
A class representing a scale division.
Definition: qwt_scale_div.h:34
double lowerBound() const
double upperBound() const
TickType
Scale tick types.
Definition: qwt_scale_div.h:38
@ MediumTick
Medium ticks.
Definition: qwt_scale_div.h:46
@ MinorTick
Minor ticks.
Definition: qwt_scale_div.h:43
@ NTickTypes
Number of valid tick types.
Definition: qwt_scale_div.h:52
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
A scale map.
Definition: qwt_scale_map.h:27
A class representing a text.
Definition: qwt_text.h:52
@ MinimumLayout
Definition: qwt_text.h:140
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition: qwt_text.cpp:494
QSizeF textSize() const
Definition: qwt_text.cpp:570
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
A transformation between coordinate systems.
Definition: qwt_transform.h:36