qwt_round_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 
10 #include "qwt_round_scale_draw.h"
11 #include "qwt_painter.h"
12 #include "qwt_scale_div.h"
13 #include "qwt_scale_map.h"
14 #include "qwt_text.h"
15 #include "qwt_math.h"
16 
17 #include <qpainter.h>
18 
20 {
21  public:
22  PrivateData()
23  : center( 50.0, 50.0 )
24  , radius( 50.0 )
25  , startAngle( -135.0 )
26  , endAngle( 135.0 )
27  {
28  }
29 
30  QPointF center;
31  double radius;
32 
33  double startAngle;
34  double endAngle;
35 };
36 
45 {
47 
48  setRadius( 50 );
50 }
51 
54 {
55  delete m_data;
56 }
57 
66 void QwtRoundScaleDraw::setRadius( double radius )
67 {
68  m_data->radius = radius;
69 }
70 
80 {
81  return m_data->radius;
82 }
83 
90 void QwtRoundScaleDraw::moveCenter( const QPointF& center )
91 {
92  m_data->center = center;
93 }
94 
97 {
98  return m_data->center;
99 }
100 
118 void QwtRoundScaleDraw::setAngleRange( double angle1, double angle2 )
119 {
120 #if 0
121  angle1 = qBound( -360.0, angle1, 360.0 );
122  angle2 = qBound( -360.0, angle2, 360.0 );
123 #endif
124 
125  m_data->startAngle = angle1;
126  m_data->endAngle = angle2;
127 
128  if ( m_data->startAngle == m_data->endAngle )
129  {
130  m_data->startAngle -= 1;
131  m_data->endAngle += 1;
132  }
133 
135 }
136 
145 void QwtRoundScaleDraw::drawLabel( QPainter* painter, double value ) const
146 {
147  const double tval = scaleMap().transform( value );
148  if ( ( tval >= m_data->startAngle + 360.0 )
149  || ( tval <= m_data->startAngle - 360.0 ) )
150  {
151  return;
152  }
153 
154  const QwtText label = tickLabel( painter->font(), value );
155  if ( label.isEmpty() )
156  return;
157 
158  double radius = m_data->radius;
161  {
162  radius += spacing();
163  }
164 
167 
168  const QSizeF sz = label.textSize( painter->font() );
169  const double arc = qwtRadians( tval );
170 
171  const double x = m_data->center.x() +
172  ( radius + sz.width() / 2.0 ) * std::sin( arc );
173  const double y = m_data->center.y() -
174  ( radius + sz.height() / 2.0 ) * std::cos( arc );
175 
176  const QRectF r( x - sz.width() / 2, y - sz.height() / 2,
177  sz.width(), sz.height() );
178  label.draw( painter, r );
179 }
180 
190 void QwtRoundScaleDraw::drawTick( QPainter* painter, double value, double len ) const
191 {
192  if ( len <= 0 )
193  return;
194 
195  const double tval = scaleMap().transform( value );
196 
197  const double cx = m_data->center.x();
198  const double cy = m_data->center.y();
199  const double radius = m_data->radius;
200 
201  if ( ( tval < m_data->startAngle + 360.0 )
202  && ( tval > m_data->startAngle - 360.0 ) )
203  {
204  const double arc = qwtRadians( tval );
205 
206  const double sinArc = std::sin( arc );
207  const double cosArc = std::cos( arc );
208 
209  const double x1 = cx + radius * sinArc;
210  const double x2 = cx + ( radius + len ) * sinArc;
211  const double y1 = cy - radius * cosArc;
212  const double y2 = cy - ( radius + len ) * cosArc;
213 
214  QwtPainter::drawLine( painter, x1, y1, x2, y2 );
215  }
216 }
217 
224 void QwtRoundScaleDraw::drawBackbone( QPainter* painter ) const
225 {
226  const double deg1 = scaleMap().p1();
227  const double deg2 = scaleMap().p2();
228 
229  const int a1 = qRound( qwtMinF( deg1, deg2 ) - 90 );
230  const int a2 = qRound( qwtMaxF( deg1, deg2 ) - 90 );
231 
232  const double radius = m_data->radius;
233  const double x = m_data->center.x() - radius;
234  const double y = m_data->center.y() - radius;
235 
236  painter->drawArc( QRectF( x, y, 2 * radius, 2 * radius ),
237  -a2 * 16, ( a2 - a1 + 1 ) * 16 ); // counterclockwise
238 }
239 
255 double QwtRoundScaleDraw::extent( const QFont& font ) const
256 {
257  double d = 0.0;
258 
260  {
261  const QwtScaleDiv& sd = scaleDiv();
262  const QList< double >& ticks = sd.ticks( QwtScaleDiv::MajorTick );
263  for ( int i = 0; i < ticks.count(); i++ )
264  {
265  const double value = ticks[i];
266  if ( !sd.contains( value ) )
267  continue;
268 
269  const double tval = scaleMap().transform( value );
270  if ( ( tval < m_data->startAngle + 360 )
271  && ( tval > m_data->startAngle - 360 ) )
272  {
273  const QwtText label = tickLabel( font, value );
274  if ( label.isEmpty() )
275  continue;
276 
277  const double arc = qwtRadians( tval );
278 
279  const QSizeF sz = label.textSize( font );
280  const double off = qMax( sz.width(), sz.height() );
281 
282  double x = off * std::sin( arc );
283  double y = off * std::cos( arc );
284 
285  const double dist = std::sqrt( x * x + y * y );
286  if ( dist > d )
287  d = dist;
288  }
289  }
290  }
291 
293  {
294  d += maxTickLength();
295  }
296 
298  {
299  d += qwtMaxF( penWidthF(), 1.0 );
300  }
301 
305  {
306  d += spacing();
307  }
308 
309  d = qwtMaxF( d, minimumExtent() );
310 
311  return d;
312 }
QwtRoundScaleDraw::drawBackbone
virtual void drawBackbone(QPainter *) const QWT_OVERRIDE
Definition: qwt_round_scale_draw.cpp:224
QwtAbstractScaleDraw::hasComponent
bool hasComponent(ScaleComponent) const
Definition: qwt_abstract_scale_draw.cpp:95
qwt_scale_div.h
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
QwtRoundScaleDraw::PrivateData::radius
double radius
Definition: qwt_round_scale_draw.cpp:38
QwtAbstractScaleDraw::scaleMap
const QwtScaleMap & scaleMap() const
Definition: qwt_abstract_scale_draw.cpp:121
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition: qwt_abstract_scale_draw.h:45
QwtRoundScaleDraw::PrivateData::center
QPointF center
Definition: qwt_round_scale_draw.cpp:37
QwtRoundScaleDraw::~QwtRoundScaleDraw
virtual ~QwtRoundScaleDraw()
Destructor.
Definition: qwt_round_scale_draw.cpp:53
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale_draw.cpp:133
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QList< double >
qwt_math.h
QwtRoundScaleDraw::moveCenter
void moveCenter(double x, double y)
Move the center of the scale draw, leaving the radius unchanged.
Definition: qwt_round_scale_draw.h:64
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtAbstractScaleDraw::Labels
@ Labels
Labels.
Definition: qwt_abstract_scale_draw.h:48
QwtScaleDiv::contains
bool contains(double value) const
Definition: qwt_scale_div.cpp:212
qwtMinF
QWT_CONSTEXPR float qwtMinF(float a, float b)
Definition: qwt_math.h:103
QwtAbstractScaleDraw::minimumExtent
double minimumExtent() const
Definition: qwt_abstract_scale_draw.cpp:302
QwtRoundScaleDraw::drawLabel
virtual void drawLabel(QPainter *, double value) const QWT_OVERRIDE
Definition: qwt_round_scale_draw.cpp:145
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition: qwt_scale_div.cpp:309
QwtScaleMap::p1
double p1() const
Definition: qwt_scale_map.h:99
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
QwtText::textSize
QSizeF textSize() const
Definition: qwt_text.cpp:570
QwtRoundScaleDraw::radius
double radius() const
Definition: qwt_round_scale_draw.cpp:79
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
qwt_scale_map.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtRoundScaleDraw::PrivateData::startAngle
double startAngle
Definition: qwt_round_scale_draw.cpp:40
qwtMaxF
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:127
QwtScaleMap::setPaintInterval
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
Definition: qwt_scale_map.cpp:119
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition: qwt_abstract_scale_draw.cpp:375
d
d
QwtRoundScaleDraw::setAngleRange
void setAngleRange(double angle1, double angle2)
Adjust the baseline circle segment for round scales.
Definition: qwt_round_scale_draw.cpp:118
qwtRadians
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:251
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtAbstractScaleDraw::penWidthF
qreal penWidthF() const
Definition: qwt_abstract_scale_draw.cpp:156
QwtRoundScaleDraw::QwtRoundScaleDraw
QwtRoundScaleDraw()
Constructor.
Definition: qwt_round_scale_draw.cpp:44
QwtRoundScaleDraw::PrivateData
Definition: qwt_round_scale_draw.cpp:19
QwtRoundScaleDraw::center
QPointF center() const
Get the center of the scale.
Definition: qwt_round_scale_draw.cpp:96
QwtRoundScaleDraw::extent
virtual double extent(const QFont &) const QWT_OVERRIDE
Definition: qwt_round_scale_draw.cpp:255
qwt_painter.h
QwtScaleMap::p2
double p2() const
Definition: qwt_scale_map.h:107
QwtText::isEmpty
bool isEmpty() const
Definition: qwt_text.cpp:739
QwtRoundScaleDraw::PrivateData::PrivateData
PrivateData()
Definition: qwt_round_scale_draw.cpp:29
qwt_round_scale_draw.h
QwtRoundScaleDraw::setRadius
void setRadius(double radius)
Definition: qwt_round_scale_draw.cpp:66
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:615
QwtAbstractScaleDraw::spacing
double spacing() const
Get the spacing.
Definition: qwt_abstract_scale_draw.cpp:271
QwtRoundScaleDraw::drawTick
virtual void drawTick(QPainter *, double value, double len) const QWT_OVERRIDE
Definition: qwt_round_scale_draw.cpp:190
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
QwtAbstractScaleDraw::tickLength
double tickLength(QwtScaleDiv::TickType) const
Definition: qwt_abstract_scale_draw.cpp:338
QwtRoundScaleDraw::PrivateData::endAngle
double endAngle
Definition: qwt_round_scale_draw.cpp:41
qwt_text.h
QwtRoundScaleDraw::m_data
PrivateData * m_data
Definition: qwt_round_scale_draw.h:66


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:24