Changed app to use Qt Mobility in screen rotation instead of default Qt screen rotation.
[jspeed] / src / orientation.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 "orientation.h"
20
21 Orientation::Orientation(QMainWindow* window): QtMobility::QOrientationSensor(window),
22 current_(QtMobility::QOrientationReading::TopUp), orientations_(0), window_(window)
23 {
24     connect(this, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
25 }
26
27 void Orientation::setSupportedOrientations(int orientations)
28 {
29     orientations_ = orientations;
30 }
31
32 void Orientation::update()
33 {
34     using QtMobility::QOrientationReading;
35
36     OrientationName orientation = LANDSCAPE;
37
38     QOrientationReading::Orientation current = reading()->orientation();
39
40     switch(current)
41     {
42     case QOrientationReading::LeftUp:
43         orientation = PORTRAIT;
44         break;
45     case QOrientationReading::FaceUp:
46         if(current_ == QOrientationReading::LeftUp)
47         {
48             orientation = PORTRAIT;
49         }
50         else
51         {
52             orientation = LANDSCAPE;
53         }
54         break;
55     default:
56         orientation = LANDSCAPE;
57         break;
58     }
59
60     current_ = current;
61
62     if(orientations_ & orientation)
63     {
64         switch(orientation)
65         {
66         case LANDSCAPE:
67             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
68             break;
69         case PORTRAIT:
70             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
71             break;
72         }
73     }
74     else
75     {
76         if(orientations_ & LANDSCAPE)
77         {
78             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
79         }
80         else if(orientations_ & PORTRAIT)
81         {
82             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
83         }
84     }
85 }
86
87 void Orientation::onReadingChanged()
88 {
89     if(reading()->orientation() != current_)
90     {
91         update();
92         emit changed();
93     }
94 }