qwt_round_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 
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:
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 d_data;
56 }
57 
67 {
68  d_data->radius = radius;
69 }
70 
80 {
81  return d_data->radius;
82 }
83 
90 void QwtRoundScaleDraw::moveCenter( const QPointF &center )
91 {
92  d_data->center = center;
93 }
94 
97 {
98  return d_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  d_data->startAngle = angle1;
126  d_data->endAngle = angle2;
127 
128  if ( d_data->startAngle == d_data->endAngle )
129  {
130  d_data->startAngle -= 1;
131  d_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 >= d_data->startAngle + 360.0 )
149  || ( tval <= d_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 = d_data->radius;
161  {
162  radius += spacing();
163  }
164 
166  radius += tickLength( QwtScaleDiv::MajorTick );
167 
168  const QSizeF sz = label.textSize( painter->font() );
169  const double arc = qwtRadians( tval );
170 
171  const double x = d_data->center.x() +
172  ( radius + sz.width() / 2.0 ) * std::sin( arc );
173  const double y = d_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 = d_data->center.x();
198  const double cy = d_data->center.y();
199  const double radius = d_data->radius;
200 
201  if ( ( tval < d_data->startAngle + 360.0 )
202  && ( tval > d_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 = d_data->radius;
233  const double x = d_data->center.x() - radius;
234  const double y = d_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 < d_data->startAngle + 360 )
271  && ( tval > d_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 }
double p1() const
Definition: qwt_scale_map.h:99
virtual void drawBackbone(QPainter *) const QWT_OVERRIDE
enum MQTTPropertyCodes value
double p2() const
QSizeF textSize() const
Definition: qwt_text.cpp:547
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:152
MQTTClient d
Definition: test10.c:1656
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:123
virtual ~QwtRoundScaleDraw()
Destructor.
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:592
A class representing a scale division.
Definition: qwt_scale_div.h:33
const QwtScaleMap & scaleMap() const
QWT_CONSTEXPR float qwtMinF(float a, float b)
Definition: qwt_math.h:99
virtual void drawLabel(QPainter *, double value) const QWT_OVERRIDE
void moveCenter(double x, double y)
Move the center of the scale draw, leaving the radius unchanged.
QPointF center() const
Get the center of the scale.
bool contains(double value) const
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:247
const QwtScaleDiv & scaleDiv() const
bool isEmpty() const
Definition: qwt_text.cpp:716
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
A class representing a text.
Definition: qwt_text.h:51
QwtRoundScaleDraw()
Constructor.
Backbone = the line where the ticks are located.
virtual QwtText label(double) const
Convert a value into its representing label.
virtual double extent(const QFont &) const QWT_OVERRIDE
void setAngleRange(double angle1, double angle2)
Adjust the baseline circle segment for round scales.
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
virtual void drawTick(QPainter *, double value, double len) const QWT_OVERRIDE
bool hasComponent(ScaleComponent) const
void setRadius(double radius)
double transform(double s) const
QList< double > ticks(int tickType) const
double tickLength(QwtScaleDiv::TickType) const
double spacing() const
Get the spacing.
int len
Definition: utf-8.c:46


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10