Skip to content

Commit 2bcf69a

Browse files
committed
gmoccapy: add comment/uncomment button for G-code edit + keybinding
1 parent d6a1f3e commit 2bcf69a

6 files changed

Lines changed: 86 additions & 4 deletions

File tree

1.09 KB
Loading
1.48 KB
Loading
1.09 KB
Loading
1.09 KB
Loading

src/emc/usr_intf/gmoccapy/gmoccapy.glade

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@
242242
<property name="icon-name">edit-undo</property>
243243
<property name="icon_size">5</property>
244244
</object>
245+
<object class="GtkImage" id="img_edit_comment">
246+
<property name="visible">True</property>
247+
<property name="can-focus">False</property>
248+
<property name="tooltip-text" translatable="yes">Toggle comment/
249+
uncomment selection</property>
250+
</object>
245251
<object class="GtkImage" id="img_edit_menu_close">
246252
<property name="visible">True</property>
247253
<property name="can-focus">False</property>
@@ -2041,10 +2047,23 @@
20412047
</packing>
20422048
</child>
20432049
<child>
2044-
<placeholder/>
2045-
</child>
2046-
<child>
2047-
<placeholder/>
2050+
<object class="GtkButton" id="btn_comment">
2051+
<property name="use-action-appearance">False</property>
2052+
<property name="name">gcode_edit</property>
2053+
<property name="width-request">48</property>
2054+
<property name="height-request">48</property>
2055+
<property name="visible">True</property>
2056+
<property name="can-focus">True</property>
2057+
<property name="receives-default">False</property>
2058+
<property name="valign">center</property>
2059+
<property name="image">img_edit_comment</property>
2060+
<signal name="clicked" handler="on_btn_toggle_comment_clicked" swapped="no"/>
2061+
</object>
2062+
<packing>
2063+
<property name="left-attach">6</property>
2064+
<property name="top-attach">0</property>
2065+
<property name="height">2</property>
2066+
</packing>
20482067
</child>
20492068
</object>
20502069
<packing>

src/emc/usr_intf/gmoccapy/gmoccapy.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,7 @@ def _init_keybindings(self):
24682468
pass
24692469
self.widgets.window1.connect("key_press_event", self.on_key_event, 1)
24702470
self.widgets.window1.connect("key_release_event", self.on_key_event, 0)
2471+
self.widgets.gcode_view.connect("key-press-event", self.on_key_event_gcode_edit)
24712472

24722473
# Initialize the file to load dialog, setting an title and the correct
24732474
# folder as well as a file filter
@@ -3557,6 +3558,64 @@ def on_key_event(self, widget, event, signal):
35573558
LOG.info("Key {0} ({1:d}) was pressed {2} {3}".format(keyname, event.keyval, signal, self.last_key_event))
35583559
self.last_key_event = keyname, signal
35593560
return True
3561+
3562+
def on_key_event_gcode_edit(self, widget, event):
3563+
# Check if Ctrl+Shift+/ is pressed
3564+
if (event.state & Gdk.ModifierType.CONTROL_MASK) and (event.state & Gdk.ModifierType.SHIFT_MASK) and \
3565+
(event.keyval == Gdk.KEY_slash):
3566+
self.toggle_comment_from_selection()
3567+
return True # Stop further handling of the event
3568+
return False
3569+
3570+
def toggle_comment_from_selection(self):
3571+
if self.widgets.gcode_view.get_editable():
3572+
buffer = self.widgets.gcode_view.get_buffer()
3573+
# Get the selection bounds
3574+
if buffer.get_has_selection():
3575+
start_iter, end_iter = buffer.get_selection_bounds()
3576+
# no selection, just cursor placed in line
3577+
else:
3578+
cursor_mark = buffer.get_insert() # Get the current cursor mark
3579+
cursor_iter = buffer.get_iter_at_mark(cursor_mark) # Get the iterator at the cursor mark
3580+
line_number = cursor_iter.get_line() # Get the current line number
3581+
start_iter = buffer.get_iter_at_line(line_number) # Start of the line
3582+
end_iter = buffer.get_iter_at_line(line_number + 1) # End of the line
3583+
3584+
# Get the current line text
3585+
line_text = buffer.get_text(start_iter, end_iter, True).strip()
3586+
3587+
# Check if the last character of the selection is a newline
3588+
if buffer.get_text(start_iter, end_iter, True).endswith('\n'):
3589+
end_iter = buffer.get_iter_at_offset(end_iter.get_offset() - 1)
3590+
3591+
# Save the selection bounds
3592+
start_line = start_iter.get_line()
3593+
end_line = end_iter.get_line()
3594+
3595+
# Iterate through each line in the selection
3596+
for line_number in range(start_line, end_line + 1):
3597+
line_start_iter = buffer.get_iter_at_line(line_number)
3598+
line_end_iter = buffer.get_iter_at_line(line_number + 1)
3599+
3600+
# Get the current line text
3601+
line_text = buffer.get_text(line_start_iter, line_end_iter, True).strip()
3602+
3603+
# Check if the line already has parentheses
3604+
if line_text.startswith('(') and line_text.endswith(')'):
3605+
# Remove the parentheses
3606+
modified_line = line_text[1:-1].strip()
3607+
else:
3608+
# Add parentheses around the line
3609+
modified_line = f"({line_text})"
3610+
3611+
# Replace the original line with the modified line
3612+
buffer.delete(line_start_iter, line_end_iter)
3613+
buffer.insert(line_start_iter, modified_line + "\n") # Add a newline
3614+
3615+
# Restore the selection
3616+
new_start_iter = buffer.get_iter_at_line(start_line)
3617+
new_end_iter = buffer.get_iter_at_line(end_line + 1)
3618+
buffer.select_range(new_start_iter, new_end_iter)
35603619

35613620
# Notification stuff.
35623621
def _init_notification(self):
@@ -4940,6 +4999,7 @@ def _set_icon_theme(self, name):
49404999
("img_edit_menu_keyboard", "keyboard", 32),
49415000
("img_edit_menu_keyboard_hide", "keyboard_hide", 32),
49425001
("img_edit_menu_close", "back_to_app", 48),
5002+
("img_edit_comment", "comment", 32),
49435003
# macro menu
49445004
("img_macro_menu_calculator", "calculator_open", 32),
49455005
("img_macro_menu_keyboard", "keyboard", 32),
@@ -5647,6 +5707,9 @@ def on_btn_replace_clicked(self, widget, data=None):
56475707
# redo changes while in edit mode
56485708
def on_btn_redo_clicked(self, widget, data=None):
56495709
self.widgets.gcode_view.redo()
5710+
5711+
def on_btn_toggle_comment_clicked(self, widget, data=None):
5712+
self.toggle_comment_from_selection()
56505713

56515714
# if we leave the edit mode, we will have to show all widgets again
56525715
def on_ntb_button_switch_page(self, *args):

0 commit comments

Comments
 (0)