示例#1
0
class FooQt(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self, app):
        super().__init__()
        self.setupUi(self)  # Это нужно для инициализации нашего дизайна

        # config loading
        self.config = config.Config(self)
        library.init(self.config.getLibraryDirs())
        self.lyrics = Lyrics(self.config)

        # make post ui setup after library is initialized
        self.postSetupUi()

        # Load config goes after postSetupUi() to be able to restore columns width
        self.config.load(app)
        self.connectEvents(app)

        self.tableModel.refreshPlaylist()

    def connectEvents(self, app):
        self.themeCombo.activated.connect(
            lambda: app.setStyle(self.themeCombo.currentText()))
        self.lyricsCombo.activated.connect(
            lambda: self.lyrics.setProvider(self.lyricsCombo.currentText()))
        self.browseDirBtn.clicked.connect(self.browseDirClick)
        self.rescanLibBtn.clicked.connect(self.rescanLibrary)
        self.settingsBtn.clicked.connect(self.openSettingsDialog)
        self.playBtn.clicked.connect(self.playBtnClick)
        self.nextBtn.clicked.connect(self.next)
        self.prevBtn.clicked.connect(self.prev)
        self.nextRndBtn.clicked.connect(self.nextRnd)
        self.searchEdit.textChanged.connect(self.searchChanged)
        self.treeView.clicked.connect(self.treeViewClick)
        self.tableView.doubleClicked.connect(self.play)
        self.posSlider.sliderMoved.connect(self.setPos)
        self.posSlider.sliderPressed.connect(self.setPos)
        self.timer.timeout.connect(self.updatePos)
        self.tableModel.modelAboutToBeReset.connect(
            self.tableModelBeforeChanged)
        self.tableModel.modelReset.connect(self.tableModelChanged)
        self.statusbar.clicked.connect(self.selectCurrentTrack)
        self.expandBtn.clicked.connect(self.expandBtnClick)
        self.volumeSlider.valueChanged.connect(self.setVolume)

    def treeViewClick(self, index: QtCore.QModelIndex):
        library.selected_dir_row = index.row()
        library.selected_dir = library.TreeModel().getDirPath(index)
        self.config.updateSelectedDir(library.selected_dir,
                                      library.selected_dir_row)
        self.searchChanged()

    def searchChanged(self):
        self.tableModel.refreshPlaylist(self.searchEdit.text())

    def stop(self):
        player.stop()
        self.updatePlayStatus()

    def updatePlayStatus(self):
        icon = qta.icon('fa.play') if player.isNoMusic() else qta.icon(
            'fa.pause')
        self.playBtn.setIcon(icon)
        if player.isNoMusic():
            self.timer.stop()
        else:
            self.timer.start()
        self.statusbar.showMessage(player.getNowPlayingMsg())

    def selectCurrentTrack(self):
        currentIndex = self.tableModel.getNowPlayIndex()
        if currentIndex is not False:
            self.tableView.setCurrentIndex(currentIndex)

    def playBtnClick(self):
        if not player.now_playing:
            return self.play((self.tableView.selectedIndexes() or [None])[0])

        player.playPause()
        self.updatePlayStatus()

    def play(self, index: QtCore.QModelIndex = None):
        if not index:
            return print('No item selected')

        track = player.play(index.row(), self.tableModel.tracks[index.row()])
        if track:
            self.selectCurrentTrack()
            self.updatePlayStatus()
            self.lyricsTxt.setText(self.lyrics.find())

    def next(self):
        nextIndex = self.tableModel.getNextIndex()
        if nextIndex:
            self.play(nextIndex)
        else:
            self.stop()

    def prev(self):
        prevIndex = self.tableModel.getPrevIndex()
        if prevIndex:
            self.play(prevIndex)
        else:
            self.stop()

    def nextRnd(self):
        rndIndex = self.tableModel.getRndIndex()
        if rndIndex:
            self.play(rndIndex)
        else:
            self.stop()

    def rescanLibrary(self):
        start_time = time.time()
        library.rescan()
        self.treeModel.loadTreeData()
        print("--- Library scan is completed in %s seconds ---" %
              (time.time() - start_time))

    def browseDirClick(self):
        newDir = QtWidgets.QFileDialog.getExistingDirectory(self)
        if newDir:
            self.config.updateLibraryDir(newDir)
            library.updateDirs([newDir, '', -1])
            self.rescanLibrary()

    def expandBtnClick(self):
        print(self.expandBtn.isChecked())
        # self.treeModel.
        # self.treeView.rootIndex().child()

    def setPos(self):
        self.timer.stop()
        pos = self.posSlider.value()
        player.mediaplayer.set_position(pos / 1000.0)
        self.timer.start()

    def updatePos(self):
        media_pos = int(player.mediaplayer.get_position() * 1000)
        # print('mediapos', media_pos)
        self.posSlider.setValue(media_pos)
        # No need to call this function if nothing is played
        if not player.mediaplayer.is_playing():
            if self.stopAfterBtn.isChecked():
                self.stopAfterBtn.setChecked(False)
                self.stop()
            else:
                self.nextRnd() if self.rndOrderBtn.isChecked() else self.next()

    def postSetupUi(self):
        # add invisible elements
        self.timer = QtCore.QTimer(self)
        self.treeModel = library.TreeModel()
        self.tableModel = library.TableModel()
        self.statusbar = StatusBar()
        # update qtDesigner non available properties
        self.timer.setInterval(100)
        # design updates
        self.themeCombo.addItems(QtWidgets.QStyleFactory.keys())
        self.lyricsCombo.addItems(self.lyrics.PROVIDERS)
        self.playBtn.setIcon(qta.icon('fa.play'))
        self.nextBtn.setIcon(qta.icon('mdi.skip-next'))
        self.prevBtn.setIcon(qta.icon('mdi.skip-previous'))
        self.nextRndBtn.setIcon(qta.icon('fa.question'))
        self.stopAfterBtn.setIcon(qta.icon('fa.stop-circle'))
        self.rndOrderBtn.setIcon(qta.icon('fa.random'))
        self.browseDirBtn.setIcon(qta.icon('fa.folder-open'))
        self.rescanLibBtn.setIcon(qta.icon('mdi.refresh'))
        self.expandBtn.setIcon(qta.icon('mdi.arrow-expand-vertical'))
        self.settingsBtn.setIcon(qta.icon('fa.cog'))

        self.posSlider.setMaximum(1000)
        # load Directory tree
        self.treeView.setModel(self.treeModel)
        # load Tracks
        self.tableView.setModel(self.tableModel)
        self.tableView.setSelectionBehavior(
            QtWidgets.QAbstractItemView.SelectRows)
        # my custom statusbar
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)
        # statusbar widgets
        self.volumeSlider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
        self.volumeSlider.setMaximum(100)
        self.volumeSlider.setValue(player.getVolume())
        self.volumeSlider.setToolTip("Volume")
        self.volumeSlider.setFixedWidth(100)
        self.statusbar.addPermanentWidget(self.volumeSlider)

    def openSettingsDialog(self):
        dialog = SettingsDialog(self)
        dialog.confirmed.connect(self.saveSettingsFromDialog)
        dialog.geniusToken.setText(self.config.getLyricsGeniusToken())
        dialog.exec()

    def saveSettingsFromDialog(self, token):
        print('token = ' + token)
        self.config.updateLyricsGeniusToken(token)
        self.lyrics.onChangeTokens()

    def setVolume(self, volume):
        player.setVolume(volume)

    def tableModelBeforeChanged(self):
        for row in self.tableModel.groupRows:
            self.tableView.setSpan(row, 0, 1, 1)

    def tableModelChanged(self):
        for row in self.tableModel.groupRows:
            self.tableView.setSpan(row, 0, 1, library.Track.colCount())

    def closeEvent(self, event):
        self.config.save()
        event.accept()