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