Copyright file added.
[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 type_(TYPE_AUTO)
24 {
25     connect(this, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
26 }
27
28 void Orientation::setSupportedOrientations(int orientations)
29 {
30     orientations_ = orientations;
31 }
32
33 void Orientation::setOrientationType(OrientationType type)
34 {
35     type_ = type;
36
37     handleManualOrientation();
38 }
39
40 void Orientation::update()
41 {
42     if(type_ != TYPE_AUTO)
43     {
44         handleManualOrientation();
45         return;
46     }
47
48     using QtMobility::QOrientationReading;
49
50     OrientationName orientation = LANDSCAPE;
51
52     QOrientationReading::Orientation current = reading()->orientation();
53
54     switch(current)
55     {
56     case QOrientationReading::LeftUp:
57         orientation = PORTRAIT;
58         break;
59     case QOrientationReading::FaceUp:
60         if(current_ == QOrientationReading::LeftUp)
61         {
62             orientation = PORTRAIT;
63         }
64         else
65         {
66             orientation = LANDSCAPE;
67         }
68         break;
69     default:
70         orientation = LANDSCAPE;
71         break;
72     }
73
74     current_ = current;
75
76     if(orientations_ & orientation)
77     {
78         switch(orientation)
79         {
80         case LANDSCAPE:
81             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
82             break;
83         case PORTRAIT:
84             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
85             break;
86         }
87     }
88     else
89     {
90         if(orientations_ & LANDSCAPE)
91         {
92             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
93         }
94         else if(orientations_ & PORTRAIT)
95         {
96             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
97         }
98     }
99 }
100
101 void Orientation::onReadingChanged()
102 {
103     if(type_ != TYPE_AUTO)
104     {
105         return;
106     }
107
108     if(reading()->orientation() != current_)
109     {
110         update();
111         emit changed();
112     }
113 }
114
115 void Orientation::handleManualOrientation()
116 {
117     switch(type_)
118     {
119     case TYPE_LANDSCAPE:
120         if(orientations_ & LANDSCAPE)
121         {
122             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
123         }
124         else if(orientations_ & PORTRAIT)
125         {
126             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
127         }
128         break;
129     case TYPE_PORTRAIT:
130         if(orientations_ & PORTRAIT)
131         {
132             window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
133         }
134         else if(orientations_ & LANDSCAPE)
135         {
136             window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
137         }
138         break;
139     default:
140         break;
141     }
142 }