-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmslider.py
132 lines (116 loc) · 4.35 KB
/
mslider.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
from PyQt5 import QtGui, QtCore,QtWidgets
import images_rc
class MSlider(QtWidgets.QWidget):
posChanged = QtCore.pyqtSignal(int)
#timeChanged = QtCore.pyqtSignal(str)
def __init__(self, parent = None):
super(MSlider,self).__init__(parent)
self.maxpos = 1000
self.pos = 0
self.images = []
self.fgLeftImgIndex = -1
self.fgCenterImgIndex = -1
self.fgRightImgIndex = -1
self.fitHeight = False
self.leftMousePressed = False
self.setImages(':/images/tb_fg_l.png',':/images/tb_fg_c.png',':/images/tb_fg_r.png')
#public slots...
def posChange(self, pos):
if self.leftMousePressed: return
if pos < 0: pos = 0
self.pos = pos
self.update()
#self.timeChanged.emit(self.getCurTime())
def maxChange(self, value):
self.maxpos = value
self.update()
#self.timeChanged.emit(self.getCurTime())
#public...
def setImages(self, leftImg, centerImg, rightImg):
if len(self.images) > 0:
self.images.clear()
self.images.append(QtGui.QPixmap(leftImg))
self.images.append(QtGui.QPixmap(centerImg))
self.images.append(QtGui.QPixmap(rightImg))
self.update()
#private...
def imginRange(self, value):
pass
def getCurTime(self):
pass
#events...
def mousePressEvent(self, event):
self.leftMousePressed = event.buttons() == QtCore.Qt.LeftButton
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
w = self.width()
x = event.x()
if x < 0: x = 0
if x > w: x = w
self.pos = round(self.maxpos * (x/w))
self.update()
def mouseReleaseEvent(self, event):
if self.leftMousePressed:
w = self.width()
x = event.x()
if x < 0: x = 0
if x > w: x = w
self.pos = round(self.maxpos * (x/w))
self.update()
self.posChanged.emit(self.pos)
self.leftMousePressed = False
def paintEvent(self, event):
if len(self.images) < 3:
super(MSlider, self).paintEvent(event)
return
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
#painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
ww = self.width()
wh = self.height()
p = round(ww*(self.pos/self.maxpos))
fgSL = QtCore.QSize(self.images[0].width(),self.images[0].height())
fgSC = QtCore.QSize(self.images[1].width(),self.images[1].height())
fgSR = QtCore.QSize(self.images[2].width(),self.images[2].height())
if self.fitHeight:
fgSL.setWidth(self.images[0].width())
fgSL.setHeight(wh)
fgSC.setWidth(self.images[1].width())
fgSC.setHeight(wh)
fgSR.setWidth(self.images[2].width())
fgSR.setHeight(wh)
x = 0;
y = 0;
if not self.fitHeight:
y = (wh - fgSL.height()) // 2
if p < fgSL.width():
if p == 0: return
painter.drawPixmap(QtCore.QRect(x,y,p,fgSL.height()),self.images[0],QtCore.QRect(0,0,p,self.images[0].height()))
else:
painter.drawPixmap(QtCore.QRect(x,y,fgSL.width(),fgSL.height()),self.images[0])
x = fgSL.width()
if not self.fitHeight:
y = (wh - fgSC.height()) // 2
else:
y = 0
cw = ww-fgSL.width()-fgSR.width()
if p < cw+fgSL.width():
cw = p-fgSL.width()
if cw > 0:
painter.drawPixmap(QtCore.QRect(x,y,cw,fgSC.height()),self.images[1],QtCore.QRect(0,0,fgSC.width(),self.images[1].height()))
x = ww-fgSR.width()
if not self.fitHeight:
y = (wh - fgSR.height()) // 2
else: y = 0
if p <= ww-fgSR.width():
pass
elif p == ww:
painter.drawPixmap(QtCore.QRect(x,y,fgSR.width(),fgSR.height()),self.images[2])
else:
cw = fgSR.width() - (ww-p)
painter.drawPixmap(QtCore.QRect(x,y,cw,fgSR.height()),self.images[2],QtCore.QRect(0,0,cw,self.images[2].height()))
painter.end()
def resizeEvent(self, event):
self.update()
#super().resizeEvent(event)