Changed app to use Qt Mobility in screen rotation instead of default Qt screen 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 <QOrientationSensor>
37 #include <QSensor>
38
39
40 MainWindow::MainWindow(): QMainWindow(0), menu_(0), themeLoader_(0),
41 mainScreen_(0), orientation_(0)
42 {
43     setWindowTitle(tr("jSpeed"));
44     showFullScreen();
45     orientation_ = new Orientation(this);
46     orientation_->start();
47     addScreens();
48     QTimer::singleShot(500, this, SLOT(loadServices()));
49 }
50
51 MainWindow::~MainWindow()
52 {
53     delete themeLoader_;
54 }
55
56 void MainWindow::loadServices()
57 {
58     Odometer::instance().start();
59     QApplication::processEvents();
60     startBacklight();
61     QApplication::processEvents();
62     PoiAlerts::instance().start();
63     QApplication::processEvents();
64     SpeedAlarm::instance().start();
65     QApplication::processEvents();
66     MediaPlayer::init();
67 }
68
69 void MainWindow::addScreens()
70 {
71     stack_ = new MainWindowStack(this);
72
73     connect(stack_, SIGNAL(minimizePressed()), this, SLOT(minimize()));
74     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
75     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
76
77     mainScreen_ = new WidgetScreen(this);
78     WidgetScreen* detailScreen = new WidgetScreen(this);
79
80     themeLoader_ = new ThemeLoader(mainScreen_, detailScreen);
81
82     if(!loadTheme())
83     {
84         return;
85     }
86
87     stack_->addScreen(mainScreen_);
88     stack_->addScreen(detailScreen);
89
90     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
91
92     setCentralWidget(stack_);
93 }
94
95 bool MainWindow::loadTheme()
96 {
97     if(!themeLoader_->load())
98     {
99         QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(themeLoader_->error()));
100         close();
101         return false;
102     }
103
104     int orientations = 0;
105
106     if(mainScreen_->orientationEnabled(WidgetScreen::LANDSCAPE))
107     {
108         orientations |= Orientation::LANDSCAPE;
109     }
110     if(mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
111     {
112         orientations |= Orientation::PORTRAIT;
113     }
114
115     orientation_->setSupportedOrientations(orientations);
116     orientation_->update();
117
118     return true;
119 }
120
121 void MainWindow::minimize()
122 {
123     QDBusConnection connection = QDBusConnection::sessionBus();
124     QDBusMessage msg = QDBusMessage::createSignal("/",
125                                                   "com.nokia.hildon_desktop",
126                                                   "exit_app_view");
127     connection.send(msg);
128 }
129
130 void MainWindow::openMenu()
131 {
132     if(!menu_)
133     {
134         menu_ = new MainMenu(this);
135         connect(menu_, SIGNAL(resetTrip()), &(Odometer::instance()), SLOT(resetTrip()));
136         connect(menu_, SIGNAL(resetAll()), &(Odometer::instance()), SLOT(resetAll()));
137         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
138         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
139         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
140     }
141
142     menu_->show();
143 }
144
145 void MainWindow::startBacklight()
146 {
147     QTimer* timer = new QTimer(this);
148     timer->setInterval(25000);
149     connect(timer, SIGNAL(timeout()), this, SLOT(keepBacklightOn()));
150     timer->start();
151 }
152
153 void MainWindow::keepBacklightOn()
154 {
155     QDBusConnection connection = QDBusConnection::systemBus();
156     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.mce",
157                                                       "/com/nokia/mce/request",
158                                                       "com.nokia.mce.request",
159                                                       "req_display_blanking_pause");
160
161     connection.call(msg);
162 }