Vibration setting is now saved
[ghostsoverboard] / seaview.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23 #include "seaview.h"
24
25 #include <QEvent>
26 #include <QTimer>
27
28 SeaView::SeaView(QWidget *parent) :
29     QGraphicsView(parent)
30 {
31
32
33     setWindowTitle("Ghosts Overboard");
34
35     pScene_ = new SeaScene ();
36
37
38     setScene(pScene_);
39
40
41     connect(this,SIGNAL(screenTapped()),pScene_,SLOT(handleScreenTapped()));
42     connect(this,SIGNAL(goingBackgroung()),pScene_,SLOT(forcePause()));
43     connect(this,SIGNAL(goingForeground()),pScene_,SLOT(softContinue()));
44
45     showFullScreen();
46
47
48
49     //the boundaries of the scene are set to match the size of the view window, which is not
50     //available in the constructor --> timer needed
51     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
52
53 }
54
55 void  SeaView::mousePressEvent(QMouseEvent *event)
56 {
57
58     QGraphicsView::mousePressEvent(event);
59     emit screenTapped();
60
61
62 }
63
64 bool SeaView::event(QEvent *event)
65 {
66     if (!event)
67         return false;
68
69     switch (event->type())
70     {
71         //pause if app goes to background
72         case QEvent::WindowDeactivate:
73
74             emit goingBackgroung();
75             break;
76
77         //un-pause if app gomes back to foreground unless it was paused before going to background
78         case QEvent::WindowActivate:
79
80             emit goingForeground();
81             break;
82
83         //Just to keep the compiler from complaining...
84         default:
85             break;
86
87      }
88
89
90
91     //pass the event to the ancestor for handling
92     return QGraphicsView::event(event);
93
94  }
95
96
97 void SeaView::initializeBoundaries()
98 {
99         //the boundaries of the scene are set to match the size of the view window, and
100         //the view is set to show exactly the whole scene area
101
102     //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
103
104 //    QPoint topleft (0,0);
105 //    QSize windowsize = pView_->size();
106 //    QRectF rectangle (topleft,windowsize);
107
108     QRectF rectangle(0,0,800,480);
109
110     pScene_->setSceneRect(rectangle);
111     setSceneRect(rectangle);
112
113 //     qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
114
115     pScene_->restartLevel();
116 }