Fix typo
[yandex-traffic] / settingsDialog.cpp
1 #include <QtGui>
2
3 #ifdef Q_WS_MAEMO_5
4 #include <QtMaemo5>
5 #else
6 #include "QtMaemo5Adapters.hpp"
7 #endif
8
9 #include "settingsDialog.hpp"
10
11
12 // --------------------------------------------------
13 // SettingsDialog
14 // --------------------------------------------------
15 SettingsDialog::SettingsDialog (Settings *settings)
16     : QDialog (),
17       _settings (settings)
18 {
19     setWindowTitle (tr ("Settings"));
20
21     QVBoxLayout *layout = new QVBoxLayout (this);
22
23     _displayButton = new QMaemo5ValueButton (tr ("Display"), this);
24     _displayButton->setValueLayout (QMaemo5ValueButton::ValueUnderText);
25     updateDisplayButtonValue ();
26     layout->addWidget (_displayButton);
27     _updateButton = new QMaemo5ValueButton (tr ("Update"), this);
28     _updateButton->setValueLayout (QMaemo5ValueButton::ValueUnderText);
29     updateUpdateButtonValue ();
30     layout->addWidget (_updateButton);
31
32     createLanguageButton (layout);
33
34     connect (_displayButton, SIGNAL (clicked ()), SLOT (displayClicked ()));
35     connect (_updateButton, SIGNAL (clicked ()), SLOT (updateClicked ()));
36 }
37
38
39 void SettingsDialog::createLanguageButton (QBoxLayout *layout)
40 {
41     _languageButton = new QMaemo5ValueButton (tr ("Interface language"), this);
42     layout->addWidget (_languageButton);
43
44 #ifdef Q_WS_MAEMO_5
45     QMaemo5ListPickSelector *selector = new QMaemo5ListPickSelector;
46     QStandardItemModel *model = new QStandardItemModel (0, 1);
47     QList<Language>::const_iterator it = _settings->languages ().begin ();
48
49     while (it != _settings->languages ().end ()) {
50         QStandardItem *item = new QStandardItem (it->title ());
51         item->setData (it->alias ());
52         model->appendRow (item);
53         it++;
54     }
55
56     selector->setModel (model);
57     selector->setCurrentIndex (_settings->languages ().indexOf (_settings->language ()));
58
59     _languageButton->setPickSelector (selector);
60
61     connect (selector, SIGNAL (selected (const QString&)), SLOT (languageChanged (const QString&)));
62 #endif
63 }
64
65
66 void SettingsDialog::displayClicked ()
67 {
68     DisplaySettingsDialog dlg (_settings);
69     dlg.exec ();
70     updateDisplayButtonValue ();
71 }
72
73
74 void SettingsDialog::updateClicked ()
75 {
76     UpdateSettingsDialog dlg (_settings);
77     dlg.exec ();
78     updateUpdateButtonValue ();
79 }
80
81
82 void SettingsDialog::languageChanged (const QString&)
83 {
84 #ifdef Q_WS_MAEMO_5
85     QMaemo5ListPickSelector *model = static_cast<QMaemo5ListPickSelector*> (_languageButton->pickSelector ());
86
87     if (!model)
88         return;
89
90     _settings->setLanguageIndex (model->currentIndex ());
91     _settings->save ();
92 #endif
93 }
94
95
96 void SettingsDialog::updateDisplayButtonValue ()
97 {
98     QString val;
99     QStringList list;
100
101     val = tr ("City:") + " " + _settings->regionName (_settings->regionID ()) + ", " + tr ("Display:") + " ";
102
103     if (_settings->check (Settings::C_ShowLight))
104         list.append (tr ("lights"));
105     if (_settings->check (Settings::C_ShowRank))
106         list.append (tr ("rank"));
107     if (_settings->check (Settings::C_ShowTime))
108         list.append (tr ("time"));
109     if (_settings->check (Settings::C_ShowHint))
110         list.append (tr ("hint"));
111
112     _displayButton->setValueText (val + list.join (", "));
113 }
114
115
116 void SettingsDialog::updateUpdateButtonValue ()
117 {
118     QStringList list, intervals = _settings->updateIntervals ();
119     QString val;
120
121     val = tr ("Interval:") + " " + intervals[_settings->getUpdateIntervalIndex ()] + ", " + tr ("Update via:") + " ";
122
123     if (_settings->check (Settings::C_UpdateOnWiFi))
124         list.append (tr ("WiFi"));
125     if (_settings->check (Settings::C_UpdateOnGSM))
126         list.append (tr ("GSM"));
127
128     val += list.join (", ");
129
130     if (_settings->check (Settings::C_UpdateWhenLocked))
131         val += ", " + tr ("Update when locked");
132     else
133         val += ", " + tr ("Not update when locked");
134
135     _updateButton->setValueText (val);
136 }
137
138
139 // --------------------------------------------------
140 // BaseSettingsDialog
141 // --------------------------------------------------
142 BaseSettingsDialog::BaseSettingsDialog (Settings *settings)
143     : QDialog (),
144       _settings (settings),
145       _layout (new QVBoxLayout)
146 {
147     QHBoxLayout *layout = new QHBoxLayout (this);
148     QVBoxLayout *right_layout = new QVBoxLayout ();
149     QSizePolicy policy;
150
151     // Right side
152     _saveButton = new QPushButton (tr ("Save"), this);
153     policy = _saveButton->sizePolicy ();
154     policy.setHorizontalPolicy (QSizePolicy::Maximum);
155     _saveButton->setSizePolicy (policy);
156     connect (_saveButton, SIGNAL (clicked ()), SLOT (saveClicked ()));
157
158     right_layout->addStretch ();
159     right_layout->addWidget (_saveButton);
160
161     // Path them together
162     layout->addLayout (_layout);
163     layout->addLayout (right_layout);
164
165     // Left side would be initialized later
166 }
167
168
169 void BaseSettingsDialog::saveClicked ()
170 {
171     saveSettings ();
172     _settings->save ();
173     accept ();
174 }
175
176
177 // --------------------------------------------------
178 // DisplaySettingsDialog
179 // --------------------------------------------------
180 DisplaySettingsDialog::DisplaySettingsDialog (Settings *_settings)
181     : BaseSettingsDialog (_settings)
182 {
183     setWindowTitle (tr ("Display settings"));
184     setMinimumSize (300, 400);
185
186     initCities (layout ());
187     initChecks (layout ());
188 }
189
190
191 void DisplaySettingsDialog::initCities (QBoxLayout *layout)
192 {
193     _cities = new QListWidget (this);
194     QStringList regions = settings ()->regionIDs ();
195     QStringList::const_iterator it = regions.begin ();
196
197     // Populate list with cities
198     while (it != regions.end ()) {
199         QListWidgetItem *item = new QListWidgetItem (settings()->regionName (*it), _cities);
200
201         item->setData (Qt::UserRole, QVariant (*it));
202         if (*it == settings ()->regionID ())
203             _cities->setCurrentItem (item);
204         it++;
205     }
206
207     layout->addWidget (_cities);
208 }
209
210
211 void DisplaySettingsDialog::initChecks (QBoxLayout *layout)
212 {
213     QGridLayout *grid = new QGridLayout;
214
215     _showLight = new QCheckBox (tr ("Light"), this);
216     _showLight->setChecked (settings ()->check (Settings::C_ShowLight));
217     _showRank = new QCheckBox (tr ("Rank"), this);
218     _showRank->setChecked (settings ()->check (Settings::C_ShowRank));
219     _showTime = new QCheckBox (tr ("Time"), this);
220     _showTime->setChecked (settings ()->check (Settings::C_ShowTime));
221     _showHint = new QCheckBox (tr ("Hint"), this);
222     _showHint->setChecked (settings ()->check (Settings::C_ShowHint));
223
224     grid->addWidget (_showLight, 0, 0);
225     grid->addWidget (_showRank, 0, 1);
226     grid->addWidget (_showTime, 1, 0);
227     grid->addWidget (_showHint, 1, 1);
228
229     layout->addLayout (grid);
230 }
231
232
233 void DisplaySettingsDialog::saveSettings ()
234 {
235     QListWidgetItem *cur = _cities->currentItem ();
236
237     if (cur)
238         settings ()->setRegionID (cur->data (Qt::UserRole).toString ());
239
240     settings ()->setCheck (Settings::C_ShowLight, _showLight->isChecked ());
241     settings ()->setCheck (Settings::C_ShowRank,  _showRank->isChecked ());
242     settings ()->setCheck (Settings::C_ShowTime,  _showTime->isChecked ());
243     settings ()->setCheck (Settings::C_ShowHint,  _showHint->isChecked ());
244 }
245
246
247 // --------------------------------------------------
248 // UpdateSettingsDialog
249 // --------------------------------------------------
250 UpdateSettingsDialog::UpdateSettingsDialog (Settings *_settings)
251     : BaseSettingsDialog (_settings)
252 {
253     setWindowTitle (tr ("Update settings"));
254
255     _wifiUpdate = new QCheckBox (tr ("Update via WiFi"), this);
256     _wifiUpdate->setChecked (settings ()->check (Settings::C_UpdateOnWiFi));
257     _gsmUpdate  = new QCheckBox (tr ("Update via GSM"), this);
258     _gsmUpdate->setChecked (settings ()->check (Settings::C_UpdateOnGSM));
259     _lockedUpdate  = new QCheckBox (tr ("Update when device locked"), this);
260     _lockedUpdate->setChecked (settings ()->check (Settings::C_UpdateWhenLocked));
261
262     initUpdateInterval (layout ());
263
264     layout ()->addWidget (_wifiUpdate);
265     layout ()->addWidget (_gsmUpdate);
266     layout ()->addWidget (_lockedUpdate);
267 }
268
269
270 void UpdateSettingsDialog::saveSettings ()
271 {
272 #ifdef Q_WS_MAEMO_5
273     QMaemo5ListPickSelector *selector = static_cast<QMaemo5ListPickSelector*> (_intervalButton->pickSelector ());
274
275     if (selector)
276         settings ()->setUpdateIntervalIndex (selector->currentIndex ());
277 #endif
278     settings ()->setCheck (Settings::C_UpdateOnWiFi, _wifiUpdate->isChecked ());
279     settings ()->setCheck (Settings::C_UpdateOnGSM,  _gsmUpdate->isChecked ());
280     settings ()->setCheck (Settings::C_UpdateWhenLocked,  _lockedUpdate->isChecked ());
281 }
282
283
284 void UpdateSettingsDialog::initUpdateInterval (QBoxLayout *layout)
285 {
286     _intervalButton = new QMaemo5ValueButton (tr ("Update interval"), this);
287     layout->addWidget (_intervalButton);
288
289 #ifdef Q_WS_MAEMO_5
290     QMaemo5ListPickSelector *selector = new QMaemo5ListPickSelector;
291     QStandardItemModel *model = new QStandardItemModel (0, 1);
292     QStringList updateIntervals = settings ()->updateIntervals ();
293     QStringList::iterator it = updateIntervals.begin ();
294
295     while (it != updateIntervals.end ()) {
296         model->appendRow (new QStandardItem (*it));
297         it++;
298     }
299
300     selector->setModel (model);
301     selector->setCurrentIndex (settings ()->getUpdateIntervalIndex ());
302
303     _intervalButton->setPickSelector (selector);
304 #endif
305 }