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