1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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.
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."
39 ****************************************************************************/
41 #include "paintedwindow.h"
43 #include <QGuiApplication>
44 #include <QOpenGLContext>
45 #include <QOpenGLPaintDevice>
52 PaintedWindow::PaintedWindow()
54 QSurfaceFormat format;
55 format.setStencilBufferSize(8);
58 setSurfaceType(QWindow::OpenGLSurface);
59 setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
64 m_context = new QOpenGLContext(this);
65 m_context->setFormat(format);
68 m_animation = new QPropertyAnimation(this, "rotation");
69 m_animation->setStartValue(qreal(0));
70 m_animation->setEndValue(qreal(1));
71 m_animation->setDuration(500);
73 requestWindowOrientation(Qt::PortraitOrientation);
75 QRect screenGeometry = screen()->availableGeometry();
77 QPoint center = screenGeometry.center();
78 QRect windowRect = screen()->isLandscape(windowOrientation()) ? QRect(0, 0, 640, 480) : QRect(0, 0, 480, 640);
79 setGeometry(QRect(center - windowRect.center(), windowRect.size()));
83 reportContentOrientationChange(screen()->orientation());
85 m_targetOrientation = contentOrientation();
86 m_nextTargetOrientation = Qt::PrimaryOrientation;
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()));
93 void PaintedWindow::resizeEvent(QResizeEvent *)
98 void PaintedWindow::exposeEvent(QExposeEvent *)
103 void PaintedWindow::mousePressEvent(QMouseEvent *)
105 Qt::ScreenOrientation o = contentOrientation();
107 case Qt::LandscapeOrientation:
108 orientationChanged(Qt::PortraitOrientation);
110 case Qt::PortraitOrientation:
111 orientationChanged(Qt::InvertedLandscapeOrientation);
113 case Qt::InvertedLandscapeOrientation:
114 orientationChanged(Qt::InvertedPortraitOrientation);
116 case Qt::InvertedPortraitOrientation:
117 orientationChanged(Qt::LandscapeOrientation);
126 void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation)
128 if (contentOrientation() == newOrientation)
131 if (m_animation->state() == QAbstractAnimation::Running) {
132 m_nextTargetOrientation = newOrientation;
136 QRect rect(0, 0, width(), height());
138 m_prevImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
139 m_nextImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
144 p.begin(&m_prevImage);
145 p.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect));
146 paint(&p, screen()->mapBetween(contentOrientation(), windowOrientation(), rect));
149 p.begin(&m_nextImage);
150 p.setTransform(screen()->transformBetween(newOrientation, windowOrientation(), rect));
151 paint(&p, screen()->mapBetween(newOrientation, windowOrientation(), rect));
154 m_deltaRotation = screen()->angleBetween(newOrientation, contentOrientation());
155 if (m_deltaRotation > 180)
156 m_deltaRotation = 180 - m_deltaRotation;
158 m_targetOrientation = newOrientation;
159 m_animation->start();
162 void PaintedWindow::rotationDone()
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;
172 void PaintedWindow::setRotation(qreal r)
174 if (r != m_rotation) {
176 emit rotationChanged(r);
180 void PaintedWindow::paint()
182 m_context->makeCurrent(this);
184 QRect rect(0, 0, width(), height());
186 QOpenGLPaintDevice device(size());
187 QPainter painter(&device);
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);
196 if (contentOrientation() != m_targetOrientation) {
197 painter.setRenderHint(QPainter::SmoothPixmapTransform);
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);
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);
210 QRect mapped = screen()->mapBetween(contentOrientation(), windowOrientation(), rect);
212 painter.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect));
213 paint(&painter, mapped);
217 m_context->swapBuffers(this);
220 void PaintedWindow::paint(QPainter *painter, const QRect &rect)
222 painter->setRenderHint(QPainter::Antialiasing);
224 font.setPixelSize(64);
225 painter->setFont(font);
226 painter->drawText(rect, Qt::AlignCenter, "Hello");