Skip to content

Commit f15aa4d

Browse files
committed
update: draw icon
1 parent c410d30 commit f15aa4d

File tree

2 files changed

+86
-19
lines changed

2 files changed

+86
-19
lines changed

drawtray.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from PySide2.QtCore import Qt, QRect, QRectF, QSize
2+
from PySide2.QtGui import QPixmap, QPainter, QFont, QColor, QPen, QFontMetrics
3+
from PySide2.QtWidgets import QApplication
4+
5+
6+
class TrayIcon:
7+
8+
def __init__(self):
9+
self.bgWidth = 128
10+
self.bgHeight = 128
11+
self.pixmap = QPixmap(self.bgWidth, self.bgHeight)
12+
self.pixmap.fill(Qt.transparent)
13+
self.painter = QPainter(self.pixmap)
14+
15+
def centerWidth(self, width):
16+
return (self.bgWidth - width) >> 1
17+
18+
def centerHeight(self, height):
19+
return (self.bgHeight - height) >> 1
20+
21+
def drawRoundedRect(self, margin, radius, battery=1.0):
22+
marginX = margin[0]
23+
marginY = margin[1]
24+
bRectWidth = self.bgWidth - marginX * 2
25+
bRectHeight = self.bgHeight - marginY * 2
26+
brt = QRectF(self.centerWidth(bRectWidth), self.centerHeight(bRectHeight), bRectWidth * battery, bRectHeight)
27+
self.painter.drawRoundedRect(brt, radius, radius)
28+
return brt
29+
30+
def drawChord(self, brt):
31+
width = height = 15
32+
cr = QRectF(brt.x() + brt.width() - 5, self.centerHeight(height), width, height)
33+
self.painter.drawChord(cr, 90 * 16, -180 * 16)
34+
35+
def drawNumIcon(self, battery):
36+
ft = QFont("Microsoft YaHei", 56)
37+
ft_rect = QFontMetrics(ft).boundingRect(str(battery))
38+
scale = ft_rect.width() / self.pixmap.width()
39+
if scale > 1:
40+
ft.setPointSize(36)
41+
elif scale < 0.5:
42+
ft.setPointSize(62)
43+
self.painter.setFont(ft)
44+
self.painter.setRenderHint(QPainter.HighQualityAntialiasing)
45+
self.painter.drawText(self.pixmap.rect(), Qt.AlignCenter, str(battery))
46+
self.painter.end()
47+
48+
def drawRectIcon(self, battery):
49+
self.painter.setBrush(Qt.black)
50+
self.painter.setPen(Qt.transparent)
51+
brt = self.drawRoundedRect((19, 42), 6)
52+
53+
self.painter.setCompositionMode(QPainter.CompositionMode_SourceOut)
54+
percentage = battery / 100
55+
mask_x = brt.x() + percentage * brt.width()
56+
mask_width = brt.width() * (1 - percentage)
57+
maskBrt = QRectF(mask_x, brt.y(), mask_width, brt.height())
58+
self.painter.fillRect(maskBrt, Qt.transparent)
59+
60+
# big rect
61+
self.painter.setCompositionMode(QPainter.CompositionMode_DestinationOver)
62+
self.painter.setPen(QPen(Qt.black, 10))
63+
self.painter.setBrush(Qt.transparent)
64+
self.drawRoundedRect((3, 28), 16)
65+
self.painter.end()
66+
67+
def draw(self, battery, style=0):
68+
if style == 0:
69+
self.drawNumIcon(battery)
70+
else:
71+
self.drawRectIcon(battery)
72+
return self.pixmap

tool.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import time
33
import resource
4+
from drawtray import TrayIcon
45
from storage import DEVICE
56
from controller import Rebt
67
from PySide2.QtUiTools import QUiLoader
@@ -20,7 +21,6 @@ def __init__(self):
2021
self.low = QIcon(':/resource/imgs/low.svg')
2122
self.empty = QIcon(':/resource/imgs/empty.svg')
2223
self.defaultIcon = QIcon(':/resource/imgs/icon.png')
23-
self.curIcon = self.defaultIcon
2424
self.setWindowIcon(self.defaultIcon)
2525
self.clickCount = 0
2626

@@ -141,28 +141,23 @@ def getBatteryInfo(self):
141141
except RuntimeError as e:
142142
battery = 0
143143
self.tray.showMessage("Error", str(e), self.defaultIcon)
144-
icon = self.full
145-
if battery <= 85:
146-
icon = self.most
147-
if battery <= 65:
148-
icon = self.half
149-
if battery <= 35:
150-
icon = self.low
151-
if battery <= 10:
152-
icon = self.empty
153-
return icon, battery
144+
145+
if battery == -1:
146+
return self.defaultIcon, TrayIcon().draw(0, 1), 0
147+
148+
if self.rebt.config.getDefault("trayStyle") == 1:
149+
trayIcon = widgetIcon = TrayIcon().draw(battery, 1)
150+
else:
151+
trayIcon = TrayIcon().draw(battery, 0)
152+
widgetIcon = TrayIcon().draw(battery, 1)
153+
return trayIcon, widgetIcon, battery
154154

155155
def updateBatteryInfo(self):
156-
icon, battery = self.getBatteryInfo()
156+
trayIcon, widgetIcon, battery = self.getBatteryInfo()
157157
self.tray.setToolTip(f"{battery}%")
158+
self.tray.setIcon(trayIcon)
158159
self.controlPanel.battery.setText(f' {battery}%')
159-
if self.rebt.config.getDefault("trayStyle"):
160-
self.tray.setIcon(self.drawTrayIcon(battery))
161-
else:
162-
self.tray.setIcon(icon)
163-
if icon != self.curIcon:
164-
self.controlPanel.battery.setIcon(icon)
165-
self.curIcon = icon
160+
self.controlPanel.battery.setIcon(widgetIcon)
166161

167162
def updateInterval(self):
168163
minutes = self.controlPanel.intervalSlider.value()

0 commit comments

Comments
 (0)