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