2626import time
2727
2828class Change ():
29- def __init__ (self ):
29+ def __init__ (self , _name ):
3030 self .changes = []
31+ self .name = _name
3132
3233 def add_change (self , x , y , prev_char ):
3334 for change in self .changes :
@@ -313,6 +314,11 @@ def __init__(self, **kwargs):
313314
314315 self .undo_changes = []
315316
317+ self .text_x = 0
318+ self .text_y = 0
319+
320+ self .text_entry .get_buffer ().connect ("changed" , self .insert_text )
321+
316322 char = " "
317323 prev_button = Gtk .ToggleButton (label = char , css_classes = ["flat" ])
318324 prev_button .connect ("toggled" , self .change_char , self .free_char_list )
@@ -460,7 +466,9 @@ def on_click_released(self, click, x, y, arg):
460466 y_char = int (self .start_y / self .y_mul )
461467
462468 if self .tool == "TEXT" :
463- self .insert_text (self .text_entry , x_char , y_char )
469+ self .text_x = x_char
470+ self .text_y = y_char
471+ self .insert_text (None )
464472
465473 elif self .tool == "PICKER" :
466474 child = self .grid .get_child_at (x_char , y_char )
@@ -550,6 +558,9 @@ def on_choose_text(self, btn):
550558 self .scrolled .set_child (None )
551559 box = Gtk .Box (orientation = 1 , name = "TEXT" )
552560 box .append (self .text_entry )
561+ write_button = Gtk .Button (label = "Enter" , margin_start = 12 , margin_end = 12 , margin_bottom = 12 )
562+ write_button .connect ("clicked" , self .insert_text , self .grid )
563+ box .append (write_button )
553564 self .scrolled .set_child (box )
554565
555566 def on_choose_free (self , btn ):
@@ -661,7 +672,6 @@ def clear_list_of_char(self, chars_list_start, chars_list_end):
661672 if not child :
662673 continue
663674 child .set_label ("" )
664- # print("finished")
665675
666676 def on_drag_begin (self , gesture , start_x , start_y ):
667677 self .start_x = start_x
@@ -674,12 +684,12 @@ def on_drag_begin(self, gesture, start_x, start_y):
674684 start_y_char = self .start_y // self .y_mul
675685
676686 if self .tool == "FREE-LINE" :
677- self .add_undo_action ()
687+ self .add_undo_action ("Free Hand Line" )
678688 self .prev_char_pos = [start_x_char , start_y_char ]
679689 elif self .tool == "FREE" :
680- self .add_undo_action ()
690+ self .add_undo_action ("Free Hand" )
681691 elif self .tool == "ERASER" :
682- self .add_undo_action ()
692+ self .add_undo_action ("Eraser" )
683693
684694 def on_drag_follow (self , gesture , end_x , end_y ):
685695 if self .flip :
@@ -714,7 +724,7 @@ def on_drag_follow(self, gesture, end_x, end_y):
714724 height += 1
715725 self .draw_rectangle (start_x_char , start_y_char , width , height , self .preview_grid )
716726 elif self .tool == "FILLED-RECTANGLE" :
717- if abs (self .prev_x ) > abs (width ) or abs (self .prev_y ) > abs (height ):
727+ if abs (self .prev_x ) > abs (width ) or abs (self .prev_y ) > abs (height ) or math . copysign ( 1 , self . prev_x ) != math . copysign ( 1 , width ) or math . copysign ( 1 , self . prev_y ) != math . copysign ( 1 , height ) :
718728 self .clear (None , self .preview_grid )
719729 self .prev_x = width
720730 self .prev_y = height
@@ -749,7 +759,8 @@ def on_drag_follow(self, gesture, end_x, end_y):
749759 self .drawing_area .queue_draw ()
750760
751761 def on_drag_end (self , gesture , delta_x , delta_y ):
752- self .force_clear (self .preview_grid )
762+ if self .tool != "TEXT" :
763+ self .force_clear (self .preview_grid )
753764 if self .flip :
754765 delta_x = - delta_x
755766 start_x_char = self .start_x // self .x_mul
@@ -760,10 +771,8 @@ def on_drag_end(self, gesture, delta_x, delta_y):
760771 self .prev_x = 0
761772 self .prev_y = 0
762773
763- self .clear (None , self .preview_grid )
764-
765774 if self .tool == "RECTANGLE" :
766- self .add_undo_action ()
775+ self .add_undo_action ("Rectangle" )
767776 if width < 0 :
768777 width = - width
769778 start_x_char -= width
@@ -774,7 +783,7 @@ def on_drag_end(self, gesture, delta_x, delta_y):
774783 height += 1
775784 self .draw_rectangle (start_x_char , start_y_char , width , height , self .grid )
776785 elif self .tool == "FILLED-RECTANGLE" :
777- self .add_undo_action ()
786+ self .add_undo_action ("Filled Rectangle" )
778787 if width < 0 :
779788 width = - width
780789 start_x_char -= width
@@ -785,7 +794,7 @@ def on_drag_end(self, gesture, delta_x, delta_y):
785794 height += 1
786795 self .draw_filled_rectangle (start_x_char , start_y_char , width , height , self .grid )
787796 elif self .tool == "LINE" or self .tool == "ARROW" :
788- self .add_undo_action ()
797+ self .add_undo_action (self . tool . capitalize () )
789798 if width < 0 :
790799 width -= 1
791800 else :
@@ -801,9 +810,10 @@ def on_drag_end(self, gesture, delta_x, delta_y):
801810 self .prev_char_pos = []
802811 self .prev_pos = []
803812
804- def add_undo_action (self ):
805- self .undo_changes .insert (0 , Change ())
813+ def add_undo_action (self , name ):
814+ self .undo_changes .insert (0 , Change (name ))
806815 self .undo_button .set_sensitive (True )
816+ self .undo_button .set_tooltip_text ("Undo " + self .undo_changes [0 ].name )
807817
808818 def drawing_area_draw (self , area , cr , width , height , data ):
809819 cr .save ()
@@ -872,28 +882,32 @@ def draw_free_line(self, new_x, new_y, grid):
872882 self .prev_char_pos = [self .prev_pos [0 ], self .prev_pos [1 ]]
873883 self .prev_pos = [new_x , new_y ]
874884
875- def insert_text (self , entry , x_coord , y_coord ):
876- x = x_coord
877- y = y_coord
878- buffer = entry .get_buffer ()
885+ def insert_text (self , widget = None , grid = None ):
886+ self .clear (None , self .preview_grid )
887+ if grid == None :
888+ grid = self .preview_grid
889+ x = self .text_x
890+ y = self .text_y
891+ buffer = self .text_entry .get_buffer ()
879892 start = buffer .get_start_iter ()
880893 end = buffer .get_end_iter ()
881894 text = buffer .get_text (start , end , False )
882- if text != "" :
883- self .add_undo_action ()
895+ if text != "" and grid == self . grid :
896+ self .add_undo_action (self . tool . capitalize () )
884897 for char in text :
885- child = self . grid .get_child_at (x , y )
898+ child = grid .get_child_at (x , y )
886899 if ord (char ) < 32 :
887900 if ord (char ) == 10 :
888901 y += 1
889- x = x_coord
902+ x = self . text_x
890903 continue
891904 continue
892905 if not child :
893906 continue
894- # print(f"{char} is {ord(char)} in {x},{y}")
895- self .undo_changes [0 ].add_change (x , y , child .get_label ())
907+ if grid == self . grid :
908+ self .undo_changes [0 ].add_change (x , y , child .get_label ())
896909 child .set_label (char )
910+ self .changed_chars .append ([x , y ])
897911 if self .flip :
898912 x -= 1
899913 else :
@@ -1114,6 +1128,8 @@ def undo_first_change(self, btn=None):
11141128 self .undo_changes .pop (0 )
11151129 if len (self .undo_changes ) == 0 :
11161130 self .undo_button .set_sensitive (False )
1131+ else :
1132+ self .undo_button .set_tooltip_text ("Undo " + self .undo_changes [0 ].name )
11171133
11181134 def top_horizontal (self ):
11191135 return self .styles [self .style - 1 ][0 ]
0 commit comments