Skip to content

Commit 88a6508

Browse files
committed
fix switch color theme too slow
1 parent 5c50357 commit 88a6508

File tree

3 files changed

+84
-63
lines changed

3 files changed

+84
-63
lines changed

Test/TestMainwindowStyle.py

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,63 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
4-
"""
5-
Created on 2019年1月18日
6-
@author: Irony
7-
@site: https://pyqt5.com https://github.com/892768447
8-
@email: 892768447@qq.com
9-
@file: Test.TestMainwindowStyle
10-
@description:
11-
"""
12-
from PyQt5.QtCore import Qt
13-
from PyQt5.QtWidgets import QWidget, QListWidget
14-
15-
from UiFiles.Ui_MainWindow import Ui_FormMainWindow
16-
from Utils.ThemeManager import ThemeManager
17-
18-
19-
__Author__ = "Irony"
20-
__Copyright__ = "Copyright (c) 2019"
21-
22-
23-
class Window(QWidget, Ui_FormMainWindow):
24-
25-
def __init__(self, *args, **kwargs):
26-
super(Window, self).__init__(*args, **kwargs)
27-
self.setAttribute(Qt.WA_TranslucentBackground, True)
28-
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
29-
self.setupUi(self)
30-
ThemeManager.loadTheme()
31-
ThemeManager.loadCursor(self)
32-
ThemeManager.loadCursor(self.buttonHead, ThemeManager.CursorPointer)
33-
34-
35-
if __name__ == '__main__':
36-
import sys
37-
import os
38-
os.chdir('../')
39-
from PyQt5.QtWidgets import QApplication
40-
app = QApplication(sys.argv)
41-
w = Window()
42-
w.resize(800, 770)
43-
w.show()
44-
w.grab().save('Resources/Themes/Default/preview.png')
45-
46-
lw = QListWidget()
47-
lw.show()
48-
lw.addItems(os.listdir('Resources/Themes'))
49-
50-
def setStyle(item):
51-
path = os.path.join('Resources/Themes', item.text(), 'style.qss')
52-
if os.path.isfile(path):
53-
ThemeManager.loadUserTheme(item.text())
54-
w.grab().save('Resources/Themes/{}/preview.png'.format(item.text()))
55-
lw.itemClicked.connect(setStyle)
56-
57-
sys.exit(app.exec_())
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2019年1月18日
6+
@author: Irony
7+
@site: https://pyqt5.com https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: Test.TestMainwindowStyle
10+
@description:
11+
"""
12+
from time import time
13+
14+
from PyQt5.QtCore import Qt
15+
from PyQt5.QtWidgets import QWidget, QListWidget
16+
17+
from UiFiles.Ui_MainWindow import Ui_FormMainWindow
18+
from Utils.ThemeManager import ThemeManager
19+
20+
21+
__Author__ = "Irony"
22+
__Copyright__ = "Copyright (c) 2019"
23+
24+
25+
class Window(QWidget, Ui_FormMainWindow):
26+
27+
def __init__(self, *args, **kwargs):
28+
super(Window, self).__init__(*args, **kwargs)
29+
self.setAttribute(Qt.WA_TranslucentBackground, True)
30+
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
31+
self.setupUi(self)
32+
t = time()
33+
ThemeManager.loadTheme()
34+
print(time() - t)
35+
ThemeManager.loadCursor(self)
36+
ThemeManager.loadCursor(self.buttonHead, ThemeManager.CursorPointer)
37+
38+
39+
if __name__ == '__main__':
40+
import sys
41+
import os
42+
os.chdir('../')
43+
from PyQt5.QtWidgets import QApplication
44+
app = QApplication(sys.argv)
45+
w = Window()
46+
w.resize(800, 770)
47+
w.show()
48+
w.grab().save('Resources/Themes/Default/preview.png')
49+
50+
lw = QListWidget()
51+
lw.show()
52+
lw.addItems(os.listdir('Resources/Themes'))
53+
54+
def setStyle(item):
55+
path = os.path.join('Resources/Themes', item.text(), 'style.qss')
56+
if os.path.isfile(path):
57+
t = time()
58+
ThemeManager.loadUserTheme(item.text())
59+
print(time()-t)
60+
w.grab().save('Resources/Themes/{}/preview.png'.format(item.text()))
61+
lw.itemClicked.connect(setStyle)
62+
63+
sys.exit(app.exec_())

Utils/ThemeManager.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def loadUserTheme(cls, theme='Default'):
299299
cls.loadTheme()
300300

301301
@classmethod
302-
def loadColourfulTheme(cls, color, widget=None):
302+
def loadColourfulTheme(cls, color, widget=None, replaces={}):
303303
"""基于当前设置主题颜色
304304
:param cls:
305305
:param color: 背景颜色
@@ -315,15 +315,20 @@ def loadColourfulTheme(cls, color, widget=None):
315315
colorstr = GradientUtils.styleSheetCode(color)
316316
if isinstance(color, QLinearGradient) or isinstance(color, QRadialGradient) or isinstance(color, QConicalGradient):
317317
color = color.stops()[0][1]
318-
styleSheet += StyleGradientTemplate.format(
318+
# 替换name
319+
templates = StyleGradientTemplate
320+
for name, value in replaces.items():
321+
templates = templates.replace(name, value)
322+
323+
styleSheet += templates.format(
319324
color.red(), color.green(), color.blue(), colorstr)
320325
widget = widget or QApplication.instance()
321326
widget.setStyleSheet(styleSheet)
322327
except Exception as e:
323328
AppLog.exception(e)
324329

325330
@classmethod
326-
def loadPictureTheme(cls, image=None, widget=None):
331+
def loadPictureTheme(cls, image=None, widget=None, replaces={}):
327332
"""设置图片背景的主题
328333
:param cls:
329334
:param image: 背景图片
@@ -341,7 +346,13 @@ def loadPictureTheme(cls, image=None, widget=None):
341346
color_thief = ColorThief(image)
342347
color = color_thief.get_color()
343348
AppLog.info('dominant color: {}'.format(str(color)))
344-
styleSheet += StylePictureTemplate.format(os.path.abspath(
349+
350+
# 替换name
351+
templates = StylePictureTemplate
352+
for name, value in replaces.items():
353+
templates = templates.replace(name, value)
354+
355+
styleSheet += templates.format(os.path.abspath(
345356
image).replace('\\', '/')) + StyleColorTemplate.format(*color)
346357
widget = widget or QApplication.instance()
347358
widget.setStyleSheet(styleSheet)

Widgets/Skins/PreviewWidget.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def setPixmap(self, which, poc):
7171
self._UiMainWindow = QWidget()
7272
ui = Ui_FormMainWindow()
7373
ui.setupUi(self._UiMainWindow)
74+
# 修改名字防止受app的style影响
75+
ui.widgetMain.setObjectName('widgetMain1')
7476
self._UiMainWindow.setAttribute(Qt.WA_TranslucentBackground, True)
7577
self._UiMainWindow.setWindowFlags(
7678
self.windowFlags() | Qt.FramelessWindowHint)
@@ -80,9 +82,11 @@ def setPixmap(self, which, poc):
8082
QPixmap(poc).scaledToWidth(400, Qt.SmoothTransformation))
8183
return
8284
elif which == self.Color:
83-
ThemeManager.loadColourfulTheme(poc, self._UiMainWindow)
85+
ThemeManager.loadColourfulTheme(poc, self._UiMainWindow, {
86+
'widgetMain': 'widgetMain1'})
8487
elif which == self.Picture:
85-
ThemeManager.loadPictureTheme(poc, self._UiMainWindow)
88+
ThemeManager.loadPictureTheme(poc, self._UiMainWindow, {
89+
'widgetMain': 'widgetMain1'})
8690
# 对隐藏窗口截图
8791
# 至于为什么要加延迟, 设置样式后可能UI还没刷新
8892
self._UiMainWindow.repaint()

0 commit comments

Comments
 (0)