Fixed bug with updates. Finished logging.
[yandex-traffic] / mainwidget.cpp
1 #include <QtGui>
2
3 #include "mainwidget.hpp"
4 #include "settingsDialog.hpp"
5 #include "connection.hpp"
6 #include "devstate.hpp"
7 #include "settings.hpp"
8 #include "log.hpp"
9
10
11 // --------------------------------------------------
12 // MainWidget
13 // --------------------------------------------------
14 MainWidget::MainWidget ()
15     : QWidget ()
16 {
17 #ifdef Q_WS_MAEMO_5
18     setAttribute(Qt::WA_TranslucentBackground);
19 #endif
20     _light = new TrafficLight (this);
21     _label = new QLabel (this);
22     _timer = new QTimer (this);
23
24     _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter);
25
26     _traffic = new Traffic;
27     _regions = new RegionsTable;
28     _settings = Settings::instance ();
29
30     QHBoxLayout *layout = new QHBoxLayout;
31     layout->addWidget (_light);
32     layout->addWidget (_label);
33     setLayout (layout);
34
35     applySettings ();
36
37     connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
38     connect (_timer, SIGNAL (timeout ()), SLOT (updateData ()));
39     connect (DeviceState::instance (), SIGNAL (lockChanged (bool)), SLOT (deviceLockChanged (bool)));
40
41     updateData ();
42 }
43
44
45 MainWidget::~MainWidget ()
46 {
47     delete _traffic;
48     delete _regions;
49     delete _settings;
50
51     delete _light;
52     delete _label;
53 }
54
55
56 void MainWidget::paintEvent(QPaintEvent *event)
57 {
58     QPainter p(this);
59     p.setBrush (QColor(0, 0, 0, 128));
60     p.setPen (Qt::NoPen);
61     p.drawRoundedRect (rect(), 10, 10);
62     p.end ();
63
64     QWidget::paintEvent (event);
65 }
66
67
68 void MainWidget::trafficUpdated ()
69 {
70     ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ());
71
72     if (info.valid ()) {
73         QString data;
74         bool first = true;
75         _light->setColor (info.color ());
76
77         if (_settings->check (Settings::C_ShowRank)) {
78             data.append (tr ("%n point(s)", "", info.level ()));
79             first = false;
80         }
81
82         if (_settings->check (Settings::C_ShowTime)) {
83             if (!first)
84                 data.append (", ");
85             data.append (info.localtime ());
86             first = false;
87         }
88
89         if (_settings->check (Settings::C_ShowHint)) {
90             if (!first)
91                 data.append ("\n");
92             data.append (info.hint ());
93         }
94
95         _label->setText (data);
96     }
97     else {
98         _light->setColor (ExtendedTrafficInfo::Unknown);
99         _label->setText (tr ("No data"));
100     }
101 }
102
103
104 void MainWidget::updateData ()
105 {
106     bool update = true;
107
108     Log::instance ()->add ("updateData called");
109
110 #if CHECK_FOR_CONNECTION
111     update = ConnectionChecker::instance ()->checkConnection (_settings->check (Settings::C_UpdateOnGSM),
112                                                               _settings->check (Settings::C_UpdateOnWiFi));
113     Log::instance ()->add (QString ("checkConnection returned %1").arg (update ? "true" : "false"));
114     if (!_settings->check (Settings::C_UpdateWhenLocked)) {
115         Log::instance ()->add ("Check for device state");
116         update &= !DeviceState::instance ()->locked ();
117     }
118 #endif
119
120     if (update) {
121         Log::instance ()->add ("Perform update");
122         _traffic->update ();
123     }
124     else
125         Log::instance ()->add ("Update not performed");
126 }
127
128
129 void MainWidget::settingsDialog ()
130 {
131     SettingsDialog dlg (_settings);
132
133     dlg.exec ();
134
135     applySettings ();
136     trafficUpdated ();
137 }
138
139
140 void MainWidget::updateSize ()
141 {
142     QSize minSize (0, 80);
143
144     if (_settings->check (Settings::C_ShowLight))
145         minSize += QSize (80, 0);
146     if (_settings->check (Settings::C_ShowHint))
147         minSize += QSize (270, 0);
148     else {
149         if (_settings->check (Settings::C_ShowTime))
150             minSize += QSize (75, 0);
151         if (_settings->check (Settings::C_ShowRank))
152             minSize += QSize (75, 0);
153     }
154
155     setFixedSize (minSize);
156 }
157
158
159
160 void MainWidget::applySettings ()
161 {
162     _light->setVisible (_settings->check (Settings::C_ShowLight));
163
164     updateSize ();
165
166     Log::instance ()->add (QString ("applySettings: updateInterval is %1").arg (_settings->updateInterval ()));
167
168     if (_settings->updateInterval () < 0) {
169         _timer->stop ();
170         Log::instance ()->add ("Timer disabled");
171     }
172     else {
173         _timer->setInterval (1000 * 60 * _settings->updateInterval ());
174         _timer->start ();
175         Log::instance ()->add (QString ("Timer interval set to %1 ms").arg (1000 * 60 * _settings->updateInterval ()));
176     }
177 }
178
179
180 void MainWidget::mousePressEvent (QMouseEvent *event)
181 {
182     QMenu menu;
183     QAction *settingsAction, *updateAction, *todo;
184
185     Log::instance ()->add (QString ("mousePressEvent at %1,%2").arg (event->pos ().x ()).arg (event->pos ().y ()));
186
187     settingsAction = menu.addAction (tr ("Settings"));
188     updateAction = menu.addAction (tr ("Update"));
189
190     todo = menu.exec (event->pos ());
191     if (!todo)
192         return;
193
194     if (todo == settingsAction)
195         settingsDialog ();
196     if (todo == updateAction)
197         _traffic->update ();
198 }
199
200
201 void MainWidget::deviceLockChanged (bool locked)
202 {
203     if (!_settings->check (Settings::C_UpdateWhenLocked))
204         if (!locked)
205             updateData ();
206 }