a55cb1f10558d352331761bccf81c50a5859b0e6
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickscreen.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickscreen_p.h"
43
44 #include "qquickitem.h"
45 #include "qquickitem_p.h"
46 #include "qquickcanvas.h"
47
48 #include <QScreen>
49
50 QT_BEGIN_NAMESPACE
51
52 /*!
53     \qmlclass Screen QQuickScreenAttached
54     \inqmlmodule QtQuick.Window 2
55     \ingroup qtquick-visual-utility
56     \brief The Screen attached object provides information about the Screen an Item is displayed on.
57
58     The Screen attached object is only valid inside Item or Item derived types, after component completion.
59     Inside these items it refers to the screen that the item is currently being displayed on.
60
61     To use this type, you will need to import the module with the following line:
62     \code
63     import QtQuick.Window 2.0
64     \endcode
65
66     Note that the Screen type is not valid at Component.onCompleted, because the Item has not been displayed on
67     a screen by this time.
68
69     Restricting this import will allow you to have a QML environment without access to window system features.
70 */
71
72 /*!
73     \qmlattachedproperty int QtQuick.Window2::Screen::width
74     \readonly
75
76     This contains the width of the screen in pixels.
77 */
78 /*!
79     \qmlattachedproperty int QtQuick.Window2::Screen::height
80     \readonly
81
82     This contains the height of the screen in pixels.
83 */
84 /*!
85     \qmlattachedproperty Qt::ScreenOrientation QtQuick.Window2::Screen::primaryOrientation
86     \readonly
87
88     This contains the primary orientation of the screen.
89 */
90 /*!
91     \qmlattachedproperty Qt::ScreenOrientation QtQuick.Window2::Screen::orientation
92     \readonly
93
94     This contains the current orientation of the screen.
95 */
96 /*!
97     \qmlattachedmethod int QtQuick.Window2::Screen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
98
99     Returns the rotation angle, in degrees, between the two specified angles.
100 */
101
102 QQuickScreenAttached::QQuickScreenAttached(QObject* attachee)
103     : QObject(attachee)
104     , m_screen(0)
105 {
106     m_attachee = qobject_cast<QQuickItem*>(attachee);
107
108     if (m_attachee) {
109         QQuickItemPrivate::get(m_attachee)->extra.value().screenAttached = this;
110
111         if (m_attachee->canvas()) //It might not be assigned to a canvas yet
112             canvasChanged(m_attachee->canvas());
113     }
114 }
115
116 int QQuickScreenAttached::width() const
117 {
118     if (!m_screen)
119         return 0;
120     return m_screen->size().width();
121 }
122
123 int QQuickScreenAttached::height() const
124 {
125     if (!m_screen)
126         return 0;
127     return m_screen->size().height();
128 }
129
130 Qt::ScreenOrientation QQuickScreenAttached::primaryOrientation() const
131 {
132     if (!m_screen)
133         return Qt::PrimaryOrientation;
134     return m_screen->primaryOrientation();
135 }
136
137 Qt::ScreenOrientation QQuickScreenAttached::orientation() const
138 {
139     if (!m_screen)
140         return Qt::PrimaryOrientation;
141     return m_screen->orientation();
142 }
143
144 int QQuickScreenAttached::angleBetween(int a, int b)
145 {
146     if (!m_screen)
147         return Qt::PrimaryOrientation;
148     return m_screen->angleBetween((Qt::ScreenOrientation)a,(Qt::ScreenOrientation)b);
149 }
150
151 void QQuickScreenAttached::canvasChanged(QQuickCanvas* c)//Called by QQuickItemPrivate::initCanvas
152 {
153     QScreen* screen = c ? c->screen() : 0;
154     if (screen != m_screen) {
155         QScreen* oldScreen = m_screen;
156         m_screen = screen;
157
158         if (oldScreen) {
159             disconnect(oldScreen, SIGNAL(sizeChanged(QSize)),
160                     this, SIGNAL(widthChanged()));
161             disconnect(oldScreen, SIGNAL(sizeChanged(QSize)),
162                     this, SIGNAL(heightChanged()));
163             disconnect(oldScreen, SIGNAL(orientationChanged(Qt::ScreenOrientation)),
164                     this, SIGNAL(orientationChanged()));
165             disconnect(oldScreen, SIGNAL(primaryOrientationChanged(Qt::ScreenOrientation)),
166                     this, SIGNAL(primaryOrientationChanged()));
167         }
168
169         if (!screen)
170             return; //Don't bother emitting signals, because the new values are garbage anyways
171
172         if (!oldScreen || screen->size() != oldScreen->size()) {
173             emit widthChanged();
174             emit heightChanged();
175         }
176
177         if (!oldScreen || screen->orientation() != oldScreen->orientation())
178             emit orientationChanged();
179         if (!oldScreen || screen->primaryOrientation() != oldScreen->primaryOrientation())
180             emit primaryOrientationChanged();
181
182
183         connect(screen, SIGNAL(sizeChanged(QSize)),
184                 this, SIGNAL(widthChanged()));
185         connect(screen, SIGNAL(sizeChanged(QSize)),
186                 this, SIGNAL(heightChanged()));
187         connect(screen, SIGNAL(orientationChanged(Qt::ScreenOrientation)),
188                 this, SIGNAL(orientationChanged()));
189         connect(screen, SIGNAL(primaryOrientationChanged(Qt::ScreenOrientation)),
190                 this, SIGNAL(primaryOrientationChanged()));
191     }
192 }
193
194 QT_END_NAMESPACE