gcode预览测试

This commit is contained in:
2026-05-14 20:21:16 +08:00
parent 65f221a5d8
commit 837996c436
17 changed files with 1363 additions and 296 deletions

View File

@@ -1,10 +1,11 @@
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel,
QFrame, QGridLayout, QSizePolicy, QLineEdit,
QFrame, QGridLayout, QSizePolicy, QLineEdit, QApplication,
)
from PyQt6.QtCore import Qt, QTimer, QPointF, QRectF
from PyQt6.QtCore import Qt, QTimer, QPointF, QRectF, QEvent
from PyQt6.QtGui import QFont, QPainter, QColor, QBrush, QPen, QDoubleValidator
from utils.config_parse import ConfigParse
from utils.floating_keyboard import FloatingKeyboard
MOVE_STEP = 10 # 每次点击移动 mm (保留备用)
@@ -222,6 +223,16 @@ class ControlPage(QWidget):
self._homed = False # 是否已轴回零
self._motor_on = True # 电机是否已使能默认True关电机后置False
# 归零后的停留位置
hp = self.config_parser.home_positions or {}
self._home_x = hp.get("x", 0.0)
self._home_y = hp.get("y", 0.0)
self._home_z = hp.get("z", 0.0)
# 悬浮键盘
self._keyboard = FloatingKeyboard()
self._keyboard_attached = False
self.init_ui()
self._sync_inputs()
self._apply_state()
@@ -371,7 +382,9 @@ class ControlPage(QWidget):
def _cmd_home(self):
self.api_client.home_axes(["x", "y", "z"])
self.pos_x = self.pos_y = self.pos_z = 0.0
self.pos_x = self._home_x
self.pos_y = self._home_y
self.pos_z = self._home_z
self._homed = True
self._motor_on = True
self._sync_inputs()
@@ -385,6 +398,45 @@ class ControlPage(QWidget):
self._motor_on = False
self._apply_state()
# ── 悬浮键盘 ─────────────────────────────────────────────
def _attach_keyboard(self, widget):
"""将悬浮键盘绑定到指定输入框并显示"""
self._keyboard.attach(widget)
self._keyboard.show_below(widget)
self._keyboard_attached = True
def _dismiss_keyboard(self):
"""关闭悬浮键盘"""
self._keyboard.hide()
self._keyboard.detach()
self._keyboard_attached = False
def _on_input_focus_in(self, widget):
"""输入框获得焦点时的处理"""
if self._keyboard_attached:
self._keyboard.attach(widget)
self._keyboard.show_below(widget)
else:
self._attach_keyboard(widget)
def eventFilter(self, obj, event):
if event.type() == QEvent.Type.FocusIn:
if obj in (self.input_x, self.input_y, self.input_z):
self._on_input_focus_in(obj)
elif self._keyboard_attached and not isinstance(obj, QLineEdit):
self._dismiss_keyboard()
elif event.type() == QEvent.Type.FocusOut:
if obj in (self.input_x, self.input_y, self.input_z):
QTimer.singleShot(100, self._check_dismiss_keyboard)
return super().eventFilter(obj, event)
def _check_dismiss_keyboard(self):
"""检查当前焦点是否还在坐标输入框上,不在则关闭键盘"""
w = self.focusWidget()
if w not in (self.input_x, self.input_y, self.input_z):
self._dismiss_keyboard()
# ── UI 构建 ──────────────────────────────────────────────
def init_ui(self):
@@ -511,6 +563,7 @@ class ControlPage(QWidget):
inp.setAlignment(Qt.AlignmentFlag.AlignCenter)
inp.setValidator(QDoubleValidator(-9999, 9999, 1))
inp.returnPressed.connect(self._on_coord_changed)
inp.installEventFilter(self)
setattr(self, f"input_{axis.lower()}", inp)
coord_row.addWidget(inp)