qwt_abstract_scale_draw.cpp
Go to the documentation of this file.
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 
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 
23 {
24  public:
26  spacing( 4.0 ),
27  penWidthF( 0.0 ),
28  minExtent( 0.0 )
29  {
33 
37  }
38 
39  ScaleComponents components;
40 
43 
44  double spacing;
46  qreal penWidthF;
47 
48  double minExtent;
49 
51 };
52 
61 {
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 {
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 =
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 
255 {
256  if ( spacing < 0 )
257  spacing = 0;
258 
260 }
261 
272 {
273  return m_data->spacing;
274 }
275 
290 {
291  if ( minExtent < 0.0 )
292  minExtent = 0.0;
293 
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  auto str = QLocale().toString( value, 'f', 6 );
378  str.remove( QRegExp("0+$") ); // Remove any number of trailing 0's
379  str.remove( QRegExp("\\.$") ); // If the last character is just a '.' then remove it
380  return str;
381 }
382 
397  const QFont& font, double value ) const
398 {
400  if ( it1 != m_data->labelCache.constEnd() )
401  return *it1;
402 
403  QwtText lbl = label( value );
404  lbl.setRenderFlags( 0 );
406 
407  ( void )lbl.textSize( font ); // initialize the internal cache
408 
409  QMap< double, QwtText >::iterator it2 = m_data->labelCache.insert( value, lbl );
410  return *it2;
411 }
412 
421 {
422  m_data->labelCache.clear();
423 }
virtual ~QwtAbstractScaleDraw()
Destructor.
virtual void drawBackbone(QPainter *painter) const =0
double lowerBound() const
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
double tickLength(QwtScaleDiv::TickType) const
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
QList< double > ticks(int tickType) const
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:123
bool contains(double value) const
virtual void drawTick(QPainter *painter, double value, double len) const =0
A class representing a scale division.
Definition: qwt_scale_div.h:33
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:37
bool hasComponent(ScaleComponent) const
const QwtScaleDiv & scaleDiv() const
virtual QwtText label(double) const
Convert a value into its representing label.
void setTransformation(QwtTransform *)
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition: qwt_text.cpp:494
A transformation between coordinate systems.
Definition: qwt_transform.h:35
A class representing a text.
Definition: qwt_text.h:51
QSizeF textSize() const
Definition: qwt_text.cpp:570
void setScaleDiv(const QwtScaleDiv &)
Backbone = the line where the ticks are located.
j template void())
Definition: json.hpp:4061
A scale map.
Definition: qwt_scale_map.h:26
Number of valid tick types.
Definition: qwt_scale_div.h:52
void setTickLength(QwtScaleDiv::TickType, double length)
double tickLength[QwtScaleDiv::NTickTypes]
void enableComponent(ScaleComponent, bool enable=true)
void setMinimumExtent(double)
Set a minimum for the extent.
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
void setTransformation(QwtTransform *)
const QwtScaleMap & scaleMap() const
Definition: core.h:1131
double upperBound() const
void setPenWidthF(qreal width)
Specify the width of the scale pen.
void setSpacing(double)
Set the spacing between tick and labels.
double spacing() const
Get the spacing.


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38