-
Notifications
You must be signed in to change notification settings - Fork 25
/
parallaxslide.py
191 lines (149 loc) · 6.22 KB
/
parallaxslide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#############################################################################
##
## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
## Contact: Qt Software Information ([email protected])
##
## This file is part of the Graphics Dojo project on Qt Labs.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 or 3.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file. Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.fsf.org/licensing/licenses/info/GPLv2.html and
## http://www.gnu.org/copyleft/gpl.html.
##
## If you are unsure which license is appropriate for your use, please
## contact the sales department at [email protected].
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################
import sys
from PyQt4.QtCore import pyqtSignature, QString, Qt, QTimeLine, SIGNAL, SLOT
from PyQt4.QtGui import *
exit = False
try:
from ui_controls import Ui_ControlsForm
except ImportError:
sys.stderr.write(
"Please process the controls.ui file before running this program. "
"For example with:\n\n"
"pyuic4 -o ui_controls.py controls.ui\n\n"
)
exit = True
try:
import parallaxslide_rc
except ImportError:
sys.stderr.write(
"Please process the parallaxslide.qrc file before running this program. "
"For example with:\n\n"
"pyrcc4 -o parallaxslide_rc.py parallaxslide.qrc\n\n"
)
exit = True
if exit:
sys.exit(1)
class ParallaxSlide(QGraphicsView):
def __init__(self):
QGraphicsView.__init__(self)
self.ofs = 0
self.factor = 1
self.scene = QGraphicsScene()
self.background = None
self.icons = []
self.iconTimeLine = QTimeLine()
self.backgroundTimeLine = QTimeLine()
self.setScene(self.scene)
self.background = self.scene.addPixmap(QPixmap(":/background.jpg"))
self.background.setZValue(0.0)
self.background.setPos(0, 0)
for i in range(7):
str = QString(":/icon%1.png").arg(i+1)
icon = self.scene.addPixmap(QPixmap(str))
icon.setPos(320+i*64, 400)
icon.setZValue(1.0)
self.icons.append(icon)
self.setFixedSize(320, 480)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.connect(self.iconTimeLine, SIGNAL("frameChanged(int)"), self, SLOT("moveIcons(int)"))
self.iconTimeLine.setCurveShape(QTimeLine.EaseInOutCurve)
self.connect(self.backgroundTimeLine, SIGNAL("frameChanged(int)"), self, SLOT("moveBackground(int)"))
self.connect(self.backgroundTimeLine, SIGNAL("finished()"), self, SLOT("adjustParameters()"))
self.backgroundTimeLine.setCurveShape(QTimeLine.EaseInOutCurve)
self.controls = Ui_ControlsForm()
toolWidget = QWidget(self)
toolWidget.setWindowFlags(Qt.Tool | Qt.WindowTitleHint)
self.controls.setupUi(toolWidget)
toolWidget.show()
self.connect(self.controls.speedSlider, SIGNAL("valueChanged(int)"),
self, SLOT("adjustParameters()"))
self.connect(self.controls.normalButton, SIGNAL("clicked()"),
self, SLOT("adjustParameters()"))
self.connect(self.controls.parallaxButton, SIGNAL("clicked()"),
self, SLOT("adjustParameters()"))
self.connect(self.controls.leftButton, SIGNAL("clicked()"),
self, SLOT("slideLeft()"))
self.connect(self.controls.rightButton, SIGNAL("clicked()"),
self, SLOT("slideRight()"))
self.slideBy(-320)
self.adjustParameters()
@pyqtSignature("")
def slideLeft(self):
if self.iconTimeLine.state() != QTimeLine.NotRunning:
return
if self.ofs > -640:
self.slideBy(-320)
@pyqtSignature("")
def slideRight(self):
if self.iconTimeLine.state() != QTimeLine.NotRunning:
return
if self.ofs < 0:
self.slideBy(320)
@pyqtSignature("int")
def slideBy(self, dx):
iconStart = self.ofs
iconEnd = self.ofs + dx
self.iconTimeLine.setFrameRange(iconStart, iconEnd)
self.iconTimeLine.start()
backgroundStart = -320 - int((-320 - iconStart)/self.factor)
backgroundEnd = -320 - int((-320 - iconEnd)/self.factor)
self.backgroundTimeLine.setFrameRange(backgroundStart, backgroundEnd)
self.backgroundTimeLine.start()
self.ofs = iconEnd
@pyqtSignature("bool")
def setParallaxEnabled(self, p):
if p:
self.factor = 2
self.setWindowTitle("Sliding - Parallax mode")
else:
self.factor = 1
self.setWindowTitle("Sliding - Normal mode")
def keyPressEvent(self, event):
if event.key() == Qt.Key_Left:
self.slideLeft()
if event.key() == Qt.Key_Right:
self.slideRight()
@pyqtSignature("int")
def moveIcons(self, x):
i = 0
for icon in self.icons:
icon.setPos(320 + x+i*64, icon.pos().y())
i += 1
@pyqtSignature("int")
def moveBackground(self, x):
self.background.setPos(x, self.background.pos().y())
@pyqtSignature("")
def adjustParameters(self):
speed = self.controls.speedSlider.value()
self.iconTimeLine.setDuration(1200 - speed*10)
self.backgroundTimeLine.setDuration(1200 - speed*10)
self.setParallaxEnabled(self.controls.parallaxButton.isChecked())
self.controls.leftButton.setEnabled(self.ofs > -640)
self.controls.rightButton.setEnabled(self.ofs < 0)
if __name__ == "__main__":
app = QApplication(sys.argv)
slider = ParallaxSlide()
slider.show()
sys.exit(app.exec_())