|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an animated widget. |
|
8 """ |
|
9 |
|
10 # |
|
11 # Code was inspired by qupzilla web browser |
|
12 # |
|
13 |
|
14 from __future__ import unicode_literals |
|
15 |
|
16 from PyQt5.QtCore import pyqtSlot, QTimeLine, QPoint |
|
17 from PyQt5.QtWidgets import QWidget |
|
18 |
|
19 |
|
20 class E5AnimatedWidget(QWidget): |
|
21 """ |
|
22 Class implementing an animated widget. |
|
23 """ |
|
24 DirectionDown = 0 |
|
25 DirectionUp = 1 |
|
26 |
|
27 def __init__(self, direction=DirectionDown, duration=300, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param direction direction of the animation |
|
32 @type int (one of DirectionDown or DirectionUp) |
|
33 @param duration duration of the animation |
|
34 @type int |
|
35 @param parent reference to the parent widget |
|
36 @type QWidget |
|
37 """ |
|
38 super(E5AnimatedWidget, self).__init__(parent) |
|
39 |
|
40 self.__direction = direction |
|
41 self.__stepHeight = 0.0 |
|
42 self.__stepY = 0.0 |
|
43 self.__startY = 0 |
|
44 self.__widget = QWidget(self) |
|
45 |
|
46 self.__timeline = QTimeLine(duration) |
|
47 self.__timeline.setFrameRange(0, 100) |
|
48 self.__timeline.frameChanged.connect(self.__animateFrame) |
|
49 |
|
50 self.setMaximumHeight(0) |
|
51 |
|
52 def widget(self): |
|
53 """ |
|
54 Public method to get a reference to the animated widget. |
|
55 |
|
56 @return reference to the animated widget |
|
57 @rtype QWidget |
|
58 """ |
|
59 |
|
60 @pyqtSlot() |
|
61 def startAnimation(self): |
|
62 """ |
|
63 Public slot to start the animation. |
|
64 """ |
|
65 if self.__timeline.state() == QTimeLine.Running: |
|
66 return |
|
67 |
|
68 shown = 0 |
|
69 hidden = 0 |
|
70 |
|
71 if self.__direction == self.DirectionDown: |
|
72 shown = 0 |
|
73 hidden = -self.__widget.height() |
|
74 |
|
75 self.__widget.move(QPoint(self.__widget.pos().x(), hidden)) |
|
76 |
|
77 self.__stepY = (hidden - shown) / 100.0 |
|
78 self.__startY = hidden |
|
79 self.__stepHeight = self.__widget.height() / 100.0 |
|
80 |
|
81 self.__timeline.setDirection(QTimeLine.Forward) |
|
82 self.__timeline.start() |
|
83 |
|
84 @pyqtSlot(int) |
|
85 def __animateFrame(self, frame): |
|
86 """ |
|
87 Private slot to animate the next frame. |
|
88 |
|
89 @param frame frame number |
|
90 @type int |
|
91 """ |
|
92 self.setFixedHeight(frame * self.__stepHeight) |
|
93 self.__widget.move(self.pos().x(), |
|
94 self.__startY - frame * self.__stepY) |
|
95 |
|
96 @pyqtSlot() |
|
97 def hide(self): |
|
98 """ |
|
99 Public slot to hide the animated widget. |
|
100 """ |
|
101 if self.__timeline.state() == QTimeLine.Running: |
|
102 return |
|
103 |
|
104 self.__timeline.setDirection(QTimeLine.Backward) |
|
105 self.__timeline.start() |
|
106 |
|
107 self.__timeline.finished.connect(self.close) |
|
108 |
|
109 p = self.parentWidget() |
|
110 if p is not None: |
|
111 p.setFocus() |
|
112 |
|
113 def resizeEvent(self, evt): |
|
114 """ |
|
115 Protected method to handle a resize event. |
|
116 |
|
117 @param evt reference to the event object |
|
118 @type QResizeEvent |
|
119 """ |
|
120 if evt.size().width() != self.__widget.width(): |
|
121 self.__widget.resize(evt.size().width(), self.__widget.height()) |
|
122 |
|
123 super(E5AnimatedWidget, self).resizeEvent(evt) |