|
| 1 | +from PyQt5.QtWidgets import QWidget, QScrollArea, QLabel, QVBoxLayout, QApplication, QHBoxLayout, QSpacerItem,\ |
| 2 | +QSizePolicy, QPushButton,QFrame,QMessageBox,QFormLayout,QProgressDialog,QTextEdit,QComboBox |
| 3 | +from githubApi import GitHub, Release |
| 4 | +from PyQt5.QtCore import QObject,pyqtSlot,Qt,pyqtSignal,QUrl |
| 5 | +from PyQt5.QtGui import QDesktopServices,QFont |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +class ReleaseFrame(QFrame): |
| 10 | + downLoadFile = pyqtSignal(Release) |
| 11 | + def __init__(self, release: Release,mode = ">", parent=None): |
| 12 | + super().__init__(parent) |
| 13 | + self.release: Release = release |
| 14 | + self.showButton = QPushButton(QObject.tr(self, "Show")) |
| 15 | + self.showButton.setCheckable(True) |
| 16 | + self.formWidget = QWidget() |
| 17 | + self.downloadButton = QPushButton(QObject.tr(self, "Download")) |
| 18 | + self.formWidget.hide() |
| 19 | + self.bodyEdit = QTextEdit() |
| 20 | + self.mode = mode |
| 21 | + self.initUi() |
| 22 | + self.initConnect() |
| 23 | + |
| 24 | + def initUi(self): |
| 25 | + |
| 26 | + # self.setFrameShape(QFrame.StyledPanel) |
| 27 | + layout = QVBoxLayout() |
| 28 | + row1 = QHBoxLayout() |
| 29 | + row1.addWidget(self.showButton) |
| 30 | + row1.addWidget(QLabel(self.release.tag_name)) |
| 31 | + row1.addItem(QSpacerItem( |
| 32 | + 20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) |
| 33 | + self.downloadButton.setEnabled(self.mode == ">") |
| 34 | + row1.addWidget(self.downloadButton) |
| 35 | + |
| 36 | + formLayout = QFormLayout() |
| 37 | + formLayout.setContentsMargins(0, 0, 0, 0) |
| 38 | + urlLabel = QLabel() |
| 39 | + urlLabel.setText("<a href='" + self.release.html_url + "'>" + QObject.tr(self,"open external links")+ "</a>") |
| 40 | + urlLabel.setOpenExternalLinks(True) |
| 41 | + dataLayout = QVBoxLayout() |
| 42 | + dataLayout.setContentsMargins(0, 0, 0, 0) |
| 43 | + formLayout.setLabelAlignment(Qt.AlignmentFlag.AlignLeft) |
| 44 | + formLayout.addRow(QObject.tr(self, "html_url"), urlLabel) |
| 45 | + formLayout.addRow(QObject.tr(self, "name"), QLabel(self.release.assets_name)) |
| 46 | + formLayout.addRow(QObject.tr(self, "content_type"), QLabel(self.release.assets_content_type)) |
| 47 | + formLayout.addRow(QObject.tr(self, "size"), QLabel(str(f"{self.release.assets_size / 1000000:.2f} MB"))) |
| 48 | + formLayout.addRow(QObject.tr(self, "download_count"), QLabel(str(self.release.assets_download_count))) |
| 49 | + formLayout.addRow(QObject.tr(self, "created_at"), QLabel(self.release.assets_created_at)) |
| 50 | + downloadUrlLabel = QLabel() |
| 51 | + downloadUrlLabel.setText("<a href='" + self.release.assets_browser_download_url + "'>" + QObject.tr(self,"open download links")+ "</a>") |
| 52 | + downloadUrlLabel.setOpenExternalLinks(True) |
| 53 | + formLayout.addRow(QObject.tr(self, "browser_download_url"), downloadUrlLabel) |
| 54 | + dataLayout.addLayout(formLayout) |
| 55 | + self.bodyEdit.setMarkdown(self.release.body) |
| 56 | + self.bodyEdit.setReadOnly(True) |
| 57 | + font = QFont("Microsoft YaHei UI", 13) |
| 58 | + font.setBold(True) |
| 59 | + self.bodyEdit.setFont(font) |
| 60 | + self.bodyEdit.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) |
| 61 | + dataLayout.addWidget(self.bodyEdit) |
| 62 | + self.formWidget.setLayout(dataLayout) |
| 63 | + |
| 64 | + layout.addLayout(row1) |
| 65 | + layout.addWidget(self.formWidget) |
| 66 | + self.setLayout(layout) |
| 67 | + rgbStr = "" |
| 68 | + if self.mode == ">": |
| 69 | + # 样式表绿色 |
| 70 | + rgbStr = "rgb(200,255,250)" |
| 71 | + elif self.mode == "=": |
| 72 | + # 样式表蓝色 |
| 73 | + rgbStr = "rgb(200,216,230)" |
| 74 | + else: |
| 75 | + # 样式表红色背景 |
| 76 | + rgbStr = "rgb(249, 179, 163)" |
| 77 | + # label字体微软雅黑Ui,大小13 |
| 78 | + self.setStyleSheet(f"QFrame{{background-color:{rgbStr}; font-family:Microsoft YaHei UI; font-size:14px;}}") |
| 79 | + |
| 80 | + def initConnect(self): |
| 81 | + self.showButton.clicked.connect(self.showButtonClicked) |
| 82 | + self.downloadButton.clicked.connect(self.downLoadButtonClicked) |
| 83 | + def showButtonClicked(self,checked:bool): |
| 84 | + if checked: |
| 85 | + self.formWidget.show() |
| 86 | + else: |
| 87 | + self.formWidget.hide() |
| 88 | + def downLoadButtonClicked(self): |
| 89 | + self.downLoadFile.emit(self.release) |
| 90 | + |
| 91 | + |
| 92 | +class CheckUpdateGui(QWidget): |
| 93 | + def __init__(self,source:str, owner:str,repo:str,version:str,versionReStr:str, parent=None): |
| 94 | + super().__init__(parent) |
| 95 | + self.github: GitHub = GitHub(source,owner,repo,version,versionReStr,self) |
| 96 | + self.checkUpdateButton = QPushButton( |
| 97 | + QObject.tr(self, "CheckUpdate"), self) |
| 98 | + self.releaseArea = QScrollArea() |
| 99 | + self.releaseArea.setWidgetResizable(True) |
| 100 | + # 禁用横向滚动条 |
| 101 | + self.releaseArea.setHorizontalScrollBarPolicy( |
| 102 | + Qt.ScrollBarPolicy.ScrollBarAlwaysOff |
| 103 | + ) |
| 104 | + self.sourceCombo = QComboBox() |
| 105 | + self.sourceCombo.addItems(["GitHub","fff666.top"]) |
| 106 | + self.currentVersionLabel = QLabel(f"Current Version:{self.github.version}") |
| 107 | + self.processDialog = None |
| 108 | + self.initUi() |
| 109 | + self.initConnect() |
| 110 | + self.resize(450, 600) |
| 111 | + self.checkUpdateButton.click() |
| 112 | + def initUi(self): |
| 113 | + font = QFont("Microsoft YaHei UI", 13) |
| 114 | + font.setBold(True) |
| 115 | + self.currentVersionLabel.setFont(font) |
| 116 | + layout = QVBoxLayout() |
| 117 | + |
| 118 | + row1 = QHBoxLayout() |
| 119 | + row1.setContentsMargins(0, 0, 0, 0) |
| 120 | + row1.addWidget(self.currentVersionLabel) |
| 121 | + row1.addItem(QSpacerItem( |
| 122 | + 20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) |
| 123 | + row1.addWidget(self.sourceCombo) |
| 124 | + row1.addWidget(self.checkUpdateButton) |
| 125 | + |
| 126 | + row2 = QHBoxLayout() |
| 127 | + row2.setContentsMargins(0, 0, 0, 0) |
| 128 | + |
| 129 | + row2.addWidget(self.releaseArea) |
| 130 | + |
| 131 | + |
| 132 | + layout.addLayout(row1) |
| 133 | + layout.addLayout(row2) |
| 134 | + |
| 135 | + self.setLayout(layout) |
| 136 | + |
| 137 | + def initConnect(self): |
| 138 | + self.checkUpdateButton.clicked.connect(lambda: self.github.releases()) |
| 139 | + self.github.releasesAsyncSignal.connect(self.checkUpdate) |
| 140 | + self.github.errorSignal.connect(self.showError) |
| 141 | + self.github.downloadReleaseAsyncStartSignal.connect(self.showDownloadDialog) |
| 142 | + self.github.downloadReleaseAsyncProgressSignal.connect(self.updateDownloadDialog) |
| 143 | + self.github.downloadReleaseAsyncFinishSignal.connect(self.hideDownloadDialog) |
| 144 | + |
| 145 | + @pyqtSlot(list) |
| 146 | + def checkUpdate(self,releases:list): |
| 147 | + widget = self.releaseArea.widget() |
| 148 | + if widget is not None: |
| 149 | + widget.deleteLater() |
| 150 | + widget = QWidget() |
| 151 | + layout = QVBoxLayout() |
| 152 | + layout.setContentsMargins(0, 0, 0, 0) |
| 153 | + for release in releases: |
| 154 | + frame = ReleaseFrame(release,self.github.compareVersion(release.tag_name)) |
| 155 | + layout.addWidget(frame) |
| 156 | + frame.downLoadFile.connect(self.github.downloadRelease) |
| 157 | + # 底部加一个空白区域 |
| 158 | + panel = QWidget() |
| 159 | + panel.setContentsMargins(0, 0, 0, 0) |
| 160 | + panel.setFixedHeight(100) |
| 161 | + layout.addWidget(panel) |
| 162 | + layout.addItem(QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)) |
| 163 | + widget.setLayout(layout) |
| 164 | + self.releaseArea.setWidget(widget) |
| 165 | + def showError(self,msg:str): |
| 166 | + QMessageBox.critical(self,QObject.tr(self, "Error"),msg) |
| 167 | + |
| 168 | + def showDownloadDialog(self,release:Release): |
| 169 | + if self.processDialog is not None: |
| 170 | + self.processDialog.close() |
| 171 | + self.processDialog = QProgressDialog(self) |
| 172 | + self.processDialog.setWindowTitle(QObject.tr(self, f"{release.tag_name} Downloading...")) |
| 173 | + def updateDownloadDialog(self,a:int,b:int): |
| 174 | + if self.processDialog is not None: |
| 175 | + self.processDialog.setValue(a) |
| 176 | + self.processDialog.setMaximum(b) |
| 177 | + self.processDialog.setLabelText(f'{a/1000000 : .2f}/{b/1000000 : .2f} MB') |
| 178 | + def hideDownloadDialog(self,path:str): |
| 179 | + if self.processDialog is not None: |
| 180 | + self.processDialog.close() |
| 181 | + self.processDialog = None |
| 182 | + # 使用系统默认方式打开文件 |
| 183 | + QDesktopServices.openUrl(QUrl.fromLocalFile(path)) |
| 184 | + |
| 185 | +if __name__ == '__main__': |
| 186 | + import sys |
| 187 | + app = QApplication(sys.argv) |
| 188 | + w = CheckUpdateGui("https://api.github.com/repos/","eee555", "Solvable-Minesweeper", "3.1.9", "(\d+\.\d+\.\d+)") |
| 189 | + w.show() |
| 190 | + sys.exit(app.exec_()) |
0 commit comments