eric6/E5Gui/E5AnimatedWidget.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2019 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 return self.__widget
60
61 @pyqtSlot()
62 def startAnimation(self):
63 """
64 Public slot to start the animation.
65 """
66 if self.__timeline.state() == QTimeLine.Running:
67 return
68
69 shown = 0
70 hidden = 0
71
72 if self.__direction == self.DirectionDown:
73 shown = 0
74 hidden = -self.__widget.height()
75
76 self.__widget.move(QPoint(self.__widget.pos().x(), hidden))
77
78 self.__stepY = (hidden - shown) / 100.0
79 self.__startY = hidden
80 self.__stepHeight = self.__widget.height() / 100.0
81
82 self.__timeline.setDirection(QTimeLine.Forward)
83 self.__timeline.start()
84
85 @pyqtSlot(int)
86 def __animateFrame(self, frame):
87 """
88 Private slot to animate the next frame.
89
90 @param frame frame number
91 @type int
92 """
93 self.setFixedHeight(frame * self.__stepHeight)
94 self.__widget.move(self.pos().x(),
95 self.__startY - frame * self.__stepY)
96
97 @pyqtSlot()
98 def hide(self):
99 """
100 Public slot to hide the animated widget.
101 """
102 if self.__timeline.state() == QTimeLine.Running:
103 return
104
105 self.__timeline.setDirection(QTimeLine.Backward)
106 self.__timeline.finished.connect(self.close)
107 self.__timeline.start()
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)

eric ide

mercurial