def loadPictureTheme(cls, image=None, widget=None, replaces={}): """设置图片背景的主题 :param cls: :param image: 背景图片 :param widget: 指定控件 """ # 加载主题取样式 path = cls.stylePath('Default') AppLog.info('stylePath: {}'.format(path)) try: styleSheet = open(path, 'rb').read().decode( 'utf-8', errors='ignore') # 需要替换部分样式 if image and os.path.isfile(image): # 获取图片主色调 color_thief = ColorThief(image) color = color_thief.get_color() AppLog.info('dominant color: {}'.format(str(color))) # 替换name templates = StylePictureTemplate for name, value in replaces.items(): templates = templates.replace(name, value) styleSheet += templates.format(os.path.abspath( image).replace('\\', '/')) + StyleColorTemplate.format(*color) widget = widget or QApplication.instance() widget.setStyleSheet(styleSheet) except Exception as e: AppLog.exception(e)
def run(self): show = True try: req = requests.get(self.Url) AppLog.info(req.text) if req.status_code != 200: AppLog.info('update thread end') UpgradeThread.quit() return content = req.json() for version, text in content: if Version.version < version: if show: Signals.updateDialogShowed.emit() QThread.msleep(1000) show = False Signals.updateTextChanged.emit( str(Version.version), str(version), text) self.download(Constants.UpgradeFile.format( version), self.ZipUrl.format(version)) Signals.updateFinished.emit(self.tr('update completed')) except Exception as e: Signals.updateFinished.emit( self.tr('update failed: {}').format(str(e))) AppLog.exception(e) AppLog.info('update thread end') UpgradeThread.quit()
def loadColourfulTheme(cls, color, widget=None, replaces={}): """基于当前设置主题颜色 :param cls: :param color: 背景颜色 :param widget: 指定控件 """ # 加载主题取样式 path = cls.stylePath('Default') AppLog.info('stylePath: {}'.format(path)) try: styleSheet = open(path, 'rb').read().decode( 'utf-8', errors='ignore') # 需要替换部分样式 colorstr = GradientUtils.styleSheetCode(color) if isinstance(color, QLinearGradient) or isinstance(color, QRadialGradient) or isinstance(color, QConicalGradient): color = color.stops()[0][1] # 替换name templates = StyleGradientTemplate for name, value in replaces.items(): templates = templates.replace(name, value) styleSheet += templates.format( color.red(), color.green(), color.blue(), colorstr) widget = widget or QApplication.instance() widget.setStyleSheet(styleSheet) except Exception as e: AppLog.exception(e)
def get_avatar(self, uid, avatar_url): try: req = requests.get(avatar_url) if req.status_code == 200: imgformat = req.headers.get( 'content-type', 'image/jpg').split('/')[1] Constants.ImageAvatar = os.path.join( Constants.ImageDir, str(uid)).replace('\\', '/') + '.jpg' AppLog.debug('image type: {}'.format(imgformat)) AppLog.debug( 'content length: {}'.format(len(req.content))) image = QImage() if image.loadFromData(req.content): # 缩放图片 if not image.isNull(): image = image.scaled(130, 130, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) AppLog.debug('save to: {}'.format( Constants.ImageAvatar)) image.save(Constants.ImageAvatar) else: AppLog.warn('avatar image is null') else: AppLog.warn('can not load from image data') except Exception as e: AppLog.exception(e)
def run(self): try: req = requests.get( UrlGetAppsByCategory.format(category=self.category, pageno=1, count=20, time=time())) content = req.json() errno = content.get('errno', 0) AppLog.debug('errno: %s', errno) AppLog.debug('msg: %s', content.get('msg', '')) if errno != 0: return content = content.get('data', {}) AppLog.debug('total_count: %s', content.get('total_count', '')) AppLog.debug('total_page: %s', content.get('total_page', '')) items = content.get('list', []) for i, item in enumerate(items): title = item.get('title', '') url = item.get('image', None) if not url: continue self.download(i, title, url) QThread.msleep(200) QThread.yieldCurrentThread() except Exception as e: AppLog.exception(e) Signals.pictureDownFinished.emit(self.widget)
def onReadChannelFinished(self): process = self.sender() message = process.readAllStandardError().data() try: message = message.decode(errors='ignore') except Exception as e: AppLog.exception(e) return if process.exitCode() != 0 and len(message.strip()) > 0: file = str(process.property('file')) reqfile = os.path.abspath(os.path.join( os.path.dirname(file), 'requirements.txt')) AppLog.debug('reqfile: {}'.format(reqfile)) dialog = ErrorDialog(message, self, reqfile=reqfile) dialog.exec_()
def download(self, index, title, url): try: dirPath = os.path.join(DirWallpaper, self.category) os.makedirs(dirPath, exist_ok=True) path = os.path.join(dirPath, os.path.basename(url)) if os.path.exists(path) and os.path.isfile(path): Signals.pictureItemAdded.emit(self.widget, index, title, path) return req = requests.get(url, headers=Headers) if req.status_code == 200: with open(path, 'wb') as fp: fp.write(req.content) Signals.pictureItemAdded.emit(self.widget, index, title, path) except Exception as e: AppLog.exception(e)
def on_lineEditAccount_textChanged(self, account): """输入框编辑完成信号,寻找头像文件是否存在 """ if account not in self._accounts: # 不存在 return # 填充密码 try: self.lineEditPassword.setText(base64.b85decode( self._accounts[account][1].encode()).decode()) except Exception as e: self.lineEditPassword.setText('') AppLog.exception(e) # 更新头像 path = os.path.join(Constants.ImageDir, self._accounts[account][0]).replace( '\\', '/') + '.jpg' if os.path.exists(path) and self.buttonHead.image != path: # 更换头像 self.buttonHead.image = path
def loadTheme(cls): """根据配置加载主题 :param cls: :param parent: """ ThemeManager.ThemeName = Setting.value('theme', 'Default', str) # 加载主题中的字体 path = cls.fontPath() AppLog.info('fontPath: {}'.format(path)) if os.path.isfile(path): QFontDatabase.addApplicationFont(path) # 加载主题取样式 path = cls.stylePath() AppLog.info('stylePath: {}'.format(path)) try: QApplication.instance().setStyleSheet( open(path, 'rb').read().decode('utf-8', errors='ignore')) return 1 except Exception as e: AppLog.exception(e)
def run(self): try: path = pygit2.discover_repository(Constants.DirProjects) if not path: # 本地项目不存在 if os.path.exists(Constants.DirProjects): # 如果文件夹存在则删除 AppLog.info('remove dir: {}'.format(Constants.DirProjects)) self.remove() AppLog.info('clone into dir: {}'.format(Constants.DirProjects)) self.clone() else: repo = pygit2.Repository(path) if repo.is_empty: # 如果项目为空 if os.path.exists(Constants.DirProjects): # 如果文件夹存在则删除 AppLog.info('remove dir: {}'.format( Constants.DirProjects)) self.remove() AppLog.info('clone into dir: {}'.format( Constants.DirProjects)) self.clone() else: # 重置并pull AppLog.info('reset dir: {}'.format(Constants.DirProjects)) repo.reset(repo.head.target, pygit2.GIT_RESET_HARD) Signals.progressUpdated.emit(5, 100) AppLog.info('pull into dir: {}'.format( Constants.DirProjects)) self.pull(repo) Signals.progressStoped.emit() except Exception as e: AppLog.exception(e) AppLog.info('clone thread end') Signals.progressStoped.emit() Signals.cloneFinished.emit('') CloneThread.quit()
def run(self): AppLog.info('start login github') try: req = requests.get(self.Url, auth=HTTPBasicAuth( self.account, self.password)) retval = req.json() if retval.get('message', '') == 'Bad credentials': Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Incorrect account or password')) AppLog.warn('Incorrect account or password') LoginThread.quit() return if 'login' not in retval: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Login failed, Unknown reason')) AppLog.warn('Login failed, Unknown reason') LoginThread.quit() return # 用户ID uid = retval.get('id', 0) AppLog.debug('user id: {}'.format(uid)) # 用户昵称 name = retval.get('name', 'Unknown') AppLog.debug('user name: {}'.format(name)) # 用户头像地址 avatar_url = retval.get('avatar_url', '') if avatar_url: # 获取头像 self.get_avatar(uid, avatar_url) Signals.loginSuccessed.emit(str(uid), name) except ConnectTimeout as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Connect Timeout')) AppLog.exception(e) except ConnectionError as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Connection Error')) AppLog.exception(e) except Exception as e: Signals.loginErrored.emit(QCoreApplication.translate( 'Repository', 'Unknown Error')) AppLog.exception(e) AppLog.info('login thread end') LoginThread.quit()