Check for device state on update.
[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     _updateButton->setValueText (val + list.join (", "));
86 }
87
88
89 // --------------------------------------------------
90 // BaseSettingsDialog
91 // --------------------------------------------------
92 BaseSettingsDialog::BaseSettingsDialog (Settings *settings)
93     : QDialog (),
94       _settings (settings),
95       _layout (new QVBoxLayout)
96 {
97     QHBoxLayout *layout = new QHBoxLayout (this);
98     QVBoxLayout *right_layout = new QVBoxLayout ();
99     QSizePolicy policy;
100
101     // Right side
102     _saveButton = new QPushButton (tr ("Save"), this);
103     policy = _saveButton->sizePolicy ();
104     policy.setHorizontalPolicy (QSizePolicy::Maximum);
105     _saveButton->setSizePolicy (policy);
106     connect (_saveButton, SIGNAL (clicked ()), SLOT (saveClicked ()));
107
108     right_layout->addStretch ();
109     right_layout->addWidget (_saveButton);
110
111     // Path them together
112     layout->addLayout (_layout);
113     layout->addLayout (right_layout);
114
115     // Left side would be initialized later
116 }
117
118
119 void BaseSettingsDialog::saveClicked ()
120 {
121     saveSettings ();
122     _settings->save ();
123     accept ();
124 }
125
126
127 // --------------------------------------------------
128 // DisplaySettingsDialog
129 // --------------------------------------------------
130 DisplaySettingsDialog::DisplaySettingsDialog (Settings *_settings)
131     : BaseSettingsDialog (_settings)
132 {
133     setWindowTitle (tr ("Display settings"));
134     setMinimumSize (300, 400);
135
136     initCities (layout ());
137     initChecks (layout ());
138 }
139
140
141 void DisplaySettingsDialog::initCities (QBoxLayout *layout)
142 {
143     _cities = new QListWidget (this);
144     QMap<QString, QString> cities_map = settings ()->cities ();
145     QMap<QString, QString>::iterator it = cities_map.begin ();
146
147     // Populate list with cities
148     while (it != cities_map.end ()) {
149         QListWidgetItem *item = new QListWidgetItem (it.value (), _cities);
150
151         item->setData (Qt::UserRole, QVariant (it.key ()));
152         if (it.key () == settings ()->regionID ())
153             _cities->setCurrentItem (item);
154         it++;
155     }
156
157     layout->addWidget (_cities);
158 }
159
160
161 void DisplaySettingsDialog::initChecks (QBoxLayout *layout)
162 {
163     QGridLayout *grid = new QGridLayout;
164
165     _showLight = new QCheckBox (tr ("Light"), this);
166     _showLight->setChecked (settings ()->check (Settings::C_ShowLight));
167     _showRank = new QCheckBox (tr ("Rank"), this);
168     _showRank->setChecked (settings ()->check (Settings::C_ShowRank));
169     _showTime = new QCheckBox (tr ("Time"), this);
170     _showTime->setChecked (settings ()->check (Settings::C_ShowTime));
171     _showHint = new QCheckBox (tr ("Hint"), this);
172     _showHint->setChecked (settings ()->check (Settings::C_ShowHint));
173
174     grid->addWidget (_showLight, 0, 0);
175     grid->addWidget (_showRank, 0, 1);
176     grid->addWidget (_showTime, 1, 0);
177     grid->addWidget (_showHint, 1, 1);
178
179     layout->addLayout (grid);
180 }
181
182
183 void DisplaySettingsDialog::saveSettings ()
184 {
185     QListWidgetItem *cur = _cities->currentItem ();
186
187     if (cur)
188         settings ()->setRegionID (cur->data (Qt::UserRole).toString ());
189
190     settings ()->setCheck (Settings::C_ShowLight, _showLight->isChecked ());
191     settings ()->setCheck (Settings::C_ShowRank,  _showRank->isChecked ());
192     settings ()->setCheck (Settings::C_ShowTime,  _showTime->isChecked ());
193     settings ()->setCheck (Settings::C_ShowHint,  _showHint->isChecked ());
194 }
195
196
197 // --------------------------------------------------
198 // UpdateSettingsDialog
199 // --------------------------------------------------
200 UpdateSettingsDialog::UpdateSettingsDialog (Settings *_settings)
201     : BaseSettingsDialog (_settings)
202 {
203     setWindowTitle (tr ("Update settings"));
204
205     _wifiUpdate = new QCheckBox (tr ("Update via WiFi"), this);
206     _wifiUpdate->setChecked (settings ()->check (Settings::C_UpdateOnWiFi));
207     _gsmUpdate  = new QCheckBox (tr ("Update via GSM"), this);
208     _gsmUpdate->setChecked (settings ()->check (Settings::C_UpdateOnGSM));
209     _lockedUpdate  = new QCheckBox (tr ("Update when device locked"), this);
210     _lockedUpdate->setChecked (settings ()->check (Settings::C_UpdateWhenLocked));
211
212     initUpdateInterval (layout ());
213
214     layout ()->addWidget (_wifiUpdate);
215     layout ()->addWidget (_gsmUpdate);
216     layout ()->addWidget (_lockedUpdate);
217 }
218
219
220 void UpdateSettingsDialog::saveSettings ()
221 {
222 #ifdef Q_WS_MAEMO_5
223     QMaemo5ListPickSelector *selector = static_cast<QMaemo5ListPickSelector*> (_intervalButton->pickSelector ());
224
225     if (selector)
226         settings ()->setUpdateIntervalIndex (selector->currentIndex ());
227 #endif
228     settings ()->setCheck (Settings::C_UpdateOnWiFi, _wifiUpdate->isChecked ());
229     settings ()->setCheck (Settings::C_UpdateOnGSM,  _gsmUpdate->isChecked ());
230     settings ()->setCheck (Settings::C_UpdateWhenLocked,  _lockedUpdate->isChecked ());
231 }
232
233
234 void UpdateSettingsDialog::initUpdateInterval (QBoxLayout *layout)
235 {
236     _intervalButton = new QMaemo5ValueButton (tr ("Update interval"), this);
237     layout->addWidget (_intervalButton);
238
239 #ifdef Q_WS_MAEMO_5
240     QMaemo5ListPickSelector *selector = new QMaemo5ListPickSelector;
241     QStandardItemModel *model = new QStandardItemModel (0, 1);
242     QStringList updateIntervals = settings ()->updateIntervals ();
243     QStringList::iterator it = updateIntervals.begin ();
244
245     while (it != updateIntervals.end ()) {
246         model->appendRow (new QStandardItem (*it));
247         it++;
248     }
249
250     selector->setModel (model);
251     selector->setCurrentIndex (settings ()->getUpdateIntervalIndex ());
252
253     _intervalButton->setPickSelector (selector);
254 #endif
255 }