Added theme scheduler, poi support and speed alarm features.
[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 "mediaplayer.h"
37 #include "soundselector.h"
38 #include "buttonbox.h"
39
40
41 SpeedAlarmSettings::SpeedAlarmSettings(QWidget* parent): QDialog(parent)
42 {
43     setWindowTitle(tr("Speed alarm"));
44
45     speedLabel_ = new QLabel;
46     speed_ = new QLineEdit;
47     speed_->setValidator(new QIntValidator(0, 999, this));
48
49     QPushButton* current = new QPushButton(tr("Current"));
50     connect(current, SIGNAL(clicked(bool)), this, SLOT(loadCurrentSpeed()));
51
52     QHBoxLayout* speedLayout = new QHBoxLayout;
53     speedLayout->addWidget(speedLabel_);
54     speedLayout->addWidget(speed_);
55     speedLayout->addWidget(current);
56
57     soundSelector_ = new SoundSelector;
58
59     enabled_ = new QCheckBox(tr("Enabled"));
60     enabled_->setChecked(Settings::instance().value("alarm_enabled", false).toBool());
61
62     ButtonBox* buttons = new ButtonBox;
63     buttons->addButton(tr("Save"), this, SLOT(saveSettings()), QDialogButtonBox::AcceptRole);
64
65     QHBoxLayout* layout = new QHBoxLayout;
66     QVBoxLayout* left = new QVBoxLayout;
67
68     left->addLayout(speedLayout);
69     left->addWidget(soundSelector_);
70     left->addWidget(enabled_);
71     layout->addLayout(left, Qt::AlignLeft);
72     layout->addWidget(buttons);
73
74     setLayout(layout);
75 }
76
77
78 void SpeedAlarmSettings::loadData()
79 {
80     speedLabel_->setText(tr("Speed threshold (%1)").arg(Odometer::getSpeedUnit()));
81     int speedValue = round(Settings::instance().value("alarm_threshold", 100).toDouble() * Odometer::getUnitMultiplier());
82     speed_->setText(QString::number(static_cast<int>(speedValue)));
83
84     QString selected = Settings::instance().value("alarm_sound", "").toString();
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 }