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

Skip to content

Commit 37d29df

Browse files
committed
Help tool.
1 parent 5daabdd commit 37d29df

13 files changed

+129
-10
lines changed

lib/matplotlib/backend_tools.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
`matplotlib.backend_managers.ToolManager`
1212
"""
1313

14+
import re
1415
import time
1516
import warnings
1617
from weakref import WeakKeyDictionary
@@ -403,7 +404,7 @@ def trigger(self, sender, event, data=None):
403404
class ToolEnableAllNavigation(ToolBase):
404405
"""Tool to enable all axes for toolmanager interaction"""
405406

406-
description = 'Enables all axes toolmanager'
407+
description = 'Enable all axes toolmanager'
407408
default_keymap = rcParams['keymap.all_axes']
408409

409410
def trigger(self, sender, event, data=None):
@@ -419,7 +420,7 @@ def trigger(self, sender, event, data=None):
419420
class ToolEnableNavigation(ToolBase):
420421
"""Tool to enable a specific axes for toolmanager interaction"""
421422

422-
description = 'Enables one axes toolmanager'
423+
description = 'Enable one axes toolmanager'
423424
default_keymap = (1, 2, 3, 4, 5, 6, 7, 8, 9)
424425

425426
def trigger(self, sender, event, data=None):
@@ -470,7 +471,7 @@ def _get_uniform_grid_state(ticks):
470471
class ToolGrid(_ToolGridBase):
471472
"""Tool to toggle the major grids of the figure"""
472473

473-
description = 'Toogle major grids'
474+
description = 'Toggle major grids'
474475
default_keymap = rcParams['keymap.grid']
475476

476477
def _get_next_grid_states(self, ax):
@@ -491,7 +492,7 @@ def _get_next_grid_states(self, ax):
491492
class ToolMinorGrid(_ToolGridBase):
492493
"""Tool to toggle the major and minor grids of the figure"""
493494

494-
description = 'Toogle major and minor grids'
495+
description = 'Toggle major and minor grids'
495496
default_keymap = rcParams['keymap.grid_minor']
496497

497498
def _get_next_grid_states(self, ax):
@@ -511,7 +512,7 @@ def _get_next_grid_states(self, ax):
511512
class ToolFullScreen(ToolToggleBase):
512513
"""Tool to toggle full screen"""
513514

514-
description = 'Toogle Fullscreen mode'
515+
description = 'Toggle fullscreen mode'
515516
default_keymap = rcParams['keymap.fullscreen']
516517

517518
def enable(self, event):
@@ -541,7 +542,7 @@ def disable(self, event):
541542
class ToolYScale(AxisScaleBase):
542543
"""Tool to toggle between linear and logarithmic scales on the Y axis"""
543544

544-
description = 'Toogle Scale Y axis'
545+
description = 'Toggle scale Y axis'
545546
default_keymap = rcParams['keymap.yscale']
546547

547548
def set_scale(self, ax, scale):
@@ -551,7 +552,7 @@ def set_scale(self, ax, scale):
551552
class ToolXScale(AxisScaleBase):
552553
"""Tool to toggle between linear and logarithmic scales on the X axis"""
553554

554-
description = 'Toogle Scale X axis'
555+
description = 'Toggle scale X axis'
555556
default_keymap = rcParams['keymap.xscale']
556557

557558
def set_scale(self, ax, scale):
@@ -1020,6 +1021,42 @@ def _mouse_move(self, event):
10201021
self.toolmanager.canvas.draw_idle()
10211022

10221023

1024+
class ToolHelpBase(ToolBase):
1025+
description = 'Print tool list, shortcuts and description'
1026+
default_keymap = rcParams['keymap.help']
1027+
image = 'help.png'
1028+
1029+
def _format_tool_keymap(self, name):
1030+
keymaps = self.toolmanager.get_tool_keymap(name)
1031+
# Capitalize "ctrl+a" -> "Ctrl+A" but leave "a" as is.
1032+
return ", ".join(re.sub(r"\w{2,}|(?<=\+)\w",
1033+
lambda m: m.group(0).capitalize(),
1034+
keymap)
1035+
for keymap in keymaps)
1036+
1037+
def _get_help_text(self):
1038+
entries = []
1039+
for name, tool in sorted(self.toolmanager.tools.items()):
1040+
if not tool.description:
1041+
continue
1042+
entries.append(
1043+
"{}: {}\n\t{}".format(
1044+
name, self._format_tool_keymap(name), tool.description))
1045+
return "\n".join(entries)
1046+
1047+
def _get_help_html(self):
1048+
fmt = "<tr><td>{}</td><td>{}</td><td>{}</td></tr>"
1049+
rows = [fmt.format(
1050+
"<b>Action</b>", "<b>Shortcuts</b>", "<b>Description</b>")]
1051+
for name, tool in sorted(self.toolmanager.tools.items()):
1052+
if not tool.description:
1053+
continue
1054+
rows.append(fmt.format(
1055+
name, self._format_tool_keymap(name), tool.description))
1056+
return ("<table><thead>" + rows[0] + "</thead>"
1057+
"<tbody>".join(rows[1:]) + "</tbody></table>")
1058+
1059+
10231060
default_tools = {'home': ToolHome, 'back': ToolBack, 'forward': ToolForward,
10241061
'zoom': ToolZoom, 'pan': ToolPan,
10251062
'subplots': 'ToolConfigureSubplots',
@@ -1037,12 +1074,13 @@ def _mouse_move(self, event):
10371074
_views_positions: ToolViewsPositions,
10381075
'cursor': 'ToolSetCursor',
10391076
'rubberband': 'ToolRubberband',
1077+
'help': 'ToolHelp',
10401078
}
10411079
"""Default tools"""
10421080

10431081
default_toolbar_tools = [['navigation', ['home', 'back', 'forward']],
10441082
['zoompan', ['pan', 'zoom', 'subplots']],
1045-
['io', ['save']]]
1083+
['io', ['save', 'help']]]
10461084
"""Default tools in the toolbar"""
10471085

10481086

lib/matplotlib/backends/_backend_tk.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import six
2-
from six.moves import tkinter as Tk
32

43
import math
54
import logging
65
import os.path
76
import sys
7+
import tkinter as Tk
8+
from tkinter.simpledialog import SimpleDialog
89

910
import numpy as np
1011

@@ -963,10 +964,18 @@ def destroy(self, *args, **kwargs):
963964
self.window = None
964965

965966

967+
class HelpTk(backend_tools.ToolHelpBase):
968+
def trigger(self, *args):
969+
dialog = SimpleDialog(
970+
self.figure.canvas._tkcanvas, self._get_help_text(), ["OK"])
971+
dialog.done = lambda num: dialog.frame.master.withdraw()
972+
973+
966974
backend_tools.ToolSaveFigure = SaveFigureTk
967975
backend_tools.ToolConfigureSubplots = ConfigureSubplotsTk
968976
backend_tools.ToolSetCursor = SetCursorTk
969977
backend_tools.ToolRubberband = RubberbandTk
978+
backend_tools.ToolHelp = HelpTk
970979
Toolbar = ToolbarTk
971980

972981

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,16 @@ def trigger(self, sender, event, data=None):
846846
self.window.present()
847847

848848

849+
class HelpGTK3(backend_tools.ToolHelpBase):
850+
def trigger(self, *args):
851+
dialog = Gtk.MessageDialog(
852+
self._figure.canvas.get_toplevel(),
853+
0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, self._get_help_text(),
854+
title="Help")
855+
dialog.run()
856+
dialog.destroy()
857+
858+
849859
# Define the file to use as the GTk icon
850860
if sys.platform == 'win32':
851861
icon_filename = 'matplotlib.png'
@@ -877,6 +887,7 @@ def error_msg_gtk(msg, parent=None):
877887
backend_tools.ToolConfigureSubplots = ConfigureSubplotsGTK3
878888
backend_tools.ToolSetCursor = SetCursorGTK3
879889
backend_tools.ToolRubberband = RubberbandGTK3
890+
backend_tools.ToolHelp = HelpGTK3
880891

881892
Toolbar = ToolbarGTK3
882893

lib/matplotlib/backends/backend_qt5.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,16 @@ def remove_rubberband(self):
10641064
self.canvas.drawRectangle(None)
10651065

10661066

1067+
class HelpQt(backend_tools.ToolHelpBase):
1068+
def trigger(self, *args):
1069+
QtWidgets.QMessageBox.information(None, "Help", self._get_help_html())
1070+
1071+
10671072
backend_tools.ToolSaveFigure = SaveFigureQt
10681073
backend_tools.ToolConfigureSubplots = ConfigureSubplotsQt
10691074
backend_tools.ToolSetCursor = SetCursorQt
10701075
backend_tools.ToolRubberband = RubberbandQt
1076+
backend_tools.ToolHelp = HelpQt
10711077

10721078

10731079
def error_msg_qt(msg, parent=None):
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
@@ -1420,6 +1420,7 @@ def _validate_linestyle(ls):
14201420
'keymap.yscale': [['l'], validate_stringlist],
14211421
'keymap.xscale': [['k', 'L'], validate_stringlist],
14221422
'keymap.all_axes': [['a'], validate_stringlist],
1423+
'keymap.help': [['f1'], validate_stringlist],
14231424

14241425
# sample data
14251426
'examples.directory': ['', validate_string],

matplotlibrc.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ backend : $TEMPLATE_BACKEND
583583
#keymap.pan : p ## pan mnemonic
584584
#keymap.zoom : o ## zoom mnemonic
585585
#keymap.save : s, ctrl+s ## saving current figure
586+
#keymap.help : f1 ## display help about active tools
586587
#keymap.quit : ctrl+w, cmd+w, q ## close the current figure
587588
#keymap.quit_all : W, cmd+W, Q ## close all figures
588589
#keymap.grid : g ## switching on/off major grids in current axes

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)