Added altitude display to detail screen. Added option to disable auto rotation.
[jspeed] / src / mainwindow.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 <QtDBus/QDBusConnection>
20 #include <QtDBus/QDBusMessage>
21 #include <QtGui/QApplication>
22 #include <QtGui/QDesktopWidget>
23 #include <QtCore/QTimer>
24 #include <QtCore/QDebug>
25 #include <QMaemo5InformationBox>
26 #include "mainwindow.h"
27 #include "mainwindowstack.h"
28 #include "themeloader.h"
29 #include "mainmenu.h"
30 #include "odometer.h"
31 #include "widgetscreen.h"
32 #include "poialerts.h"
33 #include "speedalarm.h"
34 #include "mediaplayer.h"
35 #include "orientation.h"
36 #include "settings.h"
37 #include <QOrientationSensor>
38 #include <QSensor>
39
40
41 MainWindow::MainWindow(): QMainWindow(0), menu_(0), themeLoader_(0),
42 mainScreen_(0), orientation_(0)
43 {
44     setWindowTitle(tr("jSpeed"));
45     showFullScreen();
46     orientation_ = new Orientation(this);
47     orientation_->start();
48     addScreens();
49     QTimer::singleShot(500, this, SLOT(loadServices()));
50 }
51
52 MainWindow::~MainWindow()
53 {
54     delete themeLoader_;
55 }
56
57 void MainWindow::loadServices()
58 {
59     Odometer::instance().start();
60     QApplication::processEvents();
61     startBacklight();
62     QApplication::processEvents();
63     PoiAlerts::instance().start();
64     QApplication::processEvents();
65     SpeedAlarm::instance().start();
66     QApplication::processEvents();
67     MediaPlayer::init();
68 }
69
70 void MainWindow::addScreens()
71 {
72     stack_ = new MainWindowStack(this);
73
74     connect(stack_, SIGNAL(minimizePressed()), this, SLOT(minimize()));
75     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
76     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
77
78     mainScreen_ = new WidgetScreen(this);
79     WidgetScreen* detailScreen = new WidgetScreen(this);
80
81     themeLoader_ = new ThemeLoader(mainScreen_, detailScreen);
82
83     if(!loadTheme())
84     {
85         return;
86     }
87
88     stack_->addScreen(mainScreen_);
89     stack_->addScreen(detailScreen);
90
91     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
92
93     setCentralWidget(stack_);
94 }
95
96 bool MainWindow::loadTheme()
97 {
98     if(!themeLoader_->load())
99     {
100         QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(themeLoader_->error()));
101         close();
102         return false;
103     }
104
105     int orientations = 0;
106
107     if(mainScreen_->orientationEnabled(WidgetScreen::LANDSCAPE))
108     {
109         orientations |= Orientation::LANDSCAPE;
110     }
111     if(mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
112     {
113         orientations |= Orientation::PORTRAIT;
114     }
115
116     orientation_->setSupportedOrientations(orientations);
117     onOrientationChanged();
118     orientation_->update();
119
120     return true;
121 }
122
123 void MainWindow::minimize()
124 {
125     QDBusConnection connection = QDBusConnection::sessionBus();
126     QDBusMessage msg = QDBusMessage::createSignal("/",
127                                                   "com.nokia.hildon_desktop",
128                                                   "exit_app_view");
129     connection.send(msg);
130 }
131
132 void MainWindow::openMenu()
133 {
134     if(!menu_)
135     {
136         menu_ = new MainMenu(this);
137         connect(menu_, SIGNAL(resetTrip()), &(Odometer::instance()), SLOT(resetTrip()));
138         connect(menu_, SIGNAL(resetAll()), &(Odometer::instance()), SLOT(resetAll()));
139         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
140         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
141         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
142         connect(menu_, SIGNAL(orientationChanged()), this, SLOT(onOrientationChanged()));
143     }
144
145     menu_->show();
146 }
147
148 void MainWindow::startBacklight()
149 {
150     QTimer* timer = new QTimer(this);
151     timer->setInterval(25000);
152     connect(timer, SIGNAL(timeout()), this, SLOT(keepBacklightOn()));
153     timer->start();
154 }
155
156 void MainWindow::keepBacklightOn()
157 {
158     QDBusConnection connection = QDBusConnection::systemBus();
159     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.mce",
160                                                       "/com/nokia/mce/request",
161                                                       "com.nokia.mce.request",
162                                                       "req_display_blanking_pause");
163
164     connection.call(msg);
165 }
166
167 void MainWindow::onOrientationChanged()
168 {
169     QString value = Settings::instance().value("orientation").toString();
170
171     if(value == "auto")
172     {
173         orientation_->setOrientationType(Orientation::TYPE_AUTO);
174     }
175     else if(value == "landscape")
176     {
177         orientation_->setOrientationType(Orientation::TYPE_LANDSCAPE);
178     }
179     else if(value == "portrait")
180     {
181         orientation_->setOrientationType(Orientation::TYPE_PORTRAIT);
182     }
183 }