...
[jspeed] / src / mainwindow.cpp
index e0280d3..f8e9185 100644 (file)
 #include <QMaemo5InformationBox>
 #include "mainwindow.h"
 #include "mainwindowstack.h"
-#include "theme.h"
-#include "detailwidget.h"
+#include "themeloader.h"
 #include "mainmenu.h"
 #include "odometer.h"
-
-MainWindow::MainWindow(): QMainWindow(0), menu_(0), theme_(0)
+#include "widgetscreen.h"
+#include "poialerts.h"
+#include "speedalarm.h"
+#include "mediaplayer.h"
+#include "orientation.h"
+#include "settings.h"
+#include <QOrientationSensor>
+#include <QSensor>
+
+
+MainWindow::MainWindow(): QMainWindow(0), menu_(0), themeLoader_(0),
+mainScreen_(0), orientation_(0)
 {
     setWindowTitle(tr("jSpeed"));
     showFullScreen();
-    Odometer::instance().start();
+    orientation_ = new Orientation(this);
+    orientation_->start();
     addScreens();
-    startBacklight();
+    QTimer::singleShot(500, this, SLOT(loadServices()));
 }
 
 MainWindow::~MainWindow()
 {
+    delete themeLoader_;
+}
+
+void MainWindow::loadServices()
+{
+    Odometer::instance().start();
+    QApplication::processEvents();
+    startBacklight();
+    QApplication::processEvents();
+    PoiAlerts::instance().start();
+    QApplication::processEvents();
+    SpeedAlarm::instance().start();
+    QApplication::processEvents();
+    MediaPlayer::init();
 }
 
 void MainWindow::addScreens()
@@ -51,15 +75,18 @@ void MainWindow::addScreens()
     connect(stack_, SIGNAL(settingsPressed()), this, SLOT(openMenu()));
     connect(stack_, SIGNAL(closePressed()), this, SIGNAL(quit()));
 
-    theme_ = new Theme;
+    mainScreen_ = new WidgetScreen(this);
+    WidgetScreen* detailScreen = new WidgetScreen(this);
+
+    themeLoader_ = new ThemeLoader(mainScreen_, detailScreen);
 
     if(!loadTheme())
     {
         return;
     }
 
-    stack_->addScreen(theme_);
-    stack_->addScreen(new DetailWidget(this));
+    stack_->addScreen(mainScreen_);
+    stack_->addScreen(detailScreen);
 
     connect(QApplication::desktop(), SIGNAL(resized(int)), stack_, SLOT(reArrange()));
 
@@ -68,22 +95,28 @@ void MainWindow::addScreens()
 
 bool MainWindow::loadTheme()
 {
-    if(!theme_->load())
+    if(!themeLoader_->load())
     {
-        QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(theme_->error()));
+        QMaemo5InformationBox::information(this, tr("Unable to load theme: %1").arg(themeLoader_->error()));
         close();
         return false;
     }
 
-    if(theme_->landscapeEnabled() && theme_->portraitEnabled())
+    int orientations = 0;
+
+    if(mainScreen_->orientationEnabled(WidgetScreen::LANDSCAPE))
     {
-        setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+        orientations |= Orientation::LANDSCAPE;
     }
-    else if(theme_->portraitEnabled())
+    if(mainScreen_->orientationEnabled(WidgetScreen::PORTRAIT))
     {
-        setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+        orientations |= Orientation::PORTRAIT;
     }
 
+    orientation_->setSupportedOrientations(orientations);
+    onOrientationChanged();
+    orientation_->update();
+
     return true;
 }
 
@@ -106,6 +139,7 @@ void MainWindow::openMenu()
         connect(menu_, SIGNAL(flip()), stack_, SLOT(flip()));
         connect(menu_, SIGNAL(themeChanged()), this, SLOT(loadTheme()));
         connect(menu_, SIGNAL(unitChanged()), &(Odometer::instance()), SLOT(updateUnit()));
+        connect(menu_, SIGNAL(orientationChanged()), this, SLOT(onOrientationChanged()));
     }
 
     menu_->show();
@@ -129,3 +163,21 @@ void MainWindow::keepBacklightOn()
 
     connection.call(msg);
 }
+
+void MainWindow::onOrientationChanged()
+{
+    QString value = Settings::instance().value("orientation").toString();
+
+    if(value == "auto")
+    {
+        orientation_->setOrientationType(Orientation::TYPE_AUTO);
+    }
+    else if(value == "landscape")
+    {
+        orientation_->setOrientationType(Orientation::TYPE_LANDSCAPE);
+    }
+    else if(value == "portrait")
+    {
+        orientation_->setOrientationType(Orientation::TYPE_PORTRAIT);
+    }
+}