Qwt User's Guide: qwt_round_scale_draw.cpp Source File

Qwt User's Guide 6.2.0

qwt_round_scale_draw.cpp
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
19 class QwtRoundScaleDraw::PrivateData
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
44 QwtRoundScaleDraw::QwtRoundScaleDraw()
45 {
46 m_data = new QwtRoundScaleDraw::PrivateData;
47
48 setRadius( 50 );
49 scaleMap().setPaintInterval( m_data->startAngle, m_data->endAngle );
50 }
51
53 QwtRoundScaleDraw::~QwtRoundScaleDraw()
54 {
55 delete m_data;
56 }
57
66 void QwtRoundScaleDraw::setRadius( double radius )
67 {
68 m_data->radius = radius;
69 }
70
79 double QwtRoundScaleDraw::radius() const
80 {
81 return m_data->radius;
82 }
83
90 void QwtRoundScaleDraw::moveCenter( const QPointF& center )
91 {
92 m_data->center = center;
93 }
94
96 QPointF QwtRoundScaleDraw::center() const
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
134 scaleMap().setPaintInterval( m_data->startAngle, m_data->endAngle );
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;
159 if ( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
160 hasComponent( QwtAbstractScaleDraw::Backbone ) )
161 {
162 radius += spacing();
163 }
164
165 if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
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 = 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
259 if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
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
292 if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
293 {
294 d += maxTickLength();
295 }
296
297 if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
298 {
299 d += qwtMaxF( penWidthF(), 1.0 );
300 }
301
302 if ( hasComponent( QwtAbstractScaleDraw::Labels ) &&
303 ( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
304 hasComponent( QwtAbstractScaleDraw::Backbone ) ) )
305 {
306 d += spacing();
307 }
308
309 d = qwtMaxF( d, minimumExtent() );
310
311 return d;
312 }
QList< double >
QwtAbstractScaleDraw::scaleMap
const QwtScaleMap & scaleMap() const
Definition: qwt_abstract_scale_draw.cpp:121
QwtAbstractScaleDraw::Ticks
@ Ticks
Ticks.
Definition: qwt_abstract_scale_draw.h:45
QwtAbstractScaleDraw::Backbone
@ Backbone
Backbone = the line where the ticks are located.
Definition: qwt_abstract_scale_draw.h:42
QwtAbstractScaleDraw::Labels
@ Labels
Labels.
Definition: qwt_abstract_scale_draw.h:48
QwtAbstractScaleDraw::tickLength
double tickLength(QwtScaleDiv::TickType) const
Definition: qwt_abstract_scale_draw.cpp:338
QwtAbstractScaleDraw::hasComponent
bool hasComponent(ScaleComponent) const
Definition: qwt_abstract_scale_draw.cpp:95
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:393
QwtAbstractScaleDraw::maxTickLength
double maxTickLength() const
Definition: qwt_abstract_scale_draw.cpp:355
QwtAbstractScaleDraw::minimumExtent
double minimumExtent() const
Definition: qwt_abstract_scale_draw.cpp:302
QwtAbstractScaleDraw::label
virtual QwtText label(double) const
Convert a value into its representing label.
Definition: qwt_abstract_scale_draw.cpp:375
QwtAbstractScaleDraw::penWidthF
qreal penWidthF() const
Definition: qwt_abstract_scale_draw.cpp:156
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale_draw.cpp:133
QwtAbstractScaleDraw::spacing
double spacing() const
Get the spacing.
Definition: qwt_abstract_scale_draw.cpp:271
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtRoundScaleDraw::extent
virtual double extent(const QFont &) const override
Definition: qwt_round_scale_draw.cpp:255
QwtRoundScaleDraw::setRadius
void setRadius(double radius)
Definition: qwt_round_scale_draw.cpp:66
QwtRoundScaleDraw::center
QPointF center() const
Get the center of the scale.
Definition: qwt_round_scale_draw.cpp:96
QwtRoundScaleDraw::drawBackbone
virtual void drawBackbone(QPainter *) const override
Definition: qwt_round_scale_draw.cpp:224
QwtRoundScaleDraw::drawLabel
virtual void drawLabel(QPainter *, double value) const override
Definition: qwt_round_scale_draw.cpp:145
QwtRoundScaleDraw::setAngleRange
void setAngleRange(double angle1, double angle2)
Adjust the baseline circle segment for round scales.
Definition: qwt_round_scale_draw.cpp:118
QwtRoundScaleDraw::~QwtRoundScaleDraw
virtual ~QwtRoundScaleDraw()
Destructor.
Definition: qwt_round_scale_draw.cpp:53
QwtRoundScaleDraw::radius
double radius() const
Definition: qwt_round_scale_draw.cpp:79
QwtRoundScaleDraw::QwtRoundScaleDraw
QwtRoundScaleDraw()
Constructor.
Definition: qwt_round_scale_draw.cpp:44
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
QwtRoundScaleDraw::drawTick
virtual void drawTick(QPainter *, double value, double len) const override
Definition: qwt_round_scale_draw.cpp:190
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:34
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition: qwt_scale_div.cpp:309
QwtScaleDiv::contains
bool contains(double value) const
Definition: qwt_scale_div.cpp:212
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition: qwt_scale_div.h:49
QwtScaleMap::p1
double p1() const
Definition: qwt_scale_map.h:99
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtScaleMap::setPaintInterval
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
Definition: qwt_scale_map.cpp:119
QwtScaleMap::p2
double p2() const
Definition: qwt_scale_map.h:107
QwtText
A class representing a text.
Definition: qwt_text.h:52
QwtText::textSize
QSizeF textSize() const
Definition: qwt_text.cpp:570
QwtText::isEmpty
bool isEmpty() const
Definition: qwt_text.cpp:739
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:615