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