def __init__(self, iconPath, parent=None): super(SystemTray, self).__init__(QIcon(iconPath)) self.parent = parent self.menu = QMenu(self.parent) self.setContextMenu(self.menu) self.show()
def singsFrameContextMenuEvent(self, event): item = self.detailSings.singsTable.itemAt( self.detailSings.singsTable.mapFromGlobal(QCursor.pos())) self.menu = QMenu(self.detailSings.singsTable) self.menu.addAction(self.actionNextPlay) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
def contextEvent(self, event): currentWidget = self.searchResultTableIndexs.get(self.currentName) if not currentWidget: return item = currentWidget.itemAt(currentWidget.mapFromGlobal(QCursor.pos())) self.menu = QMenu(currentWidget) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class SystemTray(QSystemTrayIcon): def __init__(self, iconPath, parent=None): super(SystemTray, self).__init__(QIcon(iconPath)) self.parent = parent self.menu = QMenu(self.parent) self.setContextMenu(self.menu) self.show() def addAction(self, action: QAction): if type(action) != QAction: print('添加的Action不是一个QAction对象。') return self.menu.addAction(action) def __del__(self): self.hide()
class SystemTray(QSystemTrayIcon): def __init__(self, iconPath, parent=None): super(SystemTray, self).__init__(QIcon(iconPath)) self.parent = parent self.menu = QMenu(self.parent) self.setContextMenu(self.menu) self.show() def addAction(self, action:QAction): if type(action) != QAction: print('添加的Action不是一个QAction对象。') return self.menu.addAction(action) def __del__(self): self.hide()
def singsFrameContextMenuEvent(self, event): item = self.detailSings.singsTable.itemAt(self.detailSings.singsTable.mapFromGlobal(QCursor.pos())) self.menu = QMenu(self.detailSings.singsTable) self.menu.addAction(self.actionNextPlay) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class ConfigDetailSings(QObject): download = pyqtSignal(dict) def __init__(self, parent=None): super(ConfigDetailSings, self).__init__() self.detailSings = parent self.musicList = [] self.currentIndex = 0 self.grandparent = self.detailSings.parent self.player = self.grandparent.playWidgets.player self.playList = self.grandparent.playWidgets self.currentMusic = self.grandparent.playWidgets.currentMusic self.transTime = transTime self.detailSings.singsTable.contextMenuEvent = self.singsFrameContextMenuEvent self.bindConnect() self.setContextMenu() def bindConnect(self): self.detailSings.playAllButton.clicked.connect( self.addAllMusicToPlayer) self.detailSings.singsTable.itemDoubleClicked.connect( self.itemDoubleClickedEvent) def setContextMenu(self): self.actionNextPlay = QAction('下一首播放', self) self.actionNextPlay.triggered.connect(self.addToNextPlay) self.actionDownloadSong = QAction('下载', self) self.actionDownloadSong.triggered.connect(self.downloadSong) def addToNextPlay(self): data = self.musicList[self.currentIndex] self.player.setAllMusics([data]) self.playList.playList.addMusic(data) self.playList.playList.addPlayList(data['name'], data['author'], data['time']) @toTask def downloadSong(self, x): musicInfo = self.musicList[self.currentIndex] url = musicInfo.get('url') if 'http:' not in url and 'https:' not in url: songId = musicInfo.get('music_id') future = aAsync(netease.singsUrl, [songId]) url = yield from future url = url[0].get('url') musicInfo['url'] = url self.download.emit(musicInfo) def addAllMusicToPlayer(self): self.playList.setPlayerAndPlaylists(self.musicList) def setupDetailFrames(self, datas, singsUrls, singsIds): result = datas self.musicList = [] self.detailSings.singsTable.clearContents() self.detailSings.titleLabel.setText(result['name']) self.detailSings.authorName.setText(result['creator']['nickname']) description = result['description'] # 有些没有简介会报错的。 if not description: description = '' self.detailSings.descriptionText.setText(description) # 这边添加歌曲的信息到table。 self.detailSings.singsTable.setRowCount(result['trackCount']) for i, j, t, x in zip(result['tracks'], range(result['trackCount']), singsUrls, singsIds): names = i['name'] musicName = QTableWidgetItem(names) self.detailSings.singsTable.setItem(j, 0, musicName) author = i['artists'][0]['name'] musicAuthor = QTableWidgetItem(author) self.detailSings.singsTable.setItem(j, 1, musicAuthor) times = self.transTime(i['duration'] / 1000) musicTime = QTableWidgetItem(times) self.detailSings.singsTable.setItem(j, 2, musicTime) music_img = i['album']['blurPicUrl'] lyric = i.get('lyric') self.musicList.append({ 'url': t, 'name': names, 'time': times, 'author': author, 'music_img': music_img, 'music_id': x, 'lyric': lyric }) # 事件。 def itemDoubleClickedEvent(self): currentRow = self.detailSings.singsTable.currentRow() data = self.musicList[currentRow] self.playList.setPlayerAndPlayList(data) def singsFrameContextMenuEvent(self, event): item = self.detailSings.singsTable.itemAt( self.detailSings.singsTable.mapFromGlobal(QCursor.pos())) self.menu = QMenu(self.detailSings.singsTable) self.menu.addAction(self.actionNextPlay) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class ConfigSearchArea(QObject): download = pyqtSignal(dict) def __init__(self, searchArea): super(ConfigSearchArea, self).__init__() # current show-table's index. self.currentIndex = 0 # current widgets name。 self.currentName = '网易云' # parent. self.searchArea = searchArea # get storage folder self.downloadFolder = self.searchArea.parent.config.getDownloadFolder() self.transTime = addition.itv2time self.searchEngineers = {'网易云': netease, '虾米': xiami, 'QQ': qqApi} # TODO # to config singsFrameBase instead of configing them respective. self.searchResultTableIndexs = { '网易云': self.searchArea.neteaseSearchFrame.singsResultTable, '虾米': self.searchArea.xiamiSearchFrame.singsResultTable, 'QQ': self.searchArea.qqSearchFrame.singsResultTable } self.musicList = [] self.noContents = "很抱歉 未能找到关于<font style='text-align: center;' color='#23518F'>“{0}”</font>的{1}。" self.bindConnect() self.setContextMenu() def bindConnect(self): self.searchArea.contentsTab.tabBarClicked.connect(self.searchBy) self.searchArea.neteaseSearchFrame.singsResultTable.itemDoubleClicked.connect( self.itemDoubleClickedEvent) self.searchArea.xiamiSearchFrame.singsResultTable.itemDoubleClicked.connect( self.itemDoubleClickedEvent) self.searchArea.qqSearchFrame.singsResultTable.itemDoubleClicked.connect( self.itemDoubleClickedEvent) self.searchArea.neteaseSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent self.searchArea.xiamiSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent self.searchArea.qqSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent def setContextMenu(self): self.actionDownloadSong = QAction('下载', self) self.actionDownloadSong.triggered.connect(self.downloadSong) @toTask def downloadSong(self, x): # x is useless, but must be. # musicInfo = self.musicList[self.currentIndex] url = musicInfo.get('url') if 'http:' not in url and 'https:' not in url: songId = musicInfo.get('music_id') future = aAsync(netease.singsUrl, [songId]) url = yield from future url = url[0].get('url') musicInfo['url'] = url self.download.emit(musicInfo) def searchBy(self, index): currentWidgetName = self.searchArea.contentsTab.tabText(index) self.currentName = currentWidgetName self.search(currentWidgetName) @toTask def search(self, name): """接受name信息,由这个引擎进行搜索。""" searchEngineer = self.searchEngineers[name] data = yield from aAsync(searchEngineer.search, self.searchArea.text) if not data['songCount']: songsIds = [] data['songs'] = [] else: songsIds = [i['id'] for i in data['songs']] if name == '网易云': songsDetail = {i: 'http' for i in songsIds} elif name == '虾米' or name == 'QQ': songsDetail = {i['id']: i['mp3Url'] for i in data['songs']} # 进行重新编辑方便索引。 songs = data['songs'] data['songs'] = [{ 'name': i['name'], 'artists': i['ar'], 'picUrl': i['al']['picUrl'], 'mp3Url': songsDetail[i['id']], 'duration': i['dt'], 'music_id': i['id'], 'lyric': i.get('lyric') } for i in songs] songsCount = data['songCount'] # 总数是0即没有找到。 if not songsCount: songs = [] else: songs = data['songs'] self.setSingsData(songs) def setSingsData(self, data): # 单曲搜索结果。 searchArea = self.searchArea.contentsTab.currentWidget() if not len(data): # self.contentsTab.addTab() searchArea.noSingsContentsLabel.setText( self.noContents.format(self.searchArea.text, '单曲')) searchArea.singsResultTable.hide() searchArea.noSingsContentsLabel.show() else: searchArea.singsResultTable.setRowCount(len(data)) musicList = [] for count, datas in enumerate(data): picUrl = datas['picUrl'] url = datas['mp3Url'] name = datas['name'] authors = ','.join([t['name'] for t in datas['artists']]) duration = self.transTime(datas['duration'] / 1000) musicId = datas['music_id'] searchArea.singsResultTable.setItem(count, 0, QTableWidgetItem(name)) searchArea.singsResultTable.setItem(count, 1, QTableWidgetItem(authors)) searchArea.singsResultTable.setItem(count, 2, QTableWidgetItem(duration)) musicList.append({ 'url': url, 'name': name, 'time': duration, 'author': authors, 'music_img': picUrl, 'music_id': musicId }) searchArea.noSingsContentsLabel.hide() searchArea.singsResultTable.show() self.musicList = musicList def itemDoubleClickedEvent(self): currentRow = self.searchArea.contentsTab.currentWidget( ).singsResultTable.currentRow() data = self.musicList[currentRow] self.searchArea.parent.playWidgets.setPlayerAndPlayList(data) def contextEvent(self, event): currentWidget = self.searchResultTableIndexs.get(self.currentName) if not currentWidget: return item = currentWidget.itemAt(currentWidget.mapFromGlobal(QCursor.pos())) self.menu = QMenu(currentWidget) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class ConfigDetailSings(QObject): download = pyqtSignal(dict) def __init__(self, parent=None): super(ConfigDetailSings, self).__init__() self.detailSings = parent self.musicList = [] self.currentIndex = 0 self.grandparent = self.detailSings.parent self.player = self.grandparent.playWidgets.player self.playList = self.grandparent.playWidgets self.currentMusic = self.grandparent.playWidgets.currentMusic self.transTime = transTime self.detailSings.singsTable.contextMenuEvent = self.singsFrameContextMenuEvent self.bindConnect() self.setContextMenu() def bindConnect(self): self.detailSings.playAllButton.clicked.connect(self.addAllMusicToPlayer) self.detailSings.singsTable.itemDoubleClicked.connect(self.itemDoubleClickedEvent) def setContextMenu(self): self.actionNextPlay = QAction('下一首播放', self) self.actionNextPlay.triggered.connect(self.addToNextPlay) self.actionDownloadSong = QAction('下载', self) self.actionDownloadSong.triggered.connect(self.downloadSong) def addToNextPlay(self): data = self.musicList[self.currentIndex] self.player.setAllMusics([data]) self.playList.playList.addMusic(data) self.playList.playList.addPlayList(data['name'], data['author'], data['time']) @toTask def downloadSong(self, x): musicInfo = self.musicList[self.currentIndex] url = musicInfo.get('url') if 'http:' not in url and 'https:' not in url: songId = musicInfo.get('music_id') future = aAsync(netease.singsUrl, [songId]) url = yield from future url = url[0].get('url') musicInfo['url'] = url self.download.emit(musicInfo) def addAllMusicToPlayer(self): self.playList.setPlayerAndPlaylists(self.musicList) def setupDetailFrames(self, datas, singsUrls, singsIds): result = datas self.musicList = [] self.detailSings.singsTable.clearContents() self.detailSings.titleLabel.setText(result['name']) self.detailSings.authorName.setText(result['creator']['nickname']) description = result['description'] # 有些没有简介会报错的。 if not description: description = '' self.detailSings.descriptionText.setText(description) # 这边添加歌曲的信息到table。 self.detailSings.singsTable.setRowCount(result['trackCount']) for i, j, t, x in zip(result['tracks'], range(result['trackCount']), singsUrls, singsIds): names = i['name'] musicName = QTableWidgetItem(names) self.detailSings.singsTable.setItem(j, 0, musicName) author = i['artists'][0]['name'] musicAuthor = QTableWidgetItem(author) self.detailSings.singsTable.setItem(j, 1, musicAuthor) times = self.transTime(i['duration']/1000) musicTime = QTableWidgetItem(times) self.detailSings.singsTable.setItem(j, 2, musicTime) music_img = i['album']['blurPicUrl'] lyric = i.get('lyric') self.musicList.append({'url': t, 'name': names, 'time':times, 'author':author, 'music_img': music_img, 'music_id':x, 'lyric': lyric}) # 事件。 def itemDoubleClickedEvent(self): currentRow = self.detailSings.singsTable.currentRow() data = self.musicList[currentRow] self.playList.setPlayerAndPlayList(data) def singsFrameContextMenuEvent(self, event): item = self.detailSings.singsTable.itemAt(self.detailSings.singsTable.mapFromGlobal(QCursor.pos())) self.menu = QMenu(self.detailSings.singsTable) self.menu.addAction(self.actionNextPlay) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class ConfigSearchArea(QObject): download = pyqtSignal(dict) def __init__(self, searchArea): super(ConfigSearchArea, self).__init__() # current show-table's index. self.currentIndex = 0 # current widgets name。 self.currentName = '网易云' # parent. self.searchArea = searchArea # get storage folder self.downloadFolder = self.searchArea.parent.config.getDownloadFolder() self.transTime = addition.itv2time self.searchEngineers = {'网易云': netease, '虾米': xiami, 'QQ': qqApi} # TODO # to config singsFrameBase instead of configing them respective. self.searchResultTableIndexs = {'网易云':self.searchArea.neteaseSearchFrame.singsResultTable, '虾米':self.searchArea.xiamiSearchFrame.singsResultTable , 'QQ':self.searchArea.qqSearchFrame.singsResultTable} self.musicList = [] self.noContents = "很抱歉 未能找到关于<font style='text-align: center;' color='#23518F'>“{0}”</font>的{1}。" self.bindConnect() self.setContextMenu() def bindConnect(self): self.searchArea.contentsTab.tabBarClicked.connect(self.searchBy) self.searchArea.neteaseSearchFrame.singsResultTable.itemDoubleClicked.connect(self.itemDoubleClickedEvent) self.searchArea.xiamiSearchFrame.singsResultTable.itemDoubleClicked.connect(self.itemDoubleClickedEvent) self.searchArea.qqSearchFrame.singsResultTable.itemDoubleClicked.connect(self.itemDoubleClickedEvent) self.searchArea.neteaseSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent self.searchArea.xiamiSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent self.searchArea.qqSearchFrame.singsResultTable.contextMenuEvent = self.contextEvent def setContextMenu(self): self.actionDownloadSong = QAction('下载', self) self.actionDownloadSong.triggered.connect(self.downloadSong) @toTask def downloadSong(self, x): # x is useless, but must be. # musicInfo = self.musicList[self.currentIndex] url = musicInfo.get('url') if 'http:' not in url and 'https:' not in url: songId = musicInfo.get('music_id') future = aAsync(netease.singsUrl, [songId]) url = yield from future url = url[0].get('url') musicInfo['url'] = url self.download.emit(musicInfo) def searchBy(self, index): currentWidgetName = self.searchArea.contentsTab.tabText(index) self.currentName = currentWidgetName self.search(currentWidgetName) @toTask def search(self, name): """接受name信息,由这个引擎进行搜索。""" searchEngineer = self.searchEngineers[name] data = yield from aAsync(searchEngineer.search, self.searchArea.text) if not data['songCount']: songsIds = [] data['songs'] = [] else: songsIds = [i['id'] for i in data['songs']] if name == '网易云': songsDetail = {i:'http' for i in songsIds} elif name == '虾米' or name == 'QQ': songsDetail = {i['id']:i['mp3Url'] for i in data['songs']} # 进行重新编辑方便索引。 songs = data['songs'] data['songs'] = [{'name':i['name'], 'artists': i['ar'], 'picUrl': i['al']['picUrl'], 'mp3Url': songsDetail[i['id']], 'duration': i['dt'], 'music_id':i['id'], 'lyric': i.get('lyric')} for i in songs] songsCount = data['songCount'] # 总数是0即没有找到。 if not songsCount: songs = [] else: songs = data['songs'] self.setSingsData(songs) def setSingsData(self, data): # 单曲搜索结果。 searchArea = self.searchArea.contentsTab.currentWidget() if not len(data): # self.contentsTab.addTab() searchArea.noSingsContentsLabel.setText(self.noContents.format(self.searchArea.text, '单曲')) searchArea.singsResultTable.hide() searchArea.noSingsContentsLabel.show() else: searchArea.singsResultTable.setRowCount(len(data)) musicList = [] for count, datas in enumerate(data): picUrl = datas['picUrl'] url = datas['mp3Url'] name = datas['name'] authors = ','.join([t['name'] for t in datas['artists']]) duration = self.transTime(datas['duration']/1000) musicId = datas['music_id'] searchArea.singsResultTable.setItem(count, 0, QTableWidgetItem(name)) searchArea.singsResultTable.setItem(count, 1, QTableWidgetItem(authors)) searchArea.singsResultTable.setItem(count, 2, QTableWidgetItem(duration)) musicList.append({'url': url, 'name': name, 'time':duration, 'author':authors, 'music_img': picUrl, 'music_id': musicId}) searchArea.noSingsContentsLabel.hide() searchArea.singsResultTable.show() self.musicList = musicList def itemDoubleClickedEvent(self): currentRow = self.searchArea.contentsTab.currentWidget().singsResultTable.currentRow() data = self.musicList[currentRow] self.searchArea.parent.playWidgets.setPlayerAndPlayList(data) def contextEvent(self, event): currentWidget = self.searchResultTableIndexs.get(self.currentName) if not currentWidget: return item = currentWidget.itemAt(currentWidget.mapFromGlobal(QCursor.pos())) self.menu = QMenu(currentWidget) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())
class ConfigDetailSings(QObject): def __init__(self, parent=None): super(ConfigDetailSings, self).__init__() self.detailSings = parent self.musicList = [] self.currentIndex = 0 self.grandparent = self.detailSings.parent self.player = self.grandparent.playWidgets.player self.playList = self.grandparent.playWidgets self.currentMusic = self.grandparent.playWidgets.currentMusic self.transTime = transTime self.detailSings.singsTable.contextMenuEvent = self.singsFrameContextMenuEvent self.bindClicked() self.setContextMenu() def bindClicked(self): self.detailSings.playAllButton.clicked.connect( self.addAllMusicToPlayer) self.detailSings.singsTable.itemDoubleClicked.connect( self.itemDoubleClickedEvent) def setContextMenu(self): self.actionNextPlay = QAction('下一首播放', self) self.actionNextPlay.triggered.connect(self.addToNextPlay) self.actionDownloadSong = QAction('下载', self) self.actionDownloadSong.triggered.connect(self.downloadSong) def addToNextPlay(self): data = self.musicList[self.currentIndex] self.player.setAllMusics([data]) self.playList.playList.addMusic(data) self.playList.playList.addPlayList(data['name'], data['author'], data['time']) @toTask def downloadSong(self, x): musicInfo = self.musicList[self.currentIndex] url = musicInfo.get('url') if 'http:' not in url and 'https:' not in url: songId = musicInfo.get('music_id') future = aAsync(netease.singsUrl, [songId]) url = yield from future url = url[0].get('url') musicInfo['url'] = url else: url = musicInfo.get('url') future = aAsync(myRequests.httpRequest, url, 'GET') data = yield from future if 'downloads' not in os.listdir('.'): os.mkdir('downloads') allMusicName = re.search(r'.*\.[a-zA-Z0-9]+', url[url.rfind('/') + 1:]).group(0) if allMusicName: musicSuffix = allMusicName[allMusicName.rfind('.') + 1:] musicName = '{name}.{suf}'.format(name=musicInfo.get('name') + ' - ' + musicInfo.get('author'), suf=musicSuffix) else: # TODO MD5。 musicName = "random_name.mp3" with open('downloads/{musicName}'.format(musicName=musicName), 'wb') as f: f.write(data.content) self.grandparent.systemTray.showMessage( "~~~", '{musicName} 下载完成'.format(musicName=musicName)) def addAllMusicToPlayer(self): self.playList.setPlayerAndPlaylists(self.musicList) def setupDetailFrames(self, datas, singsUrls, singsIds): result = datas self.musicList = [] self.detailSings.singsTable.clearContents() self.detailSings.titleLabel.setText(result['name']) self.detailSings.authorName.setText(result['creator']['nickname']) # 简介有些太长了,暂时只截取前107个字符。 description = result['description'] # 有些没有简介会报错的。 if not description: description = '' self.detailSings.descriptionText.setText(description) # 这边添加歌曲的信息到table。 self.detailSings.singsTable.setRowCount(result['trackCount']) for i, j, t, x in zip(result['tracks'], range(result['trackCount']), singsUrls, singsIds): names = i['name'] musicName = QTableWidgetItem(names) self.detailSings.singsTable.setItem(j, 0, musicName) author = i['artists'][0]['name'] musicAuthor = QTableWidgetItem(author) self.detailSings.singsTable.setItem(j, 1, musicAuthor) times = self.transTime(i['duration'] / 1000) musicTime = QTableWidgetItem(times) self.detailSings.singsTable.setItem(j, 2, musicTime) music_img = i['album']['blurPicUrl'] lyric = i.get('lyric') self.musicList.append({ 'url': t, 'name': names, 'time': times, 'author': author, 'music_img': music_img, 'music_id': x, 'lyric': lyric }) # 事件。 def itemDoubleClickedEvent(self): currentRow = self.detailSings.singsTable.currentRow() data = self.musicList[currentRow] self.playList.setPlayerAndPlayList(data) def singsFrameContextMenuEvent(self, event): item = self.detailSings.singsTable.itemAt( self.detailSings.singsTable.mapFromGlobal(QCursor.pos())) self.menu = QMenu(self.detailSings.singsTable) self.menu.addAction(self.actionNextPlay) self.menu.addAction(self.actionDownloadSong) try: self.currentIndex = item.row() - 1 # 在索引是最后一行时会获取不到。 except: self.currentIndex = -1 self.menu.exec_(QCursor.pos())