Keeps screen lit and pauses when app is backgrounded
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Wed, 15 Jun 2011 11:30:54 +0000 (14:30 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Wed, 15 Jun 2011 11:30:54 +0000 (14:30 +0300)
The screen is now kept lit unless the game is paused. The game is auto-
(paused or not) before minimizing is restored (using the state of the
pause action from menu).

ghostsoverboard.pro
mainwindow.cpp
mainwindow.h
screenlitkeeper.cpp [new file with mode: 0644]
screenlitkeeper.h [new file with mode: 0644]
seascene.cpp
seascene.h

index 70c3b4b..70fe129 100644 (file)
@@ -16,17 +16,20 @@ SOURCES += main.cpp\
     timercontrolledtursas.cpp \
     seascene.cpp \
     seaview.cpp \
-    ship.cpp
+    ship.cpp \
+    screenlitkeeper.cpp
 
 HEADERS  += mainwindow.h \
     orientationcontrolledgraphicspixmapobject.h \
     timercontrolledtursas.h \
     seascene.h \
     seaview.h \
-    ship.h
+    ship.h \
+    screenlitkeeper.h
 
 CONFIG += mobility
 MOBILITY = sensors
+MOBILITY += systeminfo
 
 symbian {
     TARGET.UID3 = 0xe3f4bbc2
index 937c0ab..20bda4f 100644 (file)
@@ -27,11 +27,11 @@ MainWindow::MainWindow(QWidget *parent)
     pView_->setScene(pScene_);
     setCentralWidget(pView_);
 
-    QAction * pPauseAction = new QAction(tr("Pause"),this);
-    pPauseAction->setCheckable(true);
-    addAction(pPauseAction);
-    connect(pPauseAction,SIGNAL(triggered(bool)),pScene_,SLOT(pause(bool)));
-    menuBar()->addAction(pPauseAction);
+    pPauseAction_ = new QAction(tr("Pause"),this);
+    pPauseAction_->setCheckable(true);
+    addAction(pPauseAction_);
+    connect(pPauseAction_,SIGNAL(triggered(bool)),pScene_,SLOT(pause(bool)));
+    menuBar()->addAction(pPauseAction_);
 
     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
     addAction(pRestartLevelAction);
@@ -146,3 +146,39 @@ void MainWindow::nextLevel()
        restartLevel();
 
 }
+
+bool MainWindow::event(QEvent *event)
+{
+
+    switch (event->type())
+    {
+        //pause if app goes to background
+        case QEvent::WindowDeactivate:
+
+            if (pScene_)
+                pScene_->pause(true);
+            break;
+
+        //un-pause if app gomes back to foreground unless it was paused before going to background
+        case QEvent::WindowActivate:
+
+
+            if (pPauseAction_ && !pPauseAction_->isChecked())
+            {
+                if (pScene_)
+                    pScene_->pause(false);
+            }
+            break;
+
+        //Just to keep the compiler from complaining...
+        default:
+            break;
+
+     }
+
+
+
+    //pass the event to the ancestor for handling
+    return QMainWindow::event(event);
+
+ }
index c8b0e11..c9bf802 100644 (file)
@@ -6,6 +6,7 @@
 #include "orientationcontrolledgraphicspixmapobject.h"
 #include "seascene.h"
 
+
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
@@ -14,6 +15,8 @@ public:
     MainWindow(QWidget *parent = 0);
     ~MainWindow();
 
+    bool event(QEvent *event);
+
 public slots:
     void initializeBoundaries();
     void restartLevel();
@@ -25,9 +28,7 @@ private:
 
 SeaScene * pScene_;
 QGraphicsView * pView_;
-
-
-
+QAction* pPauseAction_;
 
 
 };
