Connection tracker class finished.
[yandex-traffic] / mainwidget.cpp
1 #include <QtGui>
2
3 #include "mainwidget.hpp"
4
5
6
7 // --------------------------------------------------
8 // MainWidget
9 // --------------------------------------------------
10 MainWidget::MainWidget ()
11     : QWidget ()
12 {
13 #ifdef Q_WS_MAEMO_5
14         setAttribute(Qt::WA_TranslucentBackground);
15 #endif
16         _light = new TrafficLight (this);
17         _label = new QLabel (this);
18
19         QHBoxLayout *layout = new QHBoxLayout;
20         layout->addWidget (_light);
21         layout->addWidget (_label);
22         setLayout (layout);
23
24         connect (&_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
25
26         // every 5 minutes (TODO, make option)
27         startTimer (5*60*1000);
28
29         // perform update just after creation
30         _traffic.update ();
31 }
32
33
34 QSize MainWidget::sizeHint() const
35 {
36     return _label->sizeHint () + _light->sizeHint () + QSize (20, 0);
37 }
38
39
40 void MainWidget::paintEvent(QPaintEvent *event)
41 {
42     QPainter p(this);
43     p.setBrush(QColor(0, 0, 0, 128));
44     p.setPen(Qt::NoPen);
45     p.drawRoundedRect(rect(), 10, 10);
46     p.end();
47
48     QWidget::paintEvent(event);
49 }
50
51
52 void MainWidget::timerEvent (QTimerEvent *)
53 {
54     // Perform traffic information refresh
55     // TODO: only if internet connection is available
56     _traffic.update ();
57 }
58
59
60 void MainWidget::trafficUpdated ()
61 {
62     ExtendedTrafficInfo info = _traffic.lookup_ext ("1");
63
64     if (info.valid ()) {
65         _light->setColor (info.color ());
66         _label->setText (QString ("%1, %2\n%3")
67                          .arg (QString::number (info.level ()))
68                          .arg (info.localtime ())
69                          .arg (info.hint ()));
70     }
71     else
72         _light->setColor (ExtendedTrafficInfo::Unknown);
73 }