Make display according to settings.
[yandex-traffic] / mainwidget.cpp
1 #include <QtGui>
2
3 #include "mainwidget.hpp"
4 #include "settingsDialog.hpp"
5
6
7 // --------------------------------------------------
8 // MainWidget
9 // --------------------------------------------------
10 MainWidget::MainWidget ()
11     : QWidget ()
12 {
13     setMinimumSize (300, 80);
14 #ifdef Q_WS_MAEMO_5
15     setAttribute(Qt::WA_TranslucentBackground);
16 #endif
17     _light = new TrafficLight (this);
18     _label = new QLabel (this);
19
20     _traffic = new Traffic;
21     _regions = new RegionsTable;
22     _settings = new Settings;
23
24     QHBoxLayout *layout = new QHBoxLayout;
25     layout->addWidget (_light);
26     layout->addWidget (_label);
27     setLayout (layout);
28
29     _light->setVisible (_settings->check (Settings::C_Light));
30
31     connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
32
33     // every 5 minutes (TODO, make option)
34     startTimer (5*60*1000);
35
36     updateData ();
37 }
38
39
40 MainWidget::~MainWidget ()
41 {
42     delete _traffic;
43     delete _regions;
44     delete _settings;
45
46     delete _light;
47     delete _label;
48 }
49
50
51 void MainWidget::paintEvent(QPaintEvent *event)
52 {
53     QPainter p(this);
54     p.setBrush (QColor(0, 0, 0, 128));
55     p.setPen (Qt::NoPen);
56     p.drawRoundedRect (rect(), 10, 10);
57     p.end ();
58
59     QWidget::paintEvent (event);
60 }
61
62
63 void MainWidget::timerEvent (QTimerEvent *)
64 {
65     updateData ();
66 }
67
68
69
70 void MainWidget::trafficUpdated ()
71 {
72     ExtendedTrafficInfo info = _traffic->lookup_ext ("1");
73
74     if (info.valid ()) {
75         QString data;
76         bool first = true;
77         _light->setColor (info.color ());
78
79         if (_settings->check (Settings::C_Rank)) {
80             data.append (QString::number (info.level ()));
81             data.append (info.level () > 1 ? tr (" points") : tr (" point"));
82             first = false;
83         }
84
85         if (_settings->check (Settings::C_Time)) {
86             if (!first)
87                 data.append (", ");
88             data.append (info.localtime ());
89             first = false;
90         }
91
92         if (_settings->check (Settings::C_Hint)) {
93             data.append ("\n");
94             data.append (info.hint ());
95         }
96
97         _label->setText (data);
98     }
99     else
100         _light->setColor (ExtendedTrafficInfo::Unknown);
101 }
102
103
104 void MainWidget::updateData ()
105 {
106     // Here we need to check for internet connection
107     _traffic->update ();
108 }
109
110
111 void MainWidget::settingsDialog ()
112 {
113     SettingsDialog dlg (_settings);
114
115     dlg.exec ();
116
117     // Handle settings
118     _light->setVisible (_settings->check (Settings::C_Light));
119
120     trafficUpdated ();
121 }