fix(ui): 换个正常的主页
This commit is contained in:
157
app/ui/Widget.py
157
app/ui/Widget.py
@@ -25,9 +25,9 @@ v4.2
|
||||
作者:DLmaster_361
|
||||
"""
|
||||
|
||||
from PySide6.QtCore import Qt, QTime
|
||||
from PySide6.QtGui import QIcon
|
||||
from PySide6.QtWidgets import QWidget, QHBoxLayout
|
||||
from PySide6.QtCore import Qt, QTime, QEvent
|
||||
from PySide6.QtGui import QIcon, QPixmap, QPainter, QPainterPath
|
||||
from qfluentwidgets import (
|
||||
LineEdit,
|
||||
PasswordLineEdit,
|
||||
@@ -47,8 +47,13 @@ from qfluentwidgets import (
|
||||
ConfigItem,
|
||||
TimeEdit,
|
||||
OptionsConfigItem,
|
||||
TeachingTip,
|
||||
TransparentToolButton,
|
||||
TeachingTipTailPosition,
|
||||
)
|
||||
from typing import Union, List
|
||||
from qfluentwidgets.common.overload import singledispatchmethod
|
||||
import os
|
||||
from typing import Optional, Union, List
|
||||
|
||||
from app.services import Crypto
|
||||
|
||||
@@ -373,3 +378,149 @@ class QuantifiedItemCard(CardWidget):
|
||||
self.Layout.addWidget(self.Name)
|
||||
self.Layout.addStretch(1)
|
||||
self.Layout.addWidget(self.Numb)
|
||||
|
||||
|
||||
class IconButton(TransparentToolButton):
|
||||
"""包含下拉框的自定义设置卡片类。"""
|
||||
|
||||
@singledispatchmethod
|
||||
def __init__(self, parent: QWidget = None):
|
||||
TransparentToolButton.__init__(self, parent)
|
||||
|
||||
self._tooltip: Optional[TeachingTip] = None
|
||||
|
||||
@__init__.register
|
||||
def _(self, icon: Union[str, QIcon, FluentIconBase], parent: QWidget = None):
|
||||
self.__init__(parent)
|
||||
self.setIcon(icon)
|
||||
|
||||
@__init__.register
|
||||
def _(
|
||||
self,
|
||||
icon: Union[str, QIcon, FluentIconBase],
|
||||
isTooltip: bool,
|
||||
tip_title: str,
|
||||
tip_content: str,
|
||||
parent: QWidget = None,
|
||||
):
|
||||
self.__init__(parent)
|
||||
self.setIcon(icon)
|
||||
|
||||
# 处理工具提示
|
||||
if isTooltip:
|
||||
self.installEventFilter(self)
|
||||
|
||||
self.tip_title: str = tip_title
|
||||
self.tip_content: str = tip_content
|
||||
|
||||
def eventFilter(self, obj, event: QEvent) -> bool:
|
||||
"""处理鼠标事件。"""
|
||||
if event.type() == QEvent.Type.Enter:
|
||||
self._show_tooltip()
|
||||
elif event.type() == QEvent.Type.Leave:
|
||||
self._hide_tooltip()
|
||||
return super().eventFilter(obj, event)
|
||||
|
||||
def _show_tooltip(self) -> None:
|
||||
"""显示工具提示。"""
|
||||
self._tooltip = TeachingTip.create(
|
||||
target=self,
|
||||
title=self.tip_title,
|
||||
content=self.tip_content,
|
||||
tailPosition=TeachingTipTailPosition.RIGHT,
|
||||
isClosable=False,
|
||||
duration=-1,
|
||||
parent=self,
|
||||
)
|
||||
# 设置偏移
|
||||
if self._tooltip:
|
||||
tooltip_pos = self.mapToGlobal(self.rect().topRight())
|
||||
|
||||
tooltip_pos.setX(
|
||||
tooltip_pos.x() - self._tooltip.size().width() - 40
|
||||
) # 水平偏移
|
||||
tooltip_pos.setY(
|
||||
tooltip_pos.y() - self._tooltip.size().height() / 2 + 35
|
||||
) # 垂直偏移
|
||||
|
||||
self._tooltip.move(tooltip_pos)
|
||||
|
||||
def _hide_tooltip(self) -> None:
|
||||
"""隐藏工具提示。"""
|
||||
if self._tooltip:
|
||||
self._tooltip.close()
|
||||
self._tooltip = None
|
||||
|
||||
def __hash__(self):
|
||||
return id(self)
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, self.__class__):
|
||||
return NotImplemented
|
||||
return self is other
|
||||
|
||||
|
||||
class Banner(QWidget):
|
||||
"""展示带有圆角的固定大小横幅小部件"""
|
||||
|
||||
def __init__(self, image_path: str, parent=None):
|
||||
QWidget.__init__(self, parent)
|
||||
self.image_path = image_path
|
||||
self.banner_image = self.load_banner_image(image_path)
|
||||
self.scaled_image = None
|
||||
self.update_scaled_image()
|
||||
|
||||
def load_banner_image(self, image_path: str):
|
||||
"""加载横幅图片,或创建渐变备用图片"""
|
||||
if os.path.isfile(image_path):
|
||||
return QPixmap(image_path)
|
||||
return self._create_fallback_image()
|
||||
|
||||
def _create_fallback_image(self):
|
||||
"""创建渐变备用图片"""
|
||||
fallback_image = QPixmap(2560, 1280) # 使用原始图片的大小
|
||||
fallback_image.fill(Qt.GlobalColor.gray)
|
||||
return fallback_image
|
||||
|
||||
def update_scaled_image(self):
|
||||
"""按高度缩放图片,宽度保持比例,超出裁剪"""
|
||||
if self.banner_image:
|
||||
self.scaled_image = self.banner_image.scaled(
|
||||
self.size(),
|
||||
Qt.AspectRatioMode.KeepAspectRatioByExpanding,
|
||||
Qt.TransformationMode.SmoothTransformation,
|
||||
)
|
||||
self.update()
|
||||
|
||||
def paintEvent(self, event):
|
||||
"""重载 paintEvent 以绘制缩放后的图片"""
|
||||
if self.scaled_image:
|
||||
painter = QPainter(self)
|
||||
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
||||
painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
|
||||
|
||||
# 创建圆角路径
|
||||
path = QPainterPath()
|
||||
path.addRoundedRect(self.rect(), 20, 20)
|
||||
painter.setClipPath(path)
|
||||
|
||||
# 计算绘制位置,使图片居中
|
||||
x = (self.width() - self.scaled_image.width()) // 2
|
||||
y = (self.height() - self.scaled_image.height()) // 2
|
||||
|
||||
# 绘制缩放后的图片
|
||||
painter.drawPixmap(x, y, self.scaled_image)
|
||||
|
||||
def resizeEvent(self, event):
|
||||
"""重载 resizeEvent 以更新缩放后的图片"""
|
||||
self.update_scaled_image()
|
||||
QWidget.resizeEvent(self, event)
|
||||
|
||||
def set_percentage_size(self, width_percentage, height_percentage):
|
||||
"""设置 Banner 的大小为窗口大小的百分比"""
|
||||
parent = self.parentWidget()
|
||||
if parent:
|
||||
new_width = int(parent.width() * width_percentage)
|
||||
new_height = int(parent.height() * height_percentage)
|
||||
self.setFixedSize(new_width, new_height)
|
||||
self.update_scaled_image()
|
||||
|
||||
Reference in New Issue
Block a user