@@ -181,7 +181,7 @@ def __init__(self, ax, label, image=None,
181
181
transform = ax .transAxes )
182
182
183
183
self ._cnt = 0
184
- self ._observers = {}
184
+ self ._observers = cbook . CallbackRegistry ()
185
185
186
186
self .connect_event ('button_press_event' , self ._click )
187
187
self .connect_event ('button_release_event' , self ._release )
@@ -196,12 +196,13 @@ def __init__(self, ax, label, image=None,
196
196
@cbook .deprecated ("3.4" )
197
197
@property
198
198
def cnt (self ):
199
- return self ._cnt
199
+ # Not real, but close enough.
200
+ return len (self ._observers .callbacks ['clicked' ])
200
201
201
202
@cbook .deprecated ("3.4" )
202
203
@property
203
204
def observers (self ):
204
- return self ._observers
205
+ return self ._observers . callbacks [ 'clicked' ]
205
206
206
207
def _click (self , event ):
207
208
if (self .ignore (event )
@@ -219,8 +220,7 @@ def _release(self, event):
219
220
if (not self .eventson
220
221
or event .inaxes != self .ax ):
221
222
return
222
- for cid , func in self ._observers .items ():
223
- func (event )
223
+ self ._observers .process ('clicked' , event )
224
224
225
225
def _motion (self , event ):
226
226
if self .ignore (event ):
@@ -237,17 +237,11 @@ def on_clicked(self, func):
237
237
238
238
Returns a connection id, which can be used to disconnect the callback.
239
239
"""
240
- cid = self ._cnt
241
- self ._observers [cid ] = func
242
- self ._cnt += 1
243
- return cid
240
+ return self ._observers .connect ('clicked' , func )
244
241
245
242
def disconnect (self , cid ):
246
243
"""Remove the callback function with connection id *cid*."""
247
- try :
248
- del self ._observers [cid ]
249
- except KeyError :
250
- pass
244
+ self ._observers .disconnect (cid )
251
245
252
246
253
247
class Slider (AxesWidget ):
@@ -397,20 +391,20 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
397
391
verticalalignment = 'center' ,
398
392
horizontalalignment = 'left' )
399
393
400
- self ._cnt = 0
401
- self ._observers = {}
394
+ self ._observers = cbook .CallbackRegistry ()
402
395
403
396
self .set_val (valinit )
404
397
405
398
@cbook .deprecated ("3.4" )
406
399
@property
407
400
def cnt (self ):
408
- return self ._cnt
401
+ # Not real, but close enough.
402
+ return len (self ._observers .callbacks ['changed' ])
409
403
410
404
@cbook .deprecated ("3.4" )
411
405
@property
412
406
def observers (self ):
413
- return self ._observers
407
+ return self ._observers . callbacks [ 'changed' ]
414
408
415
409
def _value_in_bounds (self , val ):
416
410
"""Makes sure *val* is with given bounds."""
@@ -494,8 +488,7 @@ def set_val(self, val):
494
488
self .val = val
495
489
if not self .eventson :
496
490
return
497
- for cid , func in self ._observers .items ():
498
- func (val )
491
+ self ._observers .process ('changed' , val )
499
492
500
493
def on_changed (self , func ):
501
494
"""
@@ -513,10 +506,7 @@ def on_changed(self, func):
513
506
int
514
507
Connection id (which can be used to disconnect *func*)
515
508
"""
516
- cid = self ._cnt
517
- self ._observers [cid ] = func
518
- self ._cnt += 1
519
- return cid
509
+ return self ._observers .connect ('changed' , func )
520
510
521
511
def disconnect (self , cid ):
522
512
"""
@@ -527,10 +517,7 @@ def disconnect(self, cid):
527
517
cid : int
528
518
Connection id of the observer to be removed
529
519
"""
530
- try :
531
- del self ._observers [cid ]
532
- except KeyError :
533
- pass
520
+ self ._observers .disconnect (cid )
534
521
535
522
def reset (self ):
536
523
"""Reset the slider to the initial value"""
@@ -625,18 +612,18 @@ def __init__(self, ax, labels, actives=None):
625
612
626
613
self .connect_event ('button_press_event' , self ._clicked )
627
614
628
- self ._cnt = 0
629
- self ._observers = {}
615
+ self ._observers = cbook .CallbackRegistry ()
630
616
631
617
@cbook .deprecated ("3.4" )
632
618
@property
633
619
def cnt (self ):
634
- return self ._cnt
620
+ # Not real, but close enough.
621
+ return len (self ._observers .callbacks ['clicked' ])
635
622
636
623
@cbook .deprecated ("3.4" )
637
624
@property
638
625
def observers (self ):
639
- return self ._observers
626
+ return self ._observers . callbacks [ 'clicked' ]
640
627
641
628
def _clicked (self , event ):
642
629
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
@@ -675,8 +662,7 @@ def set_active(self, index):
675
662
676
663
if not self .eventson :
677
664
return
678
- for cid , func in self ._observers .items ():
679
- func (self .labels [index ].get_text ())
665
+ self ._observers .process ('clicked' , self .labels [index ].get_text ())
680
666
681
667
def get_status (self ):
682
668
"""
@@ -690,17 +676,11 @@ def on_clicked(self, func):
690
676
691
677
Returns a connection id, which can be used to disconnect the callback.
692
678
"""
693
- cid = self ._cnt
694
- self ._observers [cid ] = func
695
- self ._cnt += 1
696
- return cid
679
+ self ._observers .connect ('clicked' , func )
697
680
698
681
def disconnect (self , cid ):
699
682
"""Remove the observer with connection id *cid*."""
700
- try :
701
- del self ._observers [cid ]
702
- except KeyError :
703
- pass
683
+ self ._observers .disconnect (cid )
704
684
705
685
706
686
class TextBox (AxesWidget ):
@@ -760,9 +740,7 @@ def __init__(self, ax, label, initial='',
760
740
self .DIST_FROM_LEFT , 0.5 , initial , transform = self .ax .transAxes ,
761
741
verticalalignment = 'center' , horizontalalignment = 'left' )
762
742
763
- self ._cnt = 0
764
- self ._change_observers = {}
765
- self ._submit_observers = {}
743
+ self ._observers = cbook .CallbackRegistry ()
766
744
767
745
ax .set (
768
746
xlim = (0 , 1 ), ylim = (0 , 1 ), # s.t. cursor appears from first click.
@@ -788,17 +766,18 @@ def __init__(self, ax, label, initial='',
788
766
@cbook .deprecated ("3.4" )
789
767
@property
790
768
def cnt (self ):
791
- return self ._cnt
769
+ # Not real, but close enough.
770
+ return sum (len (d ) for d in self ._observers .callbacks .values ())
792
771
793
772
@cbook .deprecated ("3.4" )
794
773
@property
795
774
def change_observers (self ):
796
- return self ._change_observers
775
+ return self ._observers . callbacks [ 'change' ]
797
776
798
777
@cbook .deprecated ("3.4" )
799
778
@property
800
779
def submit_observers (self ):
801
- return self ._submit_observers
780
+ return self ._observers . callbacks [ 'submit' ]
802
781
803
782
@property
804
783
def text (self ):
@@ -828,11 +807,6 @@ def _rendercursor(self):
828
807
829
808
self .ax .figure .canvas .draw ()
830
809
831
- def _notify_submit_observers (self ):
832
- if self .eventson :
833
- for cid , func in self ._submit_observers .items ():
834
- func (self .text )
835
-
836
810
def _release (self , event ):
837
811
if self .ignore (event ):
838
812
return
@@ -871,23 +845,20 @@ def _keypress(self, event):
871
845
text [self .cursor_index + 1 :])
872
846
self .text_disp .set_text (text )
873
847
self ._rendercursor ()
874
- self ._notify_change_observers ()
875
- if key == "enter" :
876
- self ._notify_submit_observers ()
848
+ if self .eventson :
849
+ self ._observers .process ('change' , self .text )
850
+ if key == "enter" :
851
+ self ._observers .process ('submit' , self .text )
877
852
878
853
def set_val (self , val ):
879
854
newval = str (val )
880
855
if self .text == newval :
881
856
return
882
857
self .text_disp .set_text (newval )
883
858
self ._rendercursor ()
884
- self ._notify_change_observers ()
885
- self ._notify_submit_observers ()
886
-
887
- def _notify_change_observers (self ):
888
859
if self .eventson :
889
- for cid , func in self ._change_observers . items ():
890
- func ( self .text )
860
+ self ._observers . process ( 'change' , self . text )
861
+ self . _observers . process ( 'submit' , self .text )
891
862
892
863
def begin_typing (self , x ):
893
864
self .capturekeystrokes = True
@@ -920,10 +891,10 @@ def stop_typing(self):
920
891
self .capturekeystrokes = False
921
892
self .cursor .set_visible (False )
922
893
self .ax .figure .canvas .draw ()
923
- if notifysubmit :
924
- # Because _notify_submit_observers might throw an error in the
925
- # user's code, only call it once we've already done our cleanup.
926
- self ._notify_submit_observers ( )
894
+ if notifysubmit and self . eventson :
895
+ # Because process() might throw an error in the user's code, only
896
+ # call it once we've already done our cleanup.
897
+ self ._observers . process ( 'submit' , self . text )
927
898
928
899
def position_cursor (self , x ):
929
900
# now, we have to figure out where the cursor goes.
@@ -968,10 +939,7 @@ def on_text_change(self, func):
968
939
969
940
A connection id is returned which can be used to disconnect.
970
941
"""
971
- cid = self ._cnt
972
- self ._change_observers [cid ] = func
973
- self ._cnt += 1
974
- return cid
942
+ self ._observers .connect ('change' , func )
975
943
976
944
def on_submit (self , func ):
977
945
"""
@@ -980,18 +948,11 @@ def on_submit(self, func):
980
948
981
949
A connection id is returned which can be used to disconnect.
982
950
"""
983
- cid = self ._cnt
984
- self ._submit_observers [cid ] = func
985
- self ._cnt += 1
986
- return cid
951
+ self ._observers .process ('submit' , func )
987
952
988
953
def disconnect (self , cid ):
989
954
"""Remove the observer with connection id *cid*."""
990
- for reg in [self ._change_observers , self ._submit_observers ]:
991
- try :
992
- del reg [cid ]
993
- except KeyError :
994
- pass
955
+ self ._observers .disconnect (cid )
995
956
996
957
997
958
class RadioButtons (AxesWidget ):
@@ -1072,18 +1033,18 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1072
1033
1073
1034
self .connect_event ('button_press_event' , self ._clicked )
1074
1035
1075
- self ._cnt = 0
1076
- self ._observers = {}
1036
+ self ._observers = cbook .CallbackRegistry ()
1077
1037
1078
1038
@cbook .deprecated ("3.4" )
1079
1039
@property
1080
1040
def cnt (self ):
1081
- return self ._cnt
1041
+ # Not real, but close enough.
1042
+ return len (self ._observers .callbacks ['clicked' ])
1082
1043
1083
1044
@cbook .deprecated ("3.4" )
1084
1045
@property
1085
1046
def observers (self ):
1086
- return self ._observers
1047
+ return self ._observers . callbacks [ 'clicked' ]
1087
1048
1088
1049
def _clicked (self , event ):
1089
1050
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
@@ -1121,26 +1082,19 @@ def set_active(self, index):
1121
1082
1122
1083
if not self .eventson :
1123
1084
return
1124
- for cid , func in self ._observers .items ():
1125
- func (self .labels [index ].get_text ())
1085
+ self ._observers .process ('clicked' , self .labels [index ].get_text ())
1126
1086
1127
1087
def on_clicked (self , func ):
1128
1088
"""
1129
1089
Connect the callback function *func* to button click events.
1130
1090
1131
1091
Returns a connection id, which can be used to disconnect the callback.
1132
1092
"""
1133
- cid = self ._cnt
1134
- self ._observers [cid ] = func
1135
- self ._cnt += 1
1136
- return cid
1093
+ self ._observers .connect ('clicked' , func )
1137
1094
1138
1095
def disconnect (self , cid ):
1139
1096
"""Remove the observer with connection id *cid*."""
1140
- try :
1141
- del self ._observers [cid ]
1142
- except KeyError :
1143
- pass
1097
+ self ._observers .disconnect (cid )
1144
1098
1145
1099
1146
1100
class SubplotTool (Widget ):
0 commit comments