from PySide6.QtWidgets import (QWidget, QHBoxLayout, QLabel, QPushButton, QMessageBox, QSpacerItem, QSizePolicy) from PySide6.QtGui import QPixmap, QPainter, QFont, QPen from PySide6.QtCore import Qt, QDateTime, QEvent, QSize from view.widgets.switch_button import SwitchButton import resources.resources_rc from utils.image_paths import ImagePaths # 底部的控制控件,包括 系统诊断、系统中心等按钮 class BottomControlWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.initUI() def initUI(self): # 1. 加载背景图并设置控件尺寸 bg_path = ImagePaths.BOTTOM_BACKGROUND self.bg_pixmap = QPixmap(bg_path) if self.bg_pixmap.isNull(): print("警告:底部背景.png 加载失败,请检查路径!") self.setFixedSize(1280, 66) else: self.setFixedSize(self.bg_pixmap.size()) # 2. 主布局(水平布局,组件间距6px) main_layout = QHBoxLayout(self) main_layout.setContentsMargins(10, 10, 10, 10) # 适当留白避免贴边 main_layout.setSpacing(6) # 3. 逐个添加组件 ## 3.1 系统诊断按钮(带状态图) self.diagnosis_btn = self.createDiagnosisButton("系统诊断", ImagePaths.BOTTOM_SHORT_BTN1, ImagePaths.BOTTOM_SHORT_BTN2) main_layout.addWidget(self.diagnosis_btn) ## 3.2 系统状态消息按钮(长按钮) self.status_msg_btn = self.createLongButton("系统状态消息", ImagePaths.BOTTOM_LONG_BTN1, ImagePaths.BOTTOM_LONG_BTN2) main_layout.addWidget(self.status_msg_btn) ## 3.3 预警消息列表按钮(长按钮) self.warning_list_btn = self.createLongButton("预警消息列表", ImagePaths.BOTTOM_LONG_BTN1, ImagePaths.BOTTOM_LONG_BTN2) main_layout.addWidget(self.warning_list_btn) ## 3.4 系统中心按钮(短按钮,无状态图) self.center_btn = self.createShortButton("系统中心", ImagePaths.BOTTOM_SHORT_BTN1, ImagePaths.BOTTOM_SHORT_BTN2) main_layout.addWidget(self.center_btn) ## 3.5 自动模式(文字+开关) self.auto_mode_widget = self.createAutoModeWidget() main_layout.addWidget(self.auto_mode_widget) ## 3.6 版本信息标签(可点击弹框) self.version_label = QLabel("V1.0") self.version_label.setStyleSheet(""" color: #16ffff; font-size: 20px; text-decoration: underline; """) self.version_label.installEventFilter(self) # 安装事件过滤器 main_layout.addWidget(self.version_label, alignment=Qt.AlignRight | Qt.AlignBottom) def eventFilter(self, obj, event): """事件过滤器:使用QEvent枚举判断事件类型""" if obj == self.version_label and event.type() == QEvent.MouseButtonPress: self.showVersionInfo() return True # 拦截事件 return super().eventFilter(obj, event) # 其他事件正常处理 def createDiagnosisButton(self, text, normal_img, hover_img): """创建带状态图的系统诊断按钮""" # 加载短按钮图片获取尺寸 normal_pix = QPixmap(normal_img) if normal_pix.isNull(): print(f"警告:{normal_img} 加载失败") btn_width, btn_height = 120, 40 else: btn_width, btn_height = normal_pix.width(), normal_pix.height() btn = QPushButton() btn.setFixedSize(btn_width, btn_height) # 1. 按钮设置文字 btn.setText(text) # 2. 状态图标(作为按钮的子控件,单独定位) self.status_icon = QLabel(btn) status_pix = QPixmap(ImagePaths.BOTTOM_SYSTEM_GREEN) if not status_pix.isNull(): self.status_icon.setFixedSize(status_pix.width(), status_pix.height()) self.status_icon.setPixmap(status_pix) # 图标定位:左侧9px,垂直居中 icon_y = (btn_height - self.status_icon.height()) // 2 self.status_icon.move(9, icon_y) # 3. 设置鼠标手势 btn.setCursor(Qt.PointingHandCursor) # 4. 按钮样式表 btn.setStyleSheet(f""" QPushButton {{ background-image: url({normal_img}); background-repeat: no-repeat; background-position: center; border: none; color: #3bfff8; font-size: 20px; text-align: center; padding-left: 9px; }} QPushButton:hover {{ background-image: url({hover_img}); color: #001c83; /* hover状态文字颜色 */ }} """) return btn def set_system_status(self, status, count=0): """设置系统诊断按钮的状态图标并显示数量""" icon_map = { "normal": ImagePaths.BOTTOM_SYSTEM_GREEN, # 正常情况 "warning": ImagePaths.BOTTOM_SYSTEM_YELLOW, # 警告情况 "error": ImagePaths.BOTTOM_SYSTEM_RED # 异常情况 } pixmap_path = icon_map.get(status, icon_map["normal"]) pixmap = QPixmap(pixmap_path) if not pixmap.isNull(): temp_pix = pixmap.copy() # 复制一份 pixmap 用于绘制 # 绘制 异常或警告 数量(若count>0) if count > 0 and status != "normal": # 绿色的正常情况,不需要显示数字 painter = QPainter(temp_pix) # 在副本上绘制 painter.setPen(QPen(Qt.white, 2)) # 白色画笔 font = painter.font() # 获取当前字体 # 调整字体大小 if count > 99: font.setPointSize(10) else: font.setPointSize(14) painter.setFont(font) # 应用字体设置 if count <= 99: # 两位数字 painter.drawText(temp_pix.rect(), Qt.AlignCenter, str(count)) # 居中绘制数字 else: painter.drawText(temp_pix.rect(), Qt.AlignCenter, "99+") painter.end() # 手动结束绘制 # 将绘制完成的副本设置回标签 self.status_icon.setPixmap(temp_pix) def createShortButton(self, text, normal_img, hover_img): """创建短按钮""" # 以正常状态图片尺寸为准,确定按钮宽高 normal_pix = QPixmap(normal_img) if normal_pix.isNull(): print(f"警告:{normal_img} 加载失败") btn_width, btn_height = 155, 50 # 图片加载失败时的默认尺寸 else: btn_width, btn_height = normal_pix.width(), normal_pix.height() # 与背景图尺寸一致 # 1. 创建按钮并设置基础属性 btn = QPushButton() btn.setFixedSize(btn_width, btn_height) # 按钮尺寸匹配背景图 btn.setText(text) # 用按钮自带文字,替代QLabel # 2. 设置鼠标手势 btn.setCursor(Qt.PointingHandCursor) # 3. 样式表:控制文字(16px+居中+颜色)、背景图、hover效果 btn.setStyleSheet(f""" QPushButton {{ /* 背景图设置 */ background-image: url({normal_img}); background-repeat: no-repeat; background-position: center; border: none; font-size: 20px; color: #3bfff8; text-align: center; }} QPushButton:hover {{ /* hover时切换背景图 */ background-image: url({hover_img}); /* hover时文字变色 */ color: #001c83; }} """) return btn def createLongButton(self, text, normal_img, hover_img): """创建长按钮""" # 以正常状态图片尺寸为准,确定按钮宽高 normal_pix = QPixmap(normal_img) if normal_pix.isNull(): print(f"警告:{normal_img} 加载失败") btn_width, btn_height = 339, 50 # 图片加载失败时的默认尺寸 else: btn_width, btn_height = normal_pix.width(), normal_pix.height() # 1. 创建按钮并设置基础属性 btn = QPushButton() btn.setFixedSize(btn_width, btn_height) # 按钮尺寸匹配背景图 btn.setText(text) # 2. 设置鼠标手势 btn.setCursor(Qt.PointingHandCursor) # 3. 样式表:控制文字(20px字体+居中+颜色)、背景图、hover效果 btn.setStyleSheet(f""" QPushButton {{ background-image: url({normal_img}); background-repeat: no-repeat; background-position: center; border: none; font-size: 20px; color: #3bfff8; text-align: center; }} QPushButton:hover {{ background-image: url({hover_img}); color: #001c83; }} """) return btn def createAutoModeWidget(self): """创建自动模式组件""" bg_img = ImagePaths.BOTTOM_SHORT_BTN1 bg_pix = QPixmap(bg_img) if bg_pix.isNull(): print(f"警告:{bg_img} 加载失败") widget_width, widget_height = 150, 40 else: widget_width, widget_height = bg_pix.width(), bg_pix.height() widget = QWidget() widget.setFixedSize(widget_width, widget_height) widget.setObjectName("autoModeWidget") widget.setStyleSheet(f""" QWidget #autoModeWidget{{ background-image: url({bg_img}); background-repeat: no-repeat; background-position: center; border: none; }} """) layout = QHBoxLayout(widget) layout.setContentsMargins(10, 0, 10, 0) layout.setSpacing(10) # 文字 text_label = QLabel("自动模式") text_label.setStyleSheet("color: #3bfff8; font-size: 20px;") layout.addWidget(text_label, alignment=Qt.AlignVCenter) # 开关(初始的时候,自动模式是打开的) self.auto_switch = SwitchButton() self.auto_switch.setChecked(True) # 设置自动模式初始状态 layout.addWidget(self.auto_switch, alignment=Qt.AlignVCenter | Qt.AlignRight) return widget def showVersionInfo(self): """显示版本信息弹窗""" info = "智能浇筑系统\n版本:V1.0\n" QMessageBox.information(self, "版本信息", info) def paintEvent(self, event): """绘制背景图""" super().paintEvent(event) if not self.bg_pixmap.isNull(): painter = QPainter(self) painter.drawPixmap(0, 0, self.bg_pixmap) if __name__ == "__main__": import sys from PySide6.QtWidgets import QApplication app = QApplication(sys.argv) widget = BottomControlWidget() widget.set_system_status("error", 33) widget.show() sys.exit(app.exec())