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