Context Menu for snapping view to primary axis planes in 3D plots by AdwaithBatchu · Pull Request #30976 · matplotlib/matplotlib · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matplotlib/backends/_backend_tk.py
12 changes: 12 additions & 0 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,18 @@ class FigureManagerGTK3(_FigureManagerGTK):
_toolbar2_class = NavigationToolbar2GTK3
_toolmanager_toolbar_class = ToolbarGTK3

def context_menu(self, event, labels=None, actions=None):
if not labels or not actions:
return
menu = Gtk.Menu()
for label, action in zip(labels, actions):
item = Gtk.MenuItem(label=label)
menu.append(item)
item.connect('activate', lambda _, a=action: a())
item.show()
menu.connect('selection-done', lambda m: m.destroy())
menu.popup_at_pointer(event.guiEvent)


@_BackendGTK.export
class _BackendGTK3(_BackendGTK):
Expand Down
28 changes: 28 additions & 0 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,34 @@ class FigureManagerGTK4(_FigureManagerGTK):
_toolbar2_class = NavigationToolbar2GTK4
_toolmanager_toolbar_class = ToolbarGTK4

def context_menu(self, event, labels=None, actions=None):
if not labels or not actions:
return
menu = Gio.Menu()
action_group = Gio.SimpleActionGroup()
for label, action in zip(labels, actions):
action_name = label.replace(" ", "_")
g_action = Gio.SimpleAction.new(action_name, None)
g_action.connect("activate", lambda *_, a=action: a())
action_group.add_action(g_action)
menu.append(label, f"win.{action_name}")

self.canvas.insert_action_group("win", action_group)
popover = Gtk.PopoverMenu.new_from_model(menu)
popover.set_parent(self.canvas)
popover.set_has_arrow(False)
popover.set_halign(Gtk.Align.START)

scale = self.canvas.get_scale_factor()
height = self.canvas.get_height()
rect = Gdk.Rectangle()
rect.x = int(event.x / scale)
rect.y = int(height - (event.y / scale))
rect.width = 1
rect.height = 1
popover.set_pointing_to(rect)
popover.popup()


@_BackendGTK.export
class _BackendGTK4(_BackendGTK):
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ def full_screen_toggle(self):
else:
self.window.showFullScreen()

def context_menu(self, event, labels=None, actions=None):
if not labels or not actions:
return
menu = QtWidgets.QMenu(self.window)
for label, action in zip(labels, actions):
menu.addAction(label).triggered.connect(action)
if hasattr(event.guiEvent, 'globalPosition'):
menu.exec(event.guiEvent.globalPosition().toPoint())
else:
menu.exec(event.guiEvent.globalPos())

def _widgetclosed(self):
CloseEvent("close_event", self.canvas)._process()
if self.window._destroying:
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,16 @@ def full_screen_toggle(self):
# docstring inherited
self.frame.ShowFullScreen(not self.frame.IsFullScreen())

def context_menu(self, event, labels=None, actions=None):
if not labels or not actions:
return
menu = wx.Menu()
for label, action in zip(labels, actions):
item = menu.Append(wx.NewIdRef(), label)
menu.Bind(wx.EVT_MENU, lambda _, a=action: a(), item)
self.canvas.PopupMenu(menu, event.guiEvent.GetPosition())
menu.Destroy()

def get_window_title(self):
# docstring inherited
return self.window.GetTitle()
Expand Down
35 changes: 30 additions & 5 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Loading