f383234898e4fc73c17af489109bc88ab0625c71
[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 "theme.h"
29 #include "detailwidget.h"
30 #include "detailscreen.h"
31 #include "mainmenu.h"
32 #include "odometer.h"
33
34 MainWindow::MainWindow(): QMainWindow(0), menu_(0), theme_(0)
35 {
36     setWindowTitle(tr("jSpeed"));
37     showFullScreen();
38     Odometer::instance().start();
39     addScreens();
40     startBacklight();
41 }
42
43 MainWindow::~MainWindow()
44 {
45 }
46
47 void MainWindow::addScreens()
48 {
49     stack_ = new MainWindowStack(this);
50
51     connect(stack_, SIGNAL(minimizePressed()), this, SLOT(minimize()));
52     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
53     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
54
55     DetailWidget* details = new DetailWidget(this);
56
57     theme_ = new Theme(details->getScreen());
58
59     if(!loadTheme())
60     {
61         return;
62     }
63
64     stack_->addScreen(theme_);
65     stack_->addScreen(details);
66
67     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
68
69     setCentralWidget(stack_);
70 }
71
72 bool MainWindow::loadTheme()
73 {
74     if(!theme_->load())
75     {
76         QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(theme_->error()));
77         close();
78         return false;
79     }
80
81     if(theme_->landscapeEnabled() && theme_->portraitEnabled())
82     {
83         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
84     }
85     else if(theme_->portraitEnabled())
86     {
87         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
88     }
89     else
90     {
91         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
92     }
93
94     return true;
95 }
96
97 void MainWindow::minimize()
98 {
99     QDBusConnection connection = QDBusConnection::sessionBus();
100     QDBusMessage msg = QDBusMessage::createSignal("/",
101                                                   "com.nokia.hildon_desktop",
102                                                   "exit_app_view");
103     connection.send(msg);
104 }
105
106 void MainWindow::openMenu()
107 {
108     if(!menu_)
109     {
110         menu_ = new MainMenu(this);
111         connect(menu_, SIGNAL(resetTrip()), &(Odometer::instance()), SLOT(resetTrip()));
112         connect(menu_, SIGNAL(resetAll()), &(Odometer::instance()), SLOT(resetAll()));
113         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
114         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
115         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
116     }
117
118     menu_->show();
119 }
120
121 void MainWindow::startBacklight()
122 {
123     QTimer* timer = new QTimer(this);
124     timer->setInterval(25000);
125     connect(timer, SIGNAL(timeout()), this, SLOT(keepBacklightOn()));
126     timer->start();
127 }
128
129 void MainWindow::keepBacklightOn()
130 {
131     QDBusConnection connection = QDBusConnection::systemBus();
132     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.mce",
133                                                       "/com/nokia/mce/request",
134                                                       "com.nokia.mce.request",
135                                                       "req_display_blanking_pause");
136
137     connection.call(msg);
138 }