예제 #1
0
    def __init__(self, parent=None):
        """头部区域,包括图标/搜索/设置/登陆/最大/小化/关闭。"""

        super(Header, self).__init__()
        self.setObjectName('Header')

        self.parent = parent

        self.searchThread = RequestThread(self)
        self.searchThread.setTarget(self.search)
        self.searchThread.finished.connect(self.searchFinished)

        self.loginThread = RequestThread(self)
        self.loginThread.breakSignal.connect(self.emitWarning)
        self.loginThread.finished.connect(self.loginFinished)

        self.loadUserPlaylistThread = RequestThread(self)
        self.loadUserPlaylistThread.finished.connect(
            self.loadUserPlaylistFinished)

        self.loginBox = LoginBox(self)
        self.loginBox.connectLogin(self.login)

        #
        self.loginInfor = {}

        with open('QSS/header.qss', 'r', encoding='utf-8') as f:
            self.setStyleSheet(f.read())

        # 加载按钮设置。
        self.setButtons()
        # 加载标签设置。
        self.setLabels()
        # 加载输入框设置。
        self.setLineEdits()
        # 加载小细线装饰。
        self.setLines()
        # 加载布局设置。
        self.setLayouts()

        # 加载cookies.
        self.loadCookies()
예제 #2
0
    def __init__(self, parent=None):
        """头部区域,包括图标/搜索/设置/登陆/最大/小化/关闭。"""

        super(Header, self).__init__()
        self.setObjectName('Header')

        self.parent = parent

        self.loginBox = LoginBox(self)

        with open('QSS/header.qss', 'r', encoding='utf-8') as f:
            self.setStyleSheet(f.read())

        # 加载按钮设置。
        self.setButtons()
        # 加载标签设置。
        self.setLabels()
        # 加载输入框设置。
        self.setLineEdits()
        # 加载小细线装饰。
        self.setLines()
        # 加载布局设置。
        self.setLayouts()
