Loading...
Searching...
No Matches
qwt_round_scale_draw.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_round_scale_draw.h"
#include "qwt_painter.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_text.h"
#include "qwt_math.h"
#include <qpainter.h>
class QwtRoundScaleDraw::PrivateData
{
public:
PrivateData()
: center( 50.0, 50.0 )
, radius( 50.0 )
, startAngle( -135.0 )
, endAngle( 135.0 )
{
}
QPointF center;
double radius;
double startAngle;
double endAngle;
};
QwtRoundScaleDraw::QwtRoundScaleDraw()
{
m_data = new QwtRoundScaleDraw::PrivateData;
setRadius( 50 );
scaleMap().setPaintInterval( m_data->startAngle, m_data->endAngle );
}
QwtRoundScaleDraw::~QwtRoundScaleDraw()
{
delete m_data;
}
void QwtRoundScaleDraw::setRadius( double radius )
{
m_data->radius = radius;
}
double QwtRoundScaleDraw::radius() const
{
return m_data->radius;
}
void QwtRoundScaleDraw::moveCenter( const QPointF& center )
{
m_data->center = center;
}
QPointF QwtRoundScaleDraw::center() const
{
return m_data->center;
}
void QwtRoundScaleDraw::setAngleRange( double angle1, double angle2 )
{
#if 0
angle1 = qBound( -360.0, angle1, 360.0 );
angle2 = qBound( -360.0, angle2, 360.0 );
#endif
m_data->startAngle = angle1;
m_data->endAngle = angle2;
if ( m_data->startAngle == m_data->endAngle )
{
m_data->startAngle -= 1;
m_data->endAngle += 1;
}
scaleMap().setPaintInterval( m_data->startAngle, m_data->endAngle );
}
void QwtRoundScaleDraw::drawLabel( QPainter* painter, double value ) const
{
const double tval = scaleMap().transform( value );
if ( ( tval >= m_data->startAngle + 360.0 )
|| ( tval <= m_data->startAngle - 360.0 ) )
{
return;
}
const QwtText label = tickLabel( painter->font(), value );
if ( label.isEmpty() )
return;
double radius = m_data->radius;
if ( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
hasComponent( QwtAbstractScaleDraw::Backbone ) )
{
radius += spacing();
}
if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
radius += tickLength( QwtScaleDiv::MajorTick );
const QSizeF sz = label.textSize( painter->font() );
const double arc = qwtRadians( tval );
const double x = m_data->center.x() +
( radius + sz.width() / 2.0 ) * std::sin( arc );
const double y = m_data->center.y() -
( radius + sz.height() / 2.0 ) * std::cos( arc );
const QRectF r( x - sz.width() / 2, y - sz.height() / 2,
sz.width(), sz.height() );
label.draw( painter, r );
}
void QwtRoundScaleDraw::drawTick( QPainter* painter, double value, double len ) const
{
if ( len <= 0 )
return;
const double tval = scaleMap().transform( value );
const double cx = m_data->center.x();
const double cy = m_data->center.y();
const double radius = m_data->radius;
if ( ( tval < m_data->startAngle + 360.0 )
&& ( tval > m_data->startAngle - 360.0 ) )
{
const double arc = qwtRadians( tval );
const double sinArc = std::sin( arc );
const double cosArc = std::cos( arc );
const double x1 = cx + radius * sinArc;
const double x2 = cx + ( radius + len ) * sinArc;
const double y1 = cy - radius * cosArc;
const double y2 = cy - ( radius + len ) * cosArc;
QwtPainter::drawLine( painter, x1, y1, x2, y2 );
}
}
void QwtRoundScaleDraw::drawBackbone( QPainter* painter ) const
{
const double deg1 = scaleMap().p1();
const double deg2 = scaleMap().p2();
const int a1 = qRound( qwtMinF( deg1, deg2 ) - 90 );
const int a2 = qRound( qwtMaxF( deg1, deg2 ) - 90 );
const double radius = m_data->radius;
const double x = m_data->center.x() - radius;
const double y = m_data->center.y() - radius;
painter->drawArc( QRectF( x, y, 2 * radius, 2 * radius ),
-a2 * 16, ( a2 - a1 + 1 ) * 16 ); // counterclockwise
}
double QwtRoundScaleDraw::extent( const QFont& font ) const
{
double d = 0.0;
if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
{
const QwtScaleDiv& sd = scaleDiv();
const QList< double >& ticks = sd.ticks( QwtScaleDiv::MajorTick );
for ( int i = 0; i < ticks.count(); i++ )
{
const double value = ticks[i];
if ( !sd.contains( value ) )
continue;
const double tval = scaleMap().transform( value );
if ( ( tval < m_data->startAngle + 360 )
&& ( tval > m_data->startAngle - 360 ) )
{
const QwtText label = tickLabel( font, value );
if ( label.isEmpty() )
continue;
const double arc = qwtRadians( tval );
const QSizeF sz = label.textSize( font );
const double off = qMax( sz.width(), sz.height() );
double x = off * std::sin( arc );
double y = off * std::cos( arc );
const double dist = std::sqrt( x * x + y * y );
if ( dist > d )
d = dist;
}
}
}
if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
{
d += maxTickLength();
}
if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
{
d += qwtMaxF( penWidthF(), 1.0 );
}
if ( hasComponent( QwtAbstractScaleDraw::Labels ) &&
( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
hasComponent( QwtAbstractScaleDraw::Backbone ) ) )
{
d += spacing();
}
d = qwtMaxF( d, minimumExtent() );
return d;
}
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