204 lines
6.5 KiB
Python
204 lines
6.5 KiB
Python
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
|
|
QPushButton, QFrame, QSpacerItem, QSizePolicy)
|
|
from PyQt5.QtCore import Qt, pyqtSignal, QSize
|
|
from PyQt5.QtGui import QPixmap, QIcon, QFont, QPainter, QBrush, QLinearGradient, QColor
|
|
|
|
|
|
class NavBar(QWidget):
|
|
nav_item_clicked = pyqtSignal(str)
|
|
logout_clicked = pyqtSignal()
|
|
|
|
def __init__(self, username):
|
|
super().__init__()
|
|
self.username = username or "访客"
|
|
self.active_btn = None
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
self.setObjectName("navBar")
|
|
self.setFixedWidth(240)
|
|
self.setStyleSheet("""
|
|
#navBar {
|
|
background-color: #1a202c;
|
|
border-right: 1px solid #2d3748;
|
|
}
|
|
|
|
#userFrame {
|
|
background-color: #2d3748;
|
|
border-radius: 10px;
|
|
margin: 15px;
|
|
padding: 15px;
|
|
}
|
|
|
|
#userName {
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
color: #e2e8f0;
|
|
}
|
|
|
|
#navBtn {
|
|
text-align: left;
|
|
padding: 15px 25px;
|
|
color: #a0aec0;
|
|
border-radius: 8px;
|
|
margin: 5px 15px;
|
|
font-size: 14px;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
#navBtn:hover {
|
|
background-color: rgba(79, 70, 229, 0.1);
|
|
color: #cbd5e0;
|
|
padding-left: 28px;
|
|
}
|
|
|
|
#navBtn.active {
|
|
background-color: rgba(79, 70, 229, 0.15);
|
|
color: #818cf8;
|
|
font-weight: 500;
|
|
padding-left: 28px;
|
|
}
|
|
|
|
#logoutBtn {
|
|
text-align: left;
|
|
padding: 15px 25px;
|
|
border-radius: 8px;
|
|
margin: 5px 15px;
|
|
font-size: 14px;
|
|
color: #feb2b2;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
#logoutBtn:hover {
|
|
background-color: rgba(254, 178, 178, 0.1);
|
|
color: #f56565;
|
|
padding-left: 28px;
|
|
}
|
|
""")
|
|
|
|
layout = QVBoxLayout()
|
|
layout.setContentsMargins(0, 20, 0, 20)
|
|
layout.setSpacing(0)
|
|
|
|
# 用户信息卡片
|
|
user_frame = QFrame()
|
|
user_frame.setObjectName("userFrame")
|
|
user_layout = QHBoxLayout(user_frame)
|
|
user_layout.setContentsMargins(15, 15, 15, 15)
|
|
user_layout.setSpacing(15)
|
|
|
|
# 头像
|
|
avatar = QLabel()
|
|
avatar.setFixedSize(50, 50)
|
|
avatar.setStyleSheet("""
|
|
background-color: #4f46e5;
|
|
border-radius: 25px;
|
|
color: white;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
qproperty-alignment: 'AlignCenter';
|
|
""")
|
|
initial = self.username[0].upper() if self.username else "访"
|
|
avatar.setText(initial)
|
|
|
|
# 用户信息
|
|
user_info = QWidget()
|
|
user_info_layout = QVBoxLayout(user_info)
|
|
user_info_layout.setContentsMargins(0, 0, 0, 0)
|
|
user_info_layout.setSpacing(5)
|
|
|
|
user_name = QLabel(self.username)
|
|
user_name.setObjectName("userName")
|
|
|
|
status = QLabel("在线")
|
|
status.setStyleSheet("font-size: 12px; color: #68d391;")
|
|
|
|
user_info_layout.addWidget(user_name)
|
|
user_info_layout.addWidget(status)
|
|
|
|
user_layout.addWidget(avatar)
|
|
user_layout.addWidget(user_info)
|
|
|
|
# 导航项 - 添加应用中心链接
|
|
nav_items = [
|
|
{"name": "首页", "icon": "home", "id": "home"},
|
|
{"name": "应用中心", "icon": "apps", "id": "app_center"}, # 新添加的应用中心链接
|
|
{"name": "使用记录", "icon": "history", "id": "history"},
|
|
{"name": "个人设置", "icon": "settings", "id": "settings"},
|
|
]
|
|
|
|
# 导航按钮
|
|
self.nav_buttons = []
|
|
for item in nav_items:
|
|
btn = QPushButton(item["name"])
|
|
btn.setObjectName("navBtn")
|
|
btn.setProperty("nav_id", item["id"])
|
|
btn.setCursor(Qt.PointingHandCursor)
|
|
btn.setIcon(QIcon(f":/icons/{item['icon']}.png"))
|
|
btn.setIconSize(QSize(20, 20))
|
|
btn.clicked.connect(lambda _, x=item["id"]: self.nav_item_clicked.emit(x))
|
|
self.nav_buttons.append(btn)
|
|
|
|
# 退出按钮
|
|
logout_btn = QPushButton("退出登录")
|
|
logout_btn.setObjectName("logoutBtn")
|
|
logout_btn.setIcon(QIcon(":/icons/logout.png"))
|
|
logout_btn.setIconSize(QSize(20, 20))
|
|
logout_btn.clicked.connect(self.logout_clicked.emit)
|
|
|
|
# 组装布局
|
|
layout.addWidget(user_frame)
|
|
layout.addSpacing(20)
|
|
|
|
# 添加分割线
|
|
separator = QFrame()
|
|
separator.setFrameShape(QFrame.HLine)
|
|
separator.setStyleSheet("margin: 0 15px; height: 1px; background-color: #2d3748;")
|
|
layout.addWidget(separator)
|
|
layout.addSpacing(20)
|
|
|
|
# 导航按钮容器
|
|
nav_container = QWidget()
|
|
nav_container_layout = QVBoxLayout(nav_container)
|
|
nav_container_layout.setContentsMargins(0, 0, 0, 0)
|
|
nav_container_layout.setSpacing(5)
|
|
|
|
for btn in self.nav_buttons:
|
|
nav_container_layout.addWidget(btn)
|
|
|
|
layout.addWidget(nav_container)
|
|
layout.addStretch()
|
|
|
|
# 底部添加退出按钮
|
|
bottom_container = QWidget()
|
|
bottom_container_layout = QVBoxLayout(bottom_container)
|
|
bottom_container_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
# 底部添加分割线
|
|
separator_bottom = QFrame()
|
|
separator_bottom.setFrameShape(QFrame.HLine)
|
|
separator_bottom.setStyleSheet("margin: 0 15px; height: 1px; background-color: #2d3748;")
|
|
|
|
bottom_container_layout.addWidget(separator_bottom)
|
|
bottom_container_layout.addSpacing(15)
|
|
bottom_container_layout.addWidget(logout_btn)
|
|
|
|
layout.addWidget(bottom_container)
|
|
|
|
self.setLayout(layout)
|
|
|
|
# 默认激活首页
|
|
self.set_active_nav("home")
|
|
|
|
def set_active_nav(self, nav_id):
|
|
# 移除所有按钮的active类
|
|
for btn in self.nav_buttons:
|
|
btn.setProperty("class", "")
|
|
btn.setStyle(btn.style())
|
|
|
|
# 为活动按钮添加active类
|
|
for btn in self.nav_buttons:
|
|
if btn.property("nav_id") == nav_id:
|
|
btn.setProperty("class", "active")
|
|
btn.setStyle(btn.style())
|
|
self.active_btn = btn |