Files
JITToolBox/ui/components/widget.py
2025-06-29 03:04:04 +08:00

112 lines
3.8 KiB
Python

# Copyright (c) 2025 Jeffrey Hsu - JITToolBox
# #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import random
from typing import Union
from PySide6.QtCore import QTimer, Qt, Signal
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QWidget, QVBoxLayout, QFrame, QLayout
from qfluentwidgets import DisplayLabel, LargeTitleLabel, GroupHeaderCardWidget, FluentIconBase, CardGroupWidget
from module.picker.schema import PickerStudent
class Widget(QFrame):
def __init__(self, key: str, parent=None):
super().__init__(parent=parent)
# 必须给子界面设置全局唯一的对象名
self.setObjectName(key.replace(' ', '-'))
class RollingTextWidget(QWidget):
finishSignal = Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.current_index = 0
self.items = []
self.soLabel = LargeTitleLabel("", self)
self.nameLabel = DisplayLabel("", self)
self.soLabel.setAlignment(Qt.AlignCenter)
self.nameLabel.setAlignment(Qt.AlignCenter)
self.layout = QVBoxLayout()
self.layout.addWidget(self.soLabel)
self.layout.addWidget(self.nameLabel)
self.setLayout(self.layout)
self.rolling_timer = QTimer(self)
self.rolling_timer.setInterval(50) # 滚动速度(毫秒)
self.rolling_timer.timeout.connect(self.update_text)
self.stop_timer = QTimer(self)
self.stop_timer.setSingleShot(True)
self.stop_timer.timeout.connect(self.stop_rolling)
def update_text(self):
# 每次显示下一个字符
self.current_index = (self.current_index + 1) % len(self.items)
stu = self.items[self.current_index]
self.soLabel.setText(stu.so)
self.nameLabel.setText(stu.name)
def start_rolling(self):
if not self.rolling_timer.isActive():
self.rolling_timer.start()
self.stop_timer.start(2000) # 2秒后停止滚动
def stop_rolling(self):
self.rolling_timer.stop()
self.finishSignal.emit()
def set_items(self, items: list[PickerStudent]):
self.items = items[:]
random.shuffle(self.items)
def show_result(self, student: PickerStudent):
self.soLabel.setText(student.so)
self.nameLabel.setText(student.name)
def clear_text(self):
self.soLabel.clear()
self.nameLabel.clear()
class MyCardGroupWidget(CardGroupWidget):
def addLayout(self, layout: QLayout, stretch=0):
self.hBoxLayout.addLayout(layout, stretch=stretch)
class MyGroupHeaderCardWidget(GroupHeaderCardWidget):
def addGroup(self, icon: Union[str, FluentIconBase, QIcon], title: str, content: str,
object: Union[QWidget, QLayout],
stretch=0) -> CardGroupWidget:
group = MyCardGroupWidget(icon, title, content, self)
if isinstance(object, QWidget):
group.addWidget(object, stretch=stretch)
elif isinstance(object, QLayout):
group.addLayout(object, stretch=stretch)
if self.groupWidgets:
self.groupWidgets[-1].setSeparatorVisible(True)
self.groupLayout.addWidget(group)
self.groupWidgets.append(group)
return group