def getView(self): """Get a new QListView suitable for displaying the toolbox.""" v = QListView() v.setModel(self) v.setSelectionMode(v.SingleSelection) v.setDragEnabled(True) v.setDragDropMode(v.DragOnly) return v
class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setupUi(self) self.player = QMediaPlayer() self.player.error.connect(self.erroralert) self.player.play() # Setup the playlist. self.playlist = QMediaPlaylist() self.player.setPlaylist(self.playlist) # Add viewer for video playback, separate floating window. self.viewer = ViewerWindow(self) self.viewer.setWindowFlags(self.viewer.windowFlags() | Qt.WindowStaysOnTopHint) self.viewer.setMinimumSize(QSize(480, 360)) videoWidget = QVideoWidget() self.viewer.setCentralWidget(videoWidget) self.player.setVideoOutput(videoWidget) # Connect control buttons/slides for media player. self.playButton.pressed.connect(self.player.play) self.pauseButton.pressed.connect(self.player.pause) self.stopButton.pressed.connect(self.player.stop) self.volumeSlider.valueChanged.connect(self.player.setVolume) self.viewButton.toggled.connect(self.toggle_viewer) self.viewer.state.connect(self.viewButton.setChecked) self.previousButton.pressed.connect(self.playlist.previous) self.nextButton.pressed.connect(self.playlist.next) self.model = PlaylistModel(self.playlist) self.playlistView.setModel(self.model) self.playlist.currentIndexChanged.connect( self.playlist_position_changed) selection_model = self.playlistView.selectionModel() selection_model.selectionChanged.connect( self.playlist_selection_changed) self.player.durationChanged.connect(self.update_duration) self.player.positionChanged.connect(self.update_position) self.timeSlider.valueChanged.connect(self.player.setPosition) self.open_file_action.triggered.connect(self.open_file) self.setAcceptDrops(True) self.show() def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(484, 371) self.centralWidget = QWidget(MainWindow) sizePolicy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.centralWidget.sizePolicy().hasHeightForWidth()) self.centralWidget.setSizePolicy(sizePolicy) self.centralWidget.setObjectName("centralWidget") self.horizontalLayout = QHBoxLayout(self.centralWidget) self.horizontalLayout.setContentsMargins(11, 11, 11, 11) self.horizontalLayout.setSpacing(6) self.horizontalLayout.setObjectName("horizontalLayout") self.verticalLayout = QVBoxLayout() self.verticalLayout.setSpacing(6) self.verticalLayout.setObjectName("verticalLayout") self.playlistView = QListView(self.centralWidget) self.playlistView.setAcceptDrops(True) self.playlistView.setProperty("showDropIndicator", True) self.playlistView.setDragDropMode(QAbstractItemView.DropOnly) self.playlistView.setAlternatingRowColors(True) self.playlistView.setUniformItemSizes(True) self.playlistView.setObjectName("playlistView") self.verticalLayout.addWidget(self.playlistView) self.horizontalLayout_4 = QHBoxLayout() self.horizontalLayout_4.setSpacing(6) self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.currentTimeLabel = QLabel(self.centralWidget) self.currentTimeLabel.setMinimumSize(QSize(80, 0)) self.currentTimeLabel.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self.currentTimeLabel.setObjectName("currentTimeLabel") self.horizontalLayout_4.addWidget(self.currentTimeLabel) self.timeSlider = QSlider(self.centralWidget) self.timeSlider.setOrientation(Qt.Horizontal) self.timeSlider.setObjectName("timeSlider") self.horizontalLayout_4.addWidget(self.timeSlider) self.totalTimeLabel = QLabel(self.centralWidget) self.totalTimeLabel.setMinimumSize(QSize(80, 0)) self.totalTimeLabel.setAlignment(Qt.AlignLeading | Qt.AlignLeft | Qt.AlignVCenter) self.totalTimeLabel.setObjectName("totalTimeLabel") self.horizontalLayout_4.addWidget(self.totalTimeLabel) self.verticalLayout.addLayout(self.horizontalLayout_4) self.horizontalLayout_5 = QHBoxLayout() self.horizontalLayout_5.setSpacing(6) self.horizontalLayout_5.setObjectName("horizontalLayout_5") self.previousButton = QPushButton(self.centralWidget) self.previousButton.setText("") icon = QIcon() icon.addPixmap(QPixmap("images/control-skip-180.png"), QIcon.Normal, QIcon.Off) self.previousButton.setIcon(icon) self.previousButton.setObjectName("previousButton") self.horizontalLayout_5.addWidget(self.previousButton) self.playButton = QPushButton(self.centralWidget) self.playButton.setText("") icon1 = QIcon() icon1.addPixmap(QPixmap("images/control.png"), QIcon.Normal, QIcon.Off) self.playButton.setIcon(icon1) self.playButton.setObjectName("playButton") self.horizontalLayout_5.addWidget(self.playButton) self.pauseButton = QPushButton(self.centralWidget) self.pauseButton.setText("") icon2 = QIcon() icon2.addPixmap(QPixmap("images/control-pause.png"), QIcon.Normal, QIcon.Off) self.pauseButton.setIcon(icon2) self.pauseButton.setObjectName("pauseButton") self.horizontalLayout_5.addWidget(self.pauseButton) self.stopButton = QPushButton(self.centralWidget) self.stopButton.setText("") icon3 = QIcon() icon3.addPixmap(QPixmap("images/control-stop-square.png"), QIcon.Normal, QIcon.Off) self.stopButton.setIcon(icon3) self.stopButton.setObjectName("stopButton") self.horizontalLayout_5.addWidget(self.stopButton) self.nextButton = QPushButton(self.centralWidget) self.nextButton.setText("") icon4 = QIcon() icon4.addPixmap(QPixmap("images/control-skip.png"), QIcon.Normal, QIcon.Off) self.nextButton.setIcon(icon4) self.nextButton.setObjectName("nextButton") self.horizontalLayout_5.addWidget(self.nextButton) self.viewButton = QPushButton(self.centralWidget) self.viewButton.setText("") icon5 = QIcon() icon5.addPixmap(QPixmap("images/application-image.png"), QIcon.Normal, QIcon.Off) self.viewButton.setIcon(icon5) self.viewButton.setCheckable(True) self.viewButton.setObjectName("viewButton") self.horizontalLayout_5.addWidget(self.viewButton) spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) self.horizontalLayout_5.addItem(spacerItem) self.label = QLabel(self.centralWidget) self.label.setText("") self.label.setPixmap(QPixmap("images/speaker-volume.png")) self.label.setObjectName("label") self.horizontalLayout_5.addWidget(self.label) self.volumeSlider = QSlider(self.centralWidget) self.volumeSlider.setMaximum(100) self.volumeSlider.setProperty("value", 100) self.volumeSlider.setOrientation(Qt.Horizontal) self.volumeSlider.setObjectName("volumeSlider") self.horizontalLayout_5.addWidget(self.volumeSlider) self.verticalLayout.addLayout(self.horizontalLayout_5) self.horizontalLayout.addLayout(self.verticalLayout) MainWindow.setCentralWidget(self.centralWidget) self.menuBar = QMenuBar(MainWindow) self.menuBar.setGeometry(QRect(0, 0, 484, 22)) self.menuBar.setObjectName("menuBar") self.menuFIle = QMenu(self.menuBar) self.menuFIle.setObjectName("menuFIle") MainWindow.setMenuBar(self.menuBar) self.statusBar = QStatusBar(MainWindow) self.statusBar.setObjectName("statusBar") MainWindow.setStatusBar(self.statusBar) self.open_file_action = QAction(MainWindow) self.open_file_action.setObjectName("open_file_action") self.menuFIle.addAction(self.open_file_action) self.menuBar.addAction(self.menuFIle.menuAction()) self.retranslateUi(MainWindow) QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "Failamp")) self.currentTimeLabel.setText(_translate("MainWindow", "0:00")) self.totalTimeLabel.setText(_translate("MainWindow", "0:00")) self.menuFIle.setTitle(_translate("MainWindow", "FIle")) self.open_file_action.setText(_translate("MainWindow", "Open file...")) def dragEnterEvent(self, e): if e.mimeData().hasUrls(): e.acceptProposedAction() def dropEvent(self, e): for url in e.mimeData().urls(): self.playlist.addMedia(QMediaContent(url)) self.model.layoutChanged.emit() # If not playing, seeking to first of newly added + play. if self.player.state() != QMediaPlayer.PlayingState: i = self.playlist.mediaCount() - len(e.mimeData().urls()) self.playlist.setCurrentIndex(i) self.player.play() def open_file(self): path, _ = QFileDialog.getOpenFileName( self, "Open file", "", "mp3 Audio (*.mp3);mp4 Video (*.mp4);Movie files (*.mov);All files (*.*)" ) if path: self.playlist.addMedia(QMediaContent(QUrl.fromLocalFile(path))) self.model.layoutChanged.emit() def update_duration(self, duration): print("!", duration) print("?", self.player.duration()) self.timeSlider.setMaximum(duration) if duration >= 0: self.totalTimeLabel.setText(hhmmss(duration)) def update_position(self, position): if position >= 0: self.currentTimeLabel.setText(hhmmss(position)) # Disable the events to prevent updating triggering a setPosition event (can cause stuttering). self.timeSlider.blockSignals(True) self.timeSlider.setValue(position) self.timeSlider.blockSignals(False) def playlist_selection_changed(self, ix): # We receive a QItemSelection from selectionChanged. i = ix.indexes()[0].row() self.playlist.setCurrentIndex(i) def playlist_position_changed(self, i): if i > -1: ix = self.model.index(i) self.playlistView.setCurrentIndex(ix) def toggle_viewer(self, state): if state: self.viewer.show() else: self.viewer.hide() def erroralert(self, *args): print(args)