优化ui,正在优化gcode_viewer

This commit is contained in:
2026-05-16 23:21:20 +08:00
parent d80e8dd05d
commit 1c0fc59738
11 changed files with 1480 additions and 99 deletions

View File

@@ -275,7 +275,6 @@ class ControlPage(QWidget):
def _on_config_changed(self, config_instance):
self._load_limits()
self._load_speeds()
print(f"z-max-s:{self.move_speed_z}")
# ── 状态管理 ──────────────────────────────────────────

View File

@@ -8,6 +8,7 @@ from PyQt6.QtWidgets import (
QPushButton,
QLineEdit,
QMessageBox,
QDialog,
QFormLayout,
QComboBox,
QListWidgetItem,
@@ -28,6 +29,7 @@ import re
from utils.wifi_manager import WifiManager
from utils.floating_keyboard import FloatingKeyboard
from utils.get_bootstrap_icon import get_colored_icon, get_colored_pixmap
from utils.modern_confirm import ModernConfirmDialog
class DragScrollArea(QScrollArea):
@@ -344,7 +346,7 @@ class SettingPage(QWidget):
color: #f2f2f2;
font-size: 30px;
padding: 10px;
background-color: #3f3f3f;
background-color: transparent;
}
QMessageBox QPushButton {
min-width: 130px;
@@ -1016,32 +1018,25 @@ class SettingPage(QWidget):
self.settings_stack.addWidget(self._wrap_scroll(power_widget))
def _confirm_reboot(self):
reply = QMessageBox.question(
self, "确认重启",
"确定要重启系统吗?\n所有未保存的数据将丢失。",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
dlg = ModernConfirmDialog(self, "确认重启", "确定要重启系统吗?\n所有未保存的数据将丢失。", "question-circle.svg", confirm_text="重启", cancel_text="取消")
if dlg.exec() == QDialog.DialogCode.Accepted:
# QTimer.singleShot(500, lambda: print("test sudo reboot"))
QTimer.singleShot(500, lambda: os.system("sudo reboot"))
self._styled_message(
"info", self, "重启",
"系统正在重启..."
)
QTimer.singleShot(500, lambda: os.system("sudo reboot"))
def _confirm_shutdown(self):
reply = QMessageBox.question(
self, "确认关机",
"确定要关闭系统吗?\n关闭后需要手动重新开机。",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
dlg = ModernConfirmDialog(self, "确认关机", "确定要关闭系统吗?\n关闭后需要手动重新开机。", "question-circle.svg", confirm_text="关机", cancel_text="取消")
if dlg.exec() == QDialog.DialogCode.Accepted:
# QTimer.singleShot(500, lambda: print("test sudo poweroff"))
QTimer.singleShot(500, lambda: os.system("sudo poweroff"))
self._styled_message(
"info", self, "关机",
"系统正在关机..."
)
QTimer.singleShot(500, lambda: os.system("sudo poweroff"))
def init_todo_settings(self):
todo_widget = QWidget()

View File

@@ -71,14 +71,19 @@ class CardFrame(QFrame):
class TempGauge(QWidget):
"""温度计指示器"""
def __init__(self, label="Tool", parent=None):
def __init__(self, label="Tool", temp_range=(0, 300), parent=None):
super().__init__(parent)
self.setFixedSize(100, 80)
self.setFixedSize(160, 90)
self._label = label
self._actual = 0.0
self._target = 0.0
self._max_temp = temp_range[1]
self._min_temp = temp_range[0]
def set_value(self, actual, target):
def set_value(self, actual, target, temp_range=None):
if temp_range is not None:
self._max_temp = temp_range[1]
self._min_temp = temp_range[0]
self._actual = actual
self._target = target
self.update()
@@ -89,14 +94,14 @@ class TempGauge(QWidget):
w, h = self.width(), self.height()
# 背景条
bar_x, bar_w = 16, 20
bar_x, bar_w = 60, 20
bar_y, bar_h = 10, 56
p.setPen(QPen(QColor("#555555"), 1))
p.setBrush(QBrush(QColor("#2a2a2a")))
p.drawRoundedRect(bar_x, bar_y, bar_w, bar_h, 4, 4)
# 填充柱(按温度比例,最高 300°C
ratio = min(max(self._actual / 300, 0), 1)
ratio = min(max((self._actual - self._min_temp) / (self._max_temp - self._min_temp), 0), 1)
fill_h = int((bar_h - 4) * ratio)
if fill_h > 0:
grad = QLinearGradient(0, bar_y + bar_h, 0, bar_y)
@@ -108,28 +113,29 @@ class TempGauge(QWidget):
# 目标值标记线
if self._target > 0:
tgt_y = bar_y + bar_h - int((bar_h - 4) * min(self._target / 300, 1))
p.setPen(QPen(QColor("#ffffff"), 2))
tgt_y = bar_y + bar_h - int((bar_h - 4) * min((self._target - self._min_temp) / (self._max_temp - self._min_temp), 1))
p.setPen(QPen(QColor("#888888"), 2))
p.drawLine(bar_x - 2, tgt_y, bar_x + bar_w + 2, tgt_y)
# 文字
font = QFont("sans-serif", 11, QFont.Weight.Bold)
p.setFont(font)
p.setPen(QPen(QColor("#e0e0e0")))
p.drawText(44, 16, w - 44, 24, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
f"{self._actual:.0f}°")
temp_to_hex_soft = lambda t: "#{:02x}{:02x}{:02x}".format(*((lambda x: ((int(255*(0.3+0.7*(x/0.5)**0.8)), int(180*(x/0.5)**0.9), 255)if x < 0.5 else(255, int(180*(1-((x-0.5)/0.5)**1.2)), 80)))(max(0, min(t - self._min_temp, self._max_temp - self._min_temp))/(self._max_temp - self._min_temp))))
p.setPen(QPen(QColor(temp_to_hex_soft(self._actual))))
p.drawText(0, int(bar_y + bar_h*(1-ratio)-12), w - 44, 24, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
f"{self._actual:>5.1f}°C")
if self._target > 0:
tgt_y = bar_y + bar_h - int((bar_h - 4) * min(self._target / 300, 1))
tgt_y = bar_y + bar_h - int((bar_h - 4) * min((self._target - self._min_temp) / (self._max_temp - self._min_temp), 1))
font2 = QFont("sans-serif", 10)
p.setFont(font2)
p.setPen(QPen(QColor("#888888")))
p.drawText(44, tgt_y - 10, w - 44, 20, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
f"{self._target:.0f}°")
p.drawText(90, tgt_y - 10, w - 44, 20, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
f"{self._target:.1f}°C")
font3 = QFont("sans-serif", 10, QFont.Weight.Bold)
p.setFont(font3)
p.setPen(QPen(QColor("#aaaaaa")))
p.drawText(0, h - 20, w, 20, Qt.AlignmentFlag.AlignCenter, self._label)
p.drawText(0, h - 20, w-20, 20, Qt.AlignmentFlag.AlignCenter, self._label)
@@ -155,6 +161,8 @@ class StatusPage(QWidget):
self.config_parser = ConfigParse()
self.config_parser.config_changed.connect(self._on_config_changed)
self.gcode_dir = self.config_parser.gcode_dir
self.tool_temp_range = (self.config_parser.hotend_temp_range.get("min", 0), self.config_parser.hotend_temp_range.get("max", 300))
self.bed_temp_range = (self.config_parser.bed_temp_range.get("min", 0), self.config_parser.bed_temp_range.get("max", 120))
self._loaded_file = None
self.init_ui()
@@ -165,6 +173,8 @@ class StatusPage(QWidget):
def _on_config_changed(self, config_instance):
self.gcode_dir = self.config_parser.gcode_dir
self.tool_temp_range = (self.config_parser.hotend_temp_range.get("min", 0), self.config_parser.hotend_temp_range.get("max", 300))
self.bed_temp_range = (self.config_parser.bed_temp_range.get("min", 0), self.config_parser.bed_temp_range.get("max", 120))
def fresh_status_valve(self):
data = self.api_client.get_status()
@@ -272,8 +282,8 @@ class StatusPage(QWidget):
self._temp_card = CardFrame("温度")
temp_row = QHBoxLayout()
temp_row.setSpacing(8)
self._tool_gauge = TempGauge("喷头")
self._bed_gauge = TempGauge("热床")
self._tool_gauge = TempGauge("喷头", self.tool_temp_range)
self._bed_gauge = TempGauge("热床", self.bed_temp_range)
temp_row.addWidget(self._tool_gauge)
temp_row.addWidget(self._bed_gauge)
temp_row.addStretch()
@@ -361,8 +371,8 @@ class StatusPage(QWidget):
""")
# 温度
self._tool_gauge.set_value(self.tool_temp_actual, self.tool_temp_target)
self._bed_gauge.set_value(self.bed_temp_actual, self.bed_temp_target)
self._tool_gauge.set_value(self.tool_temp_actual, self.tool_temp_target, self.tool_temp_range)
self._bed_gauge.set_value(self.bed_temp_actual, self.bed_temp_target, self.bed_temp_range)
# G-code 模型加载与进度更新
if self.file_name and self.file_name != "None":
@@ -380,48 +390,3 @@ class StatusPage(QWidget):
is_printing = self.state.startswith("Printing") or self.state.startswith("Paused")
self.gcode_viewer.update_by_filepos(self.filepos, is_printing)
#TODO: Better Gcode Parser, this one is too slow for large files, need to optimize or use a separate thread to load
# def load_gcode_vertices(self, path):
# vertices = []
# x = 0
# y = 0
# z = 0
# with open(path, "r", encoding="utf-8", errors="ignore") as f:
# for line in f:
# line = line.strip()
# if not line:
# continue
# if line.startswith("G0") or line.startswith("G1"):
# old_x = x
# old_y = y
# old_z = z
# mx = re.search(r"X([-0-9.]+)", line)
# my = re.search(r"Y([-0-9.]+)", line)
# mz = re.search(r"Z([-0-9.]+)", line)
# if mx:
# x = float(mx.group(1))
# if my:
# y = float(my.group(1))
# if mz:
# z = float(mz.group(1))
# vertices.append({
# "x1": old_x,
# "y1": old_y,
# "z1": old_z,
# "x2": x,
# "y2": y,
# "z2": z,
# })
# return vertices