Removed obsolete signal from mapview and changed rest of the slots to
[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_INTERVAL = "SETTINGS_AUTOMATIC_UPDATE_INTERVAL";
34 const int LIST_MINUTES_STEP = 5;
35 const int LIST_MINUTES_MAX = 60;
36 const int LIST_HOURS_MAX = 1;
37
38 SettingsDialog::SettingsDialog(QWidget *parent)
39     : QDialog(parent),
40       m_automaticLocationUpdateOldValue(false),
41       m_automaticLocationUpdateIntervalOldValue(QTime(0, LIST_MINUTES_STEP))
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44     setWindowTitle(tr("Settings"));
45
46     QScrollArea *scrollArea = new QScrollArea(this);
47     QGridLayout *gridLayout = new QGridLayout(this);
48     QGroupBox *groupBox = new QGroupBox(scrollArea);
49
50     m_automaticLocationUpdate = new QCheckBox(tr("Use automatic location update"));
51
52     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
53     QPushButton *saveButton = buttonBox->addButton(QDialogButtonBox::Save);
54     QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
55
56 #ifdef Q_WS_MAEMO_5
57     m_automaticLocationUpdateIntervalButton = new QMaemo5ValueButton(tr("Update interval"), this);
58     m_automaticLocationUpdateIntervalButton->setDisabled(true);
59     m_timePick = new QMaemo5ListPickSelector;
60     m_automaticLocationUpdateIntervalButton->setPickSelector(m_timePick);
61     m_automaticLocationUpdateIntervalButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
62     QStandardItemModel *updateIntervalListModel = new QStandardItemModel(0, 1, this);
63     populateUpdateIntervalList(updateIntervalListModel);
64     m_timePick->setModel(updateIntervalListModel);
65
66     Q_UNUSED(cancelButton);
67 #else
68     m_automaticLocationUpdateInterval = new QTimeEdit();
69     m_automaticLocationUpdateInterval->setTimeRange(QTime(0, LIST_MINUTES_STEP),
70                                                     QTime(LIST_HOURS_MAX, 0));
71     m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm");
72     m_automaticLocationUpdateInterval->setDisabled(true);
73
74     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
75 #endif
76
77     connect(m_automaticLocationUpdate, SIGNAL(toggled(bool)),
78             this, SLOT(toggleAutomaticLocationUpdate(bool)));
79     connect(saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
80     connect(this, SIGNAL(rejected()), this, SLOT(rejectValues()));
81
82     QFormLayout *form = new QFormLayout();
83     form->setRowWrapPolicy(QFormLayout::WrapAllRows);
84     form->addWidget(m_automaticLocationUpdate);
85
86 #ifdef Q_WS_MAEMO_5
87     form->addWidget(m_automaticLocationUpdateIntervalButton);
88 #else
89     form->addRow(tr("Update interval"), m_automaticLocationUpdateInterval);
90 #endif
91
92     groupBox->setLayout(form);
93     scrollArea->setWidget(groupBox);
94     scrollArea->setWidgetResizable(true);
95     gridLayout->addWidget(scrollArea, 0, 0, 2, 1);
96     gridLayout->addWidget(buttonBox, 0, 1, 1, 1);
97     setLayout(gridLayout);
98
99     scrollArea->show();
100
101     readSettings();
102 }
103
104 SettingsDialog::~SettingsDialog()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     QSettings settings(DIRECTORY_NAME, FILE_NAME);
109     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, time());
110 }
111
112 void SettingsDialog::enableSituareSettings(bool enabled)
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     m_automaticLocationUpdate->setEnabled(enabled);
117     toggleAutomaticLocationUpdate(enabled);
118 }
119
120 void SettingsDialog::readSettings()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     QSettings settings(DIRECTORY_NAME, FILE_NAME);
125     QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
126                                       .toString();
127     if (!automaticUpdateInterval.isEmpty()) {
128         setTime(QTime::fromString(automaticUpdateInterval));
129         m_automaticLocationUpdateIntervalOldValue = time();
130     }
131 }
132
133 void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
134 {
135     qDebug() << __PRETTY_FUNCTION__;
136
137     for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
138         QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
139         item->setTextAlignment(Qt::AlignCenter);
140         item->setEditable(false);
141         model->appendRow(item);
142     }
143 }
144
145 void SettingsDialog::rejectValues()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     m_automaticLocationUpdate->setChecked(m_automaticLocationUpdateOldValue);
150     setTime(m_automaticLocationUpdateIntervalOldValue);
151 }
152
153 void SettingsDialog::saveValues()
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_automaticLocationUpdateOldValue = m_automaticLocationUpdate->isChecked();
158     m_automaticLocationUpdateIntervalOldValue = time();
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     accept();
168 }
169
170 void SettingsDialog::setTime(const QTime &time)
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174 #ifdef Q_WS_MAEMO_5
175         int index = time.minute()/LIST_MINUTES_STEP - 1;
176         m_timePick->setCurrentIndex(index);
177 #else
178         m_automaticLocationUpdateInterval->setTime(time);
179 #endif
180 }
181
182 QTime SettingsDialog::time()
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     QTime time = QTime();
187
188 #ifdef Q_WS_MAEMO_5
189     time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
190 #else
191     time = m_automaticLocationUpdateInterval->time();
192 #endif
193
194     return time;
195 }
196
197 void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
198 {
199     qDebug() << __PRETTY_FUNCTION__;
200
201 #ifdef Q_WS_MAEMO_5
202     m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
203 #else
204     m_automaticLocationUpdateInterval->setEnabled(enabled);
205 #endif
206 }