Couple of unnecessary includes removed.
[jspeed] / src / speedalarmsettings.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 <QtGui/QLineEdit>
22 #include <QtGui/QLabel>
23 #include <QtGui/QHBoxLayout>
24 #include <QtGui/QVBoxLayout>
25 #include <QtGui/QPushButton>
26 #include <QtGui/QDialogButtonBox>
27 #include <QtGui/QCheckBox>
28 #include <QtGui/QIntValidator>
29 #include <math.h>
30 #include "speedalarmsettings.h"
31 #include "speedalarm.h"
32 #include "odometer.h"
33 #include "settings.h"
34 #include "fileselector.h"
35 #include "poialerts.h"
36 #include "soundselector.h"
37 #include "buttonbox.h"
38
39
40 SpeedAlarmSettings::SpeedAlarmSettings(QWidget* parent): QDialog(parent)
41 {
42     setWindowTitle(tr("Speed alarm"));
43
44     speedLabel_ = new QLabel;
45     speed_ = new QLineEdit;
46     speed_->setValidator(new QIntValidator(0, 999, this));
47
48     QPushButton* current = new QPushButton(tr("Current"));
49     connect(current, SIGNAL(clicked(bool)), this, SLOT(loadCurrentSpeed()));
50
51     QHBoxLayout* speedLayout = new QHBoxLayout;
52     speedLayout->addWidget(speedLabel_);
53     speedLayout->addWidget(speed_);
54     speedLayout->addWidget(current);
55
56     soundSelector_ = new SoundSelector;
57
58     enabled_ = new QCheckBox(tr("Enabled"));
59     enabled_->setChecked(Settings::instance().value("alarm_enabled", false).toBool());
60
61     ButtonBox* buttons = new ButtonBox;
62     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveSettings()));
63
64     QHBoxLayout* layout = new QHBoxLayout;
65     QVBoxLayout* left = new QVBoxLayout;
66
67     left->addLayout(speedLayout);
68     left->addLayout(soundSelector_);
69     left->addWidget(enabled_);
70     layout->addLayout(left, Qt::AlignLeft);
71     layout->addWidget(buttons);
72
73     setLayout(layout);
74 }
75
76
77 void SpeedAlarmSettings::loadData()
78 {
79     speedLabel_->setText(tr("Speed threshold (%1)").arg(Odometer::getSpeedUnit()));
80     int speedValue = round(Settings::instance().value("alarm_threshold", 100).toDouble() * Odometer::getUnitMultiplier());
81     speed_->setText(QString::number(static_cast<int>(speedValue)));
82
83     QString selected = Settings::instance().value("alarm_sound", "").toString();
84     soundSelector_->load();
85     soundSelector_->setValue(selected);
86 }
87
88 void SpeedAlarmSettings::loadCurrentSpeed()
89 {
90     double speed = round(Odometer::instance().getLatestFix().speed);
91     speed_->setText(QString::number(static_cast<int>(speed)));
92 }
93
94 void SpeedAlarmSettings::saveSettings()
95 {
96     double kmSpeed = speed_->text().toInt() / Odometer::getUnitMultiplier();
97
98     Settings::instance().setValue("alarm_threshold", kmSpeed);
99     Settings::instance().setValue("alarm_enabled", enabled_->isChecked());
100     Settings::instance().setValue("alarm_sound", soundSelector_->value());
101
102     SpeedAlarm::instance().loadConfig();
103
104     hide();
105 }
106
107 void SpeedAlarmSettings::setVisible(bool visible)
108 {
109     if(visible)
110     {
111         loadData();
112     }
113
114     QDialog::setVisible(visible);
115 }