qwt_abstract_scale_draw.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 
11 #include "qwt_math.h"
12 #include "qwt_text.h"
13 #include "qwt_painter.h"
14 #include "qwt_scale_map.h"
15 #include <qpainter.h>
16 #include <qpalette.h>
17 #include <qmap.h>
18 #include <qlocale.h>
19 
21 {
22 public:
24  spacing( 4.0 ),
25  penWidth( 0 ),
26  minExtent( 0.0 )
27  {
31 
35  }
36 
38 
41 
42  double spacing;
44  int penWidth;
45 
46  double minExtent;
47 
48  QMap<double, QwtText> labelCache;
49 };
50 
59 {
61 }
62 
65 {
66  delete d_data;
67 }
68 
78  ScaleComponent component, bool enable )
79 {
80  if ( enable )
81  d_data->components |= component;
82  else
83  d_data->components &= ~component;
84 }
85 
94 {
95  return ( d_data->components & component );
96 }
97 
103 {
105  d_data->map.setScaleInterval( scaleDiv.lowerBound(), scaleDiv.upperBound() );
106  d_data->labelCache.clear();
107 }
108 
114  QwtTransform *transformation )
115 {
116  d_data->map.setTransformation( transformation );
117 }
118 
121 {
122  return d_data->map;
123 }
124 
127 {
128  return d_data->map;
129 }
130 
133 {
134  return d_data->scaleDiv;
135 }
136 
143 {
144  if ( width < 0 )
145  width = 0;
146 
147  if ( width != d_data->penWidth )
148  d_data->penWidth = width;
149 }
150 
156 {
157  return d_data->penWidth;
158 }
159 
168 void QwtAbstractScaleDraw::draw( QPainter *painter,
169  const QPalette& palette ) const
170 {
171  painter->save();
172 
173  QPen pen = painter->pen();
174  pen.setWidth( d_data->penWidth );
175  pen.setCosmetic( false );
176  painter->setPen( pen );
177 
179  {
180  painter->save();
181  painter->setPen( palette.color( QPalette::Text ) ); // ignore pen style
182 
183  const QList<double> &majorTicks =
185 
186  for ( int i = 0; i < majorTicks.count(); i++ )
187  {
188  const double v = majorTicks[i];
189  if ( d_data->scaleDiv.contains( v ) )
190  drawLabel( painter, v );
191  }
192 
193  painter->restore();
194  }
195 
197  {
198  painter->save();
199 
200  QPen pen = painter->pen();
201  pen.setColor( palette.color( QPalette::WindowText ) );
202  pen.setCapStyle( Qt::FlatCap );
203 
204  painter->setPen( pen );
205 
206  for ( int tickType = QwtScaleDiv::MinorTick;
207  tickType < QwtScaleDiv::NTickTypes; tickType++ )
208  {
209  const double tickLen = d_data->tickLength[tickType];
210  if ( tickLen <= 0.0 )
211  continue;
212 
213  const QList<double> &ticks = d_data->scaleDiv.ticks( tickType );
214  for ( int i = 0; i < ticks.count(); i++ )
215  {
216  const double v = ticks[i];
217  if ( d_data->scaleDiv.contains( v ) )
218  drawTick( painter, v, tickLen );
219  }
220  }
221 
222  painter->restore();
223  }
224 
226  {
227  painter->save();
228 
229  QPen pen = painter->pen();
230  pen.setColor( palette.color( QPalette::WindowText ) );
231  pen.setCapStyle( Qt::FlatCap );
232 
233  painter->setPen( pen );
234 
235  drawBackbone( painter );
236 
237  painter->restore();
238  }
239 
240  painter->restore();
241 }
242 
254 {
255  if ( spacing < 0 )
256  spacing = 0;
257 
259 }
260 
271 {
272  return d_data->spacing;
273 }
274 
289 {
290  if ( minExtent < 0.0 )
291  minExtent = 0.0;
292 
294 }
295 
302 {
303  return d_data->minExtent;
304 }
305 
315  QwtScaleDiv::TickType tickType, double length )
316 {
317  if ( tickType < QwtScaleDiv::MinorTick ||
318  tickType > QwtScaleDiv::MajorTick )
319  {
320  return;
321  }
322 
323  if ( length < 0.0 )
324  length = 0.0;
325 
326  const double maxTickLen = 1000.0;
327  if ( length > maxTickLen )
328  length = maxTickLen;
329 
330  d_data->tickLength[tickType] = length;
331 }
332 
338 {
339  if ( tickType < QwtScaleDiv::MinorTick ||
340  tickType > QwtScaleDiv::MajorTick )
341  {
342  return 0;
343  }
344 
345  return d_data->tickLength[tickType];
346 }
347 
355 {
356  double length = 0.0;
357  for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
358  length = qMax( length, d_data->tickLength[i] );
359 
360  return length;
361 }
362 
375 {
376  return QLocale().toString( value );
377 }
378 
393  const QFont &font, double value ) const
394 {
395  QMap<double, QwtText>::const_iterator it = d_data->labelCache.constFind( value );
396  if ( it == d_data->labelCache.constEnd() )
397  {
398  QwtText lbl = label( value );
399  lbl.setRenderFlags( 0 );
401 
402  ( void )lbl.textSize( font ); // initialize the internal cache
403 
404  it = d_data->labelCache.insert( value, lbl );
405  }
406 
407  return ( *it );
408 }
409 
418 {
419  d_data->labelCache.clear();
420 }
int v
virtual ~QwtAbstractScaleDraw()
Destructor.
virtual void drawBackbone(QPainter *painter) const =0
virtual void drawTick(QPainter *painter, double value, double len) const =0
void setSpacing(double margin)
Set the spacing between tick and labels.
A class representing a scale division.
Definition: qwt_scale_div.h:36
const QwtScaleMap & scaleMap() const
virtual void drawLabel(QPainter *painter, double value) const =0
void setScaleInterval(double s1, double s2)
Specify the borders of the scale interval.
TickType
Scale tick types.
Definition: qwt_scale_div.h:40
double upperBound() const
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
bool contains(double value) const
void setRenderFlags(int flags)
Change the render flags.
Definition: qwt_text.cpp:271
const QwtScaleDiv & scaleDiv() const
double lowerBound() const
void setTransformation(QwtTransform *)
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition: qwt_text.cpp:461
A transformation between coordinate systems.
Definition: qwt_transform.h:35
A class representing a text.
Definition: qwt_text.h:51
Backbone = the line where the ticks are located.
virtual QwtText label(double) const
Convert a value into its representing label.
A scale map.
Definition: qwt_scale_map.h:30
Number of valid tick types.
Definition: qwt_scale_div.h:55
void setTickLength(QwtScaleDiv::TickType, double length)
double tickLength[QwtScaleDiv::NTickTypes]
void enableComponent(ScaleComponent, bool enable=true)
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
void setMinimumExtent(double)
Set a minimum for the extent.
void setPenWidth(int width)
Specify the width of the scale pen.
void setScaleDiv(const QwtScaleDiv &s)
bool hasComponent(ScaleComponent) const
void setTransformation(QwtTransform *)
int i
QList< double > ticks(int tickType) const
double tickLength(QwtScaleDiv::TickType) const
std::size_t length
double spacing() const
Get the spacing.
QFlags< ScaleComponent > ScaleComponents
Scale components.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17