2a97dbccb5c36fb78d5a744305f8d9eed136594a
[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
9
10 // --------------------------------------------------
11 // MainWidget
12 // --------------------------------------------------
13 MainWidget::MainWidget ()
14     : QWidget ()
15 {
16 #ifdef Q_WS_MAEMO_5
17     setAttribute(Qt::WA_TranslucentBackground);
18 #endif
19     _light = new TrafficLight (this);
20     _label = new QLabel (this);
21     _timer = new QTimer (this);
22
23     _label->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter);
24
25     _traffic = new Traffic;
26     _regions = new RegionsTable;
27     _settings = new Settings;
28
29     QHBoxLayout *layout = new QHBoxLayout;
30     layout->addWidget (_light);
31     layout->addWidget (_label);
32     setLayout (layout);
33
34     applySettings ();
35
36     connect (_traffic, SIGNAL (updated ()), SLOT (trafficUpdated ()));
37     connect (_timer, SIGNAL (timeout ()), SLOT (updateData ()));
38     connect (DeviceState::instance (), SIGNAL (lockChanged (bool)), SLOT (deviceLockChanged (bool)));
39
40     updateData ();
41 }
42
43
44 MainWidget::~MainWidget ()
45 {
46     delete _traffic;
47     delete _regions;
48     delete _settings;
49
50     delete _light;
51     delete _label;
52 }
53
54
55 void MainWidget::paintEvent(QPaintEvent *event)
56 {
57     QPainter p(this);
58     p.setBrush (QColor(0, 0, 0, 128));
59     p.setPen (Qt::NoPen);
60     p.drawRoundedRect (rect(), 10, 10);
61     p.end ();
62
63     QWidget::paintEvent (event);
64 }
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 #if CHECK_FOR_CONNECTION
109     update = ConnectionChecker::instance ()->checkConnection (_settings->check (Settings::C_UpdateOnGSM),
110                                                               _settings->check (Settings::C_UpdateOnWiFi));
111     if (!_settings->check (Settings::C_UpdateWhenLocked))
112         update &= !DeviceState::instance ()->locked ();
113 #endif
114
115     if (update)
116         _traffic->update ();
117 }
118
119
120 void MainWidget::settingsDialog ()
121 {
122     SettingsDialog dlg (_settings);
123
124     dlg.exec ();
125
126     applySettings ();
127     trafficUpdated ();
128 }
129
130
131 void MainWidget::updateSize ()
132 {
133     QSize minSize (0, 80);
134
135     if (_settings->check (Settings::C_ShowLight))
136         minSize += QSize (80, 0);
137     if (_settings->check (Settings::C_ShowHint))
138         minSize += QSize (270, 0);
139     else {
140         if (_settings->check (Settings::C_ShowTime))
141             minSize += QSize (75, 0);
142         if (_settings->check (Settings::C_ShowRank))
143             minSize += QSize (75, 0);
144     }
145
146     setFixedSize (minSize);
147 }
148
149
150
151 void MainWidget::applySettings ()
152 {
153     _light->setVisible (_settings->check (Settings::C_ShowLight));
154
155     updateSize ();
156
157     if (_settings->updateInterval () < 0)
158         _timer->stop ();
159     else
160         _timer->setInterval (1000 * 60 * _settings->updateInterval ());
161 }
162
163
164 void MainWidget::mousePressEvent (QMouseEvent *event)
165 {
166     QMenu menu;
167     QAction *settingsAction, *updateAction, *todo;
168
169     settingsAction = menu.addAction (tr ("Settings"));
170     updateAction = menu.addAction (tr ("Update"));
171
172     todo = menu.exec (event->pos ());
173     if (!todo)
174         return;
175
176     if (todo == settingsAction)
177         settingsDialog ();
178     if (todo == updateAction)
179         _traffic->update ();
180 }
181
182
183 void MainWidget::deviceLockChanged (bool locked)
184 {
185     if (!_settings->check (Settings::C_UpdateWhenLocked))
186         if (!locked)
187             updateData ();
188 }