Skip to content

Commit d09f378

Browse files
committed
preview pic
1 parent 618d81c commit d09f378

File tree

7 files changed

+77
-24
lines changed

7 files changed

+77
-24
lines changed

Test/TestSkinDialog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from Test.BaseApplyStyle import StyleWindow
1313
from Utils.ThemeManager import ThemeManager
1414
from Widgets.Dialogs.SkinDialog import SkinDialog
15+
from Utils.CommonUtil import initLog
16+
from Utils.Constants import LogName
1517

1618

1719
__Author__ = 'Irony'
@@ -24,6 +26,9 @@
2426
import cgitb
2527
os.chdir('../')
2628
sys.excepthook = cgitb.enable(1, None, 5, '')
29+
30+
initLog(LogName)
31+
2732
from PyQt5.QtWidgets import QApplication
2833
app = QApplication(sys.argv)
2934
w = SkinDialog()

Utils/ColorThief.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def __init__(self, file):
5353
must implement `read()`, `seek()`, and `tell()` methods,
5454
and be opened in binary mode.
5555
"""
56-
self.image = QImage(file).scaledToWidth(400, Qt.SmoothTransformation)
56+
self.image = QImage(file)
57+
if self.image.width() > 420:
58+
self.image = self.image.scaledToWidth(400, Qt.SmoothTransformation)
5759

5860
def get_color(self, quality=10):
5961
"""Get the dominant color.

Utils/CommonUtil.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def initLog(name, file=None, level=logging.DEBUG, formatter=None):
5656
stream_handler.setFormatter(formatter)
5757
logger.addHandler(stream_handler)
5858

59-
file = os.path.abspath(str(file))
60-
if file:
59+
if file != None:
60+
file = os.path.abspath(str(file))
6161
file_handler = logging.FileHandler(file, mode='w', encoding='utf-8')
6262
file_handler.setFormatter(formatter)
6363
logger.addHandler(file_handler)

Utils/ThemeManager.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
__Copyright__ = "Copyright (c) 2019 Irony"
2525
__Version__ = "Version 1.0"
2626

