Rename all QWindow properties that have "window" in them
[profile/ivi/qtbase.git] / examples / opengl / paintedwindow / paintedwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "paintedwindow.h"
42
43 #include <QGuiApplication>
44 #include <QOpenGLContext>
45 #include <QOpenGLPaintDevice>
46 #include <QPainter>
47 #include <QScreen>
48 #include <QTimer>
49
50 #include <qmath.h>
51
52 PaintedWindow::PaintedWindow()
53 {
54     QSurfaceFormat format;
55     format.setStencilBufferSize(8);
56     format.setSamples(4);
57
58     setSurfaceType(QWindow::OpenGLSurface);
59     setFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
60     setFormat(format);
61
62     create();
63
64     m_context = new QOpenGLContext(this);
65     m_context->setFormat(format);
66     m_context->create();
67
68     m_animation = new QPropertyAnimation(this, "rotation");
69     m_animation->setStartValue(qreal(0));
70     m_animation->setEndValue(qreal(1));
71     m_animation->setDuration(500);
72
73     requestOrientation(Qt::PortraitOrientation);
74
75     QRect screenGeometry = screen()->availableGeometry();
76
77     QPoint center = screenGeometry.center();
78     QRect windowRect = screen()->isLandscape(orientation()) ? QRect(0, 0, 640, 480) : QRect(0, 0, 480, 640);
79     setGeometry(QRect(center - windowRect.center(), windowRect.size()));
80
81     m_rotation = 0;
82
83     reportContentOrientationChange(screen()->orientation());
84
85     m_targetOrientation = contentOrientation();
86     m_nextTargetOrientation = Qt::PrimaryOrientation;
87
88     connect(screen(), SIGNAL(orientationChanged(Qt::ScreenOrientation)), this, SLOT(orientationChanged(Qt::ScreenOrientation)));
89     connect(m_animation, SIGNAL(finished()), this, SLOT(rotationDone()));
90     connect(this, SIGNAL(rotationChanged(qreal)), this, SLOT(paint()));
91 }
92
93 void PaintedWindow::resizeEvent(QResizeEvent *)
94 {
95     paint();
96 }
97
98 void PaintedWindow::exposeEvent(QExposeEvent *)
99 {
100     paint();
101 }
102
103 void PaintedWindow::mousePressEvent(QMouseEvent *)
104 {
105     Qt::ScreenOrientation o = contentOrientation();
106     switch (o) {
107     case Qt::LandscapeOrientation:
108         orientationChanged(Qt::PortraitOrientation);
109         break;
110     case Qt::PortraitOrientation:
111         orientationChanged(Qt::InvertedLandscapeOrientation);
112         break;
113     case Qt::InvertedLandscapeOrientation:
114         orientationChanged(Qt::InvertedPortraitOrientation);
115         break;
116     case Qt::InvertedPortraitOrientation:
117         orientationChanged(Qt::LandscapeOrientation);
118         break;
119     default:
120         Q_ASSERT(false);
121     }
122
123     paint();
124 }
125
126 void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation)
127 {
128     if (contentOrientation() == newOrientation)
129         return;
130
131     if (m_animation->state() == QAbstractAnimation::Running) {
132         m_nextTargetOrientation = newOrientation;
133         return;
134     }
135
136     QRect rect(0, 0, width(), height());
137
138     m_prevImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
139     m_nextImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
140     m_prevImage.fill(0);
141     m_nextImage.fill(0);
142
143     QPainter p;
144     p.begin(&m_prevImage);
145     p.setTransform(screen()->transformBetween(contentOrientation(), orientation(), rect));
146     paint(&p, screen()->mapBetween(contentOrientation(), orientation(), rect));
147     p.end();
148
149     p.begin(&m_nextImage);
150     p.setTransform(screen()->transformBetween(newOrientation, orientation(), rect));
151     paint(&p, screen()->mapBetween(newOrientation, orientation(), rect));
152     p.end();
153
154     m_deltaRotation = screen()->angleBetween(newOrientation, contentOrientation());
155     if (m_deltaRotation > 180)
156         m_deltaRotation = 180 - m_deltaRotation;
157
158     m_targetOrientation = newOrientation;
159     m_animation->start();
160 }
161
162 void PaintedWindow::rotationDone()
163 {
164     reportContentOrientationChange(m_targetOrientation);
165     if (m_nextTargetOrientation != Qt::PrimaryOrientation) {
166         Q_ASSERT(m_animation->state() != QAbstractAnimation::Running);
167         orientationChanged(m_nextTargetOrientation);
168         m_nextTargetOrientation = Qt::PrimaryOrientation;
169     }
170 }
171
172 void PaintedWindow::setRotation(qreal r)
173 {
174     if (r != m_rotation) {
175         m_rotation = r;
176         emit rotationChanged(r);
177     }
178 }
179
180 void PaintedWindow::paint()
181 {
182     m_context->makeCurrent(this);
183
184     QRect rect(0, 0, width(), height());
185
186     QOpenGLPaintDevice device(size());
187     QPainter painter(&device);
188
189     QPainterPath path;
190     path.addEllipse(rect);
191     painter.setCompositionMode(QPainter::CompositionMode_Source);
192     painter.fillRect(rect, Qt::transparent);
193     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
194     painter.fillPath(path, Qt::blue);
195
196     if (contentOrientation() != m_targetOrientation) {
197         painter.setRenderHint(QPainter::SmoothPixmapTransform);
198         painter.save();
199         painter.translate(width() / 2, height() / 2);
200         painter.rotate(m_deltaRotation * m_rotation);
201         painter.translate(-width() / 2, -height() / 2);
202         painter.drawImage(0, 0, m_prevImage);
203         painter.restore();
204         painter.translate(width() / 2, height() / 2);
205         painter.rotate(m_deltaRotation * m_rotation - m_deltaRotation);
206         painter.translate(-width() / 2, -height() / 2);
207         painter.setOpacity(m_rotation);
208         painter.drawImage(0, 0, m_nextImage);
209     } else {
210         QRect mapped = screen()->mapBetween(contentOrientation(), orientation(), rect);
211
212         painter.setTransform(screen()->transformBetween(contentOrientation(), orientation(), rect));
213         paint(&painter, mapped);
214         painter.end();
215     }
216
217     m_context->swapBuffers(this);
218 }
219
220 void PaintedWindow::paint(QPainter *painter, const QRect &rect)
221 {
222     painter->setRenderHint(QPainter::Antialiasing);
223     QFont font;
224     font.setPixelSize(64);
225     painter->setFont(font);
226     painter->drawText(rect, Qt::AlignCenter, "Hello");
227 }