refaktor za pomoca gemini

This commit is contained in:
2025-12-29 08:25:34 +01:00
parent 7a3ff420e7
commit cb6e12386f
2 changed files with 118 additions and 143 deletions

View File

@@ -6,7 +6,7 @@ from qtpy import uic
from qtpy.QtCore import Qt from qtpy.QtCore import Qt
from qtpy.QtWidgets import QWidget, QPushButton from qtpy.QtWidgets import QWidget, QPushButton
from qtpyvcp.actions import machine, machine_actions from qtpyvcp.actions import machine_actions
from qtpyvcp.plugins import getPlugin from qtpyvcp.plugins import getPlugin
from qtpyvcp.utilities import logger from qtpyvcp.utilities import logger
from widgets.conversational.float_line_edit import FloatLineEdit from widgets.conversational.float_line_edit import FloatLineEdit
@@ -19,158 +19,133 @@ TOOL_TABLE = getPlugin('tooltable')
INI_FILE = linuxcnc.ini(os.getenv('INI_FILE_NAME')) INI_FILE = linuxcnc.ini(os.getenv('INI_FILE_NAME'))
CMD = linuxcnc.command() CMD = linuxcnc.command()
class MoveMode(Enum): class MoveMode(Enum):
"""Defines the available movement modes (absolute, work coordinate system, relative)."""
ABS = 1 ABS = 1
WCS = 2 WCS = 2
REL = 3 REL = 3
class UserTab(QWidget): class UserTab(QWidget):
"""A sidebar widget for controlling machine axis movements."""
def __init__(self, parent=None): def __init__(self, parent=None):
super(UserTab, self).__init__(parent) super(UserTab, self).__init__(parent)
ui_file = os.path.splitext(os.path.basename(__file__))[0] + ".ui" ui_file = os.path.splitext(os.path.basename(__file__))[0] + ".ui"
uic.loadUi(os.path.join(os.path.dirname(__file__), ui_file), self) uic.loadUi(os.path.join(os.path.dirname(__file__), ui_file), self)
STATUS.on.notify(self._update_controls) self.position_inputs: list[FloatLineEdit] = self.findChildren(FloatLineEdit)
STATUS.all_axes_homed.notify(self._update_controls) self.move_buttons: list[QPushButton] = self.findChildren(QPushButton)
STATUS.g5x_index.notify(self.update_wcs_label) self._connect_signals()
self.update_wcs_label() self.update_wcs_label()
self.edits: list[FloatLineEdit] = self.findChildren(FloatLineEdit) def _connect_signals(self):
"""Connects widget signals to corresponding slots."""
STATUS.on.notify(self._update_control_states)
STATUS.all_axes_homed.notify(self._update_control_states)
STATUS.g5x_index.notify(self.update_wcs_label)
for fle in self.edits: for line_edit in self.position_inputs:
fle.setEnabled(False) line_edit.setEnabled(False)
fle.editingFinished.connect(self.format_float) line_edit.editingFinished.connect(self._format_sender_float_value)
self.buttons: list[QPushButton] = self.findChildren(QPushButton) for button in self.move_buttons:
button.setEnabled(False)
button.clicked.connect(self._on_move_button_clicked)
for btn in self.buttons: def _format_sender_float_value(self):
btn.setEnabled(False) """Formats the text of the sending FloatLineEdit to its specified format."""
btn.clicked.connect(self.move_axis) sender_widget = self.sender()
if not isinstance(sender_widget, FloatLineEdit):
def format_float(self):
if not isinstance(self.sender(), FloatLineEdit):
return return
w: FloatLineEdit = self.sender() LOG.debug(f"Formatting float for {sender_widget.objectName()}")
if w is None: value = sender_widget.value()
LOG.debug(f"fn _format_float, sender None") formatted_value = sender_widget.format_string.format(value)
return sender_widget.setText(formatted_value)
LOG.debug(f"fn: _format_float, sender name: {w.objectName()}")
value = w.value() def update_wcs_label(self, *args):
fmt = w.format_string """Updates the WCS status label with the current coordinate system."""
val_fmt = fmt.format(value) wcs_index = STATUS.g5x_index
w.setText(val_fmt) self.wcs_status_label.setText(f"WCS {wcs_index}")
def _on_move_button_clicked(self):
"""Handles clicks on any of the move buttons."""
def update_wcs_label(self): sender = self.sender()
idx = STATUS.g5x_index if not isinstance(sender, QPushButton):
self.statuslabel_wcs.setText(f"WCS {idx}")
# 'G54' if ch[0] == 1 else 'G55' if ch[0] == 2 else 'G56' if ch[0] == 3 else 'none'
def move_relative(self):
# machine_actions.jog.axis(axis='x', direction=1, distance=0.5)
machine_actions.issue_mdi(f"G91 G0 X{self.fle_rel_x.value()}; G90")
LOG.debug(f"fn: move_x. fle_wcs_x:{self.fle_rel_x.value()}")
def move_axis(self):
if not isinstance(self.sender(), QPushButton):
return return
sender: QPushButton = self.sender() button_name = sender.objectName()
btn_name = sender.objectName() LOG.debug(f"Move button clicked: {button_name}")
LOG.debug(f"fn: move_axis. sender:{btn_name}")
if "abs" in btn_name: try:
dest_x = self.fle_abs_x.value() _, mode_str, axes_str = button_name.replace('_button', '').split('_')
dest_y = self.fle_abs_y.value() except ValueError:
dest_z = self.fle_abs_z.value() LOG.error(f"Could not parse button name: {button_name}")
if "x" == btn_name[-1]:
self.move_wcs(MoveMode.ABS, x=dest_x)
return
if "y" == btn_name[-1]:
self.move_wcs(MoveMode.ABS, y=dest_y)
return
if "z" == btn_name[-1]:
self.move_wcs(MoveMode.ABS, z=dest_z)
return
self.move_wcs(MoveMode.ABS, dest_x, dest_y, dest_z)
LOG.debug(f"fn: move_wcs[ABS] x:{dest_x}, y:{dest_y}, z:{dest_z}")
if "wcs" in btn_name:
dest_x = self.fle_wcs_x.value()
dest_y = self.fle_wcs_y.value()
dest_z = self.fle_wcs_z.value()
if "x" == btn_name[-1]:
self.move_wcs(MoveMode.WCS, x=dest_x)
return
if "y" == btn_name[-1]:
self.move_wcs(MoveMode.WCS, y=dest_y)
return
if "z" == btn_name[-1]:
self.move_wcs(MoveMode.WCS, z=dest_z)
return return
self.move_wcs(MoveMode.WCS, dest_x, dest_y, dest_z) move_configs = {
LOG.debug(f"fn: move_wcs[WCS] x:{dest_x}, y:{dest_y}, z:{dest_z}") 'abs': (MoveMode.ABS, self.abs_x_input, self.abs_y_input, self.abs_z_input),
'wcs': (MoveMode.WCS, self.wcs_x_input, self.wcs_y_input, self.wcs_z_input),
'rel': (MoveMode.REL, self.rel_x_input, self.rel_y_input, self.rel_z_input)
}
if "rel" in btn_name: if mode_str not in move_configs:
dest_x = self.fle_rel_x.value() LOG.error(f"Unknown move mode '{mode_str}' in button name: {button_name}")
dest_y = self.fle_rel_y.value()
dest_z = self.fle_rel_z.value()
if "x" == btn_name[-1]:
self.move_wcs(MoveMode.REL, x=dest_x)
return
if "y" == btn_name[-1]:
self.move_wcs(MoveMode.REL, y=dest_y)
return
if "z" == btn_name[-1]:
self.move_wcs(MoveMode.REL, z=dest_z)
return return
self.move_wcs(MoveMode.REL, dest_x, dest_y, dest_z) mode, x_input, y_input, z_input = move_configs[mode_str]
LOG.debug(f"fn: move_wcs[REL] x:{dest_x}, y:{dest_y}, z:{dest_z}")
x, y, z = None, None, None
if 'x' in axes_str or 'all' in axes_str:
x = x_input.value()
if 'y' in axes_str or 'all' in axes_str:
y = y_input.value()
if 'z' in axes_str or 'all' in axes_str:
z = z_input.value()
self._execute_move_command(mode, x, y, z)
LOG.debug(f"Executed move: {mode.name}, X={x}, Y={y}, Z={z}")
def move_wcs(self, mode:MoveMode, x=None, y=None, z=None): def _execute_move_command(self, mode: MoveMode, x=None, y=None, z=None):
"""Builds and sends an MDI command for the specified movement."""
mdi_command = ""
match mode: match mode:
case MoveMode.ABS: mdi_cmd = "G53 G0" case MoveMode.ABS:
case MoveMode.WCS: mdi_cmd = "G0" mdi_command = "G53 G0"
case MoveMode.REL: mdi_cmd = "G91 G0" case MoveMode.WCS:
mdi_command = "G0"
case MoveMode.REL:
mdi_command = "G91 G0"
if x is not None: if x is not None:
mdi_cmd += f" X{x}" mdi_command += f" X{x}"
if y is not None: if y is not None:
mdi_cmd += f" Y{y}" mdi_command += f" Y{y}"
if z is not None: if z is not None:
mdi_cmd += f" Z{z}" mdi_command += f" Z{z}"
if mode == MoveMode.REL: if mode == MoveMode.REL:
mdi_cmd += " ;G90" mdi_command += " ;G90"
LOG.debug(f"fn: move_wcs. MDI:{mdi_cmd}") LOG.debug(f"Sending MDI command: {mdi_command}")
machine_actions.issue_mdi(mdi_cmd) machine_actions.issue_mdi(mdi_command)
def _machine_ready(self) -> bool: def _is_machine_ready(self) -> bool:
LOG.debug(f"fn: machine_ready, on:{STATUS.on()}") """Checks if the machine is on and all axes are homed."""
LOG.debug(f"fn: machine_ready, all_axes_homed:{STATUS.all_axes_homed()}") is_ready = STATUS.on() and STATUS.all_axes_homed()
return ( LOG.debug(f"Machine ready check: on={STATUS.on()}, homed={STATUS.all_axes_homed()} -> {is_ready}")
STATUS.on() return is_ready
and STATUS.all_axes_homed()
)
def _update_controls(self, *args): def _update_control_states(self, *args):
ready = self._machine_ready() """Enables or disables UI controls based on the machine's ready state."""
LOG.debug(f"Machine ready: {ready}") is_ready = self._is_machine_ready()
LOG.debug(f"Updating control states. Machine ready: {is_ready}")
for btn in self.buttons: for button in self.move_buttons:
btn.setEnabled(ready) button.setEnabled(is_ready)
for fle in self.edits:
fle.setEnabled(ready)
for line_edit in self.position_inputs:
line_edit.setEnabled(is_ready)

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>USER</class> <class>Move</class>
<widget class="QWidget" name="USER"> <widget class="QWidget" name="Move">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@@ -41,9 +41,9 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="FloatLineEdit" name="fle_abs_x"> <widget class="FloatLineEdit" name="abs_x_input">
<property name="text"> <property name="text">
<string>1.1234</string> <string> 0.0000</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>
@@ -57,7 +57,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_abs_x"> <widget class="QPushButton" name="move_abs_x_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -83,7 +83,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<widget class="FloatLineEdit" name="fle_abs_y"> <widget class="FloatLineEdit" name="abs_y_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -96,7 +96,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_abs_y"> <widget class="QPushButton" name="move_abs_y_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -122,7 +122,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="horizontalLayout_3">
<item> <item>
<widget class="FloatLineEdit" name="fle_abs_z"> <widget class="FloatLineEdit" name="abs_z_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -135,7 +135,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_abs_z"> <widget class="QPushButton" name="move_abs_z_button">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed"> <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@@ -165,7 +165,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_abs_all"> <widget class="QPushButton" name="move_abs_all_button">
<property name="text"> <property name="text">
<string>GO XYZ</string> <string>GO XYZ</string>
</property> </property>
@@ -192,7 +192,7 @@
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
<widget class="StatusLabel" name="statuslabel_wcs"> <widget class="StatusLabel" name="wcs_status_label">
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">QLabel{ <string notr="true">QLabel{
color: rgb(238, 238, 236); color: rgb(238, 238, 236);
@@ -207,7 +207,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_4"> <layout class="QHBoxLayout" name="horizontalLayout_4">
<item> <item>
<widget class="FloatLineEdit" name="fle_wcs_x"> <widget class="FloatLineEdit" name="wcs_x_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -223,7 +223,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_wcs_x"> <widget class="QPushButton" name="move_wcs_x_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -246,7 +246,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_5"> <layout class="QHBoxLayout" name="horizontalLayout_5">
<item> <item>
<widget class="FloatLineEdit" name="fle_wcs_y"> <widget class="FloatLineEdit" name="wcs_y_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -259,7 +259,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_wcs_y"> <widget class="QPushButton" name="move_wcs_y_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -282,7 +282,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_6"> <layout class="QHBoxLayout" name="horizontalLayout_6">
<item> <item>
<widget class="FloatLineEdit" name="fle_wcs_z"> <widget class="FloatLineEdit" name="wcs_z_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -295,7 +295,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_wcs_z"> <widget class="QPushButton" name="move_wcs_z_button">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed"> <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@@ -322,7 +322,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_wcs_all"> <widget class="QPushButton" name="move_wcs_all_button">
<property name="text"> <property name="text">
<string>GO XYZ</string> <string>GO XYZ</string>
</property> </property>
@@ -364,7 +364,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_7"> <layout class="QHBoxLayout" name="horizontalLayout_7">
<item> <item>
<widget class="FloatLineEdit" name="fle_rel_x"> <widget class="FloatLineEdit" name="rel_x_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -380,7 +380,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_rel_x"> <widget class="QPushButton" name="move_rel_x_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -403,7 +403,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_8"> <layout class="QHBoxLayout" name="horizontalLayout_8">
<item> <item>
<widget class="FloatLineEdit" name="fle_rel_y"> <widget class="FloatLineEdit" name="rel_y_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -416,7 +416,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_rel_y"> <widget class="QPushButton" name="move_rel_y_button">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>50</width> <width>50</width>
@@ -439,7 +439,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_9"> <layout class="QHBoxLayout" name="horizontalLayout_9">
<item> <item>
<widget class="FloatLineEdit" name="fle_rel_z"> <widget class="FloatLineEdit" name="rel_z_input">
<property name="text"> <property name="text">
<string> 0.0000</string> <string> 0.0000</string>
</property> </property>
@@ -452,7 +452,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_rel_z"> <widget class="QPushButton" name="move_rel_z_button">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed"> <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@@ -479,7 +479,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_rel_all"> <widget class="QPushButton" name="move_rel_all_button">
<property name="text"> <property name="text">
<string>GO XYZ</string> <string>GO XYZ</string>
</property> </property>