27-
StyleTemplate = """
27+
# 修改背景图片
28+
StylePictureTemplate = """
2829
#widgetMain {{
2930
border-image: url({0}); /*背景图片*/
3031
}}
32+
"""
3133

34+
# 修改颜色
35+
StyleColorTemplate = """
3236
/*工具栏*/
3337
#widgetTools {{
34-
background-color: rgba(38, 38, 38, 10);
38+
background-color: rgba({0}, {1}, {2}, 10);
3539
}}
3640
3741
/*存放网页控件*/
@@ -41,30 +45,34 @@
4145
4246
/*搜索框中的按钮*/
4347
#buttonSearch {{
44-
qproperty-bgColor: rgba({1}, {2}, {3}, 255);
48+
qproperty-bgColor: rgba({0}, {1}, {2}, 255);
4549
}}
4650
4751
/*返回顶部,主页按钮*/
4852
#buttonBackToUp, #buttonHome {{
49-
qproperty-bgColor: rgba({1}, {2}, {3}, 255);
53+
qproperty-bgColor: rgba({0}, {1}, {2}, 255);
5054
}}
5155
5256
/*工具栏中的按钮*/
5357
#buttonGithub, #buttonQQ, #buttonGroup {{
54-
background: rgba({1}, {2}, {3}, 255);
58+
background: rgba({0}, {1}, {2}, 255);
5559
}}
5660
5761
/*登录窗口*/
5862
#widgetLogin {{
59-
background: rgba({1}, {2}, {3}, 210);
63+
background: rgba({0}, {1}, {2}, 210);
6064
}}
6165
6266
/*激活状态*/
6367
#widgetLogin[_active="true"] {{
64-
border: 1px solid rgba({1}, {2}, {3}, 255);
68+
border: 1px solid rgba({0}, {1}, {2}, 255);
6569
}}
6670
"""
6771

72+
# 渐变颜色
73+
StyleGradientTemplate = """
74+
"""
75+
6876

6977
class ThemeManager:
7078

@@ -114,18 +122,32 @@ def loadUserTheme(cls, theme='Default'):
114122
Setting.setValue('theme', theme)
115123

116124
@classmethod
117-
def loadColourfulTheme(cls, color):
125+
def loadColourfulTheme(cls, color, widget=None):
118126
"""基于当前设置主题颜色
119127
:param cls:
120128
:param color: 背景颜色
129+
:param widget: 指定控件
121130
"""
122-
pass
131+
cls.ThemeName = Setting.value('theme', 'Default', str)
132+
# 加载主题取样式
133+
path = cls.stylePath()
134+
AppLog.info('stylePath: {}'.format(path))
135+
try:
136+
styleSheet = open(path, 'rb').read().decode(
137+
'utf-8', errors='ignore')
138+
# 需要替换部分样式
139+
styleSheet += StyleColorTemplate.format(*color)
140+
widget = widget or QApplication.instance()
141+
widget.setStyleSheet(styleSheet)
142+
except Exception as e:
143+
AppLog.exception(e)
123144

124145
@classmethod
125-
def loadPictureTheme(cls, image=None):
146+
def loadPictureTheme(cls, image=None, widget=None):
126147
"""设置图片背景的主题
127148
:param cls:
128149
:param image: 背景图片
150+
:param widget: 指定控件
129151
"""
130152
cls.ThemeName = Setting.value('theme', 'Default', str)
131153
# 加载主题取样式
@@ -140,10 +162,10 @@ def loadPictureTheme(cls, image=None):
140162
color_thief = ColorThief(image)
141163
color = color_thief.get_color()
142164
AppLog.info('dominant color: {}'.format(str(color)))
143-
styleSheet += StyleTemplate.format(
144-
os.path.abspath(image).replace('\\', '/'),
145-
*color)
146-
QApplication.instance().setStyleSheet(styleSheet)
165+
styleSheet += StylePictureTemplate.format(os.path.abspath(
166+
image).replace('\\', '/')) + StyleColorTemplate.format(*color)
167+
widget = widget or QApplication.instance()
168+
widget.setStyleSheet(styleSheet)
147169
except Exception as e:
148170
AppLog.exception(e)
149171

Utils/ThemeThread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,4 @@ def run(self):
206206
QThread.yieldCurrentThread()
207207
except Exception as e:
208208
AppLog.exception(e)
209-
Signals.pictureDownFinished.emit(self.widget)
209+
Signals.pictureDownFinished.emit(self.widget)

Widgets/Dialogs/SkinDialog.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from Utils.ThemeManager import ThemeManager
2020
from Widgets.Dialogs.MoveDialog import MoveDialog
2121
from Widgets.Layouts.FlowLayout import FlowLayout
22-
from Widgets.Skins.PreviewWidget import PreviewWidget
2322
from Widgets.Skins.PictureWidget import PictureWidget
23+
from Widgets.Skins.PreviewWidget import PreviewWidget
2424

2525

2626
__Author__ = "Irony"
@@ -66,13 +66,17 @@ def onThemeItemClicked(self, name, path):
6666
self.previewWidget.setVisible(True)
6767
self.previewWidget.setTitle(name)
6868
self.previewWidget.setPixmap(
69+
PreviewWidget.Theme,
6970
QPixmap(path).scaledToWidth(400, Qt.SmoothTransformation))
7071

7172
def onColourfulItemClicked(self, name, color):
7273
"""
7374
:param name: 颜色名字
7475
:param color: 颜色
7576
"""
77+
self.previewWidget.setVisible(True)
78+
self.previewWidget.setTitle(name)
79+
self.previewWidget.setPixmap(PreviewWidget.Color, color)
7680

7781
def onPictureItemClicked(self, name, path):
7882
"""
@@ -81,8 +85,7 @@ def onPictureItemClicked(self, name, path):
8185
"""
8286
self.previewWidget.setVisible(True)
8387
self.previewWidget.setTitle(name)
84-
self.previewWidget.setPixmap(
85-
QPixmap(path).scaledToHeight(385, Qt.SmoothTransformation))
88+
self.previewWidget.setPixmap(PreviewWidget.Picture, path)
8689

8790
def on_tabWidgetSkinMain_currentChanged(self, index):
8891
"""tab标签切换"""

Widgets/Skins/PreviewWidget.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from PyQt5.QtCore import Qt, pyqtSlot
1313
from PyQt5.QtWidgets import QWidget, QGraphicsDropShadowEffect
1414

15+
from UiFiles.Ui_MainWindow import Ui_FormMainWindow
1516
from UiFiles.Ui_PreviewWidget import Ui_FormPreviewWidget
1617
from Utils.ThemeManager import ThemeManager
1718

@@ -22,6 +23,10 @@
2223

2324
class PreviewWidget(QWidget, Ui_FormPreviewWidget):
2425

26+
Theme = 0
27+
Color = 1
28+
Picture = 2
29+
2530
def __init__(self, *args, **kwargs):
2631
super(PreviewWidget, self).__init__(*args, **kwargs)
2732
self.setupUi(self)
@@ -50,11 +55,27 @@ def setTitle(self, title):
5055
self.labelPreviewTitle.setText(title)
5156
self.setWindowTitle(title)
5257

53-
def setPixmap(self, pixmap):
58+
def setPixmap(self, which, poc):
5459
"""设置图片
55-
:param pixmap:
60+
:param which: Theme = 0,Color = 1,Picture = 2
61+
:param poc: QPixmap or color or path
5662
"""
57-
self.labelPreviewImage.setPixmap(pixmap)
63+
if not hasattr(self, '_UiMainWindow'):
64+
# 创建一个隐藏的主界面
65+
self._UiMainWindow = QWidget()
66+
ui = Ui_FormMainWindow()
67+
ui.setupUi(self._UiMainWindow)
68+
self._UiMainWindow.hide()
69+
if which == self.Theme:
70+
self.labelPreviewImage.setPixmap(poc)
71+
return
72+
elif which == self.Color:
73+
ThemeManager.loadColourfulTheme(poc, self._UiMainWindow)
74+
elif which == self.Picture:
75+
ThemeManager.loadPictureTheme(poc, self._UiMainWindow)
76+
# 对隐藏窗口截图
77+
poc = self._UiMainWindow.grab().scaledToWidth(400, Qt.SmoothTransformation)
78+
self.labelPreviewImage.setPixmap(poc)
5879

5980
@pyqtSlot()
6081
def on_buttonPreviewClose_clicked(self):

0 commit comments

Comments
 (0)