Update copyright year in license headers.
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickscreen.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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     \brief The Screen attached object provides information about the Screen an Item is displayed on.
56
57     The Screen attached object is only valid inside Item or Item derived elements. Inside these elements
58     it refers to the screen that the element is currently being displayed on.
59 */
60
61 /*!
62     \qmlattachedproperty int QtQuickWindow2::Screen::width
63     \readonly
64
65     This contains the width of the screen in pixels.
66 */
67 /*!
68     \qmlattachedproperty int QtQuickWindow2::Screen::height
69     \readonly
70
71     This contains the height of the screen in pixels.
72 */
73 /*!
74     \qmlattachedproperty Qt::ScreenOrientation QtQuickWindow2::Screen::primaryOrientation
75     \readonly
76
77     This contains the primary orientation of the screen. This can only change if the screen changes.
78 */
79 /*!
80     \qmlattachedproperty Qt::ScreenOrientation QtQuickWindow2::Screen::currentOrientation
81     \readonly
82
83     This contains the current orientation of the screen.
84 */
85 /*!
86     \qmlattachedmethod int QtQuickWindow2::Screen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
87
88     Returns the rotation angle, in degrees, between the two specified angles.
89 */
90
91 QQuickScreenAttached::QQuickScreenAttached(QObject* attachee)
92     : QObject(attachee)
93     , m_screen(0)
94 {
95     m_attachee = qobject_cast<QQuickItem*>(attachee);
96
97     if (m_attachee) {
98         QQuickItemPrivate::get(m_attachee)->screenAttached = this;
99
100         if (m_attachee->canvas()) //It might not be assigned to a canvas yet
101             canvasChanged(m_attachee->canvas());
102     }
103 }
104
105 int QQuickScreenAttached::width() const
106 {
107     if (!m_screen)
108         return 0;
109     return m_screen->size().width();
110 }
111
112 int QQuickScreenAttached::height() const
113 {
114     if (!m_screen)
115         return 0;
116     return m_screen->size().height();
117 }
118
119 Qt::ScreenOrientation QQuickScreenAttached::primaryOrientation() const
120 {
121     if (!m_screen)
122         return Qt::UnknownOrientation;
123     return m_screen->primaryOrientation();
124 }
125
126 Qt::ScreenOrientation QQuickScreenAttached::currentOrientation() const
127 {
128     if (!m_screen)
129         return Qt::UnknownOrientation;
130     return m_screen->currentOrientation();
131 }
132
133 int QQuickScreenAttached::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
134 {
135     return QScreen::angleBetween(a,b);
136 }
137
138 void QQuickScreenAttached::canvasChanged(QQuickCanvas* c)//Called by QQuickItemPrivate::initCanvas
139 {
140     QScreen* screen = c ? c->screen() : 0;
141     if (screen != m_screen) {
142         QScreen* oldScreen = m_screen;
143         m_screen = screen;
144
145         if (oldScreen) {
146             disconnect(oldScreen, SIGNAL(sizeChanged(QSize)),
147                     this, SIGNAL(widthChanged()));
148             disconnect(oldScreen, SIGNAL(sizeChanged(QSize)),
149                     this, SIGNAL(heightChanged()));
150             disconnect(oldScreen, SIGNAL(currentOrientationChanged(Qt::ScreenOrientation)),
151                     this, SIGNAL(currentOrientationChanged()));
152         }
153
154         if (!screen)
155             return; //Don't bother emitting signals, because the new values are garbage anyways
156
157         if (!oldScreen || screen->size() != oldScreen->size()) {
158             emit widthChanged();
159             emit heightChanged();
160         }
161         if (!oldScreen || screen->currentOrientation() != oldScreen->currentOrientation())
162             emit currentOrientationChanged();
163         if (!oldScreen || screen->primaryOrientation() != oldScreen->primaryOrientation())
164             emit primaryOrientationChanged();
165
166
167         connect(screen, SIGNAL(sizeChanged(QSize)),
168                 this, SIGNAL(widthChanged()));
169         connect(screen, SIGNAL(sizeChanged(QSize)),
170                 this, SIGNAL(heightChanged()));
171         connect(screen, SIGNAL(currentOrientationChanged(Qt::ScreenOrientation)),
172                 this, SIGNAL(currentOrientationChanged()));
173     }
174 }
175
176 QT_END_NAMESPACE