Skip to content

Commit

Permalink
Merge branch 'main' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinaIceF authored Dec 3, 2024
2 parents 64a8d1c + de6fa95 commit 8911a3c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "A powerful and artistic UI library based on PyQt5 / PySide6"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
dependencies = ["PyQt5>=5.15.10", "typing-extensions>=4.12.2"]
dependencies = ["PyQt5>=5.15.10", "typing-extensions>=4.12.2", "python-dateutil>=2.9.0"]

[project.urls]
Repository = "https://github.com/ChinaIceF/PyQt-SiliconUI"
Expand Down
79 changes: 69 additions & 10 deletions siui/components/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtCore import QPoint, Qt, pyqtSignal
from PyQt5.QtWidgets import QAbstractButton

from siui.components.widgets.abstracts import ABCButton, ABCPushButton, ABCToggleButton, LongPressThread
Expand Down Expand Up @@ -406,17 +406,16 @@ class SiSwitch(QAbstractButton):
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setCheckable(True)

# 自定义状态属性,初始值为 False
self._checked = False

# 颜色组
self.color_group = SiColorGroup(reference=SiGlobal.siui.colors)

# 设置自身固定大小
self.setFixedSize(40, 20)

# 绑定切换事件
self.toggled.connect(self._toggle_handler)

# 开关框架
self.switch_frame = SiLabel(self)
self.switch_frame.setGeometry(0, 0, 40, 20)
Expand All @@ -434,6 +433,10 @@ def __init__(self, *args, **kwargs):
self.toggle_animation.setCurrent(3)
self.toggle_animation.ticked.connect(self._lever_move_animation_handler)

# 记录拉杆与鼠标偏移量
self._initial_pos: QPoint = QPoint(0, 0)
self._drag_offset = 0

def reloadStyleSheet(self):
"""
重载样式表
Expand All @@ -455,7 +458,7 @@ def _lever_move_animation_handler(self, x):
self.switch_lever.move(int(x), self.switch_lever.y())

# 检测拉杆的位置,如果过了半程,则改变边框样式
if (x - 3) / 20 >= 0.5:
if self._process_lever_position():
self.switch_frame.setStyleSheet(
f"""
background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1,
Expand All @@ -470,14 +473,70 @@ def _lever_move_animation_handler(self, x):
self.switch_lever.setStyleSheet(f"background-color:{self.getColor(SiColor.SWITCH_DEACTIVATE)}") # noqa: E501

def _set_animation_target(self, is_checked):
if is_checked is True:
self.toggle_animation.setTarget(23)
else:
self.toggle_animation.setTarget(3)
self.toggle_animation.setCurrent(self.switch_lever.x())
self.toggle_animation.setTarget(23 if is_checked else 3)

def _process_lever_position(self) -> bool:
"""
根据滑杆位置决定开关的选中状态。
"""
lever_position = self.switch_lever.x()
return (lever_position - 3) / 20 >= 0.5

def paintEvent(self, e):
pass

def isChecked(self):
return self._checked # 返回自定义状态

def setChecked(self, checked):
if self._checked != checked:
self._checked = checked
self.toggled.emit(self._checked) # 发射信号
self._toggle_handler(self._checked) # 更新动画

def mousePressEvent(self, event):
"""
处理鼠标按下事件,记录鼠标点击位置与滑杆的相对位置。
"""
if event.button() == Qt.LeftButton:
self._drag_offset = event.pos().x() - self.switch_lever.x() # 记录偏移量
self._initial_pos = event.pos() # 记录初始点击位置
super().mousePressEvent(event)

def mouseMoveEvent(self, event):
"""
处理滑条的鼠标移动事件,拖动时移动滑杆。
"""
if event.buttons() & Qt.LeftButton: # 检查鼠标左键是否按下
# 获取鼠标在 slider 上的位置,并使用之前记录的偏移量来移动滑杆
mouse_pos = event.pos()
target_pos = mouse_pos.x() - self._drag_offset # 保持相对位置
self._lever_move_animation_handler(min(max(target_pos, 3), 23))
super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
"""
处理鼠标松开事件,区分点击和拖动操作。
"""
if event.button() == Qt.LeftButton:
release_pos = event.pos()
drag_distance = abs(release_pos.x() - self._initial_pos.x())

if drag_distance < 3: # 点击操作
self._checked = not self._checked # 切换状态
self.toggled.emit(self._checked) # 手动发射toggled信号
else: # 拖动操作
new_checked_state = self._process_lever_position()
if self._checked != new_checked_state:
self._checked = new_checked_state
self.toggled.emit(self._checked) # 手动发射toggled信号

# 更新动画
self._toggle_handler(self._checked)

super().mouseReleaseEvent(event)

def _toggle_handler(self, is_checked):
self._set_animation_target(is_checked)
self.toggle_animation.try_to_start()
1 change: 0 additions & 1 deletion siui/core/painter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

if TYPE_CHECKING:
from PyQt5.QtGui import QFont, QPaintDevice

from siui.typing import T_Brush, T_PenStyle, T_RenderHint


Expand Down
25 changes: 24 additions & 1 deletion siui/gui/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TYPE_CHECKING

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import qApp

from siui.core.token import FontStyle, GlobalFont, GlobalFontSize, GlobalFontWeight

Expand All @@ -11,13 +12,35 @@


class SiFont:
"""SiUI Font Class
You can use low-level API to customize fonts details,
or use tokenized global fonts to quickly create fonts.
"""

@staticmethod
def getFont(
families: Sequence[str] = ["Segoe UI", "Microsoft YaHei", "San Francisco Fonts", "PingFang SC"],
families: Sequence[str] = qApp.font().families()
or ["Segoe UI", "Microsoft YaHei", "San Francisco Fonts", "PingFang SC"],
size: int = 14,
weight: QFont.Weight = QFont.Weight.Normal,
italic: bool = False,
) -> QFont:
"""Low-level API for creating font instance
Application-level configuration takes the highest priority,
and it is recommended to use the tokenized Hier-level API.
Args:
- families: 字体族列表,如果指定了应用程序级别的全局配置,则会覆盖默认字体家族
- size: 字体大小
- weight: 字体粗细
- italic: 是否斜体
Returns:
- QFont: 字体实例
"""
font = QFont()
font.setFamilies(families)
font.setPixelSize(size)
Expand Down
2 changes: 1 addition & 1 deletion siui/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"""Type of QBrush"""

T_RenderHint: TypeAlias = Optional[Union[QPainter.RenderHint, int]]
"""Type of QPainter.RenderHint"""
"""Type of QPainter.RenderHint"""

0 comments on commit 8911a3c

Please sign in to comment.