Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50eb67e

Browse filesBrowse files
authored
Merge pull request #17082 from anntzer/textbox
MNT: Cleanup TextBox implementation.
2 parents 47a15d9 + d6fadfc commit 50eb67e
Copy full SHA for 50eb67e

File tree

Expand file treeCollapse file tree

1 file changed

+39
-82
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+39
-82
lines changed

‎lib/matplotlib/widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/widgets.py
+39-82Lines changed: 39 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ def __init__(self, ax, label, image=None,
190190
self.color = color
191191
self.hovercolor = hovercolor
192192

193-
self._lastcolor = color
194-
195193
def _click(self, event):
196194
if (self.ignore(event)
197195
or event.inaxes != self.ax
@@ -726,76 +724,58 @@ def __init__(self, ax, label, initial='',
726724

727725
self.DIST_FROM_LEFT = .05
728726

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')
735733

736734
self.cnt = 0
737735
self.change_observers = {}
738736
self.submit_observers = {}
739737

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=[])
744742

745743
self.cursor_index = 0
746744

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())
751747

752748
self.connect_event('button_press_event', self._click)
753749
self.connect_event('button_release_event', self._release)
754750
self.connect_event('motion_notify_event', self._motion)
755751
self.connect_event('key_press_event', self._keypress)
756752
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+
761754
self.color = color
762755
self.hovercolor = hovercolor
763756

764-
self._lastcolor = color
765-
766757
self.capturekeystrokes = False
767758

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()
773762

774763
def _rendercursor(self):
775764
# this is a hack to figure out where the cursor should go.
776765
# we draw the text up to where the cursor should go, measure
777766
# and save its dimensions, draw the real text, then put the cursor
778767
# at the saved dimensions
779768

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)
797778

798-
self.cursor = self.ax.vlines(bb[1, 0], bb[0, 1], bb[1, 1])
799779
self.ax.figure.canvas.draw()
800780

801781
def _notify_submit_observers(self):
@@ -815,33 +795,31 @@ def _keypress(self, event):
815795
return
816796
if self.capturekeystrokes:
817797
key = event.key
818-
798+
text = self.text
819799
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:])
822802
self.cursor_index += 1
823803
elif key == "right":
824-
if self.cursor_index != len(self.text):
804+
if self.cursor_index != len(text):
825805
self.cursor_index += 1
826806
elif key == "left":
827807
if self.cursor_index != 0:
828808
self.cursor_index -= 1
829809
elif key == "home":
830810
self.cursor_index = 0
831811
elif key == "end":
832-
self.cursor_index = len(self.text)
812+
self.cursor_index = len(text)
833813
elif key == "backspace":
834814
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:])
837817
self.cursor_index -= 1
838818
elif key == "delete":
839819
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)
845823
self._rendercursor()
846824
self._notify_change_observers()
847825
if key == "enter":
@@ -851,9 +829,7 @@ def set_val(self, val):
851829
newval = str(val)
852830
if self.text == newval:
853831
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)
857833
self._rendercursor()
858834
self._notify_change_observers()
859835
self._notify_submit_observers()
@@ -905,23 +881,8 @@ def position_cursor(self, x):
905881
self.cursor_index = 0
906882
else:
907883
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)
923885
self.cursor_index = int(len(self.text) * ratio)
924-
925886
self._rendercursor()
926887

927888
def _click(self, event):
@@ -944,13 +905,9 @@ def _resize(self, event):
944905
def _motion(self, event):
945906
if self.ignore(event):
946907
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():
952910
self.ax.set_facecolor(c)
953-
self._lastcolor = c
954911
if self.drawon:
955912
self.ax.figure.canvas.draw()
956913

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.