| 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 |  | 
| 63 | PaintedWindow::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(orientation: 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(orientation: screen()->orientation()); | 
| 93 |  | 
| 94 |     m_targetOrientation = contentOrientation(); | 
| 95 |     m_nextTargetOrientation = Qt::PrimaryOrientation; | 
| 96 |  | 
| 97 |     connect(sender: screen(), signal: &QScreen::orientationChanged, receiver: this, slot: &PaintedWindow::orientationChanged); | 
| 98 |     connect(sender: m_animation, signal: &QAbstractAnimation::finished, receiver: this, slot: &PaintedWindow::rotationDone); | 
| 99 |     connect(sender: this, signal: &PaintedWindow::rotationChanged, receiver: this, slot: QOverload<>::of(ptr: &PaintedWindow::paint)); | 
| 100 | } | 
| 101 |  | 
| 102 | void PaintedWindow::exposeEvent(QExposeEvent *) | 
| 103 | { | 
| 104 |     if (isExposed()) | 
| 105 |         paint(); | 
| 106 | } | 
| 107 |  | 
| 108 | void PaintedWindow::mousePressEvent(QMouseEvent *) | 
| 109 | { | 
| 110 |     Qt::ScreenOrientation o = contentOrientation(); | 
| 111 |     switch (o) { | 
| 112 |     case Qt::LandscapeOrientation: | 
| 113 |         orientationChanged(newOrientation: Qt::PortraitOrientation); | 
| 114 |         break; | 
| 115 |     case Qt::PortraitOrientation: | 
| 116 |         orientationChanged(newOrientation: Qt::InvertedLandscapeOrientation); | 
| 117 |         break; | 
| 118 |     case Qt::InvertedLandscapeOrientation: | 
| 119 |         orientationChanged(newOrientation: Qt::InvertedPortraitOrientation); | 
| 120 |         break; | 
| 121 |     case Qt::InvertedPortraitOrientation: | 
| 122 |         orientationChanged(newOrientation: Qt::LandscapeOrientation); | 
| 123 |         break; | 
| 124 |     default: | 
| 125 |         Q_ASSERT(false); | 
| 126 |     } | 
| 127 |  | 
| 128 |     paint(); | 
| 129 | } | 
| 130 |  | 
| 131 | void 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(pixel: 0); | 
| 146 |     m_nextImage.fill(pixel: 0); | 
| 147 |  | 
| 148 |     QPainter p; | 
| 149 |     p.begin(&m_prevImage); | 
| 150 |     p.setTransform(transform: screen()->transformBetween(a: contentOrientation(), b: screen()->orientation(), target: rect)); | 
| 151 |     paint(painter: &p, rect: screen()->mapBetween(a: contentOrientation(), b: screen()->orientation(), rect)); | 
| 152 |     p.end(); | 
| 153 |  | 
| 154 |     p.begin(&m_nextImage); | 
| 155 |     p.setTransform(transform: screen()->transformBetween(a: newOrientation, b: screen()->orientation(), target: rect)); | 
| 156 |     paint(painter: &p, rect: screen()->mapBetween(a: newOrientation, b: screen()->orientation(), rect)); | 
| 157 |     p.end(); | 
| 158 |  | 
| 159 |     m_deltaRotation = screen()->angleBetween(a: newOrientation, b: contentOrientation()); | 
| 160 |     if (m_deltaRotation > 180) | 
| 161 |         m_deltaRotation = 180 - m_deltaRotation; | 
| 162 |  | 
| 163 |     m_targetOrientation = newOrientation; | 
| 164 |     m_animation->start(); | 
| 165 | } | 
| 166 |  | 
| 167 | void PaintedWindow::rotationDone() | 
| 168 | { | 
| 169 |     reportContentOrientationChange(orientation: m_targetOrientation); | 
| 170 |     if (m_nextTargetOrientation != Qt::PrimaryOrientation) { | 
| 171 |         Q_ASSERT(m_animation->state() != QAbstractAnimation::Running); | 
| 172 |         orientationChanged(newOrientation: m_nextTargetOrientation); | 
| 173 |         m_nextTargetOrientation = Qt::PrimaryOrientation; | 
| 174 |     } | 
| 175 | } | 
| 176 |  | 
| 177 | void PaintedWindow::setRotation(qreal r) | 
| 178 | { | 
| 179 |     if (r != m_rotation) { | 
| 180 |         m_rotation = r; | 
| 181 |         emit rotationChanged(rotation: r); | 
| 182 |     } | 
| 183 | } | 
| 184 |  | 
| 185 | void PaintedWindow::paint() | 
| 186 | { | 
| 187 |     m_context->makeCurrent(surface: 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(r: rect, c: Qt::transparent); | 
| 198 |     painter.setCompositionMode(QPainter::CompositionMode_SourceOver); | 
| 199 |     painter.fillPath(path, brush: Qt::blue); | 
| 200 |  | 
| 201 |     if (contentOrientation() != m_targetOrientation) { | 
| 202 |         painter.setRenderHint(hint: QPainter::SmoothPixmapTransform); | 
| 203 |         painter.save(); | 
| 204 |         painter.translate(dx: width() / 2, dy: height() / 2); | 
| 205 |         painter.rotate(a: m_deltaRotation * m_rotation); | 
| 206 |         painter.translate(dx: -width() / 2, dy: -height() / 2); | 
| 207 |         painter.drawImage(x: 0, y: 0, image: m_prevImage); | 
| 208 |         painter.restore(); | 
| 209 |         painter.translate(dx: width() / 2, dy: height() / 2); | 
| 210 |         painter.rotate(a: m_deltaRotation * m_rotation - m_deltaRotation); | 
| 211 |         painter.translate(dx: -width() / 2, dy: -height() / 2); | 
| 212 |         painter.setOpacity(m_rotation); | 
| 213 |         painter.drawImage(x: 0, y: 0, image: m_nextImage); | 
| 214 |     } else { | 
| 215 |         QRect mapped = screen()->mapBetween(a: contentOrientation(), b: screen()->orientation(), rect); | 
| 216 |  | 
| 217 |         painter.setTransform(transform: screen()->transformBetween(a: contentOrientation(), b: screen()->orientation(), target: rect)); | 
| 218 |         paint(painter: &painter, rect: mapped); | 
| 219 |         painter.end(); | 
| 220 |     } | 
| 221 |  | 
| 222 |     m_context->swapBuffers(surface: this); | 
| 223 | } | 
| 224 |  | 
| 225 | void PaintedWindow::paint(QPainter *painter, const QRect &rect) | 
| 226 | { | 
| 227 |     painter->setRenderHint(hint: QPainter::Antialiasing); | 
| 228 |     QFont font; | 
| 229 |     font.setPixelSize(64); | 
| 230 |     painter->setFont(font); | 
| 231 |     painter->drawText(r: rect, flags: Qt::AlignCenter, text: "Hello" ); | 
| 232 | } | 
| 233 |  |