Skip to content

Commit d1e194e

Browse files
committed
add picture code
1 parent fd7372f commit d1e194e

File tree

9 files changed

+421
-40
lines changed

9 files changed

+421
-40
lines changed

UiFiles/SkinDialog.ui

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,54 @@
137137
<string>Colourful</string>
138138
</attribute>
139139
</widget>
140-
<widget class="PictureWidget" name="tabPicture">
140+
<widget class="QWidget" name="tabPicture">
141141
<attribute name="title">
142142
<string>Picture</string>
143143
</attribute>
144+
<layout class="QVBoxLayout" name="verticalLayout_4">
145+
<property name="spacing">
146+
<number>0</number>
147+
</property>
148+
<property name="leftMargin">
149+
<number>0</number>
150+
</property>
151+
<property name="topMargin">
152+
<number>0</number>
153+
</property>
154+
<property name="rightMargin">
155+
<number>0</number>
156+
</property>
157+
<property name="bottomMargin">
158+
<number>0</number>
159+
</property>
160+
<item>
161+
<widget class="QWidget" name="widget" native="true">
162+
<layout class="QVBoxLayout" name="verticalLayout_5">
163+
<property name="spacing">
164+
<number>0</number>
165+
</property>
166+
<property name="leftMargin">
167+
<number>30</number>
168+
</property>
169+
<property name="topMargin">
170+
<number>20</number>
171+
</property>
172+
<property name="rightMargin">
173+
<number>30</number>
174+
</property>
175+
<property name="bottomMargin">
176+
<number>20</number>
177+
</property>
178+
<item>
179+
<widget class="QWidget" name="widgetCategories" native="true"/>
180+
</item>
181+
</layout>
182+
</widget>
183+
</item>
184+
<item>
185+
<widget class="QStackedWidget" name="stackedWidgetPictures"/>
186+
</item>
187+
</layout>
144188
</widget>
145189
</widget>
146190
</item>
@@ -244,12 +288,6 @@
244288
<header location="global">Widgets.Skins.ColourfulWidget</header>
245289
<container>1</container>
246290
</customwidget>
247-
<customwidget>
248-
<class>PictureWidget</class>
249-
<extends>QWidget</extends>
250-
<header location="global">Widgets.Skins.PictureWidget</header>
251-
<container>1</container>
252-
</customwidget>
253291
</customwidgets>
254292
<resources/>
255293
<connections>

UiFiles/Ui_SkinDialog.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,25 @@ def setupUi(self, FormSkinDialog):
5555
self.tabColourful = ColourfulWidget()
5656
self.tabColourful.setObjectName("tabColourful")
5757
self.tabWidgetSkinMain.addTab(self.tabColourful, "")
58-
self.tabPicture = PictureWidget()
58+
self.tabPicture = QtWidgets.QWidget()
5959
self.tabPicture.setObjectName("tabPicture")
60+
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tabPicture)
61+
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
62+
self.verticalLayout_4.setSpacing(0)
63+
self.verticalLayout_4.setObjectName("verticalLayout_4")
64+
self.widget = QtWidgets.QWidget(self.tabPicture)
65+
self.widget.setObjectName("widget")
66+
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.widget)
67+
self.verticalLayout_5.setContentsMargins(30, 20, 30, 20)
68+
self.verticalLayout_5.setSpacing(0)
69+
self.verticalLayout_5.setObjectName("verticalLayout_5")
70+
self.widgetCategories = QtWidgets.QWidget(self.widget)
71+
self.widgetCategories.setObjectName("widgetCategories")
72+
self.verticalLayout_5.addWidget(self.widgetCategories)
73+
self.verticalLayout_4.addWidget(self.widget)
74+
self.stackedWidgetPictures = QtWidgets.QStackedWidget(self.tabPicture)
75+
self.stackedWidgetPictures.setObjectName("stackedWidgetPictures")
76+
self.verticalLayout_4.addWidget(self.stackedWidgetPictures)
6077
self.tabWidgetSkinMain.addTab(self.tabPicture, "")
6178
self.verticalLayout_3.addWidget(self.tabWidgetSkinMain)
6279
self.widgetBottom = QtWidgets.QWidget(self.widgetSkinBg)
@@ -104,7 +121,6 @@ def retranslateUi(self, FormSkinDialog):
104121
self.buttonRestore.setText(_translate("FormSkinDialog", "Restore the default theme"))
105122

106123
from Widgets.Skins.ColourfulWidget import ColourfulWidget
107-
from Widgets.Skins.PictureWidget import PictureWidget
108124
from Widgets.Skins.ThemeWidget import ThemeWidget
109125

