1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45import math 46 47from PyQt5.QtCore import QPointF, Qt, QTimer 48from PyQt5.QtGui import (QBrush, QColor, QLinearGradient, QPen, QPainter, 49 QPixmap, QRadialGradient) 50from PyQt5.QtWidgets import (QApplication, QFrame, QGraphicsDropShadowEffect, 51 QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsScene, QGraphicsView) 52 53 54class Lighting(QGraphicsView): 55 def __init__(self, parent=None): 56 super(Lighting, self).__init__(parent) 57 58 self.angle = 0.0 59 self.m_scene = QGraphicsScene() 60 self.m_lightSource = None 61 self.m_items = [] 62 63 self.setScene(self.m_scene) 64 65 self.setupScene() 66 67 timer = QTimer(self) 68 timer.timeout.connect(self.animate) 69 timer.setInterval(30) 70 timer.start() 71 72 self.setRenderHint(QPainter.Antialiasing) 73 self.setFrameStyle(QFrame.NoFrame) 74 75 def setupScene(self): 76 self.m_scene.setSceneRect(-300, -200, 600, 460) 77 78 linearGrad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100)) 79 linearGrad.setColorAt(0, QColor(255, 255, 255)) 80 linearGrad.setColorAt(1, QColor(192, 192, 255)) 81 self.setBackgroundBrush(linearGrad) 82 83 radialGrad = QRadialGradient(30, 30, 30) 84 radialGrad.setColorAt(0, Qt.yellow) 85 radialGrad.setColorAt(0.2, Qt.yellow) 86 radialGrad.setColorAt(1, Qt.transparent) 87 88 pixmap = QPixmap(60, 60) 89 pixmap.fill(Qt.transparent) 90 91 painter = QPainter(pixmap) 92 painter.setPen(Qt.NoPen) 93 painter.setBrush(radialGrad) 94 painter.drawEllipse(0, 0, 60, 60) 95 painter.end() 96 97 self.m_lightSource = self.m_scene.addPixmap(pixmap) 98 self.m_lightSource.setZValue(2) 99 100 for i in range(-2, 3): 101 for j in range(-2, 3): 102 if (i + j) & 1: 103 item = QGraphicsEllipseItem(0, 0, 50, 50) 104 else: 105 item = QGraphicsRectItem(0, 0, 50, 50) 106 107 item.setPen(QPen(Qt.black, 1)) 108 item.setBrush(QBrush(Qt.white)) 109 110 effect = QGraphicsDropShadowEffect(self) 111 effect.setBlurRadius(8) 112 item.setGraphicsEffect(effect) 113 item.setZValue(1) 114 item.setPos(i * 80, j * 80) 115 self.m_scene.addItem(item) 116 self.m_items.append(item) 117 118 def animate(self): 119 self.angle += (math.pi / 30) 120 xs = 200 * math.sin(self.angle) - 40 + 25 121 ys = 200 * math.cos(self.angle) - 40 + 25 122 self.m_lightSource.setPos(xs, ys) 123 124 for item in self.m_items: 125 effect = item.graphicsEffect() 126 127 delta = QPointF(item.x() - xs, item.y() - ys) 128 effect.setOffset(QPointF(delta.toPoint() / 30)) 129 130 dd = math.hypot(delta.x(), delta.y()) 131 color = effect.color() 132 color.setAlphaF(max(0.4, min(1 - dd / 200.0, 0.7))) 133 effect.setColor(color) 134 135 self.m_scene.update() 136 137 138if __name__ == '__main__': 139 140 import sys 141 142 app = QApplication(sys.argv) 143 144 lighting = Lighting() 145 lighting.setWindowTitle("Lighting and Shadows") 146 lighting.resize(640, 480) 147 lighting.show() 148 149 sys.exit(app.exec_()) 150