-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_panel.py
More file actions
757 lines (627 loc) · 27.7 KB
/
frame_panel.py
File metadata and controls
757 lines (627 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
"""
Frame Management Panel for ThorRR Robot Arm GUI
Provides UI for:
- Frame/tool selection
- Creating/deleting frames
- 3-point teaching workflow
- Frame configuration editing
"""
import logging
from typing import Optional, Callable, List
from PyQt5.QtWidgets import (
QFrame, QVBoxLayout, QHBoxLayout, QGridLayout,
QLabel, QPushButton, QComboBox, QLineEdit,
QDoubleSpinBox, QTableWidget, QTableWidgetItem,
QHeaderView, QMessageBox, QWidget, QScrollArea
)
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5 import QtGui
from calibration_panel import CollapsibleSection
from frame_controller import FrameController
from frame_teaching import TeachingProgress, TeachingState
logger = logging.getLogger(__name__)
class FrameSelectionWidget(QFrame):
"""Compact frame and tool selector for embedding in other panels"""
frame_changed = pyqtSignal(str)
tool_changed = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.setFrameShape(QFrame.StyledPanel)
layout = QHBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)
layout.setSpacing(10)
# Frame selector
layout.addWidget(QLabel("Frame:"))
self.frame_combo = QComboBox()
self.frame_combo.setMinimumWidth(100)
self.frame_combo.currentTextChanged.connect(self._on_frame_changed)
layout.addWidget(self.frame_combo)
# Tool selector
layout.addWidget(QLabel("Tool:"))
self.tool_combo = QComboBox()
self.tool_combo.setMinimumWidth(100)
self.tool_combo.currentTextChanged.connect(self._on_tool_changed)
layout.addWidget(self.tool_combo)
layout.addStretch()
def update_frames(self, frames: List[str]):
"""Update frame list"""
current = self.frame_combo.currentText()
self.frame_combo.blockSignals(True)
self.frame_combo.clear()
self.frame_combo.addItems(frames)
if current in frames:
self.frame_combo.setCurrentText(current)
self.frame_combo.blockSignals(False)
def update_tools(self, tools: List[str]):
"""Update tool list"""
current = self.tool_combo.currentText()
self.tool_combo.blockSignals(True)
self.tool_combo.clear()
self.tool_combo.addItems(tools)
if current in tools:
self.tool_combo.setCurrentText(current)
self.tool_combo.blockSignals(False)
def set_current_frame(self, frame: str):
"""Set current frame selection"""
self.frame_combo.blockSignals(True)
self.frame_combo.setCurrentText(frame)
self.frame_combo.blockSignals(False)
def set_current_tool(self, tool: str):
"""Set current tool selection"""
self.tool_combo.blockSignals(True)
self.tool_combo.setCurrentText(tool)
self.tool_combo.blockSignals(False)
def _on_frame_changed(self, text):
if text:
self.frame_changed.emit(text)
def _on_tool_changed(self, text):
if text:
self.tool_changed.emit(text)
class FrameManagementPanel(QFrame):
"""Full frame management panel for FRAMES mode"""
def __init__(self, frame_controller: Optional[FrameController] = None):
super().__init__()
self.frame_controller = frame_controller
self.setFrameShape(QFrame.StyledPanel)
self._setup_ui()
def set_controller(self, controller: FrameController):
"""Set or update frame controller"""
self.frame_controller = controller
# Connect controller callbacks
controller.on_frames_updated = self._on_frames_updated
controller.on_tools_updated = self._on_tools_updated
controller.on_workpieces_updated = self._on_workpieces_updated
controller.on_teaching_progress = self._on_teaching_progress
# Initialise UI state
self._refresh_all_lists()
def _setup_ui(self):
"""Setup the panel UI"""
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(10, 10, 10, 10)
main_layout.setSpacing(10)
# Title
title = QLabel("Coordinate Frames")
title_font = QtGui.QFont()
title_font.setPointSize(14)
title_font.setBold(True)
title.setFont(title_font)
main_layout.addWidget(title)
# Panel description
desc = QLabel(
"Define coordinate frames for your workpieces and tools. "
"The working frame determines how Cartesian jog commands are interpreted."
)
desc.setWordWrap(True)
desc.setStyleSheet("color: #666; margin-bottom: 4px;")
main_layout.addWidget(desc)
# --- Active Selection (always visible) ---
selection_frame = QFrame()
selection_frame.setFrameShape(QFrame.StyledPanel)
selection_layout = QHBoxLayout(selection_frame)
selection_layout.setContentsMargins(8, 6, 8, 6)
selection_layout.addWidget(QLabel("Working Frame:"))
self.frame_combo = QComboBox()
self.frame_combo.setMinimumWidth(150)
self.frame_combo.currentTextChanged.connect(self._on_frame_selected)
selection_layout.addWidget(self.frame_combo)
selection_layout.addSpacing(20)
selection_layout.addWidget(QLabel("Active Tool:"))
self.tool_combo = QComboBox()
self.tool_combo.setMinimumWidth(120)
self.tool_combo.currentTextChanged.connect(self._on_tool_selected)
selection_layout.addWidget(self.tool_combo)
selection_layout.addStretch()
main_layout.addWidget(selection_frame)
# --- Scrollable content for collapsible sections ---
scroll = QScrollArea()
scroll.setWidgetResizable(True)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll.setFrameShape(QFrame.NoFrame)
scroll_content = QWidget()
scroll_layout = QVBoxLayout(scroll_content)
scroll_layout.setContentsMargins(0, 0, 0, 0)
scroll_layout.setSpacing(6)
# ── Workpiece Frames ──
workpiece_section = CollapsibleSection("Workpiece Frames", is_expanded=True)
wp_desc = QLabel(
"Define coordinate frames on your workpiece using 3-point teaching. "
"Move the TCP to three points to define the frame origin, X-axis, "
"and XY plane."
)
wp_desc.setWordWrap(True)
wp_desc.setStyleSheet("color: #666; margin-bottom: 4px;")
workpiece_section.add_widget(wp_desc)
# Workpiece table
self.workpiece_table = QTableWidget()
self.workpiece_table.setColumnCount(4)
self.workpiece_table.setHorizontalHeaderLabels(["Name", "X", "Y", "Z"])
self.workpiece_table.horizontalHeader().setSectionResizeMode(
0, QHeaderView.Stretch
)
self.workpiece_table.setMaximumHeight(150)
workpiece_section.add_widget(self.workpiece_table)
# Workpiece buttons
wp_btn_layout = QHBoxLayout()
self.btn_teach_workpiece = QPushButton("Teach New")
self.btn_teach_workpiece.clicked.connect(self._show_teaching_ui)
wp_btn_layout.addWidget(self.btn_teach_workpiece)
self.btn_delete_workpiece = QPushButton("Delete Selected")
self.btn_delete_workpiece.clicked.connect(self._delete_selected_workpiece)
wp_btn_layout.addWidget(self.btn_delete_workpiece)
wp_btn_layout.addStretch()
workpiece_section.add_layout(wp_btn_layout)
# --- Inline Teaching UI (hidden by default) ---
self.teaching_container = QWidget()
self.teaching_container.setVisible(False)
teaching_layout = QVBoxLayout(self.teaching_container)
teaching_layout.setContentsMargins(0, 8, 0, 0)
separator = QFrame()
separator.setFrameShape(QFrame.HLine)
separator.setStyleSheet("color: #ccc;")
teaching_layout.addWidget(separator)
# Name input + step indicator row
name_row = QHBoxLayout()
name_row.addWidget(QLabel("Frame Name:"))
self.teaching_name_input = QLineEdit()
self.teaching_name_input.setPlaceholderText("Enter frame name...")
name_row.addWidget(self.teaching_name_input)
name_row.addSpacing(10)
self.step_dots = QLabel()
self.step_dots.setStyleSheet("font-size: 14px;")
self._update_step_dots(0)
name_row.addWidget(self.step_dots)
teaching_layout.addLayout(name_row)
# Teaching message
self.teaching_message = QLabel(
"Enter a name and click Start to begin teaching."
)
self.teaching_message.setWordWrap(True)
self.teaching_message.setStyleSheet("color: #666; padding: 5px;")
teaching_layout.addWidget(self.teaching_message)
# Teaching buttons
teach_btn_layout = QHBoxLayout()
self.btn_start_teaching = QPushButton("Start Teaching")
self.btn_start_teaching.clicked.connect(self._start_teaching)
teach_btn_layout.addWidget(self.btn_start_teaching)
self.btn_record_point = QPushButton("Record Point")
self.btn_record_point.setEnabled(False)
self.btn_record_point.clicked.connect(self._record_teaching_point)
teach_btn_layout.addWidget(self.btn_record_point)
self.btn_finish_teaching = QPushButton("Finish")
self.btn_finish_teaching.setEnabled(False)
self.btn_finish_teaching.clicked.connect(self._finish_teaching)
teach_btn_layout.addWidget(self.btn_finish_teaching)
self.btn_cancel_teaching = QPushButton("Cancel")
self.btn_cancel_teaching.setEnabled(False)
self.btn_cancel_teaching.clicked.connect(self._cancel_teaching)
teach_btn_layout.addWidget(self.btn_cancel_teaching)
teaching_layout.addLayout(teach_btn_layout)
workpiece_section.add_widget(self.teaching_container)
scroll_layout.addWidget(workpiece_section)
# ── Tool Frames ──
tool_section = CollapsibleSection("Tool Frames", is_expanded=False)
tool_desc = QLabel(
"Define tool centre point (TCP) offsets. The Z offset is the "
"distance from the flange to the tool tip along the tool axis."
)
tool_desc.setWordWrap(True)
tool_desc.setStyleSheet("color: #666; margin-bottom: 4px;")
tool_section.add_widget(tool_desc)
# Tool table
self.tool_table = QTableWidget()
self.tool_table.setColumnCount(4)
self.tool_table.setHorizontalHeaderLabels(
["Name", "Z Offset", "Description", ""]
)
self.tool_table.horizontalHeader().setSectionResizeMode(
0, QHeaderView.Stretch
)
self.tool_table.setMaximumHeight(120)
self.tool_table.cellChanged.connect(self._on_tool_table_edited)
tool_section.add_widget(self.tool_table)
# Add tool row
add_tool_layout = QHBoxLayout()
add_tool_layout.addWidget(QLabel("Name:"))
self.tool_name_input = QLineEdit()
self.tool_name_input.setMaximumWidth(100)
add_tool_layout.addWidget(self.tool_name_input)
add_tool_layout.addWidget(QLabel("Z Offset:"))
self.tool_z_spin = QDoubleSpinBox()
self.tool_z_spin.setRange(-200, 200)
self.tool_z_spin.setSuffix(" mm")
self.tool_z_spin.setMaximumWidth(100)
add_tool_layout.addWidget(self.tool_z_spin)
self.btn_add_tool = QPushButton("Save Tool")
self.btn_add_tool.clicked.connect(self._add_tool)
add_tool_layout.addWidget(self.btn_add_tool)
self.tool_z_spin.valueChanged.connect(self._on_tool_z_changed)
add_tool_layout.addStretch()
tool_section.add_layout(add_tool_layout)
scroll_layout.addWidget(tool_section)
# ── Base Frame ──
base_section = CollapsibleSection("Base Frame", is_expanded=False)
base_desc = QLabel(
"Adjust if the robot base is not at the world origin. "
"Most users can leave this at defaults."
)
base_desc.setWordWrap(True)
base_desc.setStyleSheet("color: #666; margin-bottom: 4px;")
base_section.add_widget(base_desc)
base_widget = QWidget()
base_layout = QGridLayout(base_widget)
base_layout.setContentsMargins(0, 0, 0, 0)
# Position
base_layout.addWidget(QLabel("Position:"), 0, 0)
self.base_x_spin = QDoubleSpinBox()
self.base_x_spin.setRange(-10000, 10000)
self.base_x_spin.setSuffix(" mm")
base_layout.addWidget(QLabel("X:"), 0, 1)
base_layout.addWidget(self.base_x_spin, 0, 2)
self.base_y_spin = QDoubleSpinBox()
self.base_y_spin.setRange(-10000, 10000)
self.base_y_spin.setSuffix(" mm")
base_layout.addWidget(QLabel("Y:"), 0, 3)
base_layout.addWidget(self.base_y_spin, 0, 4)
self.base_z_spin = QDoubleSpinBox()
self.base_z_spin.setRange(-10000, 10000)
self.base_z_spin.setSuffix(" mm")
base_layout.addWidget(QLabel("Z:"), 0, 5)
base_layout.addWidget(self.base_z_spin, 0, 6)
# Orientation
base_layout.addWidget(QLabel("Orientation:"), 1, 0)
self.base_roll_spin = QDoubleSpinBox()
self.base_roll_spin.setRange(-180, 180)
self.base_roll_spin.setSuffix(" deg")
base_layout.addWidget(QLabel("Roll:"), 1, 1)
base_layout.addWidget(self.base_roll_spin, 1, 2)
self.base_pitch_spin = QDoubleSpinBox()
self.base_pitch_spin.setRange(-180, 180)
self.base_pitch_spin.setSuffix(" deg")
base_layout.addWidget(QLabel("Pitch:"), 1, 3)
base_layout.addWidget(self.base_pitch_spin, 1, 4)
self.base_yaw_spin = QDoubleSpinBox()
self.base_yaw_spin.setRange(-180, 180)
self.base_yaw_spin.setSuffix(" deg")
base_layout.addWidget(QLabel("Yaw:"), 1, 5)
base_layout.addWidget(self.base_yaw_spin, 1, 6)
self.btn_apply_base = QPushButton("Apply Base Frame")
self.btn_apply_base.clicked.connect(self._apply_base_frame)
base_layout.addWidget(self.btn_apply_base, 2, 0, 1, 7)
base_section.add_widget(base_widget)
scroll_layout.addWidget(base_section)
# Accordion behavior
self._sections = [workpiece_section, tool_section, base_section]
for section in self._sections:
section.expanded.connect(self._on_section_expanded)
scroll_layout.addStretch()
scroll.setWidget(scroll_content)
main_layout.addWidget(scroll)
# ── Accordion ──
def _on_section_expanded(self, opened_section):
"""Accordion: collapse every section except the one just opened."""
for section in self._sections:
if section is not opened_section:
section.collapse()
# ── Step dots ──
def _update_step_dots(self, points_recorded: int):
"""Update the step dot indicator (0-3 points)."""
filled = "\u25CF" # ●
empty = "\u25CB" # ○
dots = " ".join(
filled if i < points_recorded else empty for i in range(3)
)
self.step_dots.setText(f"{points_recorded}/3 {dots}")
# ── Teaching UI visibility ──
def _show_teaching_ui(self):
"""Show the inline teaching UI within the workpiece section."""
self.teaching_container.setVisible(True)
self.teaching_name_input.clear()
self.teaching_name_input.setFocus()
self.btn_start_teaching.setEnabled(True)
self.teaching_name_input.setEnabled(True)
self.btn_record_point.setEnabled(False)
self.btn_finish_teaching.setEnabled(False)
self.btn_cancel_teaching.setEnabled(True)
self._update_step_dots(0)
self.teaching_message.setText(
"Enter a name and click Start to begin teaching."
)
self.teaching_message.setStyleSheet("color: #666; padding: 5px;")
def _hide_teaching_ui(self):
"""Hide the inline teaching UI."""
self.teaching_container.setVisible(False)
# ── Refresh ──
def _refresh_all_lists(self):
"""Refresh all frame lists from controller"""
if not self.frame_controller:
return
# Update frame combo
frames = self.frame_controller.get_selectable_frames()
self.frame_combo.blockSignals(True)
self.frame_combo.clear()
self.frame_combo.addItems(frames)
self.frame_combo.setCurrentText(self.frame_controller.get_active_frame())
self.frame_combo.blockSignals(False)
# Update tool combo
tools = self.frame_controller.get_tools()
self.tool_combo.blockSignals(True)
self.tool_combo.clear()
self.tool_combo.addItems(tools)
self.tool_combo.setCurrentText(self.frame_controller.get_active_tool())
self.tool_combo.blockSignals(False)
# Update tables
self._update_workpiece_table()
self._update_tool_table()
def _update_workpiece_table(self):
"""Update workpiece frames table"""
if not self.frame_controller:
return
workpieces = self.frame_controller.get_workpieces()
self.workpiece_table.setRowCount(len(workpieces))
for row, name in enumerate(workpieces):
info = self.frame_controller.get_frame_info(name)
if info:
self.workpiece_table.setItem(row, 0, QTableWidgetItem(name))
pos = info['position']
self.workpiece_table.setItem(row, 1, QTableWidgetItem(f"{pos[0]:.1f}"))
self.workpiece_table.setItem(row, 2, QTableWidgetItem(f"{pos[1]:.1f}"))
self.workpiece_table.setItem(row, 3, QTableWidgetItem(f"{pos[2]:.1f}"))
def _update_tool_table(self):
"""Update tool frames table"""
if not self.frame_controller:
return
self.tool_table.blockSignals(True)
tools = self.frame_controller.get_tools()
self.tool_table.setRowCount(len(tools))
for row, name in enumerate(tools):
info = self.frame_controller.get_frame_info(name)
if info:
self.tool_table.setItem(row, 0, QTableWidgetItem(name))
pos = info['position']
self.tool_table.setItem(row, 1, QTableWidgetItem(f"{pos[2]:.1f}"))
self.tool_table.setItem(row, 2, QTableWidgetItem(info.get('description', '')))
# Delete button
if name != "default_tool":
btn = QPushButton("Delete")
btn.clicked.connect(lambda checked, n=name: self._delete_tool(n))
self.tool_table.setCellWidget(row, 3, btn)
self.tool_table.blockSignals(False)
# ── Callbacks from controller ──
def _on_frames_updated(self, frames: List[str]):
"""Callback when frames list changes"""
self.frame_combo.blockSignals(True)
current = self.frame_combo.currentText()
self.frame_combo.clear()
self.frame_combo.addItems(frames)
if current in frames:
self.frame_combo.setCurrentText(current)
self.frame_combo.blockSignals(False)
def _on_tools_updated(self, tools: List[str]):
"""Callback when tools list changes"""
self.tool_combo.blockSignals(True)
current = self.tool_combo.currentText()
self.tool_combo.clear()
self.tool_combo.addItems(tools)
if current in tools:
self.tool_combo.setCurrentText(current)
self.tool_combo.blockSignals(False)
self._update_tool_table()
def _on_workpieces_updated(self, workpieces: List[str]):
"""Callback when workpieces list changes"""
self._update_workpiece_table()
# Also refresh frame combo since workpieces are selectable
if self.frame_controller:
frames = self.frame_controller.get_selectable_frames()
self._on_frames_updated(frames)
def _on_teaching_progress(self, progress: TeachingProgress):
"""Callback for teaching progress updates"""
self._update_step_dots(progress.points_recorded)
self.teaching_message.setText(progress.message)
is_teaching = progress.is_teaching
self.btn_start_teaching.setEnabled(not is_teaching)
self.teaching_name_input.setEnabled(not is_teaching)
self.btn_record_point.setEnabled(is_teaching)
self.btn_cancel_teaching.setEnabled(
is_teaching or progress.state == TeachingState.ERROR
)
self.btn_finish_teaching.setEnabled(progress.state == TeachingState.COMPLETE)
# Style message by state
if progress.state == TeachingState.ERROR:
self.teaching_message.setStyleSheet(
"color: red; padding: 5px; font-weight: bold;"
)
elif progress.state == TeachingState.COMPLETE:
self.teaching_message.setStyleSheet(
"color: green; padding: 5px; font-weight: bold;"
)
else:
self.teaching_message.setStyleSheet("color: #666; padding: 5px;")
# Keep teaching UI visible during active teaching
if is_teaching or progress.state == TeachingState.COMPLETE:
self.teaching_container.setVisible(True)
# ── Selection handlers ──
def _on_frame_selected(self, frame_name: str):
"""Handle frame selection change"""
if self.frame_controller and frame_name:
self.frame_controller.select_frame(frame_name)
def _on_tool_selected(self, tool_name: str):
"""Handle tool selection change — select and populate edit fields."""
if self.frame_controller and tool_name:
self.frame_controller.select_tool(tool_name)
# Populate edit fields with selected tool's values
info = self.frame_controller.get_frame_info(tool_name)
if info:
self._updating_tool = True
self.tool_name_input.setText(tool_name)
self.tool_z_spin.setValue(info['position'][2])
self._updating_tool = False
# ── Teaching actions ──
def _start_teaching(self):
"""Start the teaching process"""
name = self.teaching_name_input.text().strip()
if not name:
QMessageBox.warning(self, "Error", "Please enter a frame name")
return
if self.frame_controller:
self.frame_controller.start_teaching_workpiece(name)
def _record_teaching_point(self):
"""Record current TCP position"""
if self.frame_controller:
self.frame_controller.record_teaching_point()
def _finish_teaching(self):
"""Finish teaching and create frame"""
if self.frame_controller:
frame = self.frame_controller.finish_teaching()
if frame:
QMessageBox.information(
self, "Success",
f"Created workpiece frame: {frame.name}"
)
self._hide_teaching_ui()
def _cancel_teaching(self):
"""Cancel teaching process"""
if self.frame_controller:
self.frame_controller.cancel_teaching()
self._hide_teaching_ui()
# ── Workpiece actions ──
def _delete_selected_workpiece(self):
"""Delete selected workpiece frame"""
row = self.workpiece_table.currentRow()
if row < 0:
QMessageBox.warning(self, "Error", "Please select a workpiece to delete")
return
name_item = self.workpiece_table.item(row, 0)
if name_item and self.frame_controller:
name = name_item.text()
reply = QMessageBox.question(
self, "Confirm Delete",
f"Delete workpiece frame '{name}'?",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
self.frame_controller.delete_frame(name)
# ── Tool actions ──
def _on_tool_table_edited(self, row: int, col: int):
"""Handle in-place edit of tool table cells (Z Offset column)."""
if col != 1: # Only handle Z Offset column
return
if not self.frame_controller:
return
if getattr(self, '_updating_tool', False):
return
name_item = self.tool_table.item(row, 0)
value_item = self.tool_table.item(row, 1)
if not name_item or not value_item:
return
name = name_item.text()
try:
z_offset = float(value_item.text())
except ValueError:
return
self._updating_tool = True
try:
self.frame_controller.delete_frame(name)
self.frame_controller.create_tool_frame(name, offset_z=z_offset)
self.frame_controller.select_tool(name)
finally:
self._updating_tool = False
def _on_tool_z_changed(self, value: float):
"""Live-update the active tool when Z offset spinbox changes."""
if not self.frame_controller:
return
if getattr(self, '_updating_tool', False):
return
name = self.tool_name_input.text().strip()
if not name or not self.frame_controller.frame_manager.frame_exists(name):
return
# Only live-update if this is the currently active tool
if name != self.frame_controller.get_active_tool():
return
self._updating_tool = True
try:
self.frame_controller.delete_frame(name)
self.frame_controller.create_tool_frame(name, offset_z=value)
self.frame_controller.select_tool(name)
finally:
self._updating_tool = False
def _add_tool(self):
"""Add or update a tool frame"""
name = self.tool_name_input.text().strip()
if not name:
QMessageBox.warning(self, "Error", "Please enter a tool name")
return
z_offset = self.tool_z_spin.value()
if self.frame_controller:
# If tool already exists, delete it first (update semantics)
if self.frame_controller.frame_manager.frame_exists(name):
self.frame_controller.delete_frame(name)
success = self.frame_controller.create_tool_frame(
name, offset_z=z_offset
)
if success:
# Auto-select the created/updated tool
self.frame_controller.select_tool(name)
self.tool_name_input.clear()
self.tool_z_spin.setValue(0)
else:
QMessageBox.warning(self, "Error", f"Failed to create tool '{name}'")
def _delete_tool(self, name: str):
"""Delete a tool frame"""
if self.frame_controller:
reply = QMessageBox.question(
self, "Confirm Delete",
f"Delete tool '{name}'?",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
self.frame_controller.delete_frame(name)
# ── Base frame ──
def _apply_base_frame(self):
"""Apply base frame settings"""
if not self.frame_controller:
return
self.frame_controller.update_base_frame(
x=self.base_x_spin.value(),
y=self.base_y_spin.value(),
z=self.base_z_spin.value(),
roll=self.base_roll_spin.value(),
pitch=self.base_pitch_spin.value(),
yaw=self.base_yaw_spin.value()
)
QMessageBox.information(self, "Success", "Base frame updated")
if __name__ == "__main__":
# Test the panel
import sys
from PyQt5.QtWidgets import QApplication
logging.basicConfig(level=logging.DEBUG)
app = QApplication(sys.argv)
# Create controller
from frame_controller import FrameController
controller = FrameController()
# Create panel
panel = FrameManagementPanel()
panel.set_controller(controller)
panel.setWindowTitle("Frame Management Panel Test")
panel.resize(600, 700)
panel.show()
sys.exit(app.exec_())