def __createWidgets(self): """ 创建小部件 """ self.delayTimer = QTimer(self) # 创建滚动区域和抬头 self.scrollArea = ScrollArea(self) self.scrollWidget = QWidget() self.editAlbumInfoLabel = QLabel("编辑专辑信息", self) # 上半部分 self.albumCover = AlbumCoverWindow( self.cover_path, (170, 170), self.scrollWidget ) self.albumNameLineEdit = LineEdit(self.albumName, self.scrollWidget) self.albumSongerLineEdit = LineEdit(self.songer, self.scrollWidget) self.tconLineEdit = LineEdit(self.tcon, self.scrollWidget) self.albumNameLabel = QLabel("专辑标题", self.scrollWidget) self.albumSongerLabel = QLabel("专辑歌手", self.scrollWidget) self.tconLabel = QLabel("类型", self.scrollWidget) # 下半部分 self.songInfoWidget_list = [] for songInfo in self.songInfo_list: songInfoWidget = SongInfoWidget(songInfo, self.scrollWidget) songInfoWidget.isTrackNumEmptySig.connect(self.__trackNumEmptySlot) self.songInfoWidget_list.append(songInfoWidget) self.saveButton = PerspectivePushButton("保存", self) self.cancelButton = PerspectivePushButton("取消", self) # 创建gif self.loadingLabel = QLabel(self) self.movie = QMovie(r"app\resource\images\loading_gif\loading.gif", parent=self)
def __init__(self, songInfo: dict, parent=None): super().__init__(parent) self.songInfo = songInfo # 创建小部件 self.trackNumLabel = QLabel("曲目", self) self.songNameLabel = QLabel("歌名", self) self.songerLabel = QLabel("歌手", self) self.trackNumLineEdit = LineEdit(songInfo["tracknumber"], self, False) self.songNameLineEdit = LineEdit(songInfo["songName"], self) self.songerLineEdit = LineEdit(songInfo["songer"], self) self.errorIcon = ErrorIcon(self) self.bottomErrorIcon = ErrorIcon(self) self.bottomErrorLabel = QLabel("曲目必须是1000以下的数字", self) # 初始化 self.__initWidget()
def __createWidgets(self): """ 实例化小部件 """ # 实例化按钮 self.saveButton = PerspectivePushButton("保存", self) self.cancelButton = PerspectivePushButton("取消", self) # 实例化标签 self.yearLabel = QLabel("年", self) self.tconLabel = QLabel("类型", self) self.diskLabel = QLabel("光盘", self) self.trackNumLabel = QLabel("曲目", self) self.songNameLabel = QLabel("歌曲名", self) self.songPathLabel = QLabel("文件位置", self) self.albumNameLabel = QLabel("专辑标题", self) self.songerNameLabel = QLabel("歌曲歌手", self) self.albumSongerLabel = QLabel("专辑歌手", self) self.editInfoLabel = QLabel("编辑歌曲信息", self) self.songPath = QLabel(self.songInfo["songPath"], self) self.emptyTrackErrorIcon = ErrorIcon(self) self.bottomErrorIcon = ErrorIcon(self) self.bottomErrorLabel = QLabel(self) # 实例化单行输入框 self.diskEditLine = LineEdit("1", self) self.tconEditLine = LineEdit(self.songInfo["tcon"], self) self.yearEditLine = LineEdit(self.songInfo["year"], self) self.albumNameEditLine = LineEdit(self.songInfo["album"], self) self.songNameEditLine = LineEdit(self.songInfo["songName"], self) self.songerNameEditLine = LineEdit(self.songInfo["songer"], self) self.albumSongerEditLine = LineEdit(self.songInfo["songer"], self) self.trackNumEditLine = LineEdit(self.songInfo["tracknumber"], self) # 流派补全 tcons = [ "POP流行", "Blues", "SOUNDTRACK原声", "Japanese Pop & Rock", "摇滚", "Soundtrack", "J-Pop", "RAP/HIP HOP", "Soundtrack", "古典", "经典", "Country", "R&B", "ROCK摇滚", "anime", ] self.tconCompleter = QCompleter(tcons, self) self.tconCompleter.setCompletionMode(QCompleter.InlineCompletion) self.tconCompleter.setCaseSensitivity(Qt.CaseInsensitive) self.tconEditLine.setCompleter(self.tconCompleter) # 创建集中管理小部件的列表 self.leftLabel_list = [ self.songNameLabel, self.trackNumLabel, self.albumNameLabel, self.tconLabel, ] self.rightLabel_list = [ self.songerNameLabel, self.diskLabel, self.albumSongerLabel, self.yearLabel, ] self.leftEditLine_list = [ self.songNameEditLine, self.trackNumEditLine, self.albumNameEditLine, self.tconEditLine, ] self.rightEditLine_list = [ self.songerNameEditLine, self.diskEditLine, self.albumSongerEditLine, self.yearEditLine, ] self.editLine_list = [ self.songNameEditLine, self.songerNameEditLine, self.trackNumEditLine, self.diskEditLine, self.albumNameEditLine, self.albumSongerEditLine, self.tconEditLine, self.yearEditLine, ]
class SubSongInfoEditPanel(QWidget): """ 歌曲信息编辑面板的子窗口 """ saveInfoSig = pyqtSignal(dict, dict) def __init__(self, songInfo: dict, parent): super().__init__(parent) self.songInfo = deepcopy(songInfo) self.oldSongInfo = deepcopy(songInfo) # 实例化小部件 self.__createWidgets() # 初始化小部件 self.__initWidget() self.__initLayout() self.setShadowEffect() # 设置层叠样式 self.__setQss() def __createWidgets(self): """ 实例化小部件 """ # 实例化按钮 self.saveButton = PerspectivePushButton("保存", self) self.cancelButton = PerspectivePushButton("取消", self) # 实例化标签 self.yearLabel = QLabel("年", self) self.tconLabel = QLabel("类型", self) self.diskLabel = QLabel("光盘", self) self.trackNumLabel = QLabel("曲目", self) self.songNameLabel = QLabel("歌曲名", self) self.songPathLabel = QLabel("文件位置", self) self.albumNameLabel = QLabel("专辑标题", self) self.songerNameLabel = QLabel("歌曲歌手", self) self.albumSongerLabel = QLabel("专辑歌手", self) self.editInfoLabel = QLabel("编辑歌曲信息", self) self.songPath = QLabel(self.songInfo["songPath"], self) self.emptyTrackErrorIcon = ErrorIcon(self) self.bottomErrorIcon = ErrorIcon(self) self.bottomErrorLabel = QLabel(self) # 实例化单行输入框 self.diskEditLine = LineEdit("1", self) self.tconEditLine = LineEdit(self.songInfo["tcon"], self) self.yearEditLine = LineEdit(self.songInfo["year"], self) self.albumNameEditLine = LineEdit(self.songInfo["album"], self) self.songNameEditLine = LineEdit(self.songInfo["songName"], self) self.songerNameEditLine = LineEdit(self.songInfo["songer"], self) self.albumSongerEditLine = LineEdit(self.songInfo["songer"], self) self.trackNumEditLine = LineEdit(self.songInfo["tracknumber"], self) # 流派补全 tcons = [ "POP流行", "Blues", "SOUNDTRACK原声", "Japanese Pop & Rock", "摇滚", "Soundtrack", "J-Pop", "RAP/HIP HOP", "Soundtrack", "古典", "经典", "Country", "R&B", "ROCK摇滚", "anime", ] self.tconCompleter = QCompleter(tcons, self) self.tconCompleter.setCompletionMode(QCompleter.InlineCompletion) self.tconCompleter.setCaseSensitivity(Qt.CaseInsensitive) self.tconEditLine.setCompleter(self.tconCompleter) # 创建集中管理小部件的列表 self.leftLabel_list = [ self.songNameLabel, self.trackNumLabel, self.albumNameLabel, self.tconLabel, ] self.rightLabel_list = [ self.songerNameLabel, self.diskLabel, self.albumSongerLabel, self.yearLabel, ] self.leftEditLine_list = [ self.songNameEditLine, self.trackNumEditLine, self.albumNameEditLine, self.tconEditLine, ] self.rightEditLine_list = [ self.songerNameEditLine, self.diskEditLine, self.albumSongerEditLine, self.yearEditLine, ] self.editLine_list = [ self.songNameEditLine, self.songerNameEditLine, self.trackNumEditLine, self.diskEditLine, self.albumNameEditLine, self.albumSongerEditLine, self.tconEditLine, self.yearEditLine, ] def __initWidget(self): """ 初始化小部件的属性 """ self.resize(932, 652) self.setWindowFlags(Qt.FramelessWindowHint) self.setAttribute(Qt.WA_StyledBackground) # 默认选中歌名编辑框 self.songNameEditLine.setFocus() self.songNameEditLine.clearButton.show() # 给每个单行输入框设置大小 for editLine in self.editLine_list: editLine.setFixedSize(408, 40) # 设置按钮的大小 self.saveButton.setFixedSize(165, 41) self.cancelButton.setFixedSize(165, 41) # 设置报警标签位置 self.bottomErrorLabel.setMinimumWidth(100) self.emptyTrackErrorIcon.move(7, 224) self.bottomErrorIcon.hide() self.bottomErrorLabel.hide() self.emptyTrackErrorIcon.hide() self.installEventFilter(self) # 如果曲目为空就禁用保存按钮并更改属性 self.trackNumEditLine.setProperty("hasText", "true") if not self.trackNumEditLine.text(): self.saveButton.setEnabled(False) self.emptyTrackErrorIcon.show() self.trackNumEditLine.setProperty("hasText", "false") # 给输入框设置过滤器 rex_trackNum = QRegExp(r"(\d)|([1-9]\d{1,2})") rex_year = QRegExp(r"\d{4}年{0,1}") validator_tracknum = QRegExpValidator(rex_trackNum, self.trackNumEditLine) validator_disk = QRegExpValidator(rex_trackNum, self.diskEditLine) validator_year = QRegExpValidator(rex_year, self.yearEditLine) self.trackNumEditLine.setValidator(validator_tracknum) self.diskEditLine.setValidator(validator_disk) self.yearEditLine.setValidator(validator_year) # 将曲目输入框数字改变的信号连接到槽函数 self.trackNumEditLine.textChanged.connect(self.checkTrackEditLine) # 将按钮点击信号连接到槽函数 self.saveButton.clicked.connect(self.saveInfo) if not self.parent(): self.cancelButton.clicked.connect(self.deleteLater) # 分配ID self.editInfoLabel.setObjectName("editSongInfo") self.songerNameEditLine.setObjectName("songer") self.albumSongerEditLine.setObjectName("songer") self.songPath.setObjectName("songPath") self.bottomErrorLabel.setObjectName("bottomErrorLabel") def __initLayout(self): """ 初始化小部件的排版 """ self.editInfoLabel.move(30, 30) self.songPathLabel.move(30, 470) self.songPath.move(30, 502) self.saveButton.move(566, 595) self.cancelButton.move(736, 595) label_top_y = 95 for i, (label_left, label_right) in enumerate( zip(self.leftLabel_list, self.rightLabel_list) ): label_left.setObjectName("infoTypeLabel") label_right.setObjectName("infoTypeLabel") label_left.move(30, label_top_y + i * 87) label_right.move(494, label_top_y + i * 87) editLine_top_y = 127 for i, (editLine_left, editLine_right) in enumerate( zip(self.leftEditLine_list, self.rightEditLine_list) ): editLine_left.move(30, editLine_top_y + i * 87) editLine_right.move(494, editLine_top_y + i * 87) # 调整高度 newSongPath, isWordWrap = autoWrap(self.songPath.text(), 100) if isWordWrap: self.songPath.setText(newSongPath) self.resize(self.width(), self.height() + 25) self.cancelButton.move(self.cancelButton.x(), self.cancelButton.y() + 25) self.saveButton.move(self.saveButton.x(), self.saveButton.y() + 25) # 调整报错标签的位置 self.bottomErrorIcon.move(30, self.height() - 110) self.bottomErrorLabel.move(55, self.height() - 112) def __setQss(self): """ 设置层叠样式表 """ with open(r"app\resource\css\infoEditPanel.qss", encoding="utf-8") as f: self.setStyleSheet(f.read()) def paintEvent(self, event): """ 绘制背景和阴影 """ # 创建画笔 self.pen = QPen(QColor(0, 153, 188)) painter = QPainter(self) # 绘制边框 painter.setPen(self.pen) painter.drawRect(0, 0, self.width() - 1, self.height() - 1) def saveInfo(self): """ 保存标签卡信息 """ album_list = adjustAlbumName(self.albumNameEditLine.text()) self.songInfo["songName"] = self.songNameEditLine.text() self.songInfo["songer"] = self.songerNameEditLine.text() self.songInfo["album"] = album_list[0] self.songInfo["modifiedAlbum"] = album_list[-1] # 根据后缀名选择曲目标签的写入方式 self.songInfo["tracknumber"] = self.trackNumEditLine.text() self.songInfo["tcon"] = self.tconEditLine.text() if self.yearEditLine.text()[:4] != "未知年份": self.songInfo["year"] = self.yearEditLine.text()[:4] + "年" else: self.songInfo["year"] = "未知年份" if not modifySongInfo(self.songInfo): self.bottomErrorLabel.setText("遇到未知错误,请稍后再试") self.bottomErrorLabel.show() self.bottomErrorIcon.show() else: self.saveInfoSig.emit(self.oldSongInfo, self.songInfo) self.parent().deleteLater() def checkTrackEditLine(self): """ 检查曲目输入框的内容是否为空 """ if not self.trackNumEditLine.text(): self.bottomErrorLabel.setText("曲目必须是1000以下的数字") self.bottomErrorLabel.show() self.emptyTrackErrorIcon.show() self.bottomErrorIcon.show() self.saveButton.setEnabled(False) self.trackNumEditLine.setProperty("hasText", "false") else: self.trackNumEditLine.setProperty("hasText", "true") self.bottomErrorLabel.hide() self.bottomErrorIcon.hide() self.emptyTrackErrorIcon.hide() self.saveButton.setEnabled(True) self.trackNumEditLine.setStyle(QApplication.style()) def setShadowEffect(self): """ 添加阴影 """ self.shadowEffect = QGraphicsDropShadowEffect(self) self.shadowEffect.setBlurRadius(50) self.shadowEffect.setOffset(0, 5) self.setGraphicsEffect(self.shadowEffect)
class SubAlbumInfoEditPanel(QWidget): """ 专辑信息编辑面板主界面 """ saveInfoSig = pyqtSignal(dict) adjustHeightSig = pyqtSignal() MAXHEIGHT = 755 def __init__(self, albumInfo: dict, parent): super().__init__(parent) self.albumInfo = albumInfo self.tcon = self.albumInfo["tcon"] # type:str self.songer = self.albumInfo["songer"] # type:str self.albumName = self.albumInfo["album"] # type:str self.cover_path = self.albumInfo["cover_path"] # type:str self.songInfo_list = self.albumInfo["songInfo_list"] # type:list self.newAlbumCoverPath = None # 创建小部件 self.__createWidgets() # 初始化 self.__initWidget() def __createWidgets(self): """ 创建小部件 """ self.delayTimer = QTimer(self) # 创建滚动区域和抬头 self.scrollArea = ScrollArea(self) self.scrollWidget = QWidget() self.editAlbumInfoLabel = QLabel("编辑专辑信息", self) # 上半部分 self.albumCover = AlbumCoverWindow( self.cover_path, (170, 170), self.scrollWidget ) self.albumNameLineEdit = LineEdit(self.albumName, self.scrollWidget) self.albumSongerLineEdit = LineEdit(self.songer, self.scrollWidget) self.tconLineEdit = LineEdit(self.tcon, self.scrollWidget) self.albumNameLabel = QLabel("专辑标题", self.scrollWidget) self.albumSongerLabel = QLabel("专辑歌手", self.scrollWidget) self.tconLabel = QLabel("类型", self.scrollWidget) # 下半部分 self.songInfoWidget_list = [] for songInfo in self.songInfo_list: songInfoWidget = SongInfoWidget(songInfo, self.scrollWidget) songInfoWidget.isTrackNumEmptySig.connect(self.__trackNumEmptySlot) self.songInfoWidget_list.append(songInfoWidget) self.saveButton = PerspectivePushButton("保存", self) self.cancelButton = PerspectivePushButton("取消", self) # 创建gif self.loadingLabel = QLabel(self) self.movie = QMovie(r"app\resource\images\loading_gif\loading.gif", parent=self) def __initWidget(self): """ 初始化小部件 """ self.setFixedWidth(936) self.setMaximumHeight(self.MAXHEIGHT) self.setAttribute(Qt.WA_StyledBackground) self.loadingLabel.setAttribute(Qt.WA_TranslucentBackground) self.loadingLabel.setMovie(self.movie) self.scrollArea.setWidget(self.scrollWidget) self.songInfoWidgetNum = len(self.songInfoWidget_list) # type:int # 初始化定时器 self.delayTimer.setInterval(300) self.delayTimer.timeout.connect(self.__showFileDialog) # 设置滚动区域的大小 if self.songInfoWidgetNum <= 4: self.scrollArea.resize(931, 216 + self.songInfoWidgetNum * 83) else: self.scrollArea.resize(931, 595) # 初始化布局 self.__initLayout() # 信号连接到槽 self.__connectSignalToSlot() # 分配ID self.scrollArea.setObjectName("infoEditScrollArea") self.editAlbumInfoLabel.setObjectName("editAlbumInfo") # 设置阴影和层叠样式 self.__setShadowEffect() self.__setQss() def __initLayout(self): """ 初始化布局 """ self.editAlbumInfoLabel.move(30, 30) self.scrollArea.move(2, 62) self.albumCover.move(30, 13) self.albumNameLabel.move(225, 7) self.albumSongerLabel.move(578, 7) self.tconLabel.move(225, 77) self.albumNameLineEdit.move(225, 36) self.albumSongerLineEdit.move(578, 36) self.tconLineEdit.move(225, 106) for i, songInfoWidget in enumerate(self.songInfoWidget_list): songInfoWidget.move(0, songInfoWidget.height() * i + 216) self.scrollWidget.resize(931, self.songInfoWidgetNum * 83 + 216) self.albumNameLineEdit.resize(327, 40) self.albumSongerLineEdit.resize(326, 40) self.tconLineEdit.resize(327, 40) self.saveButton.resize(168, 40) self.cancelButton.resize(168, 40) self.resize(936, self.scrollArea.y() + self.scrollArea.height() + 98) self.saveButton.move(563, self.height() - 16 - self.saveButton.height()) self.cancelButton.move(735, self.height() - 16 - self.saveButton.height()) def __setShadowEffect(self): """ 添加阴影 """ self.shadowEffect = QGraphicsDropShadowEffect(self) self.shadowEffect.setBlurRadius(50) self.shadowEffect.setOffset(0, 5) self.setGraphicsEffect(self.shadowEffect) def __setQss(self): """ 设置层叠样式表 """ with open(r"app\resource\css\infoEditPanel.qss", encoding="utf-8") as f: self.setStyleSheet(f.read()) def paintEvent(self, event): """ 绘制背景和阴影 """ # 创建画笔 painter = QPainter(self) # 绘制边框 pen = QPen(QColor(0, 153, 188)) painter.setPen(pen) painter.drawRect(0, 0, self.width() - 1, self.height() - 1) def __trackNumEmptySlot(self, isShowErrorMsg: bool): """ 如果曲目为空则禁用保存按钮 """ self.saveButton.setEnabled(not isShowErrorMsg) if not self.sender().bottomErrorLabel.isVisible(): # 获取曲目为空的小部件的index senderIndex = self.songInfoWidget_list.index(self.sender()) # 调整面板高度 self.__adjustWidgetPos(senderIndex, isShowErrorMsg) def saveErrorSlot(self, index: int): """ 保存信息失败槽函数 """ songInfoWidget = self.songInfoWidget_list[index] if not songInfoWidget.bottomErrorLabel.isVisible(): senderIndex = self.songInfoWidget_list.index(songInfoWidget) self.__adjustWidgetPos(senderIndex, True) songInfoWidget.setSaveSongInfoErrorMsgHidden(False) def saveCompleteSlot(self): """ 保存成功槽函数 """ self.saveAlbumCover() self.parent().deleteLater() def __adjustWidgetPos(self, senderIndex, isShowErrorMsg: bool): """ 调整小部件位置 """ # 调整面板高度 deltaHeight = 54 if isShowErrorMsg else -54 if self.height() == self.MAXHEIGHT: if deltaHeight < 0: height = self.scrollWidget.height() + deltaHeight if height < 600: self.resize(936, height + 155) self.adjustHeightSig.emit() self.scrollArea.resize(931, height) elif self.MAXHEIGHT - abs(deltaHeight) < self.height() < self.MAXHEIGHT: if deltaHeight > 0: self.resize(936, self.MAXHEIGHT) self.adjustHeightSig.emit() self.scrollArea.resize(931, 600) else: self.__adjustHeight(deltaHeight) elif self.height() <= self.MAXHEIGHT - abs(deltaHeight): self.__adjustHeight(deltaHeight) self.scrollWidget.resize(931, self.scrollWidget.height() + deltaHeight) self.saveButton.move(563, self.height() - 16 - self.saveButton.height()) self.cancelButton.move(735, self.height() - 16 - self.saveButton.height()) # 调整后面的小部件的位置 for songInfoWidget in self.songInfoWidget_list[senderIndex + 1 :]: songInfoWidget.move(0, songInfoWidget.y() + deltaHeight) def __adjustHeight(self, deltaHeight): """ 调整高度 """ self.resize(936, self.height() + deltaHeight) self.adjustHeightSig.emit() self.scrollArea.resize(931, self.scrollArea.height() + deltaHeight) def __saveAlbumInfo(self): """ 保存专辑信息 """ # 禁用小部件 self.__showLoadingGif() self.__setWidgetEnable(False) # 显示动图 # 更新标签信息 self.albumInfo["album"] = self.albumNameLineEdit.text() self.albumInfo["songer"] = self.albumSongerLineEdit.text() self.albumInfo["tcon"] = self.tconLineEdit.text() album_list = adjustAlbumName(self.albumNameLineEdit.text()) for songInfo, songInfoWidget in zip( self.songInfo_list, self.songInfoWidget_list ): songInfo["album"] = album_list[0] songInfo["modifiedAlbum"] = album_list[-1] songInfo["songName"] = songInfoWidget.songNameLineEdit.text() songInfo["songer"] = songInfoWidget.songerLineEdit.text() songInfo["tcon"] = self.tconLineEdit.text() # 根据后缀名选择曲目标签的写入方式 songInfo["tracknumber"] = songInfoWidget.trackNumLineEdit.text() self.saveInfoSig.emit(self.albumInfo) # 保存失败时重新启用编辑框 self.__setWidgetEnable(True) self.loadingLabel.hide() self.movie.stop() def __connectSignalToSlot(self): """ 信号连接到槽 """ self.saveButton.clicked.connect(self.__saveAlbumInfo) self.cancelButton.clicked.connect(self.parent().deleteLater) self.albumCover.clicked.connect(self.delayTimer.start) def __showFileDialog(self): """ 显示专辑图片选取对话框 """ self.delayTimer.stop() path, filterType = QFileDialog.getOpenFileName( self, "打开", "./", "所有文件(*.png;*.jpg;*.jpeg;*jpe;*jiff)" ) if path: # 复制图片到封面文件夹下 if os.path.abspath(self.cover_path) == path: return # 暂存图片地址并刷新图片 self.newAlbumCoverPath = path self.albumCover.setAlbumCover(path) def saveAlbumCover(self): """ 保存新专辑封面 """ if not self.newAlbumCoverPath: return with open(self.newAlbumCoverPath, "rb") as f: picData = f.read() # 给专辑中的所有文件写入同一张封面 # 更换文件夹下的封面图片 if self.newAlbumCoverPath == os.path.abspath(self.cover_path): return # 判断文件格式后修改后缀名 newSuffix = getPicSuffix(picData) # 如果封面路径是默认专辑封面,就修改封面路径 if self.cover_path == "app\\resource\\images\\未知专辑封面_200_200.png": self.cover_path = "app\\resource\\Album_Cover\\{0}\\{0}{1}".format( self.albumInfo["modifiedAlbum"], newSuffix ) with open(self.cover_path, "wb") as f: f.write(picData) oldName, oldSuffix = os.path.splitext(self.cover_path) if newSuffix != oldSuffix: os.rename(self.cover_path, oldName + newSuffix) self.cover_path = oldName + newSuffix self.albumInfo["cover_path"] = self.cover_path def __setWidgetEnable(self, isEnable: bool): """ 设置编辑框是否启用 """ self.setEnabled(isEnable) # 更新样式 self.setStyle(QApplication.style()) def __showLoadingGif(self): """ 显示正在加载动画 """ self.loadingLabel.resize(77, 77) self.loadingLabel.move( int(self.width() / 2 - self.loadingLabel.width() / 2), int(self.height() / 2 - self.loadingLabel.height() / 2), ) self.loadingLabel.raise_() self.loadingLabel.show() self.movie.start()
class SongInfoWidget(QWidget): """ 歌曲信息窗口 """ isTrackNumEmptySig = pyqtSignal(bool) def __init__(self, songInfo: dict, parent=None): super().__init__(parent) self.songInfo = songInfo # 创建小部件 self.trackNumLabel = QLabel("曲目", self) self.songNameLabel = QLabel("歌名", self) self.songerLabel = QLabel("歌手", self) self.trackNumLineEdit = LineEdit(songInfo["tracknumber"], self, False) self.songNameLineEdit = LineEdit(songInfo["songName"], self) self.songerLineEdit = LineEdit(songInfo["songer"], self) self.errorIcon = ErrorIcon(self) self.bottomErrorIcon = ErrorIcon(self) self.bottomErrorLabel = QLabel("曲目必须是1000以下的数字", self) # 初始化 self.__initWidget() def __initWidget(self): """ 初始化小部件 """ self.resize(903, 83) self.__initLayout() # 分配ID和属性 self.bottomErrorLabel.setObjectName("bottomErrorLabel") self.trackNumLineEdit.setProperty("hasClearBt", "false") self.trackNumLineEdit.setProperty("hasText", "false") self.__setTrackNumEmptyErrorMsgHidden(True) self.__checkTrackNum() # 给输入框设置过滤器 rex_trackNum = QRegExp(r"(\d)|([1-9]\d{1,2})") validator_tracknum = QRegExpValidator(rex_trackNum, self.trackNumLineEdit) self.trackNumLineEdit.setValidator(validator_tracknum) # 信号连接到槽 self.trackNumLineEdit.textChanged.connect(self.__checkTrackNum) def __initLayout(self): """ 初始化布局 """ self.trackNumLabel.move(30, 0) self.songNameLabel.move(135, 0) self.songerLabel.move(532, 0) self.trackNumLineEdit.move(30, 26) self.songNameLineEdit.move(135, 26) self.songerLineEdit.move(532, 26) self.errorIcon.move(7, 36) self.bottomErrorIcon.move(30, 95) self.bottomErrorLabel.move(59, 94) self.trackNumLineEdit.resize(80, 41) self.songerLineEdit.resize(371, 41) self.songNameLineEdit.resize(371, 41) def __checkTrackNum(self): """ 检查曲目是否为空,为空则发出禁用保存按钮信号,并显示错误标签 """ if ( self.trackNumLineEdit.text() and self.trackNumLineEdit.property("hasText") == "false" ): self.__setTrackNumEmptyErrorMsgHidden(True) self.resize(903, 83) self.isTrackNumEmptySig.emit(False) self.trackNumLineEdit.setProperty("hasText", "true") self.setStyle(QApplication.style()) elif not self.trackNumLineEdit.text(): self.isTrackNumEmptySig.emit(True) self.resize(903, 137) self.__setTrackNumEmptyErrorMsgHidden(False) self.trackNumLineEdit.setProperty("hasText", "false") self.setStyle(QApplication.style()) def __setTrackNumEmptyErrorMsgHidden(self, isHidden: bool): """ 设置曲目为空错误信息是否显示 """ self.errorIcon.setHidden(isHidden) self.bottomErrorIcon.setHidden(isHidden) self.bottomErrorLabel.setText("曲目必须是1000以下的数字") self.bottomErrorLabel.adjustSize() self.bottomErrorLabel.setHidden(isHidden) def setSaveSongInfoErrorMsgHidden(self, isHidden: bool): """ 设置保存歌曲元数据错误信息是否显示 """ if not isHidden: self.resize(903, 137) else: self.resize(903, 83) self.errorIcon.setHidden(isHidden) self.bottomErrorIcon.setHidden(isHidden) self.bottomErrorLabel.setText("遇到未知错误,请稍后再试") self.bottomErrorLabel.adjustSize() self.bottomErrorLabel.setHidden(isHidden) def setLineEditEnable(self, isEnable: bool): """ 设置编辑框是否启用 """ self.songNameLineEdit.setEnabled(isEnable) self.songerLineEdit.setEnabled(isEnable) self.trackNumLineEdit.setEnabled(isEnable)
def __createWidgets(self): """ 实例化小部件 """ # 实例化按钮 self.saveButton = PerspectivePushButton("保存", self) self.cancelButton = PerspectivePushButton("取消", self) # 实例化标签 self.yearLabel = QLabel("年", self) self.tconLabel = QLabel("类型", self) self.diskLabel = QLabel("光盘", self) self.trackNumLabel = QLabel("曲目", self) self.songNameLabel = QLabel("歌曲名", self) self.songPathLabel = QLabel("文件位置", self) self.albumNameLabel = QLabel("专辑标题", self) self.songerNameLabel = QLabel("歌曲歌手", self) self.albumSongerLabel = QLabel("专辑歌手", self) self.editInfoLabel = QLabel("编辑歌曲信息", self) self.songPath = QLabel(self.songInfo["songPath"], self) self.emptyTrackErrorIcon = ErrorIcon(self) self.bottomErrorIcon = ErrorIcon(self) self.bottomErrorLabel = QLabel(self) # 实例化单行输入框 self.diskEditLine = LineEdit("1", self) self.tconEditLine = LineEdit(self.songInfo["tcon"], self) self.yearEditLine = LineEdit(self.songInfo["year"], self) self.albumNameEditLine = LineEdit(self.songInfo["album"], self) self.songNameEditLine = LineEdit(self.songInfo["songName"], self) self.songerNameEditLine = LineEdit(self.songInfo["songer"], self) self.albumSongerEditLine = LineEdit(self.songInfo["songer"], self) self.trackNumEditLine = LineEdit(self.songInfo["tracknumber"], self) # 创建集中管理小部件的列表 self.leftLabel_list = [ self.songNameLabel, self.trackNumLabel, self.albumNameLabel, self.tconLabel, ] self.rightLabel_list = [ self.songerNameLabel, self.diskLabel, self.albumSongerLabel, self.yearLabel, ] self.leftEditLine_list = [ self.songNameEditLine, self.trackNumEditLine, self.albumNameEditLine, self.tconEditLine, ] self.rightEditLine_list = [ self.songerNameEditLine, self.diskEditLine, self.albumSongerEditLine, self.yearEditLine, ] self.editLine_list = [ self.songNameEditLine, self.songerNameEditLine, self.trackNumEditLine, self.diskEditLine, self.albumNameEditLine, self.albumSongerEditLine, self.tconEditLine, self.yearEditLine, ]