예제 #1
0
파일: main.py 프로젝트: vkolev/qexfm
    def initUI(self):
        # SearchBox
        self.searchBox = QtGui.QLineEdit(self)
        self.searchBox.setPlaceholderText("Search...")
        self.searchBox.setMaximumWidth(200)
        self.searchBox.clearFocus()
        self.searchBox.returnPressed.connect(self.do_search)
        self.searchBoxWidget = QtGui.QWidgetAction(self)
        self.searchBoxWidget.setDefaultWidget(self.searchBox)

        # prevButton
        prevAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/prev24.svg')), 'Previous', self)
        prevAction.setShortcut('Ctrl+B')
        prevAction.triggered.connect(self.previous_song)

        # playButton
        self.playAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/play24.svg')), 'Play/Stop', self)
        self.playAction.setShortcut('Ctrl+P')
        self.playAction.triggered.connect(self.play_song)

        # nextButton
        nextAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/next24.svg')), 'Next', self)
        nextAction.setShortcut('Ctrl+N')
        nextAction.triggered.connect(self.next_song)

        # saveButton
        saveAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/save24.svg')), 'Save', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.triggered.connect(self.save_song)

        # CurrentSong
        infoHbox = QtGui.QHBoxLayout()
        infoBox = QtGui.QWidget()
        infoBox.setLayout(infoHbox)
        self.currentSongLabel = TruncatedLabel()
        font = QtGui.QFont()
        font.setPointSize(9)
        titleSeek = QtGui.QVBoxLayout()
        titleSeek.setSpacing(0)
        playContainer = QtGui.QWidget()
        playContainer.setMaximumHeight(32)
        self.currentSongLabel.setText("Artist - Title")
        self.currentSongLabel.setMinimumHeight(16)
        self.currentSongLabel.setFont(font)
        self.currentSongLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.currentSongLabel.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.seeker = Phonon.SeekSlider()
        self.seeker.setMinimumHeight(20)
        self.seeker.setIconVisible(False)
        titleSeek.addWidget(self.currentSongLabel)
        titleSeek.addWidget(self.seeker)
        playContainer.setLayout(titleSeek)
        self.currentSongImage = QtGui.QLabel()
        self.currentSongImage.setMaximumHeight(24)
        infoHbox.addWidget(self.currentSongImage)
        infoHbox.addWidget(playContainer)
        spacerActionWidget = QtGui.QWidgetAction(self)
        spacerActionWidget.setDefaultWidget(infoBox)

        # The ToolButton
        self.toolButton = QtGui.QToolButton(self)
        self.toolButton.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/cogs24.svg')))
        self.toolButton.setPopupMode(2)
        self.toolButton.setArrowType(0)
        self.toolButton.setMenu(self.__menu())
        toolButtonWidget = QtGui.QWidgetAction(self)
        toolButtonWidget.setDefaultWidget(self.toolButton)

        self.toolbar = self.addToolBar('Main')
        self.toolbar.setMovable(False)
        self.toolbar.addAction(self.searchBoxWidget)
        self.toolbar.addSeparator()
        self.toolbar.addAction(prevAction)
        self.toolbar.addAction(self.playAction)
        self.toolbar.addAction(nextAction)
        self.toolbar.addSeparator()
        self.toolbar.addAction(saveAction)
        self.toolbar.addAction(spacerActionWidget)
        self.toolbar.addAction(toolButtonWidget)

        vbox = QtGui.QVBoxLayout()
        container = QtGui.QWidget()
        self.comboBox = QtGui.QComboBox()
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/trending_icon.png')), 'Trending')
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/explore_icon.png')), 'Explore')
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/sites_icon.png')), 'Sites')
        self.comboBox.currentIndexChanged.connect(self.site_changed);
        container.setMaximumWidth(200)
        self.leftlist = QtGui.QListWidget(self)
        self.leftlist.setFrameShape(QtGui.QFrame.NoFrame)
        self.leftlist.setMaximumWidth(200)
        self.leftlist.addItems(GANRES)
        self.leftlist.itemDoubleClicked.connect(self.change_music)
        vbox.addWidget(self.comboBox)
        vbox.addWidget(self.leftlist)
        container.setLayout(vbox)
        self.rightlist = QtGui.QListWidget(self)
        self.rightlist.itemDoubleClicked.connect(self.play_song)

        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter.addWidget(container)
        splitter.addWidget(self.rightlist)

        self.setCentralWidget(splitter)
        self.setMinimumSize(800, 500)
        self.setWindowTitle('ExfmPlayer')
        self.setWindowIcon(QtGui.QIcon(os.path.join(PATH, 'data/exfmplayer24.png')))
        self.show()
