@@ -190,8 +190,6 @@ def __init__(self, ax, label, image=None,
190
190
self .color = color
191
191
self .hovercolor = hovercolor
192
192
193
- self ._lastcolor = color
194
-
195
193
def _click (self , event ):
196
194
if (self .ignore (event )
197
195
or event .inaxes != self .ax
@@ -726,76 +724,58 @@ def __init__(self, ax, label, initial='',
726
724
727
725
self .DIST_FROM_LEFT = .05
728
726
729
- self .text = initial
730
- self . label = ax . text ( - label_pad , 0.5 , label ,
731
- verticalalignment = 'center' ,
732
- horizontalalignment = 'right' ,
733
- transform = ax .transAxes )
734
- self . text_disp = self . _make_text_disp ( self . text )
727
+ self .label = ax . text (
728
+ - label_pad , 0.5 , label , transform = ax . transAxes ,
729
+ verticalalignment = 'center' , horizontalalignment = 'right' )
730
+ self . text_disp = self . ax . text (
731
+ self . DIST_FROM_LEFT , 0.5 , initial , transform = self . ax .transAxes ,
732
+ verticalalignment = 'center' , horizontalalignment = 'left' )
735
733
736
734
self .cnt = 0
737
735
self .change_observers = {}
738
736
self .submit_observers = {}
739
737
740
- # If these lines are removed, the cursor won't appear the first
741
- # time the box is clicked:
742
- self . ax . set_xlim ( 0 , 1 )
743
- self . ax . set_ylim ( 0 , 1 )
738
+ ax . set (
739
+ xlim = ( 0 , 1 ), ylim = ( 0 , 1 ), # s.t. cursor appears from first click.
740
+ navigate = False , facecolor = color ,
741
+ xticks = [], yticks = [] )
744
742
745
743
self .cursor_index = 0
746
744
747
- # Because this is initialized, _render_cursor
748
- # can assume that cursor exists.
749
- self .cursor = self .ax .vlines (0 , 0 , 0 )
750
- self .cursor .set_visible (False )
745
+ self .cursor = ax .vlines (0 , 0 , 0 , visible = False ,
746
+ transform = mpl .transforms .IdentityTransform ())
751
747
752
748
self .connect_event ('button_press_event' , self ._click )
753
749
self .connect_event ('button_release_event' , self ._release )
754
750
self .connect_event ('motion_notify_event' , self ._motion )
755
751
self .connect_event ('key_press_event' , self ._keypress )
756
752
self .connect_event ('resize_event' , self ._resize )
757
- ax .set_navigate (False )
758
- ax .set_facecolor (color )
759
- ax .set_xticks ([])
760
- ax .set_yticks ([])
753
+
761
754
self .color = color
762
755
self .hovercolor = hovercolor
763
756
764
- self ._lastcolor = color
765
-
766
757
self .capturekeystrokes = False
767
758
768
- def _make_text_disp (self , string ):
769
- return self .ax .text (self .DIST_FROM_LEFT , 0.5 , string ,
770
- verticalalignment = 'center' ,
771
- horizontalalignment = 'left' ,
772
- transform = self .ax .transAxes )
759
+ @property
760
+ def text (self ):
761
+ return self .text_disp .get_text ()
773
762
774
763
def _rendercursor (self ):
775
764
# this is a hack to figure out where the cursor should go.
776
765
# we draw the text up to where the cursor should go, measure
777
766
# and save its dimensions, draw the real text, then put the cursor
778
767
# at the saved dimensions
779
768
780
- widthtext = self .text [:self .cursor_index ]
781
- no_text = False
782
- if widthtext in ["" , " " , " " ]:
783
- no_text = widthtext == ""
784
- widthtext = ","
785
-
786
- wt_disp = self ._make_text_disp (widthtext )
787
-
788
- self .ax .figure .canvas .draw ()
789
- bb = wt_disp .get_window_extent ()
790
- inv = self .ax .transData .inverted ()
791
- bb = inv .transform (bb )
792
- wt_disp .set_visible (False )
793
- if no_text :
794
- bb [1 , 0 ] = bb [0 , 0 ]
795
- # hack done
796
- self .cursor .set_visible (False )
769
+ text = self .text_disp .get_text () # Save value before overwriting it.
770
+ widthtext = text [:self .cursor_index ]
771
+ self .text_disp .set_text (widthtext or "," )
772
+ bb = self .text_disp .get_window_extent ()
773
+ if not widthtext : # Use the comma for the height, but keep width to 0.
774
+ bb .x1 = bb .x0
775
+ self .cursor .set (
776
+ segments = [[(bb .x1 , bb .y0 ), (bb .x1 , bb .y1 )]], visible = True )
777
+ self .text_disp .set_text (text )
797
778
798
- self .cursor = self .ax .vlines (bb [1 , 0 ], bb [0 , 1 ], bb [1 , 1 ])
799
779
self .ax .figure .canvas .draw ()
800
780
801
781
def _notify_submit_observers (self ):
@@ -815,33 +795,31 @@ def _keypress(self, event):
815
795
return
816
796
if self .capturekeystrokes :
817
797
key = event .key
818
-
798
+ text = self . text
819
799
if len (key ) == 1 :
820
- self . text = (self . text [:self .cursor_index ] + key +
821
- self . text [self .cursor_index :])
800
+ text = (text [:self .cursor_index ] + key +
801
+ text [self .cursor_index :])
822
802
self .cursor_index += 1
823
803
elif key == "right" :
824
- if self .cursor_index != len (self . text ):
804
+ if self .cursor_index != len (text ):
825
805
self .cursor_index += 1
826
806
elif key == "left" :
827
807
if self .cursor_index != 0 :
828
808
self .cursor_index -= 1
829
809
elif key == "home" :
830
810
self .cursor_index = 0
831
811
elif key == "end" :
832
- self .cursor_index = len (self . text )
812
+ self .cursor_index = len (text )
833
813
elif key == "backspace" :
834
814
if self .cursor_index != 0 :
835
- self . text = (self . text [:self .cursor_index - 1 ] +
836
- self . text [self .cursor_index :])
815
+ text = (text [:self .cursor_index - 1 ] +
816
+ text [self .cursor_index :])
837
817
self .cursor_index -= 1
838
818
elif key == "delete" :
839
819
if self .cursor_index != len (self .text ):
840
- self .text = (self .text [:self .cursor_index ] +
841
- self .text [self .cursor_index + 1 :])
842
-
843
- self .text_disp .remove ()
844
- self .text_disp = self ._make_text_disp (self .text )
820
+ text = (text [:self .cursor_index ] +
821
+ text [self .cursor_index + 1 :])
822
+ self .text_disp .set_text (text )
845
823
self ._rendercursor ()
846
824
self ._notify_change_observers ()
847
825
if key == "enter" :
@@ -851,9 +829,7 @@ def set_val(self, val):
851
829
newval = str (val )
852
830
if self .text == newval :
853
831
return
854
- self .text = newval
855
- self .text_disp .remove ()
856
- self .text_disp = self ._make_text_disp (self .text )
832
+ self .text_disp .set_text (newval )
857
833
self ._rendercursor ()
858
834
self ._notify_change_observers ()
859
835
self ._notify_submit_observers ()
@@ -905,23 +881,8 @@ def position_cursor(self, x):
905
881
self .cursor_index = 0
906
882
else :
907
883
bb = self .text_disp .get_window_extent ()
908
-
909
- trans = self .ax .transData
910
- inv = self .ax .transData .inverted ()
911
- bb = trans .transform (inv .transform (bb ))
912
-
913
- text_start = bb [0 , 0 ]
914
- text_end = bb [1 , 0 ]
915
-
916
- ratio = (x - text_start ) / (text_end - text_start )
917
-
918
- if ratio < 0 :
919
- ratio = 0
920
- if ratio > 1 :
921
- ratio = 1
922
-
884
+ ratio = np .clip ((x - bb .x0 ) / bb .width , 0 , 1 )
923
885
self .cursor_index = int (len (self .text ) * ratio )
924
-
925
886
self ._rendercursor ()
926
887
927
888
def _click (self , event ):
@@ -944,13 +905,9 @@ def _resize(self, event):
944
905
def _motion (self , event ):
945
906
if self .ignore (event ):
946
907
return
947
- if event .inaxes == self .ax :
948
- c = self .hovercolor
949
- else :
950
- c = self .color
951
- if c != self ._lastcolor :
908
+ c = self .hovercolor if event .inaxes == self .ax else self .color
909
+ if c != self .ax .get_facecolor ():
952
910
self .ax .set_facecolor (c )
953
- self ._lastcolor = c
954
911
if self .drawon :
955
912
self .ax .figure .canvas .draw ()
956
913
0 commit comments