1 #include <qlayout.h>
2 #include <qtimer.h>
3 #include <qwt_analog_clock.h>
4 #include "attitude_indicator.h"
5 #include "speedo_meter.h"
6 #include "cockpit_grid.h"
7 
8 #if QT_VERSION < 0x040000
9 typedef QColorGroup Palette;
10 #else
11 typedef QPalette Palette;
12 #endif
13 
CockpitGrid(QWidget * parent)14 CockpitGrid::CockpitGrid(QWidget *parent):
15     QFrame(parent)
16 {
17 #if QT_VERSION >= 0x040100
18     setAutoFillBackground(true);
19 #endif
20 
21     setPalette(colorTheme(QColor(Qt::darkGray).darker(150)));
22 
23     QGridLayout *layout = new QGridLayout(this);
24     layout->setSpacing(5);
25     layout->setMargin(0);
26 
27     int i;
28     for ( i = 0; i < 3; i++ )
29     {
30         QwtDial *dial = createDial(i);
31         layout->addWidget(dial, 0, i);
32     }
33 
34 #if QT_VERSION < 0x040000
35     for ( i = 0; i < layout->numCols(); i++ )
36         layout->setColStretch(i, 1);
37 #else
38     for ( i = 0; i < layout->columnCount(); i++ )
39         layout->setColumnStretch(i, 1);
40 #endif
41 }
42 
createDial(int pos)43 QwtDial *CockpitGrid::createDial(int pos)
44 {
45     QwtDial *dial = NULL;
46     switch(pos)
47     {
48         case 0:
49         {
50             d_clock = new QwtAnalogClock(this);
51 
52             const QColor knobColor = QColor(Qt::gray).lighter(130);
53 
54             for ( int i = 0; i < QwtAnalogClock::NHands; i++)
55             {
56                 QColor handColor = QColor(Qt::gray).lighter(150);
57                 int width = 8;
58 
59                 if ( i == QwtAnalogClock::SecondHand )
60                 {
61                     handColor = Qt::gray;
62                     width = 5;
63                 }
64 
65                 QwtDialSimpleNeedle *hand = new QwtDialSimpleNeedle(
66                     QwtDialSimpleNeedle::Arrow, true, handColor, knobColor);
67                 hand->setWidth(width);
68 
69                 d_clock->setHand((QwtAnalogClock::Hand)i, hand);
70             }
71 
72             QTimer *timer = new QTimer(d_clock);
73             timer->connect(timer, SIGNAL(timeout()),
74                 d_clock, SLOT(setCurrentTime()));
75             timer->start(1000);
76 
77             dial = d_clock;
78             break;
79         }
80         case 1:
81         {
82             d_speedo = new SpeedoMeter(this);
83             d_speedo->setRange(0.0, 240.0);
84             d_speedo->setScale(-1, 2, 20);
85 
86             QTimer *timer = new QTimer(d_speedo);
87             timer->connect(timer, SIGNAL(timeout()),
88                 this, SLOT(changeSpeed()));
89             timer->start(50);
90 
91             dial = d_speedo;
92             break;
93         }
94         case 2:
95         {
96             d_ai = new AttitudeIndicator(this);
97 
98             QTimer *gradientTimer = new QTimer(d_ai);
99             gradientTimer->connect(gradientTimer, SIGNAL(timeout()),
100                 this, SLOT(changeGradient()));
101             gradientTimer->start(100);
102 
103             QTimer *angleTimer = new QTimer(d_ai);
104             angleTimer->connect(angleTimer, SIGNAL(timeout()),
105                 this, SLOT(changeAngle()));
106             angleTimer->start(100);
107 
108             dial = d_ai;
109             break;
110         }
111 
112     }
113 
114     if ( dial )
115     {
116         dial->setReadOnly(true);
117         dial->scaleDraw()->setPenWidth(3);
118         dial->setLineWidth(4);
119         dial->setFrameShadow(QwtDial::Sunken);
120     }
121     return dial;
122 }
123 
colorTheme(const QColor & base) const124 QPalette CockpitGrid::colorTheme(const QColor &base) const
125 {
126     const QColor background = base.darker(150);
127     const QColor foreground = base.darker(200);
128 
129     const QColor mid = base.darker(110);
130     const QColor dark = base.darker(170);
131     const QColor light = base.lighter(170);
132     const QColor text = foreground.lighter(800);
133 
134     QPalette palette;
135     for ( int i = 0; i < QPalette::NColorGroups; i++ )
136     {
137         QPalette::ColorGroup cg = (QPalette::ColorGroup)i;
138 
139         palette.setColor(cg, Palette::Base, base);
140         palette.setColor(cg, Palette::Window, background);
141         palette.setColor(cg, Palette::Mid, mid);
142         palette.setColor(cg, Palette::Light, light);
143         palette.setColor(cg, Palette::Dark, dark);
144         palette.setColor(cg, Palette::Text, text);
145         palette.setColor(cg, Palette::WindowText, foreground);
146     }
147 
148     return palette;
149 }
150 
changeSpeed()151 void CockpitGrid::changeSpeed()
152 {
153     static double offset = 0.8;
154 
155     double speed = d_speedo->value();
156 
157     if ( (speed < 40.0 && offset < 0.0 ) ||
158         (speed > 160.0 && offset > 0.0) )
159     {
160         offset = -offset;
161     }
162 
163     static int counter = 0;
164     switch(counter++ % 12 )
165     {
166         case 0:
167         case 2:
168         case 7:
169         case 8:
170             break;
171         default:
172             d_speedo->setValue(speed + offset);
173     }
174 }
175 
changeAngle()176 void CockpitGrid::changeAngle()
177 {
178     static double offset = 0.05;
179 
180     double angle = d_ai->angle();
181     if ( angle > 180.0 )
182         angle -= 360.0;
183 
184     if ( (angle < -5.0 && offset < 0.0 ) ||
185         (angle > 5.0 && offset > 0.0) )
186     {
187         offset = -offset;
188     }
189 
190     d_ai->setAngle(angle + offset);
191 }
192 
changeGradient()193 void CockpitGrid::changeGradient()
194 {
195     static double offset = 0.005;
196 
197     double gradient = d_ai->gradient();
198 
199     if ( (gradient < -0.05 && offset < 0.0 ) ||
200         (gradient > 0.05 && offset > 0.0) )
201     {
202         offset = -offset;
203     }
204 
205     d_ai->setGradient(gradient + offset);
206 }
207