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:
25  PrivateData():
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 {
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 
254 void QwtAbstractScaleDraw::setSpacing( double spacing )
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 
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  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 }
QwtAbstractScaleDraw::draw
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
Definition: qwt_abstract_scale_draw.cpp:169
QwtAbstractScaleDraw::QwtAbstractScaleDraw
QwtAbstractScaleDraw()
Constructor.
Definition: qwt_abstract_scale_draw.cpp:60
QwtAbstractScaleDraw::setTransformation
void setTransformation(QwtTransform *)
Definition: qwt_abstract_scale_draw.cpp:115
QwtAbstractScaleDraw::drawTick
virtual void drawTick(QPainter *painter, double value, double len) const =0
QwtAbstractScaleDraw::hasComponent
bool hasComponent(ScaleComponent) const
Definition: qwt_abstract_scale_draw.cpp:95
QwtAbstractScaleDraw::PrivateData::PrivateData
PrivateData()
Definition: qwt_abstract_scale_draw.cpp:32
QwtAbstractScaleDraw::setSpacing
void setSpacing(double)
Set the spacing between tick and labels.
Definition: qwt_abstract_scale_draw.cpp:254
QwtScaleDiv::MediumTick
@ MediumTick
Medium ticks.
Definition: qwt_scale_div.h:46
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:396
QwtAbstractScaleDraw::maxTickLength
double maxTickLength() const
Definition: qwt_abstract_scale_draw.cpp:355
QwtAbstractScaleDraw::scaleMap
const QwtScaleMap & scaleMap() const
Definition: qwt_abstract_scale_draw.cpp:121
QwtAbstractScaleDraw::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Definition: qwt_abstract_scale_draw.cpp:104
QwtTransform
A transformation between coordinate systems.
Definition: qwt_transform.h:35
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition: qwt_abstract_scale_draw.h:45
QwtAbstractScaleDraw::PrivateData::scaleDiv
QwtScaleDiv scaleDiv
Definition: qwt_abstract_scale_draw.cpp:49
QwtAbstractScaleDraw::invalidateCache
void invalidateCache()
Definition: qwt_abstract_scale_draw.cpp:420
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale_draw.cpp:133
QwtScaleDiv::lowerBound
double lowerBound() const
Definition: qwt_scale_div.cpp:131
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
QList< double >
qwt_math.h
QwtAbstractScaleDraw::Labels
@ Labels
Labels.
Definition: qwt_abstract_scale_draw.h:48
QwtAbstractScaleDraw::PrivateData::labelCache
QMap< double, QwtText > labelCache
Definition: qwt_abstract_scale_draw.cpp:57
QwtAbstractScaleDraw::drawBackbone
virtual void drawBackbone(QPainter *painter) const =0
QwtScaleDiv::contains
bool contains(double value) const
Definition: qwt_scale_div.cpp:212
QMap< double, QwtText >
QwtAbstractScaleDraw::minimumExtent
double minimumExtent() const
Definition: qwt_abstract_scale_draw.cpp:302
QwtAbstractScaleDraw::PrivateData::minExtent
double minExtent
Definition: qwt_abstract_scale_draw.cpp:55
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition: qwt_scale_div.cpp:309
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
QwtText::textSize
QSizeF textSize() const
Definition: qwt_text.cpp:570
QwtScaleMap::setTransformation
void setTransformation(QwtTransform *)
Definition: qwt_scale_map.cpp:76
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
QwtScaleDiv::NTickTypes
@ NTickTypes
Number of valid tick types.
Definition: qwt_scale_div.h:52
qwt_scale_map.h
QwtAbstractScaleDraw::enableComponent
void enableComponent(ScaleComponent, bool enable=true)
Definition: qwt_abstract_scale_draw.cpp:79
QwtAbstractScaleDraw::ScaleComponent
ScaleComponent
Definition: qwt_abstract_scale_draw.h:39
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtAbstractScaleDraw::setMinimumExtent
void setMinimumExtent(double)
Set a minimum for the extent.
Definition: qwt_abstract_scale_draw.cpp:289
qwtMaxF
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:127
QwtScaleDiv::upperBound
double upperBound() const
Definition: qwt_scale_div.cpp:151
QwtAbstractScaleDraw::m_data
PrivateData * m_data
Definition: qwt_abstract_scale_draw.h:134
QwtAbstractScaleDraw::setTickLength
void setTickLength(QwtScaleDiv::TickType, double length)
Definition: qwt_abstract_scale_draw.cpp:315
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition: qwt_abstract_scale_draw.cpp:375
qwt_abstract_scale_draw.h
nlohmann::detail::void
j template void())
Definition: json.hpp:4061
sol::meta::enable
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
QwtAbstractScaleDraw::penWidthF
qreal penWidthF() const
Definition: qwt_abstract_scale_draw.cpp:156
QwtAbstractScaleDraw::PrivateData
Definition: qwt_abstract_scale_draw.cpp:22
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtText::MinimumLayout
@ MinimumLayout
Definition: qwt_text.h:140
QwtAbstractScaleDraw::drawLabel
virtual void drawLabel(QPainter *painter, double value) const =0
QwtAbstractScaleDraw::~QwtAbstractScaleDraw
virtual ~QwtAbstractScaleDraw()
Destructor.
Definition: qwt_abstract_scale_draw.cpp:66
qwt_painter.h
QwtScaleDiv::TickType
TickType
Scale tick types.
Definition: qwt_scale_div.h:37
QwtAbstractScaleDraw::setPenWidthF
void setPenWidthF(qreal width)
Specify the width of the scale pen.
Definition: qwt_abstract_scale_draw.cpp:144
QwtAbstractScaleDraw::PrivateData::tickLength
double tickLength[QwtScaleDiv::NTickTypes]
Definition: qwt_abstract_scale_draw.cpp:52
QwtAbstractScaleDraw::PrivateData::map
QwtScaleMap map
Definition: qwt_abstract_scale_draw.cpp:48
QwtText::setLayoutAttribute
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition: qwt_text.cpp:494
QwtAbstractScaleDraw::PrivateData::spacing
double spacing
Definition: qwt_abstract_scale_draw.cpp:51
QwtScaleMap::setScaleInterval
void setScaleInterval(double s1, double s2)
Specify the borders of the scale interval.
Definition: qwt_scale_map.cpp:100
QwtAbstractScaleDraw::spacing
double spacing() const
Get the spacing.
Definition: qwt_abstract_scale_draw.cpp:271
QwtScaleDiv::MinorTick
@ MinorTick
Minor ticks.
Definition: qwt_scale_div.h:43
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
QwtAbstractScaleDraw::PrivateData::components
ScaleComponents components
Definition: qwt_abstract_scale_draw.cpp:46
QwtAbstractScaleDraw::tickLength
double tickLength(QwtScaleDiv::TickType) const
Definition: qwt_abstract_scale_draw.cpp:338
qwt_text.h
QwtAbstractScaleDraw::PrivateData::penWidthF
qreal penWidthF
Definition: qwt_abstract_scale_draw.cpp:53


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45