예제 #2
0
파일: main.py 프로젝트: vkolev/qexfm
class ExfmPlayer(QtGui.QMainWindow):

    def __init__(self):
        super(ExfmPlayer, self).__init__()
        self.songs = []
        self.currentPosition = None
        self.currentSong = None
        self.searchTerm = ""
        self.m_media = None
        self.lastsearch = ""
        self.initUI()

    def initUI(self):
        # SearchBox
        self.searchBox = QtGui.QLineEdit(self)
        self.searchBox.setPlaceholderText("Search...")
        self.searchBox.setMaximumWidth(200)
        self.searchBox.clearFocus()
        self.searchBox.returnPressed.connect(self.do_search)
        self.searchBoxWidget = QtGui.QWidgetAction(self)
        self.searchBoxWidget.setDefaultWidget(self.searchBox)

        # prevButton
        prevAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/prev24.svg')), 'Previous', self)
        prevAction.setShortcut('Ctrl+B')
        prevAction.triggered.connect(self.previous_song)

        # playButton
        self.playAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/play24.svg')), 'Play/Stop', self)
        self.playAction.setShortcut('Ctrl+P')
        self.playAction.triggered.connect(self.play_song)

        # nextButton
        nextAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/next24.svg')), 'Next', self)
        nextAction.setShortcut('Ctrl+N')
        nextAction.triggered.connect(self.next_song)

        # saveButton
        saveAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/save24.svg')), 'Save', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.triggered.connect(self.save_song)

        # CurrentSong
        infoHbox = QtGui.QHBoxLayout()
        infoBox = QtGui.QWidget()
        infoBox.setLayout(infoHbox)
        self.currentSongLabel = TruncatedLabel()
        font = QtGui.QFont()
        font.setPointSize(9)
        titleSeek = QtGui.QVBoxLayout()
        titleSeek.setSpacing(0)
        playContainer = QtGui.QWidget()
        playContainer.setMaximumHeight(32)
        self.currentSongLabel.setText("Artist - Title")
        self.currentSongLabel.setMinimumHeight(16)
        self.currentSongLabel.setFont(font)
        self.currentSongLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.currentSongLabel.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.seeker = Phonon.SeekSlider()
        self.seeker.setMinimumHeight(20)
        self.seeker.setIconVisible(False)
        titleSeek.addWidget(self.currentSongLabel)
        titleSeek.addWidget(self.seeker)
        playContainer.setLayout(titleSeek)
        self.currentSongImage = QtGui.QLabel()
        self.currentSongImage.setMaximumHeight(24)
        infoHbox.addWidget(self.currentSongImage)
        infoHbox.addWidget(playContainer)
        spacerActionWidget = QtGui.QWidgetAction(self)
        spacerActionWidget.setDefaultWidget(infoBox)

        # The ToolButton
        self.toolButton = QtGui.QToolButton(self)
        self.toolButton.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/cogs24.svg')))
        self.toolButton.setPopupMode(2)
        self.toolButton.setArrowType(0)
        self.toolButton.setMenu(self.__menu())
        toolButtonWidget = QtGui.QWidgetAction(self)
        toolButtonWidget.setDefaultWidget(self.toolButton)

        self.toolbar = self.addToolBar('Main')
        self.toolbar.setMovable(False)
        self.toolbar.addAction(self.searchBoxWidget)
        self.toolbar.addSeparator()
        self.toolbar.addAction(prevAction)
        self.toolbar.addAction(self.playAction)
        self.toolbar.addAction(nextAction)
        self.toolbar.addSeparator()
        self.toolbar.addAction(saveAction)
        self.toolbar.addAction(spacerActionWidget)
        self.toolbar.addAction(toolButtonWidget)

        vbox = QtGui.QVBoxLayout()
        container = QtGui.QWidget()
        self.comboBox = QtGui.QComboBox()
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/trending_icon.png')), 'Trending')
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/explore_icon.png')), 'Explore')
        self.comboBox.addItem(QtGui.QIcon(os.path.join(PATH, 'data/sites_icon.png')), 'Sites')
        self.comboBox.currentIndexChanged.connect(self.site_changed);
        container.setMaximumWidth(200)
        self.leftlist = QtGui.QListWidget(self)
        self.leftlist.setFrameShape(QtGui.QFrame.NoFrame)
        self.leftlist.setMaximumWidth(200)
        self.leftlist.addItems(GANRES)
        self.leftlist.itemDoubleClicked.connect(self.change_music)
        vbox.addWidget(self.comboBox)
        vbox.addWidget(self.leftlist)
        container.setLayout(vbox)
        self.rightlist = QtGui.QListWidget(self)
        self.rightlist.itemDoubleClicked.connect(self.play_song)

        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter.addWidget(container)
        splitter.addWidget(self.rightlist)

        self.setCentralWidget(splitter)
        self.setMinimumSize(800, 500)
        self.setWindowTitle('ExfmPlayer')
        self.setWindowIcon(QtGui.QIcon(os.path.join(PATH, 'data/exfmplayer24.png')))
        self.show()

    def __menu(self):
        menu = QtGui.QMenu()
        aboutAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/about24.svg')), 'About', self)
        aboutAction.triggered.connect(self.show_about)
        exitAction = QtGui.QAction(QtGui.QIcon(os.path.join(PATH, 'data/exit24.svg')), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(QtGui.qApp.quit)
        menu.addAction(aboutAction)
        menu.addAction(exitAction)
        return menu

    def show_about(self, sender):
        aboutDlg = MyDialog(self)
        aboutDlg.exec_()


    def previous_song(self, sender):
        if self.currentPosition == None:
            self.currentPosition = self.rightlist.count() - 1
        else:
            self.currentPosition -= 1
        songWidget = self.rightlist.item(self.currentPosition)
        songWidget.setSelected(True)
        self.currentSong = songWidget.get_song()
        self.currentSongLabel.setText("%s - %s" % (self.currentSong.artist, self.currentSong.title))
        output = Phonon.AudioOutput(Phonon.MusicCategory, self)
        self.m_media = Phonon.MediaObject()
        Phonon.createPath(self.m_media, output)
        self.m_media.setCurrentSource(Phonon.MediaSource(QtCore.QUrl(self.currentSong.get_url())))
        self.seeker.setMediaObject(self.m_media)
        self.m_media.finished.connect(self.next_song)
        self.playAction.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/pause24.svg')))
        self.m_media.play()

    def play_song(self, sender):
        if self.currentSong is None:
            if self.rightlist.count() != 0:
                if type(sender) is bool:
                    pass
                elif type(sender) is QtGui.QListWidgetItem:
                    self.load_more(sender)
                else:
                    self.playAction.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/pause24.svg')))
                    self.currentSong = sender.get_song()
                    self.currentPosition = self.rightlist.currentRow()
                    self.currentSongLabel.setText("%s - %s" % (self.currentSong.artist, self.currentSong.title))
                    self.currentSongImage.setPixmap(self.currentSong.image.get_small_pixmap())
                    self.start_player()
        else:
            if type(sender) is not SongWidgetItem:
                self.currentSong = None
                self.playAction.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/play24.svg')))
                self.currentSongLabel.setText("Artist - Title")
                self.m_media.stop()
            elif type(sender) is QtGui.QListWidgetItem:
                self.load_more(sender)
            else:
                self.currentSong = sender.get_song()
                self.currentSongLabel.setText("%s - %s" % (self.currentSong.artist, self.currentSong.title))
                self.currentPosition = sender.index()
                self.currentPosition = 0
                self.start_player()
        print "Play/Stop Button pressed"

    def start_player(self):
        output = Phonon.AudioOutput(Phonon.MusicCategory, self)
        self.m_media = Phonon.MediaObject()
        Phonon.createPath(self.m_media, output)
        self.m_media.setCurrentSource(Phonon.MediaSource(QtCore.QUrl(self.currentSong.get_url())))
        self.seeker.setMediaObject(self.m_media)
        self.m_media.finished.connect(self.next_song)
        self.m_media.play()


    def next_song(self):
        if self.currentPosition == None:
            self.currentPosition = 0
        else:
            self.currentPosition += 1
        songWidget = self.rightlist.item(self.currentPosition)
        songWidget.setSelected(True)
        self.currentSong = songWidget.get_song()
        self.currentSongLabel.setText("%s - %s" % (self.currentSong.artist, self.currentSong.title))
        output = Phonon.AudioOutput(Phonon.MusicCategory, self)
        self.m_media = Phonon.MediaObject()
        Phonon.createPath(self.m_media, output)
        self.m_media.setCurrentSource(Phonon.MediaSource(QtCore.QUrl(self.currentSong.get_url())))
        self.seeker.setMediaObject(self.m_media)
        self.m_media.finished.connect(self.next_song)
        self.playAction.setIcon(QtGui.QIcon(os.path.join(PATH, 'data/pause24.svg')))
        self.m_media.play()

    def load_more(self, sender=None):
        client = ExFmLib()
        if self.rightlist.count() < 100:
            self.rightlist.item(self.rightlist.count() - 1).setHidden(True)
            try:
                search = client.get_search(self.searchTerm, self.rightlist.count(), 20)
                for song in search.songs:
                    try:
                        self.rightlist.addItem(SongWidgetItem(song, QtGui.QIcon('data/folder-music.svg')))
                    except TypeError:
                        pass
                if self.rightlist.count() < 100:
                    loadmore = QtGui.QListWidgetItem(QtGui.QIcon(os.path.join(PATH, 'data/go-next.svg')), "Load next 20 songs")
                    self.rightlist.addItem(loadmore)
                    print self.rightlist.count()
            except KeyError:
                pass


    def do_search(self, sender=None):
        client = ExFmLib()
        term = str(self.searchBox.text())
        if self.searchTerm != term:
            self.searchTerm = term
            self.rightlist.clear()
        self.setWindowTitle("ExFmPlayer [%s]" % self.searchTerm)
        search = client.get_search(self.searchTerm, self.rightlist.count(), 20)
        for song in search.songs:
            try:
                self.rightlist.addItem(SongWidgetItem(song, QtGui.QIcon('data/folder-music.svg')))
            except TypeError:
                pass
        loadmore = QtGui.QListWidgetItem(QtGui.QIcon(os.path.join(PATH, 'data/go-next.svg')), "Load next 20 songs")
        self.rightlist.addItem(loadmore)

    def save_song(self, sender):
        print "Saving current song"

    def change_music(self, sender):
        client = ExFmLib()
        tag = str(sender.text())
        category = str(self.comboBox.currentText())
        self.setWindowTitle("ExFmPlayer [%s in %s]" % (tag, category))
        search = None
        if category == "Trending":
            search = client.get_trending(tag.lower(), self.rightlist.count(), 20)
        elif category == "Explore":
            search = client.get_explore(tag.lower(), self.rightlist.count(), 20)
        else:
            search = client.get_trending(tag.lower(), self.rightlist.count(), 20)
        self.rightlist.clear()
        for song in search.songs:
            try:
                self.rightlist.addItem(SongWidgetItem(song,
                                QtGui.QIcon(os.path.join(PATH, 'data/folder-music.svg'))))
            except TypeError:
                pass

    def site_changed(self, sender):
        if sender != 2:
            self.leftlist.clear()
            self.leftlist.addItems(GANRES)
        else:
            self.leftlist.clear()