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