Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5773918

Browse files
author
Federico Ariza
committed
adding help tool
1 parent b884161 commit 5773918

File tree

9 files changed

+121
-2
lines changed

9 files changed

+121
-2
lines changed

lib/matplotlib/backend_tools.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from matplotlib import rcParams
1616
from matplotlib._pylab_helpers import Gcf
17+
from matplotlib.table import Table
18+
import textwrap
1719
import matplotlib.cbook as cbook
1820
from weakref import WeakKeyDictionary
1921
import six
@@ -1018,6 +1020,68 @@ def _mouse_move(self, event):
10181020
self.toolmanager.canvas.draw_idle()
10191021

10201022

1023+
class HelpTool(ToolToggleBase):
1024+
description = 'Print tool list, shortcuts and description'
1025+
default_keymap = rcParams['keymap.help']
1026+
image = 'help.png'
1027+
1028+
def __init__(self, *args):
1029+
ToolToggleBase.__init__(self, *args)
1030+
self.text_axes = None
1031+
1032+
def enable(self, *args):
1033+
self.text_axes = self.figure.add_axes((0, 0, 1, 1))
1034+
table = Table(self.text_axes, bbox=[0, 0, 1, 1])
1035+
table.edges = 'B'
1036+
self.text_axes.add_table(table)
1037+
chars_in_width = self._find_chars_in_width(table.FONTSIZE)
1038+
1039+
table.auto_set_font_size(False)
1040+
col_chars_width = int(chars_in_width / 4) - 2
1041+
content = self._get_content(col_chars_width, col_chars_width,
1042+
2 * col_chars_width)
1043+
for i, v in enumerate(content):
1044+
h = v[0]
1045+
table.add_cell(i, 0, text=v[1], width=1, height=h, loc='left',
1046+
fontproperties='monospace')
1047+
table.add_cell(i, 1, text=v[2], width=1, height=h, loc='left',
1048+
fontproperties='monospace')
1049+
table.add_cell(i, 2, text=v[3], width=2, height=h, loc='left',
1050+
fontproperties='monospace')
1051+
self.figure.canvas.draw_idle()
1052+
1053+
def _find_chars_in_width(self, fontsize):
1054+
"""Number of characters in figure width approx"""
1055+
# https://web.archive.org/web/20010717031241/plainlanguagenetwork.org/type/utbo211.htmhttps://web.archive.org/web/20010717031241/plainlanguagenetwork.org/type/utbo211.htm
1056+
# Approximately width = 60% of height for monospace fonts
1057+
charwidth = 0.6 * fontsize / 72.0 * self.figure.dpi
1058+
figwidth = self.figure.get_figwidth() * self.figure.dpi
1059+
return figwidth / charwidth
1060+
1061+
def disable(self, *args):
1062+
self.text_axes.remove()
1063+
self.figure.canvas.draw_idle()
1064+
1065+
def _get_content(self, w0, w1, w2):
1066+
rows = [(1, 'NAME', 'KEYS', 'DESCRIPTION')]
1067+
tools = self.toolmanager.tools
1068+
for name in sorted(tools):
1069+
if not tools[name].description:
1070+
continue
1071+
1072+
keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name)))
1073+
name_lines = textwrap.wrap(name, w0)
1074+
keys_lines = textwrap.wrap(keys, w1)
1075+
desc_lines = textwrap.wrap(tools[name].description, w2)
1076+
# Height of the row is the maximum number of lines
1077+
height = max(len(name_lines), len(keys_lines), len(desc_lines))
1078+
rows.append((height,
1079+
'\n'.join(name_lines),
1080+
'\n'.join(keys_lines),
1081+
'\n'.join(desc_lines)))
1082+
return rows
1083+
1084+
10211085
default_tools = {'home': ToolHome, 'back': ToolBack, 'forward': ToolForward,
10221086
'zoom': ToolZoom, 'pan': ToolPan,
10231087
'subplots': 'ToolConfigureSubplots',
@@ -1035,12 +1099,13 @@ def _mouse_move(self, event):
10351099
_views_positions: ToolViewsPositions,
10361100
'cursor': 'ToolSetCursor',
10371101
'rubberband': 'ToolRubberband',
1102+
'help': HelpTool
10381103
}
10391104
"""Default tools"""
10401105

10411106
default_toolbar_tools = [['navigation', ['home', 'back', 'forward']],
10421107
['zoompan', ['pan', 'zoom', 'subplots']],
1043-
['io', ['save']]]
1108+
['io', ['save', 'help']]]
10441109
"""Default tools in the toolbar"""
10451110

10461111

1.77 KB
Binary file not shown.
472 Bytes
Loading
1.7 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Loading
747 Bytes
Loading
6.76 KB
Binary file not shown.

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ def _validate_linestyle(ls):
13731373
'keymap.yscale': [['l'], validate_stringlist],
13741374
'keymap.xscale': [['k', 'L'], validate_stringlist],
13751375
'keymap.all_axes': [['a'], validate_stringlist],
1376+
'keymap.help': [['f1'], validate_stringlist],
13761377

13771378
# sample data
13781379
'examples.directory': ['', six.text_type],

tools/make_icons.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def make_matplotlib_icon():
9797
('move', 0xf047),
9898
('filesave', 0xf0c7),
9999
('subplots', 0xf1de),
100-
('qt4_editor_options', 0xf201)]
100+
('qt4_editor_options', 0xf201),
101+
('help', 0xf128)]
101102

102103

103104
def make_icons():

0 commit comments

Comments
 (0)