1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <qevent.h>
#include <qpainter.h>
#include <qwt_math.h>
#include "attitude_indicator.h"
AttitudeIndicatorNeedle::AttitudeIndicatorNeedle(const QColor &c)
{
QPalette palette;
for ( int i = 0; i < QPalette::NColorGroups; i++ )
{
palette.setColor((QPalette::ColorGroup)i,
QColorGroup::Text, c);
}
setPalette(palette);
}
void AttitudeIndicatorNeedle::draw(QPainter *painter, const QPoint ¢er,
int length, double direction, QPalette::ColorGroup cg) const
{
direction *= M_PI / 180.0;
int triangleSize = qRound(length * 0.1);
painter->save();
const QPoint p0(QPoint(center.x() + 1, center.y() + 1));
const QPoint p1 = qwtPolar2Pos(p0,
length - 2 * triangleSize - 2, direction);
QPointArray pa(3);
pa.setPoint(0, qwtPolar2Pos(p1, 2 * triangleSize, direction));
pa.setPoint(1, qwtPolar2Pos(p1, triangleSize, direction + M_PI_2));
pa.setPoint(2, qwtPolar2Pos(p1, triangleSize, direction - M_PI_2));
painter->setBrush(colorGroup(cg).text());
painter->drawPolygon(pa);
painter->setPen(QPen(colorGroup(cg).text(), 3));
painter->drawLine(qwtPolar2Pos(p0, length - 2, direction + M_PI_2),
qwtPolar2Pos(p0, length - 2, direction - M_PI_2));
painter->restore();
}
AttitudeIndicator::AttitudeIndicator(
QWidget *parent, const char *name):
QwtDial(parent, name),
d_gradient(0.0)
{
setMode(RotateScale);
setWrapping(TRUE);
setOrigin(270.0);
setScaleOptions(ScaleTicks);
setScale(0, 0, 30.0);
setNeedle(new AttitudeIndicatorNeedle(colorGroup().text()));
}
void AttitudeIndicator::setGradient(double gradient)
{
if ( gradient < -1.0 )
gradient = -1.0;
else if ( gradient > 1.0 )
gradient = 1.0;
if ( d_gradient != gradient )
{
d_gradient = gradient;
update();
}
}
void AttitudeIndicator::drawScale(QPainter *painter, const QPoint ¢er,
int radius, double origin, double minArc, double maxArc) const
{
double dir = (360.0 - origin) * M_PI / 180.0; // counter clockwise, radian
int offset = 4;
const QPoint p0 = qwtPolar2Pos(center, offset, dir + M_PI);
const int w = contentsRect().width();
QPointArray pa(4);
pa.setPoint(0, qwtPolar2Pos(p0, w, dir - M_PI_2));
pa.setPoint(1, qwtPolar2Pos(pa.point(0), 2 * w, dir + M_PI_2));
pa.setPoint(2, qwtPolar2Pos(pa.point(1), w, dir));
pa.setPoint(3, qwtPolar2Pos(pa.point(2), 2 * w, dir - M_PI_2));
painter->save();
painter->setClipRegion(pa); // swallow 180 - 360 degrees
QwtDial::drawScale(painter, center, radius, origin,
minArc, maxArc);
painter->restore();
}
void AttitudeIndicator::drawScaleContents(QPainter *painter,
const QPoint &, int) const
{
int dir = 360 - qRound(origin() - value()); // counter clockwise
int arc = 90 + qRound(gradient() * 90);
const QColor skyColor(38, 151, 221);
painter->save();
painter->setBrush(skyColor);
painter->drawChord(scaleContentsRect(),
(dir - arc) * 16, 2 * arc * 16 );
painter->restore();
}
void AttitudeIndicator::keyPressEvent(QKeyEvent *e)
{
switch(e->key())
{
case Qt::Key_Plus:
setGradient(gradient() + 0.05);
break;
case Qt::Key_Minus:
setGradient(gradient() - 0.05);
break;
default:
QwtDial::keyPressEvent(e);
}
}
|