@@ -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