Small change to text element.
[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 "poireader.h"
39 #include "buttonbox.h"
40 #include "soundselector.h"
41 #include "odometer.h"
42
43
44 PoiSettings::PoiSettings(QWidget* parent): QDialog(parent)
45 {
46     setWindowTitle(tr("Speed camera alerts"));
47     enabled_ = new QCheckBox(tr("Enabled"));
48     enabled_->setChecked(Settings::instance().value("alert_enabled", false).toBool());
49
50     QHBoxLayout* poiLayout = new QHBoxLayout;
51     poiFileSelector_ = new FileSelector(tr("Poi file (.asc)"));
52     QPushButton* importButton = new QPushButton(tr("Import"));
53     connect(importButton, SIGNAL(clicked(bool)), this, SLOT(importFile()));
54     poiLayout->addWidget(poiFileSelector_, Qt::AlignLeft);
55     poiLayout->addWidget(importButton);
56
57     soundSelector_ = new SoundSelector;
58
59     distanceLabel_ = new QLabel;
60     distance_ = new QLineEdit;
61     distance_->setValidator(new QIntValidator(0, 9999, this));
62     QHBoxLayout* distance = new QHBoxLayout;
63     distance->addWidget(distanceLabel_);
64     distance->addWidget(distance_);
65     onlyOnRoute_ = new QCheckBox(tr("Alert only if poi is on route"));
66     onlyOnRoute_->setChecked(Settings::instance().value("alert_only_on_route", true).toBool());
67
68     ButtonBox* buttons = new ButtonBox;
69     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveSettings()));
70
71     QHBoxLayout* layout = new QHBoxLayout;
72     QVBoxLayout* left = new QVBoxLayout;
73
74     left->addWidget(enabled_);
75     left->addLayout(poiLayout);
76     left->addLayout(soundSelector_);
77     left->addLayout(distance);
78     left->addWidget(onlyOnRoute_);
79
80     layout->addLayout(left, Qt::AlignLeft);
81     layout->addWidget(buttons);
82
83     setLayout(layout);
84 }
85
86
87 void PoiSettings::importFile()
88 {
89     if(!poiFileSelector_->importFile(PoiAlerts::getPoiDir(),
90                                      "Poi files",
91                                      PoiReader::getFormatPattern(),
92                                      true))
93     {
94         qDebug() << "Unable to import";
95     }
96 }
97
98 void PoiSettings::loadFiles()
99 {
100     distanceLabel_->setText((tr("Alert distance (%1)").arg(Odometer::getMeterUnit())));
101     int speedValue = round(Settings::instance().value("alert_distance", 300).toDouble() * Odometer::getMeterMultiplier());
102     distance_->setText(QString::number(speedValue));
103
104     QDir poiDir(PoiAlerts::getPoiDir());
105
106     QString selectedSound = Settings::instance().value("alert_sound", "").toString();
107     soundSelector_->load();
108     soundSelector_->setValue(selectedSound);
109
110     poiFileSelector_->clear();
111     QString selectedPoi = Settings::instance().value("alert_poi_file", "").toString();
112     poiFileSelector_->loadFiles(PoiAlerts::getPoiDir(), PoiReader::getFormatPattern(), true);
113     poiFileSelector_->selectByValue(selectedPoi);
114
115 }
116
117 void PoiSettings::setVisible(bool visible)
118 {
119     if(visible)
120     {
121         loadFiles();
122     }
123
124     QDialog::setVisible(visible);
125 }
126
127 void PoiSettings::saveSettings()
128 {
129     if(poiFileSelector_->currentIndex() < 0 && enabled_->isChecked())
130     {
131         QMaemo5InformationBox::information(this, tr("No poi file selected"));
132         return;
133     }
134
135     double distance = distance_->text().toInt() / Odometer::getMeterMultiplier();
136
137     Settings::instance().setValue("alert_enabled", enabled_->isChecked());
138     Settings::instance().setValue("alert_only_on_route", onlyOnRoute_->isChecked());
139     Settings::instance().setValue("alert_distance", distance);
140     Settings::instance().setValue("alert_sound", soundSelector_->value());
141     Settings::instance().setValue("alert_poi_file", poiFileSelector_->value());
142
143     if(!PoiAlerts::instance().loadConfig())
144     {
145         QMaemo5InformationBox::information(this, tr("Unable to load poi file: %1.").arg(PoiAlerts::instance().error()),
146                                            QMaemo5InformationBox::NoTimeout);
147     }
148     else
149     {
150         hide();
151     }
152 }