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_math.h"
15 #include <qpen.h>
16 #include <qpainter.h>
17 #include <qfontmetrics.h>
18 #include <qmath.h>
19 
21 {
22 public:
24  center( 50.0, 50.0 ),
25  radius( 50.0 ),
26  startAngle( -135.0 ),
27  endAngle( 135.0 )
28  {
29  }
30 
31  QPointF center;
32  double radius;
33 
34  double startAngle;
35  double endAngle;
36 };
37 
46 {
48 
49  setRadius( 50 );
51 }
52 
55 {
56  delete d_data;
57 }
58 
68 {
69  d_data->radius = radius;
70 }
71 
81 {
82  return d_data->radius;
83 }
84 
91 void QwtRoundScaleDraw::moveCenter( const QPointF &center )
92 {
93  d_data->center = center;
94 }
95 
98 {
99  return d_data->center;
100 }
101 
119 void QwtRoundScaleDraw::setAngleRange( double angle1, double angle2 )
120 {
121 #if 0
122  angle1 = qBound( -360.0, angle1, 360.0 );
123  angle2 = qBound( -360.0, angle2, 360.0 );
124 #endif
125 
126  d_data->startAngle = angle1;
127  d_data->endAngle = angle2;
128 
129  if ( d_data->startAngle == d_data->endAngle )
130  {
131  d_data->startAngle -= 1;
132  d_data->endAngle += 1;
133  }
134 
136 }
137 
146 void QwtRoundScaleDraw::drawLabel( QPainter *painter, double value ) const
147 {
148  const double tval = scaleMap().transform( value );
149  if ( ( tval >= d_data->startAngle + 360.0 )
150  || ( tval <= d_data->startAngle - 360.0 ) )
151  {
152  return;
153  }
154 
155  const QwtText label = tickLabel( painter->font(), value );
156  if ( label.isEmpty() )
157  return;
158 
159  double radius = d_data->radius;
162  {
163  radius += spacing();
164  }
165 
167  radius += tickLength( QwtScaleDiv::MajorTick );
168 
169  const QSizeF sz = label.textSize( painter->font() );
170  const double arc = qwtRadians( tval );
171 
172  const double x = d_data->center.x() +
173  ( radius + sz.width() / 2.0 ) * qSin( arc );
174  const double y = d_data->center.y() -
175  ( radius + sz.height() / 2.0 ) * qCos( arc );
176 
177  const QRectF r( x - sz.width() / 2, y - sz.height() / 2,
178  sz.width(), sz.height() );
179  label.draw( painter, r );
180 }
181 
191 void QwtRoundScaleDraw::drawTick( QPainter *painter, double value, double len ) const
192 {
193  if ( len <= 0 )
194  return;
195 
196  const double tval = scaleMap().transform( value );
197 
198  const double cx = d_data->center.x();
199  const double cy = d_data->center.y();
200  const double radius = d_data->radius;
201 
202  if ( ( tval < d_data->startAngle + 360.0 )
203  && ( tval > d_data->startAngle - 360.0 ) )
204  {
205  const double arc = qwtRadians( tval );
206 
207  const double sinArc = qSin( arc );
208  const double cosArc = qCos( arc );
209 
210  const double x1 = cx + radius * sinArc;
211  const double x2 = cx + ( radius + len ) * sinArc;
212  const double y1 = cy - radius * cosArc;
213  const double y2 = cy - ( radius + len ) * cosArc;
214 
215  QwtPainter::drawLine( painter, x1, y1, x2, y2 );
216  }
217 }
218 
225 void QwtRoundScaleDraw::drawBackbone( QPainter *painter ) const
226 {
227  const double deg1 = scaleMap().p1();
228  const double deg2 = scaleMap().p2();
229 
230  const int a1 = qRound( qMin( deg1, deg2 ) - 90 );
231  const int a2 = qRound( qMax( deg1, deg2 ) - 90 );
232 
233  const double radius = d_data->radius;
234  const double x = d_data->center.x() - radius;
235  const double y = d_data->center.y() - radius;
236 
237  painter->drawArc( QRectF( x, y, 2 * radius, 2 * radius ),
238  -a2 * 16, ( a2 - a1 + 1 ) * 16 ); // counterclockwise
239 }
240 
256 double QwtRoundScaleDraw::extent( const QFont &font ) const
257 {
258  double d = 0.0;
259 
261  {
262  const QwtScaleDiv &sd = scaleDiv();
263  const QList<double> &ticks = sd.ticks( QwtScaleDiv::MajorTick );
264  for ( int i = 0; i < ticks.count(); i++ )
265  {
266  const double value = ticks[i];
267  if ( !sd.contains( value ) )
268  continue;
269 
270  const double tval = scaleMap().transform( value );
271  if ( ( tval < d_data->startAngle + 360 )
272  && ( tval > d_data->startAngle - 360 ) )
273  {
274  const QwtText label = tickLabel( font, value );
275  if ( label.isEmpty() )
276  continue;
277 
278  const double arc = qwtRadians( tval );
279 
280  const QSizeF sz = label.textSize( font );
281  const double off = qMax( sz.width(), sz.height() );
282 
283  double x = off * qSin( arc );
284  double y = off * qCos( arc );
285 
286  const double dist = qSqrt( x * x + y * y );
287  if ( dist > d )
288  d = dist;
289  }
290  }
291  }
292 
294  {
295  d += maxTickLength();
296  }
297 
299  {
300  const double pw = qMax( 1, penWidth() ); // pen width can be zero
301  d += pw;
302  }
303 
307  {
308  d += spacing();
309  }
310 
311  d = qMax( d, minimumExtent() );
312 
313  return d;
314 }
d
double p1() const
static void drawLine(QPainter *, double x1, double y1, double x2, double y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:147
double p2() const
virtual void drawTick(QPainter *, double val, double len) const
virtual ~QwtRoundScaleDraw()
Destructor.
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
virtual double extent(const QFont &) const
A class representing a scale division.
Definition: qwt_scale_div.h:36
const QwtScaleMap & scaleMap() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
T::first_type a1
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.
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
bool contains(double value) const
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:144
const QwtScaleDiv & scaleDiv() const
bool isEmpty() const
Definition: qwt_text.h:213
T value
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.
TFSIMD_FORCE_INLINE const tfScalar & x() const
Backbone = the line where the ticks are located.
virtual QwtText label(double) const
Convert a value into its representing label.
virtual void drawLabel(QPainter *, double val) const
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.
T::first_type a2
bool hasComponent(ScaleComponent) const
void setRadius(double radius)
double transform(double s) const
virtual void drawBackbone(QPainter *) const
int i
QList< double > ticks(int tickType) const
double tickLength(QwtScaleDiv::TickType) const
double spacing() const
Get the spacing.


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