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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#!/usr/bin/env python
# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui
import easing_rc
from ui_form import Ui_Form
class Animation(QtCore.QPropertyAnimation):
LinearPath, CirclePath = range(2)
def __init__(self, target, prop):
super(Animation, self).__init__(target, prop)
self.setPathType(Animation.LinearPath)
def setPathType(self, pathType):
self.m_pathType = pathType
self.m_path = QtGui.QPainterPath()
def updateCurrentTime(self, currentTime):
if self.m_pathType == Animation.CirclePath:
if self.m_path.isEmpty():
end = self.endValue()
start = self.startValue()
self.m_path.moveTo(start)
self.m_path.addEllipse(QtCore.QRectF(start, end))
dura = self.duration()
if dura == 0:
progress = 1.0
else:
progress = (((currentTime - 1) % dura) + 1) / float(dura)
easedProgress = self.easingCurve().valueForProgress(progress)
if easedProgress > 1.0:
easedProgress -= 1.0
elif easedProgress < 0:
easedProgress += 1.0
pt = self.m_path.pointAtPercent(easedProgress)
self.updateCurrentValue(pt)
self.valueChanged.emit(pt)
else:
super(Animation, self).updateCurrentTime(currentTime)
# PyQt doesn't support deriving from more than one wrapped class so we use
# composition and delegate the property.
class PixmapItem(QtCore.QObject):
def __init__(self, pix):
super(PixmapItem, self).__init__()
self.pixmap_item = QtGui.QGraphicsPixmapItem(pix)
def _set_pos(self, pos):
self.pixmap_item.setPos(pos)
pos = QtCore.pyqtProperty(QtCore.QPointF, fset=_set_pos)
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(QtGui.QWidget, self).__init__(parent)
self.m_iconSize = QtCore.QSize(64, 64)
self.m_scene = QtGui.QGraphicsScene()
self.m_ui = Ui_Form()
self.m_ui.setupUi(self)
self.m_ui.easingCurvePicker.setIconSize(self.m_iconSize)
self.m_ui.easingCurvePicker.setMinimumHeight(self.m_iconSize.height() + 50)
self.m_ui.buttonGroup.setId(self.m_ui.lineRadio, 0)
self.m_ui.buttonGroup.setId(self.m_ui.circleRadio, 1)
dummy = QtCore.QEasingCurve()
self.m_ui.periodSpinBox.setValue(dummy.period())
self.m_ui.amplitudeSpinBox.setValue(dummy.amplitude())
self.m_ui.overshootSpinBox.setValue(dummy.overshoot())
self.m_ui.easingCurvePicker.currentRowChanged.connect(self.curveChanged)
self.m_ui.buttonGroup.buttonClicked[int].connect(self.pathChanged)
self.m_ui.periodSpinBox.valueChanged.connect(self.periodChanged)
self.m_ui.amplitudeSpinBox.valueChanged.connect(self.amplitudeChanged)
self.m_ui.overshootSpinBox.valueChanged.connect(self.overshootChanged)
self.createCurveIcons()
pix = QtGui.QPixmap(':/images/qt-logo.png')
self.m_item = PixmapItem(pix)
self.m_scene.addItem(self.m_item.pixmap_item)
self.m_ui.graphicsView.setScene(self.m_scene)
self.m_anim = Animation(self.m_item, 'pos')
self.m_anim.setEasingCurve(QtCore.QEasingCurve.OutBounce)
self.m_ui.easingCurvePicker.setCurrentRow(int(QtCore.QEasingCurve.OutBounce))
self.startAnimation()
def createCurveIcons(self):
pix = QtGui.QPixmap(self.m_iconSize)
painter = QtGui.QPainter()
gradient = QtGui.QLinearGradient(0, 0, 0, self.m_iconSize.height())
gradient.setColorAt(0.0, QtGui.QColor(240, 240, 240))
gradient.setColorAt(1.0, QtGui.QColor(224, 224, 224))
brush = QtGui.QBrush(gradient)
# The original C++ code uses undocumented calls to get the names of the
# different curve types. We do the Python equivalant (but without
# cheating).
curve_types = [(n, c) for n, c in QtCore.QEasingCurve.__dict__.items()
if isinstance(c, QtCore.QEasingCurve.Type) and c != QtCore.QEasingCurve.Custom]
curve_types.sort(key=lambda ct: ct[1])
painter.begin(pix)
for curve_name, curve_type in curve_types:
painter.fillRect(
QtCore.QRect(QtCore.QPoint(0, 0), self.m_iconSize), brush)
curve = QtCore.QEasingCurve(curve_type)
painter.setPen(QtGui.QColor(0, 0, 255, 64))
xAxis = self.m_iconSize.height() / 1.5
yAxis = self.m_iconSize.width() / 3.0
painter.drawLine(0, xAxis, self.m_iconSize.width(), xAxis)
painter.drawLine(yAxis, 0, yAxis, self.m_iconSize.height())
curveScale = self.m_iconSize.height() / 2.0;
painter.setPen(QtCore.Qt.NoPen)
# Start point.
painter.setBrush(QtCore.Qt.red)
start = QtCore.QPoint(yAxis,
xAxis - curveScale * curve.valueForProgress(0))
painter.drawRect(start.x() - 1, start.y() - 1, 3, 3)
# End point.
painter.setBrush(QtCore.Qt.blue)
end = QtCore.QPoint(yAxis + curveScale,
xAxis - curveScale * curve.valueForProgress(1))
painter.drawRect(end.x() - 1, end.y() - 1, 3, 3)
curvePath = QtGui.QPainterPath()
curvePath.moveTo(QtCore.QPointF(start))
t = 0.0
while t <= 1.0:
to = QtCore.QPointF(yAxis + curveScale * t,
xAxis - curveScale * curve.valueForProgress(t))
curvePath.lineTo(to)
t += 1.0 / curveScale
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.strokePath(curvePath, QtGui.QColor(32, 32, 32))
painter.setRenderHint(QtGui.QPainter.Antialiasing, False)
item = QtGui.QListWidgetItem()
item.setIcon(QtGui.QIcon(pix))
item.setText(curve_name)
self.m_ui.easingCurvePicker.addItem(item)
painter.end()
def startAnimation(self):
self.m_anim.setStartValue(QtCore.QPointF(0, 0))
self.m_anim.setEndValue(QtCore.QPointF(100, 100))
self.m_anim.setDuration(2000)
self.m_anim.setLoopCount(-1)
self.m_anim.start()
def curveChanged(self, row):
curveType = QtCore.QEasingCurve.Type(row)
self.m_anim.setEasingCurve(curveType)
self.m_anim.setCurrentTime(0)
isElastic = (curveType >= QtCore.QEasingCurve.InElastic and curveType <= QtCore.QEasingCurve.OutInElastic)
isBounce = (curveType >= QtCore.QEasingCurve.InBounce and curveType <= QtCore.QEasingCurve.OutInBounce)
self.m_ui.periodSpinBox.setEnabled(isElastic)
self.m_ui.amplitudeSpinBox.setEnabled(isElastic or isBounce)
self.m_ui.overshootSpinBox.setEnabled(curveType >= QtCore.QEasingCurve.InBack and curveType <= QtCore.QEasingCurve.OutInBack)
def pathChanged(self, index):
self.m_anim.setPathType(index)
def periodChanged(self, value):
curve = self.m_anim.easingCurve()
curve.setPeriod(value)
self.m_anim.setEasingCurve(curve)
def amplitudeChanged(self, value):
curve = self.m_anim.easingCurve()
curve.setAmplitude(value)
self.m_anim.setEasingCurve(curve)
def overshootChanged(self, value):
curve = self.m_anim.easingCurve()
curve.setOvershoot(value)
self.m_anim.setEasingCurve(curve)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Window()
w.resize(400, 400)
w.show()
sys.exit(app.exec_())
|