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