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