110126
if __name__ == "__main__":

Utils/CommonUtil.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ class _Signals(QObject):
156156
# 主推item点击,路径
157157
themeItemClicked = pyqtSignal(str, object)
158158

159+
# 获取壁纸目录
160+
getCategoriesFinished = pyqtSignal(list)
161+
# 分类获取完成
162+
getCategoryFinished = pyqtSignal(object, list)
163+
# 分类图片下载完成信号
164+
pictureDownloadFinished = pyqtSignal(object, int, list)
165+
# 分类图片item点击,路径
166+
pictureItemClicked = pyqtSignal(str, object)
167+
159168

160169
# 说白了就是全局信号定义
161170
Signals = _Signals()

Utils/Constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
LogFormatter = '[%(asctime)s %(name)s] %(levelname)-8s %(message)s'
4040
LogFile = 'Resources/Data/app.log'
4141

42+
DirImages = 'Resources/Images'
4243
DirThemes = 'Resources/Themes' # 主题目录
4344
DirErrors = 'Resources/Data/Errors' # 错误日志目录
4445
DirProject = 'Resources/Data/Projects' # 本地项目目录
@@ -52,6 +53,8 @@
5253
UrlQQ = 'tencent://message/?uin=892768447'
5354
UrlGroup = 'tencent://groupwpa/?subcmd=all&param=7B2267726F757055696E223A3234363236393931392C2274696D655374616D70223A313531383537323831357D0A'
5455
UrlIssues = 'https://github.com/PyQt5/PyQtClient/issues/new'
56+
UrlGetAllCategoriesV2 = 'http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2'
57+
UrlGetAppsByCategory = 'http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid={cid}&start={start}&count={count}&from=360chrome'
5558

5659
_Sha = ''
5760
_Account = ''

Utils/ThemeThread.py

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@
99
@file: Utils.ThemeThread
1010
@description:
1111
"""
12+
import os
1213
from pathlib import Path
1314

14-
from PyQt5.QtCore import QObject, QThread
15+
from PyQt5.QtCore import QObject, QThread, QRunnable
1516
from PyQt5.QtGui import QLinearGradient, QColor
17+
import requests
1618

1719
from Utils.CommonUtil import AppLog, Signals
18-
from Utils.Constants import DirThemes
20+
from Utils.Constants import DirThemes, UrlGetAllCategoriesV2,\
21+
UrlGetAppsByCategory, DirImages
1922

2023

2124
__Author__ = "Irony"
2225
__Copyright__ = "Copyright (c) 2019"
2326

27+
Headers = {
28+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
29+
'Accept-Language': 'zh-CN,zh;q=0.9',
30+
'Cache-Control': 'max-age=0',
31+
'Upgrade-Insecure-Requests': '1',
32+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6824.400 QQBrowser/10.3.3137.400'
33+
}
34+
2435

2536
def splistList(src, length):
2637
# 等分列表
@@ -141,3 +152,65 @@ def run(self):
141152

142153
Signals.themeItemAddFinished.emit()
143154
AppLog.info('theme thread end')
155+
156+
157+
class GetAllCategoriesRunnable(QRunnable):
158+
159+
def __init__(self, *args, **kwargs):
160+
super(GetAllCategoriesRunnable, self).__init__(*args, **kwargs)
161+
self.setAutoDelete(True)
162+
163+
def run(self):
164+
categories = []
165+
try:
166+
req = requests.get(UrlGetAllCategoriesV2)
167+
content = req.json()
168+
categories = content.get('data', [])
169+
AppLog.debug('errmsg: %s', content.get('errmsg', ''))
170+
AppLog.debug('consume: %s', content.get('consume', ''))
171+
AppLog.debug('total: %s', content.get('total', ''))
172+
except Exception as e:
173+
AppLog.exception(e)
174+
Signals.getCategoriesFinished.emit(categories)
175+
176+
177+
class GetAllCategoryRunnable(QRunnable):
178+
179+
def __init__(self, cid, widget, *args, **kwargs):
180+
super(GetAllCategoryRunnable, self).__init__(*args, **kwargs)
181+
self.setAutoDelete(True)
182+
self.cid = cid
183+
self.widget = widget
184+
185+
def download(self, index, url):
186+
try:
187+
dirPath = os.path.join(DirImages, self.cid)
188+
os.makedirs(dirPath, exist_ok=True)
189+
req = requests.get(url, headers=Headers)
190+
if req.status_code == 200:
191+
path = os.path.join(dirPath, os.path.basename(url))
192+
with open(path, 'wb') as fp:
193+
fp.write(req.content)
194+
self.pictureDownloadFinished.emit(self, index, path)
195+
except Exception as e:
196+
AppLog.exception(e)
197+
198+
def run(self):
199+
datas = []
200+
try:
201+
req = requests.get(UrlGetAppsByCategory.format(
202+
cid=self.cid, start=0, count=200))
203+
content = req.json()
204+
datas = content.get('data', [])
205+
AppLog.debug('errmsg: %s', content.get('errmsg', ''))
206+
AppLog.debug('consume: %s', content.get('consume', ''))
207+
AppLog.debug('total: %s', content.get('total', ''))
208+
# 加载前20个
209+
for i, item in enumerate(datas[:20]):
210+
url = item.get('url', None)
211+
if not url:
212+
continue
213+
self.download(i, url)
214+
except Exception as e:
215+
AppLog.exception(e)
216+
Signals.getCategoryFinished.emit(self.widget, datas)

Widgets/Dialogs/SkinDialog.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
@description:
1111
"""
1212

