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 (pyqtSignal, QPointF, QPropertyAnimation, QRect,
46        QRectF, QState, QStateMachine, Qt)
47from PyQt5.QtGui import QPixmap
48from PyQt5.QtWidgets import (QApplication, QGraphicsScene, QGraphicsView,
49        QGraphicsWidget)
50
51import appchooser_rc
52
53
54class Pixmap(QGraphicsWidget):
55    clicked = pyqtSignal()
56
57    def __init__(self, pix, parent=None):
58        super(Pixmap, self).__init__(parent)
59
60        self.orig = QPixmap(pix)
61        self.p = QPixmap(pix)
62
63    def paint(self, painter, option, widget):
64        painter.drawPixmap(QPointF(), self.p)
65
66    def mousePressEvent(self, ev):
67        self.clicked.emit()
68
69    def setGeometry(self, rect):
70        super(Pixmap, self).setGeometry(rect)
71
72        if rect.size().width() > self.orig.size().width():
73            self.p = self.orig.scaled(rect.size().toSize())
74        else:
75            self.p = QPixmap(self.orig)
76
77
78def createStates(objects, selectedRect, parent):
79    for obj in objects:
80        state = QState(parent)
81        state.assignProperty(obj, 'geometry', selectedRect)
82        parent.addTransition(obj.clicked, state)
83
84
85def createAnimations(objects, machine):
86    for obj in objects:
87        animation = QPropertyAnimation(obj, b'geometry', obj)
88        machine.addDefaultAnimation(animation)
89
90
91if __name__ == '__main__':
92
93    import sys
94
95    app = QApplication(sys.argv)
96
97    p1 = Pixmap(QPixmap(':/digikam.png'))
98    p2 = Pixmap(QPixmap(':/akregator.png'))
99    p3 = Pixmap(QPixmap(':/accessories-dictionary.png'))
100    p4 = Pixmap(QPixmap(':/k3b.png'))
101
102    p1.setGeometry(QRectF(0.0, 0.0, 64.0, 64.0))
103    p2.setGeometry(QRectF(236.0, 0.0, 64.0, 64.0))
104    p3.setGeometry(QRectF(236.0, 236.0, 64.0, 64.0))
105    p4.setGeometry(QRectF(0.0, 236.0, 64.0, 64.0))
106
107    scene = QGraphicsScene(0, 0, 300, 300)
108    scene.setBackgroundBrush(Qt.white)
109    scene.addItem(p1)
110    scene.addItem(p2)
111    scene.addItem(p3)
112    scene.addItem(p4)
113
114    window = QGraphicsView(scene)
115    window.setFrameStyle(0)
116    window.setAlignment(Qt.AlignLeft | Qt.AlignTop)
117    window.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
118    window.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
119
120    machine = QStateMachine()
121    machine.setGlobalRestorePolicy(QStateMachine.RestoreProperties)
122
123    group = QState(machine)
124    selectedRect = QRect(86, 86, 128, 128)
125
126    idleState = QState(group)
127    group.setInitialState(idleState)
128
129    objects = [p1, p2, p3, p4]
130    createStates(objects, selectedRect, group)
131    createAnimations(objects, machine)
132
133    machine.setInitialState(group)
134    machine.start()
135
136    window.resize(300, 300)
137    window.show()
138
139    sys.exit(app.exec_())
140