예제 #3
0
class Header(QFrame):

    # 装饰器初始化时还不会调用__init__.
    loginCookiesFolder = 'cookies/headers/loginInfor.cks'
    allCookiesFolder = [loginCookiesFolder]

    def __init__(self, parent=None):
        """头部区域,包括图标/搜索/设置/登陆/最大/小化/关闭。"""

        super(Header, self).__init__()
        self.setObjectName('Header')

        self.parent = parent

        self.searchThread = RequestThread(self)
        self.searchThread.setTarget(self.search)
        self.searchThread.finished.connect(self.searchFinished)

        self.loginThread = RequestThread(self)
        self.loginThread.breakSignal.connect(self.emitWarning)
        self.loginThread.finished.connect(self.loginFinished)

        self.loadUserPlaylistThread = RequestThread(self)
        self.loadUserPlaylistThread.finished.connect(
            self.loadUserPlaylistFinished)

        self.loginBox = LoginBox(self)
        self.loginBox.connectLogin(self.login)

        #
        self.loginInfor = {}

        with open('QSS/header.qss', 'r', encoding='utf-8') as f:
            self.setStyleSheet(f.read())

        # 加载按钮设置。
        self.setButtons()
        # 加载标签设置。
        self.setLabels()
        # 加载输入框设置。
        self.setLineEdits()
        # 加载小细线装饰。
        self.setLines()
        # 加载布局设置。
        self.setLayouts()

        # 加载cookies.
        self.loadCookies()

    # 布局。
    def setButtons(self):
        """创建所有的按钮。"""

        self.closeButton = QPushButton('×', self)
        self.closeButton.setObjectName("closeButton")
        self.closeButton.setMinimumSize(21, 17)
        self.closeButton.clicked.connect(self.parent.close)

        self.showminButton = QPushButton('_', self)
        self.showminButton.setObjectName("minButton")
        self.showminButton.setMinimumSize(21, 17)
        self.showminButton.clicked.connect(self.parent.showMinimized)

        self.loginButton = QPushButton("未登录 ▼", self)
        self.loginButton.setObjectName("loginButton")
        self.loginButton.clicked.connect(self.showLoginBox)

        self.prevButton = QPushButton("<")
        self.prevButton.setObjectName("prevButton")
        self.prevButton.setMaximumSize(28, 22)
        self.prevButton.setMinimumSize(28, 22)
        self.prevButton.clicked.connect(self.parent.prevTab)

        self.nextButton = QPushButton(">")
        self.nextButton.setObjectName("nextButton")
        self.nextButton.setMaximumSize(28, 22)
        self.nextButton.setMinimumSize(28, 22)
        self.nextButton.clicked.connect(self.parent.nextTab)

    def setLabels(self):
        """创建所需的所有标签。"""
        self.logoLabel = PicLabel(r'resource/format.png', 32, 32)

        self.descriptionLabel = QLabel(self)
        self.descriptionLabel.setText("<b>Music<b>")

        self.userPix = PicLabel(r'resource/no_music.png', 32, 32,
                                r'resource/user_pic_mask.png')
        self.userPix.setMinimumSize(22, 22)
        self.userPix.setObjectName("userPix")

    def setLineEdits(self):
        """创建搜素框。"""
        self.searchLine = addition.SearchLineEdit(self)
        self.searchLine.setPlaceholderText("搜索音乐, 歌手, 歌词, 用户")
        self.searchLine.setButtonSlot(self.searchThread.start)

    def setLines(self):
        """设置装饰用小细线。"""
        self.line1 = QFrame(self)
        self.line1.setObjectName("line1")
        self.line1.setFrameShape(QFrame.VLine)
        self.line1.setFrameShadow(QFrame.Plain)
        self.line1.setMaximumSize(1, 25)

    def setLayouts(self):
        """设置布局。"""
        self.mainLayout = QHBoxLayout()
        self.mainLayout.setSpacing(0)
        self.mainLayout.addWidget(self.logoLabel)
        self.mainLayout.addWidget(self.descriptionLabel)
        self.mainLayout.addSpacing(70)
        self.mainLayout.addWidget(self.prevButton)
        self.mainLayout.addWidget(self.nextButton)
        self.mainLayout.addSpacing(10)
        self.mainLayout.addWidget(self.searchLine)
        # self.mainLayout.addWidget(self.searchButton)
        self.mainLayout.addStretch(1)
        self.mainLayout.addWidget(self.userPix)
        self.mainLayout.addSpacing(7)
        self.mainLayout.addWidget(self.loginButton)
        self.mainLayout.addSpacing(7)
        self.mainLayout.addWidget(self.line1)
        self.mainLayout.addSpacing(30)
        self.mainLayout.addWidget(self.showminButton)
        self.mainLayout.addSpacing(3)
        self.mainLayout.addWidget(self.closeButton)

        self.setLayout(self.mainLayout)

    # 功能。
    def search(self):
        text = self.searchLine.text()
        self.result = netEase.search(text)

        if not self.result['songCount']:
            songsIds = []
            self.result['songs'] = []
        else:
            songsIds = [i['id'] for i in self.result['songs']]
            self.songsDetail = netEase.singsUrl(songsIds)
            self.songsDetail = {i['id']: i['url'] for i in self.songsDetail}
            # 进行重新编辑方便索引。
            songs = self.result['songs']
            self.result['songs'] = [{
                'name': i['name'],
                'artists': i['ar'],
                'picUrl': i['al']['picUrl'],
                'mp3Url': self.songsDetail[i['id']],
                'duration': i['dt']
            } for i in songs]

    def searchFinished(self):
        text = self.searchLine.text()
        songsCount = self.result['songCount']

        # 总数是0即没有找到。
        if not songsCount:
            songs = []
        else:
            songs = self.result['songs']

        self.parent.searchArea.setText(text)

        self.parent.searchArea.setSingsData(songs)

        self.parent.setTabIndex(3)

    def showLoginBox(self):
        self.loginBox.open()

    def login(self):
        informations = self.loginBox.checkAndGetLoginInformation()

        if not informations:
            return

        self.loginThread.setTarget(self.loadLoginInformations)
        self.loginThread.setArgs(informations)
        self.loginThread.start()

    def loadLoginInformations(self, informations: tuple):
        result = netEase.login(*informations)
        # 网络不通或其他问题。
        if not result:
            self.loginThread.breakSignal.emit('请检查网络后重试~.')
            return

        code = result.get('code')
        if code != 200 or code != '200':
            self.loginThread.breakSignal.emit(str(result.get('msg')))

        self.loginInfor = result

    def loginFinished(self):
        self.loginBox.accept()
        self.setUserData()
        # profile = self.loginInfor['profile']
        # avatarUrl = profile['avatarUrl']
        # self.userPix.setSrc(avatarUrl)

        # # 加载该账户创建及喜欢的歌单。
        # userId = profile['userId']
        # self.loadUserPlaylistThread.setTarget(netEase.user_playlist)
        # self.loadUserPlaylistThread.setArgs(userId)
        # self.loadUserPlaylistThread.start()

        # nickname = profile['nickname']
        # self.loginButton.setText(nickname)

    def setUserData(self):
        profile = self.loginInfor['profile']
        avatarUrl = profile['avatarUrl']
        self.userPix.setSrc(avatarUrl)
        # 加载该账户创建及喜欢的歌单。
        userId = profile['userId']
        self.loadUserPlaylistThread.setTarget(netEase.user_playlist)
        self.loadUserPlaylistThread.setArgs(userId)
        self.loadUserPlaylistThread.start()

        nickname = profile['nickname']
        self.loginButton.setText(nickname)

    def emitWarning(self, warningStr):
        self.loginBox.setWarningAndShowIt(warningStr)

    def exitLogin(self):
        self.loginButton.setText('未登录 ▼')
        self.loginButton.clicked.connect(self.showLoginBox)
        self.userPix.setSrc('resource/nouser.png')

    def loadUserPlaylistFinished(self):
        result = self.loadUserPlaylistThread.result
        self.parent.navigation.setPlaylists(result)

    @checkFolder(allCookiesFolder)
    def saveCookies(self):
        with open(self.loginCookiesFolder, 'wb') as f:
            pickle.dump(self.loginInfor, f)

    @checkFolder(allCookiesFolder)
    def loadCookies(self):
        with open(self.loginCookiesFolder, 'rb') as f:
            self.loginInfor = pickle.load(f)
        self.setUserData()

    # 事件。
    """重写鼠标事件,实现窗口拖动。"""

    def mousePressEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.parent.m_drag = True
            self.parent.m_DragPosition = event.globalPos() - self.parent.pos()
            event.accept()

    def mouseMoveEvent(self, event):
        try:
            if event.buttons() and Qt.LeftButton:
                self.parent.move(event.globalPos() -
                                 self.parent.m_DragPosition)
                event.accept()
        except AttributeError:
            pass

    def mouseReleaseEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.m_drag = False