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