13-
from PyQt5.QtCore import Qt
13+
from PyQt5.QtCore import Qt, QThreadPool
14+
from PyQt5.QtWidgets import QPushButton, QButtonGroup
1415

1516
from UiFiles.Ui_SkinDialog import Ui_FormSkinDialog
17+
from Utils.CommonUtil import Signals
1618
from Utils.ThemeManager import ThemeManager
19+
from Utils.ThemeThread import GetAllCategoriesRunnable
1720
from Widgets.Dialogs.MoveDialog import MoveDialog
21+
from Widgets.Layouts.FlowLayout import FlowLayout
22+
from Widgets.Skins.PictureWidget import PictureWidget
1823

1924

2025
__Author__ = "Irony"
@@ -31,10 +36,64 @@ def __init__(self, *args, **kwargs):
3136
# 无边框
3237
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
3338
self.widgetBottom.setVisible(False)
39+
# 背景图片面板
40+
Signals.getCategoriesFinished.connect(self.onGetCategoriesFinished)
41+
Signals.getCategoryFinished.connect(self.onGetCategoryFinished)
42+
Signals.pictureDownloadFinished.connect(self.onPictureDownloadFinished)
43+
self.categoryLayout = FlowLayout(self.widgetCategories)
44+
self.categoryLayout.setSpacing(20)
3445
# 加载鼠标样式
3546
ThemeManager.loadCursor(self)
3647
self.on_tabWidgetSkinMain_currentChanged(0)
3748

3849
def on_tabWidgetSkinMain_currentChanged(self, index):
3950
"""tab标签切换"""
40-
self.tabWidgetSkinMain.widget(index).init()
51+
w = self.tabWidgetSkinMain.widget(index)
52+
if w == self.tabPicture:
53+
if self.stackedWidgetPictures.count() > 0:
54+
return
55+
QThreadPool.globalInstance().start(GetAllCategoriesRunnable())
56+
else:
57+
w.init()
58+
59+
def onGetCategoriesFinished(self, categories):
60+
"""添加分类标签页
61+
:param categories: 分类
62+
"""
63+
self.categoryBtnGroups = QButtonGroup(self)
64+
self.categoryBtnGroups.buttonToggled.connect(self.onCategoryChanged)
65+
for category in categories:
66+
button = QPushButton(category.get(
67+
'name', '未知'), self.widgetCategories)
68+
button.setCheckable(True)
69+
self.categoryBtnGroups.addButton(button)
70+
self.categoryLayout.addWidget(button)
71+
self.stackedWidgetPictures.addWidget(
72+
PictureWidget(category.get('id', '36'), self.stackedWidgetPictures))
73+
self.categoryBtnGroups.buttons()[0].setChecked(True)
74+
75+
def onCategoryChanged(self, button, toggled):
76+
"""分类切换
77+
:param button: 分类按钮
78+
:param toggled: 是否选中
79+
"""
80+
if not toggled:
81+
return
82+
index = self.categoryBtnGroups.id(button)
83+
self.stackedWidgetPictures.setCurrentIndex(index)
84+
self.stackedWidgetPictures.widget(index).init()
85+
86+
def onGetCategoryFinished(self, widget, items):
87+
"""某个分类json下载完成
88+
:param widget: 该分类对应的PictureWidget
89+
:param items: 分类中的数组
90+
"""
91+
widget.setItems(items)
92+
93+
def onPictureDownloadFinished(self, widget, index, path):
94+
"""分类图片下载完成
95+
:param widget: 该分类对应的PictureWidget
96+
:param index: 序号
97+
:param path: 图片路径
98+
"""
99+
widget.addItem(index, path)

0 commit comments

Comments
 (0)