2011-09-14 02:03:34 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2012-09-19 12:28:29 +00:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-09-14 02:03:34 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2012-09-19 12:28:29 +00:00
|
|
|
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
|
|
|
** of its contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
2011-09-14 02:03:34 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-09-14 02:03:34 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
#include "paintedwindow.h"
|
|
|
|
|
2011-10-04 11:11:30 +00:00
|
|
|
#include <QGuiApplication>
|
2011-08-22 08:49:28 +00:00
|
|
|
#include <QOpenGLContext>
|
2011-09-07 08:31:03 +00:00
|
|
|
#include <QOpenGLPaintDevice>
|
2011-08-22 08:49:28 +00:00
|
|
|
#include <QPainter>
|
2011-10-04 11:11:30 +00:00
|
|
|
#include <QScreen>
|
2011-08-22 08:49:28 +00:00
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <qmath.h>
|
|
|
|
|
|
|
|
PaintedWindow::PaintedWindow()
|
|
|
|
{
|
|
|
|
QSurfaceFormat format;
|
|
|
|
format.setStencilBufferSize(8);
|
2011-09-07 08:31:03 +00:00
|
|
|
format.setSamples(4);
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
setSurfaceType(QWindow::OpenGLSurface);
|
|
|
|
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
|
|
|
setFormat(format);
|
|
|
|
|
|
|
|
create();
|
|
|
|
|
|
|
|
m_context = new QOpenGLContext(this);
|
|
|
|
m_context->setFormat(format);
|
|
|
|
m_context->create();
|
2011-11-23 08:36:51 +00:00
|
|
|
|
|
|
|
m_animation = new QPropertyAnimation(this, "rotation");
|
|
|
|
m_animation->setStartValue(qreal(0));
|
|
|
|
m_animation->setEndValue(qreal(1));
|
|
|
|
m_animation->setDuration(500);
|
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
requestWindowOrientation(Qt::PortraitOrientation);
|
|
|
|
|
|
|
|
QRect screenGeometry = screen()->availableGeometry();
|
|
|
|
|
|
|
|
QPoint center = screenGeometry.center();
|
|
|
|
QRect windowRect = screen()->isLandscape(windowOrientation()) ? QRect(0, 0, 640, 480) : QRect(0, 0, 480, 640);
|
|
|
|
setGeometry(QRect(center - windowRect.center(), windowRect.size()));
|
|
|
|
|
2011-11-23 08:36:51 +00:00
|
|
|
m_rotation = 0;
|
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
reportContentOrientationChange(screen()->orientation());
|
2011-11-23 08:36:51 +00:00
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
m_targetOrientation = contentOrientation();
|
|
|
|
m_nextTargetOrientation = Qt::PrimaryOrientation;
|
|
|
|
|
|
|
|
connect(screen(), SIGNAL(orientationChanged(Qt::ScreenOrientation)), this, SLOT(orientationChanged(Qt::ScreenOrientation)));
|
2011-11-23 08:36:51 +00:00
|
|
|
connect(m_animation, SIGNAL(finished()), this, SLOT(rotationDone()));
|
|
|
|
connect(this, SIGNAL(rotationChanged(qreal)), this, SLOT(paint()));
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PaintedWindow::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
2011-09-07 08:31:03 +00:00
|
|
|
paint();
|
|
|
|
}
|
2011-08-22 08:49:28 +00:00
|
|
|
|
2011-09-07 08:31:03 +00:00
|
|
|
void PaintedWindow::exposeEvent(QExposeEvent *)
|
|
|
|
{
|
|
|
|
paint();
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
2011-10-04 11:11:30 +00:00
|
|
|
void PaintedWindow::mousePressEvent(QMouseEvent *)
|
|
|
|
{
|
2012-01-13 09:31:11 +00:00
|
|
|
Qt::ScreenOrientation o = contentOrientation();
|
2011-10-04 11:11:30 +00:00
|
|
|
switch (o) {
|
|
|
|
case Qt::LandscapeOrientation:
|
2011-11-23 08:36:51 +00:00
|
|
|
orientationChanged(Qt::PortraitOrientation);
|
2011-10-04 11:11:30 +00:00
|
|
|
break;
|
|
|
|
case Qt::PortraitOrientation:
|
2011-11-23 08:36:51 +00:00
|
|
|
orientationChanged(Qt::InvertedLandscapeOrientation);
|
2011-10-04 11:11:30 +00:00
|
|
|
break;
|
|
|
|
case Qt::InvertedLandscapeOrientation:
|
2011-11-23 08:36:51 +00:00
|
|
|
orientationChanged(Qt::InvertedPortraitOrientation);
|
2011-10-04 11:11:30 +00:00
|
|
|
break;
|
|
|
|
case Qt::InvertedPortraitOrientation:
|
2011-11-23 08:36:51 +00:00
|
|
|
orientationChanged(Qt::LandscapeOrientation);
|
2011-10-04 11:11:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
paint();
|
|
|
|
}
|
|
|
|
|
2011-11-23 08:36:51 +00:00
|
|
|
void PaintedWindow::orientationChanged(Qt::ScreenOrientation newOrientation)
|
|
|
|
{
|
2012-01-13 09:31:11 +00:00
|
|
|
if (contentOrientation() == newOrientation)
|
2011-11-23 08:36:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_animation->state() == QAbstractAnimation::Running) {
|
|
|
|
m_nextTargetOrientation = newOrientation;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect rect(0, 0, width(), height());
|
|
|
|
|
|
|
|
m_prevImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
m_nextImage = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
m_prevImage.fill(0);
|
|
|
|
m_nextImage.fill(0);
|
|
|
|
|
|
|
|
QPainter p;
|
|
|
|
p.begin(&m_prevImage);
|
2012-01-13 09:31:11 +00:00
|
|
|
p.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect));
|
|
|
|
paint(&p, screen()->mapBetween(contentOrientation(), windowOrientation(), rect));
|
2011-11-23 08:36:51 +00:00
|
|
|
p.end();
|
|
|
|
|
|
|
|
p.begin(&m_nextImage);
|
2012-01-13 09:31:11 +00:00
|
|
|
p.setTransform(screen()->transformBetween(newOrientation, windowOrientation(), rect));
|
|
|
|
paint(&p, screen()->mapBetween(newOrientation, windowOrientation(), rect));
|
2011-11-23 08:36:51 +00:00
|
|
|
p.end();
|
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
m_deltaRotation = screen()->angleBetween(newOrientation, contentOrientation());
|
2011-11-23 08:36:51 +00:00
|
|
|
if (m_deltaRotation > 180)
|
|
|
|
m_deltaRotation = 180 - m_deltaRotation;
|
|
|
|
|
|
|
|
m_targetOrientation = newOrientation;
|
|
|
|
m_animation->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaintedWindow::rotationDone()
|
|
|
|
{
|
2012-01-13 09:31:11 +00:00
|
|
|
reportContentOrientationChange(m_targetOrientation);
|
|
|
|
if (m_nextTargetOrientation != Qt::PrimaryOrientation) {
|
2011-11-23 08:36:51 +00:00
|
|
|
Q_ASSERT(m_animation->state() != QAbstractAnimation::Running);
|
|
|
|
orientationChanged(m_nextTargetOrientation);
|
2012-01-13 09:31:11 +00:00
|
|
|
m_nextTargetOrientation = Qt::PrimaryOrientation;
|
2011-11-23 08:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaintedWindow::setRotation(qreal r)
|
|
|
|
{
|
|
|
|
if (r != m_rotation) {
|
|
|
|
m_rotation = r;
|
|
|
|
emit rotationChanged(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
void PaintedWindow::paint()
|
|
|
|
{
|
|
|
|
m_context->makeCurrent(this);
|
|
|
|
|
2011-10-04 11:11:30 +00:00
|
|
|
QRect rect(0, 0, width(), height());
|
|
|
|
|
2011-11-23 08:36:51 +00:00
|
|
|
QOpenGLPaintDevice device(size());
|
2011-09-07 08:31:03 +00:00
|
|
|
QPainter painter(&device);
|
2011-11-23 08:36:51 +00:00
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.addEllipse(rect);
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
painter.fillRect(rect, Qt::transparent);
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
2011-08-22 08:49:28 +00:00
|
|
|
painter.fillPath(path, Qt::blue);
|
2011-11-23 08:36:51 +00:00
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
if (contentOrientation() != m_targetOrientation) {
|
2011-11-23 08:36:51 +00:00
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter.save();
|
|
|
|
painter.translate(width() / 2, height() / 2);
|
|
|
|
painter.rotate(m_deltaRotation * m_rotation);
|
|
|
|
painter.translate(-width() / 2, -height() / 2);
|
|
|
|
painter.drawImage(0, 0, m_prevImage);
|
|
|
|
painter.restore();
|
|
|
|
painter.translate(width() / 2, height() / 2);
|
|
|
|
painter.rotate(m_deltaRotation * m_rotation - m_deltaRotation);
|
|
|
|
painter.translate(-width() / 2, -height() / 2);
|
|
|
|
painter.setOpacity(m_rotation);
|
|
|
|
painter.drawImage(0, 0, m_nextImage);
|
|
|
|
} else {
|
2012-01-13 09:31:11 +00:00
|
|
|
QRect mapped = screen()->mapBetween(contentOrientation(), windowOrientation(), rect);
|
2011-11-23 08:36:51 +00:00
|
|
|
|
2012-01-13 09:31:11 +00:00
|
|
|
painter.setTransform(screen()->transformBetween(contentOrientation(), windowOrientation(), rect));
|
2011-11-23 08:36:51 +00:00
|
|
|
paint(&painter, mapped);
|
|
|
|
painter.end();
|
|
|
|
}
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
m_context->swapBuffers(this);
|
|
|
|
}
|
2011-11-23 08:36:51 +00:00
|
|
|
|
|
|
|
void PaintedWindow::paint(QPainter *painter, const QRect &rect)
|
|
|
|
{
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
QFont font;
|
|
|
|
font.setPixelSize(64);
|
|
|
|
painter->setFont(font);
|
|
|
|
painter->drawText(rect, Qt::AlignCenter, "Hello");
|
|
|
|
}
|