diff --git a/screenlitkeeper.cpp b/screenlitkeeper.cpp
new file mode 100644 (file)
index 0000000..6606209
--- /dev/null
@@ -0,0 +1,63 @@
+/**************************************************************************
+        ScreenLitKeeper
+
+        Copyright (C) 2010  Heli Hyvättinen
+        
+        This file is free software: you can redistribute it and/or modify
+        it under the terms of the GNU General Public License as published by
+        the Free Software Foundation, either version 3 of the License, or
+        (at your option) any later version.
+
+        This program is distributed in the hope that it will be useful,
+        but WITHOUT ANY WARRANTY; without even the implied warranty of
+        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+        GNU General Public License for more details.
+
+        You should have received a copy of the GNU General Public License
+        along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+**************************************************************************/
+
+
+
+
+
+#include "screenlitkeeper.h"
+
+ScreenLitKeeper::ScreenLitKeeper(QObject *parent) :
+    QObject(parent)
+{
+    p_screensaver_ = NULL;
+    isKeptLit_ = false;
+}
+
+void ScreenLitKeeper::keepScreenLit(bool keepLit)
+{
+
+    //If the requested state is the same as the current state do nothing.
+    if (keepLit == isKeptLit_)
+        return;
+
+
+
+    if (keepLit == true )
+    {
+        //a new screensaver is created, parent is given so that it is automatically destroyed when this object is destroyed
+        p_screensaver_ = new QSystemScreenSaver(this);
+        //screensaver is disabled, which keeps the screen lit on N900
+        p_screensaver_->setScreenSaverInhibit();
+        isKeptLit_ = true;
+
+    }
+
+    else if (p_screensaver_ != NULL) //just to be on the safe side, it should never be NULL if this line is reached
+    {
+      delete p_screensaver_; //The object must be deleted to reverse the effect of setScreenSaverInhibit()
+      p_screensaver_ = NULL;
+      isKeptLit_ = false;
+
+    }
+
+    return;
+
+}
diff --git a/screenlitkeeper.h b/screenlitkeeper.h
new file mode 100644 (file)
index 0000000..24dd565
--- /dev/null
@@ -0,0 +1,69 @@
+/**************************************************************************
+        ScreenLitKeeper
+
+        Copyright (C) 2010  Heli Hyvättinen
+        
+        This file is free software: you can redistribute it and/or modify
+        it under the terms of the GNU General Public License as published by
+        the Free Software Foundation, either version 3 of the License, or
+        (at your option) any later version.
+
+        This program is distributed in the hope that it will be useful,
+        but WITHOUT ANY WARRANTY; without even the implied warranty of
+        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+        GNU General Public License for more details.
+
+        You should have received a copy of the GNU General Public License
+        along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+**************************************************************************/
+
+
+
+
+
+#ifndef SCREENLITKEEPER_H
+#define SCREENLITKEEPER_H
+
+#include <QObject>
+#include <QSystemScreenSaver>
+using namespace QtMobility;
+
+
+/*! Allows keeping the sreen lit by disabling the screensaver
+
+Works at least on N900.
+The sreen can be set to be kept lit or not.
+To use this class, you must have the following lines  in your .pro file:
+CONFIG += mobility
+MOBILITY += systeminfo
+
+@author Heli Hyvättinen
+@date 2010-09-07
+@version 1
+
+  */
+
+class ScreenLitKeeper : public QObject
+{
+    Q_OBJECT
+public:
+    explicit ScreenLitKeeper(QObject *parent = 0);
+
+signals:
+
+public slots:
+ /*!
+Sets whether the screen is to be kept lit
+@param keepLit true for keeping lit, false for allowing blanking
+*/
+
+    void keepScreenLit(bool keepLit);
+
+private:
+     QSystemScreenSaver * p_screensaver_;
+     bool isKeptLit_;
+
+};
+
+#endif // SCREENLITKEEPER_H
index eb14504..4c2e72c 100644 (file)
@@ -15,6 +15,7 @@ SeaScene::SeaScene(QObject *parent) :
     QGraphicsScene(parent)
 {
     paused_ = false;
+    screenLitKeeper_.keepScreenLit(true);
 
     //set background
 
@@ -26,6 +27,8 @@ SeaScene::SeaScene(QObject *parent) :
 
     qsrand(QTime::currentTime().msec()+2);  //+2 to avoid setting it to 1
 
+
+
 }
 
 void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
@@ -269,11 +272,13 @@ void SeaScene::pause(bool paused)
         {
      //       qDebug() << "starting to move again";
             emit pauseOff();
+            screenLitKeeper_.keepScreenLit(true);
         }
 
         else
         {
      //       qDebug("about to stop movement");
             emit pauseOn();
+            screenLitKeeper_.keepScreenLit(false);
         }
 }
index ef1eac5..70334e3 100644 (file)
@@ -2,6 +2,7 @@
 #define SEASCENE_H
 
 #include <QGraphicsScene>
+#include "screenlitkeeper.h"
 
 class SeaScene : public QGraphicsScene
 {
@@ -54,6 +55,8 @@ protected:
 
     bool paused_;
 
+    ScreenLitKeeper screenLitKeeper_;
+