5601c230d34f6c2768578cc07edb532f7e4e859c
[jspeed] / src / poisettings.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDir>
20 #include <QtCore/QDebug>
21 #include <QtCore/QString>
22 #include <QtCore/QStringList>
23 #include <QtGui/QFileDialog>
24 #include <QtGui/QDialogButtonBox>
25 #include <QtGui/QPushButton>
26 #include <QtGui/QHBoxLayout>
27 #include <QtGui/QVBoxLayout>
28 #include <QtGui/QLabel>
29 #include <QtGui/QCheckBox>
30 #include <QtGui/QLineEdit>
31 #include <QtGui/QIntValidator>
32 #include <QMaemo5InformationBox>
33 #include <math.h>
34 #include "poisettings.h"
35 #include "fileselector.h"
36 #include "poialerts.h"
37 #include "settings.h"
38 #include "mediaplayer.h"
39 #include "poireader.h"
40 #include "buttonbox.h"
41 #include "soundselector.h"
42 #include "odometer.h"
43
44
45 PoiSettings::PoiSettings(QWidget* parent): QDialog(parent)
46 {
47     setWindowTitle(tr("Speed camera alerts"));
48     enabled_ = new QCheckBox(tr("Enabled"));
49     enabled_->setChecked(Settings::instance().value("alert_enabled", false).toBool());
50
51     QHBoxLayout* poiLayout = new QHBoxLayout;
52     poiFileSelector_ = new FileSelector(tr("Poi file (.asc)"));
53     QPushButton* importButton = new QPushButton(tr("Import"));
54     connect(importButton, SIGNAL(clicked(bool)), this, SLOT(importFile()));
55     poiLayout->addWidget(poiFileSelector_, Qt::AlignLeft);
56     poiLayout->addWidget(importButton);
57
58     soundSelector_ = new SoundSelector;
59
60     distanceLabel_ = new QLabel;
61     distance_ = new QLineEdit;
62     distance_->setValidator(new QIntValidator(0, 5000, this));
63     QHBoxLayout* distance = new QHBoxLayout;
64     distance->addWidget(distanceLabel_);
65     distance->addWidget(distance_);
66     onlyOnRoute_ = new QCheckBox(tr("Alert only if poi is in route"));
67     onlyOnRoute_->setChecked(Settings::instance().value("alert_only_on_route", true).toBool());
68
69     ButtonBox* buttons = new ButtonBox;
70     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveSettings()));
71
72     QHBoxLayout* layout = new QHBoxLayout;
73     QVBoxLayout* left = new QVBoxLayout;
74
75     left->addWidget(enabled_);
76     left->addLayout(poiLayout);
77     left->addLayout(soundSelector_);
78     left->addLayout(distance);
79     left->addWidget(onlyOnRoute_);
80
81     layout->addLayout(left, Qt::AlignLeft);
82     layout->addWidget(buttons);
83
84     setLayout(layout);
85 }
86
87
88 void PoiSettings::importFile()
89 {
90     if(!poiFileSelector_->importFile(PoiAlerts::getPoiDir(),
91                                      "Poi files",
92                                      PoiReader::getFormatPattern(),
93                                      true))
94     {
95         qDebug() << "Unable to import";
96     }
97 }
98
99 void PoiSettings::loadFiles()
100 {
101     distanceLabel_->setText((tr("Alert distance (%1)").arg(Odometer::getMeterUnit())));
102     int speedValue = round(Settings::instance().value("alert_distance", 300).toDouble() * Odometer::getMeterMultiplier());
103     distance_->setText(QString::number(speedValue));
104
105     QDir poiDir(PoiAlerts::getPoiDir());
106
107     QString selectedSound = Settings::instance().value("alert_sound", "").toString();
108     soundSelector_->load();
109     soundSelector_->setValue(selectedSound);
110
111     poiFileSelector_->clear();
112     QString selectedPoi = Settings::instance().value("alert_poi_file", "").toString();
113     poiFileSelector_->loadFiles(PoiAlerts::getPoiDir(), PoiReader::getFormatPattern());
114     poiFileSelector_->selectByValue(selectedPoi);
115
116 }
117
118 void PoiSettings::setVisible(bool visible)
119 {
120     if(visible)
121     {
122         loadFiles();
123     }
124
125     QDialog::setVisible(visible);
126 }
127
128 void PoiSettings::saveSettings()
129 {
130     if(poiFileSelector_->currentIndex() < 0 && enabled_->isChecked())
131     {
132         QMaemo5InformationBox::information(this, tr("No poi file selected"));
133         return;
134     }
135
136     double distance = distance_->text().toInt() / Odometer::getMeterMultiplier();
137
138     Settings::instance().setValue("alert_enabled", enabled_->isChecked());
139     Settings::instance().setValue("alert_only_on_route", onlyOnRoute_->isChecked());
140     Settings::instance().setValue("alert_distance", distance);
141     Settings::instance().setValue("alert_sound", soundSelector_->value());
142     Settings::instance().setValue("alert_poi_file", poiFileSelector_->value());
143
144     if(!PoiAlerts::instance().loadConfig())
145     {
146         QMaemo5InformationBox::information(this, tr("Unable to load poi file: %1.").arg(PoiAlerts::instance().error()),
147                                            QMaemo5InformationBox::NoTimeout);
148     }
149     else
150     {
151         hide();
152     }
153 }