Removed qWarning from Engine.
[situare] / src / ui / settingsdialog.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Katri Kaikkonen - katri.kaikkonen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #ifdef Q_WS_MAEMO_5
23 #include <QMaemo5TimePickSelector>
24 #include <QMaemo5ValueButton>
25 #endif
26
27 #include <QtGui>
28 #include <QDebug>
29 #include <QTime>
30 #include "common.h"
31 #include "settingsdialog.h"
32
33 const QString SETTINGS_AUTOMATIC_UPDATE_ENABLED = "SETTINGS_AUTOMATIC_UPDATE_ENABLED";
34 const QString SETTINGS_AUTOMATIC_UPDATE_INTERVAL = "SETTINGS_AUTOMATIC_UPDATE_INTERVAL";
35 const int LIST_MINUTES_STEP = 5;
36 const int LIST_MINUTES_MAX = 60;
37 const int LIST_HOURS_MAX = 1;
38
39 SettingsDialog::SettingsDialog(QWidget *parent)
40     : QDialog(parent),
41       m_automaticLocationUpdateOldValue(false),
42       m_automaticLocationUpdateIntervalOldValue(QTime(0, LIST_MINUTES_STEP))
43 {
44     qDebug() << __PRETTY_FUNCTION__;
45     setWindowTitle(tr("Settings"));
46
47     QScrollArea *scrollArea = new QScrollArea(this);
48     QGridLayout *gridLayout = new QGridLayout(this);
49     QGroupBox *groupBox = new QGroupBox(scrollArea);
50
51     m_automaticLocationUpdate = new QCheckBox(tr("Use automatic location update"));
52
53     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
54     QPushButton *saveButton = buttonBox->addButton(QDialogButtonBox::Save);
55     QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
56
57 #ifdef Q_WS_MAEMO_5
58     m_automaticLocationUpdateIntervalButton = new QMaemo5ValueButton(tr("Update interval"), this);
59     m_automaticLocationUpdateIntervalButton->setDisabled(true);
60     m_timePick = new QMaemo5ListPickSelector;
61     m_automaticLocationUpdateIntervalButton->setPickSelector(m_timePick);
62     m_automaticLocationUpdateIntervalButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
63     QStandardItemModel *updateIntervalListModel = new QStandardItemModel(0, 1, this);
64     populateUpdateIntervalList(updateIntervalListModel);
65     m_timePick->setModel(updateIntervalListModel);
66     m_automaticLocationUpdateIntervalButton->setValueText(
67             updateIntervalListModel->item(0, 0)->text());
68     Q_UNUSED(cancelButton);
69 #else
70     m_automaticLocationUpdateInterval = new QTimeEdit();
71     m_automaticLocationUpdateInterval->setTimeRange(QTime(0, LIST_MINUTES_STEP),
72                                                     QTime(LIST_HOURS_MAX, 0));
73     m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm");
74     m_automaticLocationUpdateInterval->setDisabled(true);
75
76     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
77 #endif
78
79     connect(m_automaticLocationUpdate, SIGNAL(toggled(bool)),
80             this, SLOT(toggleAutomaticLocationUpdate(bool)));
81     connect(saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
82     connect(this, SIGNAL(rejected()), this, SLOT(rejectValues()));
83
84     QFormLayout *form = new QFormLayout();
85     form->setRowWrapPolicy(QFormLayout::WrapAllRows);
86     form->addWidget(m_automaticLocationUpdate);
87
88 #ifdef Q_WS_MAEMO_5
89     form->addWidget(m_automaticLocationUpdateIntervalButton);
90 #else
91     form->addRow(tr("Update interval"), m_automaticLocationUpdateInterval);
92 #endif
93
94     groupBox->setLayout(form);
95     scrollArea->setWidget(groupBox);
96     scrollArea->setWidgetResizable(true);
97     gridLayout->addWidget(scrollArea, 0, 0, 2, 1);
98     gridLayout->addWidget(buttonBox, 0, 1, 1, 1);
99     setLayout(gridLayout);
100
101     scrollArea->show();
102
103     readSettings();
104 }
105
106 SettingsDialog::~SettingsDialog()
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109
110     QSettings settings(DIRECTORY_NAME, FILE_NAME);
111     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdateOldValue);
112     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL,
113                       m_automaticLocationUpdateIntervalOldValue);
114 }
115
116 void SettingsDialog::setAutomaticLocationUpdateSettings(bool checked)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_automaticLocationUpdate->setChecked(checked);
121     m_automaticLocationUpdateOldValue = checked;
122 }
123
124 void SettingsDialog::enableSituareSettings(bool enabled)
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     m_automaticLocationUpdate->setEnabled(enabled);
129
130     if (enabled)
131         toggleAutomaticLocationUpdate(m_automaticLocationUpdate->isChecked());
132     else
133         toggleAutomaticLocationUpdate(false);
134 }
135
136 void SettingsDialog::readSettings()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     QSettings settings(DIRECTORY_NAME, FILE_NAME);
141     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
142     QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
143                                       .toString();
144
145     m_automaticLocationUpdate->setChecked(automaticUpdateEnabled);
146
147     if (!automaticUpdateInterval.isEmpty())
148         setTime(QTime::fromString(automaticUpdateInterval, "hh:mm:ss"));
149     else
150         setTime(QTime(0, LIST_MINUTES_STEP));
151
152     m_automaticLocationUpdateOldValue = automaticUpdateEnabled;
153     m_automaticLocationUpdateIntervalOldValue = time();
154 }
155
156 void SettingsDialog::emitAutomaticLocationUpdateSettings()
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     if (m_automaticLocationUpdate->isChecked()) {
161         QTime emptyTime = QTime();
162         emit enableAutomaticLocationUpdate(true, emptyTime.msecsTo(time()));
163     }
164     else {
165         emit enableAutomaticLocationUpdate(false);
166     }
167 }
168
169 void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
174         QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
175         item->setTextAlignment(Qt::AlignCenter);
176         item->setEditable(false);
177         model->appendRow(item);
178     }
179 }
180
181 void SettingsDialog::rejectValues()
182 {
183     qDebug() << __PRETTY_FUNCTION__;
184
185     m_automaticLocationUpdate->setChecked(m_automaticLocationUpdateOldValue);
186     setTime(m_automaticLocationUpdateIntervalOldValue);
187 }
188
189 void SettingsDialog::saveValues()
190 {
191     qDebug() << __PRETTY_FUNCTION__;
192
193     m_automaticLocationUpdateOldValue = m_automaticLocationUpdate->isChecked();
194     m_automaticLocationUpdateIntervalOldValue = time();
195
196     emitAutomaticLocationUpdateSettings();
197
198     accept();
199 }
200
201 void SettingsDialog::setTime(const QTime &time)
202 {
203     qDebug() << __PRETTY_FUNCTION__;
204
205 #ifdef Q_WS_MAEMO_5
206         // Convert time to index in list
207         int index = time.minute()/LIST_MINUTES_STEP - 1;
208
209         if (index < 0)
210             index = 0;
211         if (index >= m_timePick->model()->rowCount())
212             index = m_timePick->model()->rowCount() - 1;
213
214         m_timePick->setCurrentIndex(index);
215 #else
216         m_automaticLocationUpdateInterval->setTime(time);
217 #endif
218 }
219
220 QTime SettingsDialog::time()
221 {
222     qDebug() << __PRETTY_FUNCTION__;
223
224     QTime time = QTime();
225
226 #ifdef Q_WS_MAEMO_5
227     time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
228 #else
229     time = m_automaticLocationUpdateInterval->time();
230 #endif
231
232     if (time < QTime(0, LIST_MINUTES_STEP))
233         time = QTime(0, LIST_MINUTES_STEP);
234     if (time > QTime(LIST_HOURS_MAX, 0))
235         time = QTime(LIST_HOURS_MAX, 0);
236
237     return time;
238 }
239
240 void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
241 {
242     qDebug() << __PRETTY_FUNCTION__;
243
244 #ifdef Q_WS_MAEMO_5
245     m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
246 #else
247     m_automaticLocationUpdateInterval->setEnabled(enabled);
248 #endif
249 }