A class for drawing round scales. More...
#include <qwt_round_scale_draw.h>
Classes | |
class | PrivateData |
Public Member Functions | |
QwtRoundScaleDraw () | |
Constructor. | |
QwtRoundScaleDraw (const QwtRoundScaleDraw &) | |
Copy constructor. | |
virtual | ~QwtRoundScaleDraw () |
Destructor. | |
QwtRoundScaleDraw & | operator= (const QwtRoundScaleDraw &other) |
Assignment operator. | |
void | setRadius (int radius) |
int | radius () const |
void | moveCenter (int x, int y) |
Move the center of the scale draw, leaving the radius unchanged. | |
void | moveCenter (const QPoint &) |
QPoint | center () const |
Get the center of the scale. | |
void | setAngleRange (double angle1, double angle2) |
Adjust the baseline circle segment for round scales. | |
virtual int | extent (const QPen &, const QFont &) const |
Protected Member Functions | |
virtual void | drawTick (QPainter *p, double val, int len) const |
virtual void | drawBackbone (QPainter *p) const |
virtual void | drawLabel (QPainter *p, double val) const |
A class for drawing round scales.
QwtRoundScaleDraw can be used to draw round scales. The circle segment can be adjusted by QwtRoundScaleDraw::setAngleRange(). The geometry of the scale can be specified with QwtRoundScaleDraw::moveCenter() and QwtRoundScaleDraw::setRadius().
After a scale division has been specified as a QwtScaleDiv object using QwtAbstractScaleDraw::setScaleDiv(const QwtScaleDiv &s), the scale can be drawn with the QwtAbstractScaleDraw::draw() member.
Definition at line 32 of file qwt_round_scale_draw.h.
QwtRoundScaleDraw::QwtRoundScaleDraw | ( | ) |
Constructor.
The range of the scale is initialized to [0, 100], The center is set to (50, 50) with a radius of 50. The angle range is set to [-135, 135].
Definition at line 46 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::endAngle, QwtAbstractScaleDraw::scaleMap(), QwtScaleMap::setPaintInterval(), setRadius(), and QwtRoundScaleDraw::PrivateData::startAngle.
{ d_data = new QwtRoundScaleDraw::PrivateData; setRadius(50); scaleMap().setPaintInterval(d_data->startAngle, d_data->endAngle); }
QwtRoundScaleDraw::QwtRoundScaleDraw | ( | const QwtRoundScaleDraw & | other ) |
Copy constructor.
Definition at line 55 of file qwt_round_scale_draw.cpp.
: QwtAbstractScaleDraw(other) { d_data = new QwtRoundScaleDraw::PrivateData(*other.d_data); }
QwtRoundScaleDraw::~QwtRoundScaleDraw | ( | ) | [virtual] |
QPoint QwtRoundScaleDraw::center | ( | ) | const |
Get the center of the scale.
Definition at line 113 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::center.
Referenced by moveCenter().
{ return d_data->center; }
void QwtRoundScaleDraw::drawBackbone | ( | QPainter * | painter ) | const [protected, virtual] |
Draws the baseline of the scale
painter | Painter |
Implements QwtAbstractScaleDraw.
Definition at line 239 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::center, QwtAbstractScaleDraw::map(), qwtMax, qwtMin, QwtRoundScaleDraw::PrivateData::radius, and radius().
{ const int a1 = qRound(qwtMin(map().p1(), map().p2()) - 90 * 16); const int a2 = qRound(qwtMax(map().p1(), map().p2()) - 90 * 16); const int radius = d_data->radius; const int x = d_data->center.x() - radius; const int y = d_data->center.y() - radius; painter->drawArc(x, y, 2 * radius, 2 * radius, -a2, a2 - a1 + 1); // counterclockwise }
void QwtRoundScaleDraw::drawLabel | ( | QPainter * | painter, |
double | value | ||
) | const [protected, virtual] |
Draws the label for a major scale tick
painter | Painter |
value | Value |
Implements QwtAbstractScaleDraw.
Definition at line 160 of file qwt_round_scale_draw.cpp.
References QwtAbstractScaleDraw::Backbone, QwtRoundScaleDraw::PrivateData::center, QwtText::draw(), QwtAbstractScaleDraw::hasComponent(), QwtText::isEmpty(), QwtAbstractScaleDraw::label(), M_PI, QwtAbstractScaleDraw::majTickLength(), QwtAbstractScaleDraw::map(), QwtRoundScaleDraw::PrivateData::radius, radius(), QwtAbstractScaleDraw::spacing(), QwtRoundScaleDraw::PrivateData::startAngle, QwtText::textSize(), QwtAbstractScaleDraw::tickLabel(), QwtAbstractScaleDraw::Ticks, and QwtScaleMap::transform().
{ const QwtText label = tickLabel(painter->font(), value); if ( label.isEmpty() ) return; const int tval = map().transform(value); if ((tval > d_data->startAngle + 359 * 16) || (tval < d_data->startAngle - 359 * 16)) { return; } double radius = d_data->radius; if ( hasComponent(QwtAbstractScaleDraw::Ticks) || hasComponent(QwtAbstractScaleDraw::Backbone) ) { radius += spacing(); } if ( hasComponent(QwtAbstractScaleDraw::Ticks) ) radius += majTickLength(); const QSize sz = label.textSize(painter->font()); const double arc = tval / 16.0 / 360.0 * 2 * M_PI; const int x = d_data->center.x() + qRound((radius + sz.width() / 2.0) * sin(arc)); const int y = d_data->center.y() - qRound( (radius + sz.height() / 2.0) * cos(arc)); const QRect r(x - sz.width() / 2, y - sz.height() / 2, sz.width(), sz.height() ); label.draw(painter, r); }
void QwtRoundScaleDraw::drawTick | ( | QPainter * | painter, |
double | value, | ||
int | len | ||
) | const [protected, virtual] |
Draw a tick
painter | Painter |
value | Value of the tick |
len | Lenght of the tick |
Implements QwtAbstractScaleDraw.
Definition at line 205 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::center, QwtPainter::drawLine(), M_PI, QwtAbstractScaleDraw::map(), QwtRoundScaleDraw::PrivateData::radius, radius(), QwtRoundScaleDraw::PrivateData::startAngle, and QwtScaleMap::transform().
{ if ( len <= 0 ) return; const int tval = map().transform(value); const int cx = d_data->center.x(); const int cy = d_data->center.y(); const int radius = d_data->radius; if ((tval <= d_data->startAngle + 359 * 16) || (tval >= d_data->startAngle - 359 * 16)) { const double arc = double(tval) / 16.0 * M_PI / 180.0; const double sinArc = sin(arc); const double cosArc = cos(arc); const int x1 = qRound( cx + radius * sinArc ); const int x2 = qRound( cx + (radius + len) * sinArc ); const int y1 = qRound( cy - radius * cosArc ); const int y2 = qRound( cy - (radius + len) * cosArc ); QwtPainter::drawLine(painter, x1, y1, x2, y2); } }
int QwtRoundScaleDraw::extent | ( | const QPen & | pen, |
const QFont & | font | ||
) | const [virtual] |
Calculate the extent of the scale
The extent is the distcance between the baseline to the outermost pixel of the scale draw. radius() + extent() is an upper limit for the radius of the bounding circle.
pen | Pen that is used for painting backbone and ticks |
font | Font used for painting the labels |
Implements QwtAbstractScaleDraw.
Definition at line 267 of file qwt_round_scale_draw.cpp.
References QwtAbstractScaleDraw::Backbone, QwtScaleDiv::contains(), QuadProgPP::dist(), QwtAbstractScaleDraw::hasComponent(), int(), QwtText::isEmpty(), QwtAbstractScaleDraw::label(), QwtAbstractScaleDraw::Labels, M_PI, QwtScaleDiv::MajorTick, QwtAbstractScaleDraw::majTickLength(), QwtAbstractScaleDraw::map(), QwtAbstractScaleDraw::minimumExtent(), qwtMax, QwtAbstractScaleDraw::scaleDiv(), QwtAbstractScaleDraw::spacing(), QuadProgPP::sqrt(), QwtRoundScaleDraw::PrivateData::startAngle, QwtText::textSize(), QwtAbstractScaleDraw::tickLabel(), QwtAbstractScaleDraw::Ticks, QwtScaleDiv::ticks(), and QwtScaleMap::transform().
Referenced by QwtKnob::minimumSizeHint(), QwtDial::minimumSizeHint(), QwtDial::scaleContentsRect(), and QwtDial::sizeHint().
{ int d = 0; if ( hasComponent(QwtAbstractScaleDraw::Labels) ) { const QwtScaleDiv &sd = scaleDiv(); const QwtValueList &ticks = sd.ticks(QwtScaleDiv::MajorTick); for (uint i = 0; i < (uint)ticks.count(); i++) { const double value = ticks[i]; if ( !sd.contains(value) ) continue; const QwtText label = tickLabel(font, value); if ( label.isEmpty() ) continue; const int tval = map().transform(value); if ((tval < d_data->startAngle + 360 * 16) && (tval > d_data->startAngle - 360 * 16)) { const double arc = tval / 16.0 / 360.0 * 2 * M_PI; const QSize sz = label.textSize(font); const double off = qwtMax(sz.width(), sz.height()); double x = off * sin(arc); double y = off * cos(arc); const int dist = (int)ceil(sqrt(x * x + y * y) + 1 ); if ( dist > d ) d = dist; } } } if ( hasComponent(QwtAbstractScaleDraw::Ticks) ) { d += majTickLength(); } if ( hasComponent(QwtAbstractScaleDraw::Backbone) ) { const int pw = qwtMax( 1, pen.width() ); // penwidth can be zero d += pw; } if ( hasComponent(QwtAbstractScaleDraw::Labels) && ( hasComponent(QwtAbstractScaleDraw::Ticks) || hasComponent(QwtAbstractScaleDraw::Backbone) ) ) { d += spacing(); } d = qwtMax(d, minimumExtent()); return d; }
void QwtRoundScaleDraw::moveCenter | ( | const QPoint & | center ) |
Move the center of the scale draw, leaving the radius unchanged
center | New center |
Definition at line 107 of file qwt_round_scale_draw.cpp.
References center(), and QwtRoundScaleDraw::PrivateData::center.
void QwtRoundScaleDraw::moveCenter | ( | int | x, |
int | y | ||
) | [inline] |
Move the center of the scale draw, leaving the radius unchanged.
Definition at line 64 of file qwt_round_scale_draw.h.
Referenced by QwtDial::drawScale().
{ moveCenter(QPoint(x, y)); }
QwtRoundScaleDraw & QwtRoundScaleDraw::operator= | ( | const QwtRoundScaleDraw & | other ) |
Assignment operator.
Definition at line 69 of file qwt_round_scale_draw.cpp.
{ *(QwtAbstractScaleDraw*)this = (const QwtAbstractScaleDraw &)other; *d_data = *other.d_data; return *this; }
int QwtRoundScaleDraw::radius | ( | ) | const |
Get the radius
Radius is the radius of the backbone without ticks and labels.
Definition at line 96 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::radius.
Referenced by drawBackbone(), drawLabel(), drawTick(), and setRadius().
{ return d_data->radius; }
void QwtRoundScaleDraw::setAngleRange | ( | double | angle1, |
double | angle2 | ||
) |
Adjust the baseline circle segment for round scales.
The baseline will be drawn from min(angle1,angle2) to max(angle1, angle2). The default setting is [ -135, 135 ]. An angle of 0 degrees corresponds to the 12 o'clock position, and positive angles count in a clockwise direction.
angle1 | |
angle2 | boundaries of the angle interval in degrees. |
Definition at line 135 of file qwt_round_scale_draw.cpp.
References QwtRoundScaleDraw::PrivateData::endAngle, qwtLim(), QwtAbstractScaleDraw::scaleMap(), QwtScaleMap::setPaintInterval(), and QwtRoundScaleDraw::PrivateData::startAngle.
Referenced by QwtDial::drawScale(), and QwtKnob::setTotalAngle().
{ angle1 = qwtLim(angle1, -360.0, 360.0); angle2 = qwtLim(angle2, -360.0, 360.0); d_data->startAngle = qRound(angle1 * 16.0); d_data->endAngle = qRound(angle2 * 16.0); if (d_data->startAngle == d_data->endAngle) { d_data->startAngle -= 1; d_data->endAngle += 1; } scaleMap().setPaintInterval(d_data->startAngle, d_data->endAngle); }
void QwtRoundScaleDraw::setRadius | ( | int | radius ) |
Change of radius the scale
Radius is the radius of the backbone without ticks and labels.
radius | New Radius |
Definition at line 84 of file qwt_round_scale_draw.cpp.
References radius(), and QwtRoundScaleDraw::PrivateData::radius.
Referenced by QwtDial::drawScale(), and QwtRoundScaleDraw().