...
[jspeed] / src / unitselector.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/QString>
20 #include <QtGui/QHBoxLayout>
21 #include <QtGui/QPushButton>
22 #include <QtGui/QDialogButtonBox>
23 #include "unitselector.h"
24 #include "buttonselector.h"
25 #include "settings.h"
26 #include "buttonbox.h"
27
28 UnitSelector::UnitSelector(QWidget* parent): QDialog(parent)
29 {
30     setWindowTitle(tr("Options"));
31
32     unit_ = Settings::instance().value("unit", "km").toString();
33
34     QHBoxLayout* mainLayout = new QHBoxLayout;
35
36     QVBoxLayout* layout = new QVBoxLayout;
37
38     selector_ = new ButtonSelector(tr("Unit"));
39
40     selector_->addItem(tr("Kilometers"), "km");
41     selector_->addItem(tr("Miles"), "mile");
42
43     if(unit_ == "mile")
44     {
45         selector_->setCurrentIndex(1);
46     }
47
48     layout->addWidget(selector_);
49
50     orientation_ = Settings::instance().value("orientation", "auto").toString();
51
52     orientationSelector_ = new ButtonSelector(tr("Screen orientation"));
53
54     orientationSelector_->addItem(tr("Automatic"), "auto");
55     orientationSelector_->addItem(tr("Landscape"), "landscape");
56     orientationSelector_->addItem(tr("Portrait"), "portrait");
57
58     if(orientation_ == "landscape")
59     {
60         orientationSelector_->setCurrentIndex(1);
61     }
62     else if(orientation_ == "portrait")
63     {
64         orientationSelector_->setCurrentIndex(2);
65     }
66
67     layout->addWidget(orientationSelector_);
68
69     ButtonBox* buttons = new ButtonBox;
70     buttons->setOrientation(Qt::Horizontal);
71     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveUnit()));
72
73     mainLayout->addLayout(layout, Qt::AlignLeft);
74     mainLayout->addWidget(buttons);
75
76     setLayout(mainLayout);
77 }
78
79 void UnitSelector::saveUnit()
80 {
81     QString unitValue = selector_->value().toString();
82     QString orientationValue = orientationSelector_->value().toString();
83
84     if(unitValue != unit_)
85     {
86         Settings::instance().setValue("unit", unitValue);
87         unit_ = unitValue;
88         emit unitChanged();
89     }
90
91     if(orientationValue != orientation_)
92     {
93         Settings::instance().setValue("orientation", orientationValue);
94         orientation_ = orientationValue;
95         emit orientationChanged();
96     }
97
98     hide();
99 }