def __init__(self, parent=None): super(MediaWidget, self).__init__(parent) self.setStyleSheet('background: rgba(0,0,0,0);') self.totalTime = QtCore.QTime(0, 0, 0) self.dragPos = QtCore.QPoint(0, 0) self.position = (-1, -1, -1) self.current = 0 self.selected = False self.media = None self.actionsList = [] self.actionIndex = -1 self.countdownMode = True self.levelVisible = False self.button = QClickLabel(self) self.button.setWordWrap(True) self.button.setAlignment(QtCore.Qt.AlignCenter) self.button.setFocusPolicy(QtCore.Qt.NoFocus) self.button.setText('Empty\nMedia') self.button.setStyleSheet( 'background-color: ' + Media.MEDIA_DEFAULT_CONF['color'] + ';\ color: ' + Media.MEDIA_DEFAULT_CONF['font-color'] + ';\ border: 1 solid rgb(0,0,0);\ border-radius: 6;') self.button.contextMenuEvent = lambda e: self.onContextAction.emit(self, e.globalPos()) self.button.clicked.connect(self.onClick) self.button.mouseMoveEvent = self.mouseMoveEvent self.slider = QClickSlider(self) self.slider.setOrientation(QtCore.Qt.Horizontal) self.slider.setFocusPolicy(QtCore.Qt.NoFocus) self.slider.setVisible(False) self.level = QLevelPlotter(self) self.level.setVisible(False) self.slider.setMinimum(0) self.countBar = QtGui.QProgressBar(self) self.countBar.setTextVisible(False) self.count = QtGui.QLCDNumber(self.countBar) self.count.setDigitCount(8) self.count.setSegmentStyle(QtGui.QLCDNumber.Flat) self.count.enterEvent = self.enterEvent #self.count.setStyleSheet('background: rgba(0,0,0,0);') self.count.display('00:00:00') self.countBar.resizeEvent = lambda e: self.count.resize(e.size()) self.resizer = AutoResizer(self) self.installEventFilter(self.resizer)
class MediaWidget(QtGui.QWidget): UNDERLINE_CSS = 'text-decoration: underline;' onFocus = QtCore.pyqtSignal(object) onContextAction = QtCore.pyqtSignal(object, QtCore.QPoint) onEditAction = QtCore.pyqtSignal(object) def __init__(self, parent=None): super(MediaWidget, self).__init__(parent) self.setStyleSheet('background: rgba(0,0,0,0);') self.totalTime = QtCore.QTime(0, 0, 0) self.dragPos = QtCore.QPoint(0, 0) self.position = (-1, -1, -1) self.current = 0 self.selected = False self.media = None self.actionsList = [] self.actionIndex = -1 self.countdownMode = True self.levelVisible = False self.button = QClickLabel(self) self.button.setWordWrap(True) self.button.setAlignment(QtCore.Qt.AlignCenter) self.button.setFocusPolicy(QtCore.Qt.NoFocus) self.button.setText('Empty\nMedia') self.button.setStyleSheet( 'background-color: ' + Media.MEDIA_DEFAULT_CONF['color'] + ';\ color: ' + Media.MEDIA_DEFAULT_CONF['font-color'] + ';\ border: 1 solid rgb(0,0,0);\ border-radius: 6;') self.button.contextMenuEvent = lambda e: self.onContextAction.emit(self, e.globalPos()) self.button.clicked.connect(self.onClick) self.button.mouseMoveEvent = self.mouseMoveEvent self.slider = QClickSlider(self) self.slider.setOrientation(QtCore.Qt.Horizontal) self.slider.setFocusPolicy(QtCore.Qt.NoFocus) self.slider.setVisible(False) self.level = QLevelPlotter(self) self.level.setVisible(False) self.slider.setMinimum(0) self.countBar = QtGui.QProgressBar(self) self.countBar.setTextVisible(False) self.count = QtGui.QLCDNumber(self.countBar) self.count.setDigitCount(8) self.count.setSegmentStyle(QtGui.QLCDNumber.Flat) self.count.enterEvent = self.enterEvent #self.count.setStyleSheet('background: rgba(0,0,0,0);') self.count.display('00:00:00') self.countBar.resizeEvent = lambda e: self.count.resize(e.size()) self.resizer = AutoResizer(self) self.installEventFilter(self.resizer) def mouseMoveEvent(self, e): if(e.buttons() == QtCore.Qt.LeftButton and (e.modifiers() == QtCore.Qt.ControlModifier or e.modifiers() == QtCore.Qt.ShiftModifier)): mimeData = QtCore.QMimeData() mimeData.setText('GridLayout_Drag&Drop') drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(QtGui.QPixmap.grabWidget(self, self.rect())) self.dragPos = e.pos() self.preDragPos = self.pos() if(e.modifiers() == QtCore.Qt.ControlModifier): drag.start(QtCore.Qt.MoveAction) else: drag.start(QtCore.Qt.CopyAction) def setMedia(self, media): if(self.media is not None): raise Exception('Media is assigned yet') self.media = media self.setPos(self.position) def reset(): self.actionIndex = -1 self.media.onStop.connect(reset) self.media.onEndOfStream.connect(reset) self.media.onError.connect(reset) self.media.mediaTime.progress.connect(self.updateCount) self.media.onDuration.connect(self.onDuration, QtCore.Qt.QueuedConnection) self.media.onConfigurationChanged.connect(self.updateConf, QtCore.Qt.QueuedConnection) self.media.onFadeIn.connect(self.fadeInIndicator, QtCore.Qt.QueuedConnection) self.media.onFadeOut.connect(self.fadeOutIndicator, QtCore.Qt.QueuedConnection) self.media.onEndOfStream.connect(self.updateConf, QtCore.Qt.QueuedConnection) self.media.onEndOfStream.connect(self.level.reset) self.media.onLevel.connect(self.level.plot) self.media.onStopped.connect(self.level.reset) self.slider.sliderMoved.connect(self.seek) self.slider.sliderJumped.connect(self.media.seek) self.updateConf() self.onDuration() def setPos(self, pos): self.position = pos if(self.media is not None): self.media.conf['pos'] = pos def select(self): self.selected = not self.selected self.selectionIndicator() def onClick(self, e): if(e.modifiers() == QtCore.Qt.ShiftModifier): self.onEditAction.emit(self.media) elif(e.modifiers() == QtCore.Qt.ControlModifier): self.select() else: self.performAction() def seek(self, time_ms): if(self.media.state != Media.NONE): self.media.seek(time_ms) def performAction(self): if(len(self.actionsList) == 0): self.playback() elif(self.actionIndex == -1): self.playback() self.actionIndex += 1 elif(self.actionIndex == len(self.actionsList)): self.playback() else: self.actionsList[self.actionIndex].exec_() self.actionIndex += 1 def playback(self): if(self.media.state == Media.PLAYING): if(self.media.conf['pause']): self.media.pause() else: self.media.stop() self.actionIndex = -1 elif(self.media.state == Media.PAUSED or self.media.state == Media.STOPPED): self.media.play() def fadeOutIndicator(self, fade): if(fade): self.button.setStyleSheet(self.widgetColor() + '\ border: 2 solid rgba(160,0,0,255);\ border-radius: 6px;\ color:' + self.media.conf['font-color'] + ';\ font-size: ' + str(self.media.conf['font-size']) + 'pt;') else: self.button.setStyleSheet(self.widgetColor() + '\ border: 1 solid rgb(0,0,0);\ border-radius: 6px;\ color:' + self.media.conf['font-color'] + ';\ font-size: ' + str(self.media.conf['font-size']) + 'pt;') def fadeInIndicator(self, fade): if(fade): self.button.setStyleSheet(self.widgetColor() + '\ border: 2 solid rgba(0,100,0,255);\ border-radius: 6px;\ color:' + self.media.conf['font-color'] + ';\ font-size: ' + str(self.media.conf['font-size']) + 'pt;') else: self.button.setStyleSheet(self.widgetColor() + '\ border: 1 solid rgb(0,0,0);\ border-radius: 6px;\ color:' + self.media.conf['font-color'] + ';\ font-size: ' + str(self.media.conf['font-size']) + 'pt;') def selectionIndicator(self): if(not self.selected): self.button.setStyleSheet(self.button.styleSheet().replace(self.UNDERLINE_CSS, '')) else: self.button.setStyleSheet(self.button.styleSheet() + self.UNDERLINE_CSS) def updateConf(self): self.media.conf['pos'] = self.position self.button.setText(self.media.conf['name']) self.button.setStyleSheet(self.widgetColor() + '\ border: 1 solid rgb(0,0,0);\ border-radius: 6px;\ color:' + self.media.conf['font-color'] + ';\ font-size: ' + str(self.media.conf['font-size']) + 'pt;') self.selectionIndicator() def widgetColor(self): return 'background-color: ' + self.media.conf['color'] + ';' def updateCount(self, time, ignoreVisibility=False): if(not (not ignoreVisibility and self.visibleRegion().isEmpty())): if(self.media.conf['duration'] != -1): duration = self.media.conf['duration'] / self.media.conf['pitch']['tempo'] if(time == duration): time = 0 if(time < 0): time = 0 self.slider.setValue(time) if(self.countdownMode): time = duration - time self.countBar.setValue(time) displayTime = QtCore.QTime(0, (time / 60000) % 60, second=(time / 1000) % 60, msec=time % 1000) if(self.countdownMode and time / 1000 < 60): self.count.display(displayTime.toString('mm:ss:zzz')[:-2] + '0') else: self.count.display(displayTime.toString('mm:ss') + ':00') def onDuration(self): duration = self.media.conf['duration'] / self.media.conf['pitch']['tempo'] self.slider.setMaximum(duration) self.countBar.setMaximum(duration) if(self.media.state != Media.PLAYING): self.updateCount(duration, True) def showLevels(self, visible): self.level.setVisible(visible) self.levelVisible = visible self.update() def setCountdownMode(self, mode): self.countdownMode = mode self.updateCount(self.media.currentTime()) def update(self): super(MediaWidget, self).update() xdim = self.geometry().width() ydim = self.geometry().height() / 5 ypos = self.geometry().height() - ydim self.countBar.setGeometry(0, ypos, xdim, ydim) ydim2 = self.geometry().height() / 5 ypos = self.geometry().height() - (ydim + ydim2) ydim = self.geometry().height() - ydim if(self.levelVisible): xdim -= self.geometry().width() / 6 self.button.setGeometry(0, 0, xdim, ydim - 1) self.slider.setGeometry(0, ypos, xdim, ydim2 - 4) if(self.levelVisible): xpos = xdim xdim = self.geometry().width() / 6 self.level.setGeometry(xpos + 2, 0, xdim - 4, ydim)