Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
19class 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
45{
46 m_data = new QwtRoundScaleDraw::PrivateData;
47
48 setRadius( 50 );
49 scaleMap().setPaintInterval( m_data->startAngle, m_data->endAngle );
50}
51
54{
55 delete m_data;
56}
57
66void QwtRoundScaleDraw::setRadius( double radius )
67{
68 m_data->radius = radius;
69}
70
80{
81 return m_data->radius;
82}
83
90void QwtRoundScaleDraw::moveCenter( const QPointF& center )
91{
92 m_data->center = center;
93}
94
97{
98 return m_data->center;
99}
100
118void 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
145void 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
190void 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
224void 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
255double 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}
const QwtScaleMap & scaleMap() const
@ Backbone
Backbone = the line where the ticks are located.
double tickLength(QwtScaleDiv::TickType) const
bool hasComponent(ScaleComponent) const
const QwtText & tickLabel(const QFont &, double value) const
Convert a value into its representing label and cache it.
virtual QwtText label(double) const
Convert a value into its representing label.
const QwtScaleDiv & scaleDiv() const
double spacing() const
Get the spacing.
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
virtual double extent(const QFont &) const override
void setRadius(double radius)
QPointF center() const
Get the center of the scale.
virtual void drawBackbone(QPainter *) const override
virtual void drawLabel(QPainter *, double value) const override
void setAngleRange(double angle1, double angle2)
Adjust the baseline circle segment for round scales.
virtual ~QwtRoundScaleDraw()
Destructor.
QwtRoundScaleDraw()
Constructor.
void moveCenter(double x, double y)
Move the center of the scale draw, leaving the radius unchanged.
virtual void drawTick(QPainter *, double value, double len) const override
A class representing a scale division.
QList< double > ticks(int tickType) const
bool contains(double value) const
@ MajorTick
Major ticks.
double p1() const
double transform(double s) const
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
double p2() const
A class representing a text.
Definition qwt_text.h:52
QSizeF textSize() const
Definition qwt_text.cpp:570
bool isEmpty() const
Definition qwt_text.cpp:739
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615