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 45from PyQt5.QtCore import (QPointF, QPropertyAnimation, QRect, QRectF, 46 QSequentialAnimationGroup, QSizeF, QState, QStateMachine, Qt) 47from PyQt5.QtGui import QPixmap 48from PyQt5.QtWidgets import (QApplication, QGraphicsLinearLayout, 49 QGraphicsObject, QGraphicsProxyWidget, QGraphicsScene, QGraphicsView, 50 QGraphicsWidget, QGroupBox, QPushButton, QRadioButton, QTextEdit, 51 QVBoxLayout) 52 53import states_rc 54 55 56class Pixmap(QGraphicsObject): 57 def __init__(self, pix): 58 super(Pixmap, self).__init__() 59 60 self.p = QPixmap(pix) 61 62 def paint(self, painter, option, widget): 63 painter.drawPixmap(QPointF(), self.p) 64 65 def boundingRect(self): 66 return QRectF(QPointF(0, 0), QSizeF(self.p.size())) 67 68 69if __name__ == '__main__': 70 71 import sys 72 73 app = QApplication(sys.argv) 74 75 # Text edit and button. 76 edit = QTextEdit() 77 edit.setText("asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " 78 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " 79 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " 80 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy!") 81 82 button = QPushButton() 83 buttonProxy = QGraphicsProxyWidget() 84 buttonProxy.setWidget(button) 85 editProxy = QGraphicsProxyWidget() 86 editProxy.setWidget(edit) 87 88 box = QGroupBox() 89 box.setFlat(True) 90 box.setTitle("Options") 91 92 layout2 = QVBoxLayout() 93 box.setLayout(layout2) 94 layout2.addWidget(QRadioButton("Herring")) 95 layout2.addWidget(QRadioButton("Blue Parrot")) 96 layout2.addWidget(QRadioButton("Petunias")) 97 layout2.addStretch() 98 99 boxProxy = QGraphicsProxyWidget() 100 boxProxy.setWidget(box) 101 102 # Parent widget. 103 widget = QGraphicsWidget() 104 layout = QGraphicsLinearLayout(Qt.Vertical, widget) 105 layout.addItem(editProxy) 106 layout.addItem(buttonProxy) 107 widget.setLayout(layout) 108 109 p1 = Pixmap(QPixmap(':/digikam.png')) 110 p2 = Pixmap(QPixmap(':/akregator.png')) 111 p3 = Pixmap(QPixmap(':/accessories-dictionary.png')) 112 p4 = Pixmap(QPixmap(':/k3b.png')) 113 p5 = Pixmap(QPixmap(':/help-browser.png')) 114 p6 = Pixmap(QPixmap(':/kchart.png')) 115 116 scene = QGraphicsScene(0, 0, 400, 300) 117 scene.setBackgroundBrush(scene.palette().window()) 118 scene.addItem(widget) 119 scene.addItem(boxProxy) 120 scene.addItem(p1) 121 scene.addItem(p2) 122 scene.addItem(p3) 123 scene.addItem(p4) 124 scene.addItem(p5) 125 scene.addItem(p6) 126 127 machine = QStateMachine() 128 state1 = QState(machine) 129 state2 = QState(machine) 130 state3 = QState(machine) 131 machine.setInitialState(state1) 132 133 # State 1. 134 state1.assignProperty(button, 'text', "Switch to state 2") 135 state1.assignProperty(widget, 'geometry', QRectF(0, 0, 400, 150)) 136 state1.assignProperty(box, 'geometry', QRect(-200, 150, 200, 150)) 137 state1.assignProperty(p1, 'pos', QPointF(68, 185)) 138 state1.assignProperty(p2, 'pos', QPointF(168, 185)) 139 state1.assignProperty(p3, 'pos', QPointF(268, 185)) 140 state1.assignProperty(p4, 'pos', QPointF(68 - 150, 48 - 150)) 141 state1.assignProperty(p5, 'pos', QPointF(168, 48 - 150)) 142 state1.assignProperty(p6, 'pos', QPointF(268 + 150, 48 - 150)) 143 state1.assignProperty(p1, 'rotation', 0.0) 144 state1.assignProperty(p2, 'rotation', 0.0) 145 state1.assignProperty(p3, 'rotation', 0.0) 146 state1.assignProperty(p4, 'rotation', -270.0) 147 state1.assignProperty(p5, 'rotation', -90.0) 148 state1.assignProperty(p6, 'rotation', 270.0) 149 state1.assignProperty(boxProxy, 'opacity', 0.0) 150 state1.assignProperty(p1, 'opacity', 1.0) 151 state1.assignProperty(p2, 'opacity', 1.0) 152 state1.assignProperty(p3, 'opacity', 1.0) 153 state1.assignProperty(p4, 'opacity', 0.0) 154 state1.assignProperty(p5, 'opacity', 0.0) 155 state1.assignProperty(p6, 'opacity', 0.0) 156 157 # State 2. 158 state2.assignProperty(button, 'text', "Switch to state 3") 159 state2.assignProperty(widget, 'geometry', QRectF(200, 150, 200, 150)) 160 state2.assignProperty(box, 'geometry', QRect(9, 150, 190, 150)) 161 state2.assignProperty(p1, 'pos', QPointF(68 - 150, 185 + 150)) 162 state2.assignProperty(p2, 'pos', QPointF(168, 185 + 150)) 163 state2.assignProperty(p3, 'pos', QPointF(268 + 150, 185 + 150)) 164 state2.assignProperty(p4, 'pos', QPointF(64, 48)) 165 state2.assignProperty(p5, 'pos', QPointF(168, 48)) 166 state2.assignProperty(p6, 'pos', QPointF(268, 48)) 167 state2.assignProperty(p1, 'rotation', -270.0) 168 state2.assignProperty(p2, 'rotation', 90.0) 169 state2.assignProperty(p3, 'rotation', 270.0) 170 state2.assignProperty(p4, 'rotation', 0.0) 171 state2.assignProperty(p5, 'rotation', 0.0) 172 state2.assignProperty(p6, 'rotation', 0.0) 173 state2.assignProperty(boxProxy, 'opacity', 1.0) 174 state2.assignProperty(p1, 'opacity', 0.0) 175 state2.assignProperty(p2, 'opacity', 0.0) 176 state2.assignProperty(p3, 'opacity', 0.0) 177 state2.assignProperty(p4, 'opacity', 1.0) 178 state2.assignProperty(p5, 'opacity', 1.0) 179 state2.assignProperty(p6, 'opacity', 1.0) 180 181 # State 3. 182 state3.assignProperty(button, 'text', "Switch to state 1") 183 state3.assignProperty(p1, 'pos', QPointF(0, 5)) 184 state3.assignProperty(p2, 'pos', QPointF(0, 5 + 64 + 5)) 185 state3.assignProperty(p3, 'pos', QPointF(5, 5 + (64 + 5) + 64)) 186 state3.assignProperty(p4, 'pos', QPointF(5 + 64 + 5, 5)) 187 state3.assignProperty(p5, 'pos', QPointF(5 + 64 + 5, 5 + 64 + 5)) 188 state3.assignProperty(p6, 'pos', QPointF(5 + 64 + 5, 5 + (64 + 5) + 64)) 189 state3.assignProperty(widget, 'geometry', QRectF(138, 5, 400 - 138, 200)) 190 state3.assignProperty(box, 'geometry', QRect(5, 205, 400, 90)) 191 state3.assignProperty(p1, 'opacity', 1.0) 192 state3.assignProperty(p2, 'opacity', 1.0) 193 state3.assignProperty(p3, 'opacity', 1.0) 194 state3.assignProperty(p4, 'opacity', 1.0) 195 state3.assignProperty(p5, 'opacity', 1.0) 196 state3.assignProperty(p6, 'opacity', 1.0) 197 198 t1 = state1.addTransition(button.clicked, state2) 199 animation1SubGroup = QSequentialAnimationGroup() 200 animation1SubGroup.addPause(250) 201 animation1SubGroup.addAnimation(QPropertyAnimation(box, b'geometry', state1)) 202 t1.addAnimation(animation1SubGroup) 203 t1.addAnimation(QPropertyAnimation(widget, b'geometry', state1)) 204 t1.addAnimation(QPropertyAnimation(p1, b'pos', state1)) 205 t1.addAnimation(QPropertyAnimation(p2, b'pos', state1)) 206 t1.addAnimation(QPropertyAnimation(p3, b'pos', state1)) 207 t1.addAnimation(QPropertyAnimation(p4, b'pos', state1)) 208 t1.addAnimation(QPropertyAnimation(p5, b'pos', state1)) 209 t1.addAnimation(QPropertyAnimation(p6, b'pos', state1)) 210 t1.addAnimation(QPropertyAnimation(p1, b'rotation', state1)) 211 t1.addAnimation(QPropertyAnimation(p2, b'rotation', state1)) 212 t1.addAnimation(QPropertyAnimation(p3, b'rotation', state1)) 213 t1.addAnimation(QPropertyAnimation(p4, b'rotation', state1)) 214 t1.addAnimation(QPropertyAnimation(p5, b'rotation', state1)) 215 t1.addAnimation(QPropertyAnimation(p6, b'rotation', state1)) 216 t1.addAnimation(QPropertyAnimation(p1, b'opacity', state1)) 217 t1.addAnimation(QPropertyAnimation(p2, b'opacity', state1)) 218 t1.addAnimation(QPropertyAnimation(p3, b'opacity', state1)) 219 t1.addAnimation(QPropertyAnimation(p4, b'opacity', state1)) 220 t1.addAnimation(QPropertyAnimation(p5, b'opacity', state1)) 221 t1.addAnimation(QPropertyAnimation(p6, b'opacity', state1)) 222 223 t2 = state2.addTransition(button.clicked, state3) 224 t2.addAnimation(QPropertyAnimation(box, b'geometry', state2)) 225 t2.addAnimation(QPropertyAnimation(widget, b'geometry', state2)) 226 t2.addAnimation(QPropertyAnimation(p1, b'pos', state2)) 227 t2.addAnimation(QPropertyAnimation(p2, b'pos', state2)) 228 t2.addAnimation(QPropertyAnimation(p3, b'pos', state2)) 229 t2.addAnimation(QPropertyAnimation(p4, b'pos', state2)) 230 t2.addAnimation(QPropertyAnimation(p5, b'pos', state2)) 231 t2.addAnimation(QPropertyAnimation(p6, b'pos', state2)) 232 t2.addAnimation(QPropertyAnimation(p1, b'rotation', state2)) 233 t2.addAnimation(QPropertyAnimation(p2, b'rotation', state2)) 234 t2.addAnimation(QPropertyAnimation(p3, b'rotation', state2)) 235 t2.addAnimation(QPropertyAnimation(p4, b'rotation', state2)) 236 t2.addAnimation(QPropertyAnimation(p5, b'rotation', state2)) 237 t2.addAnimation(QPropertyAnimation(p6, b'rotation', state2)) 238 t2.addAnimation(QPropertyAnimation(p1, b'opacity', state2)) 239 t2.addAnimation(QPropertyAnimation(p2, b'opacity', state2)) 240 t2.addAnimation(QPropertyAnimation(p3, b'opacity', state2)) 241 t2.addAnimation(QPropertyAnimation(p4, b'opacity', state2)) 242 t2.addAnimation(QPropertyAnimation(p5, b'opacity', state2)) 243 t2.addAnimation(QPropertyAnimation(p6, b'opacity', state2)) 244 245 t3 = state3.addTransition(button.clicked, state1) 246 t3.addAnimation(QPropertyAnimation(box, b'geometry', state3)) 247 t3.addAnimation(QPropertyAnimation(widget, b'geometry', state3)) 248 t3.addAnimation(QPropertyAnimation(p1, b'pos', state3)) 249 t3.addAnimation(QPropertyAnimation(p2, b'pos', state3)) 250 t3.addAnimation(QPropertyAnimation(p3, b'pos', state3)) 251 t3.addAnimation(QPropertyAnimation(p4, b'pos', state3)) 252 t3.addAnimation(QPropertyAnimation(p5, b'pos', state3)) 253 t3.addAnimation(QPropertyAnimation(p6, b'pos', state3)) 254 t3.addAnimation(QPropertyAnimation(p1, b'rotation', state3)) 255 t3.addAnimation(QPropertyAnimation(p2, b'rotation', state3)) 256 t3.addAnimation(QPropertyAnimation(p3, b'rotation', state3)) 257 t3.addAnimation(QPropertyAnimation(p4, b'rotation', state3)) 258 t3.addAnimation(QPropertyAnimation(p5, b'rotation', state3)) 259 t3.addAnimation(QPropertyAnimation(p6, b'rotation', state3)) 260 t3.addAnimation(QPropertyAnimation(p1, b'opacity', state3)) 261 t3.addAnimation(QPropertyAnimation(p2, b'opacity', state3)) 262 t3.addAnimation(QPropertyAnimation(p3, b'opacity', state3)) 263 t3.addAnimation(QPropertyAnimation(p4, b'opacity', state3)) 264 t3.addAnimation(QPropertyAnimation(p5, b'opacity', state3)) 265 t3.addAnimation(QPropertyAnimation(p6, b'opacity', state3)) 266 267 machine.start() 268 269 view = QGraphicsView(scene) 270 view.show() 271 272 sys.exit(app.exec_()) 273