Skip to content

Commit

Permalink
Editor: Initial edit menu move to use mainmenu plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Mar 9, 2023
1 parent 4e2892e commit abeebfd
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 112 deletions.
210 changes: 113 additions & 97 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def signal_handler(signum, frame=None):
self.selectall_action = None

# Menu bars
self.edit_menu = None
self.edit_menu_actions = []
# self.edit_menu = None
# self.edit_menu_actions = []

# TODO: Move to corresponding Plugins
self.file_toolbar = None
Expand Down Expand Up @@ -410,7 +410,7 @@ def register_plugin(self, plugin_name, external=False, omit_conf=False):
self.unmaximize_plugin)

if isinstance(plugin, SpyderDockablePlugin):
plugin.sig_focus_changed.connect(self.plugin_focus_changed)
# plugin.sig_focus_changed.connect(self.plugin_focus_changed)
plugin.sig_switch_to_plugin_requested.connect(
self.switch_to_plugin)
plugin.sig_update_ancestor_requested.connect(
Expand Down Expand Up @@ -816,9 +816,9 @@ def setup(self):
# TODO: Remove when all menus are migrated to use the Main Menu Plugin
logger.info("Creating Menus...")
from spyder.plugins.mainmenu.api import (
ApplicationMenus, FileMenuSections)
ApplicationMenus, EditMenuSections, FileMenuSections)
mainmenu = self.mainmenu
self.edit_menu = mainmenu.get_application_menu("edit_menu")
# self.edit_menu = mainmenu.get_application_menu(ApplicationMenus.Edit)

# Switcher shortcuts
self.file_switcher_action = create_action(
Expand All @@ -843,37 +843,53 @@ def setup(self):
self.register_shortcut(self.symbol_finder_action, context="_",
name="symbol finder", add_shortcut_to_tip=True)

def create_edit_action(text, tr_text, icon):
textseq = text.split(' ')
method_name = textseq[0].lower() + "".join(textseq[1:])
action = create_action(self, tr_text,
icon=icon,
triggered=self.global_callback,
data=method_name,
context=Qt.WidgetShortcut)
self.register_shortcut(action, "Editor", text)
return action

self.undo_action = create_edit_action('Undo', _('Undo'),
ima.icon('undo'))
self.redo_action = create_edit_action('Redo', _('Redo'),
ima.icon('redo'))
self.copy_action = create_edit_action('Copy', _('Copy'),
ima.icon('editcopy'))
self.cut_action = create_edit_action('Cut', _('Cut'),
ima.icon('editcut'))
self.paste_action = create_edit_action('Paste', _('Paste'),
ima.icon('editpaste'))
self.selectall_action = create_edit_action("Select All",
_("Select All"),
ima.icon('selectall'))

self.edit_menu_actions += [self.undo_action, self.redo_action,
None, self.cut_action, self.copy_action,
self.paste_action, self.selectall_action,
None]
if self.get_plugin(Plugins.Editor, error=False):
self.edit_menu_actions += self.editor.edit_menu_actions
# def create_edit_action(text, tr_text, icon):
# textseq = text.split(' ')
# method_name = textseq[0].lower() + "".join(textseq[1:])
# action = create_action(self, tr_text,
# icon=icon,
# triggered=self.global_callback,
# data=method_name,
# context=Qt.WidgetShortcut)
# self.register_shortcut(action, "Editor", text)
# return action

# self.undo_action = create_edit_action('Undo', _('Undo'),
# ima.icon('undo'))
# self.redo_action = create_edit_action('Redo', _('Redo'),
# ima.icon('redo'))
# self.copy_action = create_edit_action('Copy', _('Copy'),
# ima.icon('editcopy'))
# self.cut_action = create_edit_action('Cut', _('Cut'),
# ima.icon('editcut'))
# self.paste_action = create_edit_action('Paste', _('Paste'),
# ima.icon('editpaste'))
# self.selectall_action = create_edit_action("Select All",
# _("Select All"),
# ima.icon('selectall'))

# for action in [self.undo_action, self.redo_action]:
# mainmenu.add_item_to_application_menu(
# action,
# menu_id=ApplicationMenus.Edit,
# section=EditMenuSections.UndoRedo,
# before_section=EditMenuSections.Editor,
# omit_id=True
# )

# for action in [
# self.cut_action, self.copy_action, self.paste_action,
# self.selectall_action
# ]:
# mainmenu.add_item_to_application_menu(
# action,
# menu_id=ApplicationMenus.Edit,
# section=EditMenuSections.Copy,
# before_section=EditMenuSections.Editor,
# omit_id=True
# )
# if self.get_plugin(Plugins.Editor, error=False):
# self.edit_menu_actions += self.editor.edit_menu_actions

switcher_actions = [
self.file_switcher_action,
Expand All @@ -898,7 +914,7 @@ def create_edit_action(text, tr_text, icon):

# TODO: Migrate to use the MainMenu Plugin instead of list of actions
# Filling out menu/toolbar entries:
add_actions(self.edit_menu, self.edit_menu_actions)
# add_actions(self.edit_menu, self.edit_menu_actions)

def __getattr__(self, attr):
"""
Expand Down Expand Up @@ -938,12 +954,12 @@ def pre_visible_setup(self):
self.splash.hide()

# Menu about to show
for child in self.menuBar().children():
if isinstance(child, QMenu):
try:
child.aboutToShow.connect(self.update_edit_menu)
except TypeError:
pass
# for child in self.menuBar().children():
# if isinstance(child, QMenu):
# try:
# child.aboutToShow.connect(self.update_edit_menu)
# except TypeError:
# pass

# Register custom layouts
if self.layouts is not None:
Expand Down Expand Up @@ -1185,9 +1201,9 @@ def free_memory(self):
"""Free memory after event."""
gc.collect()

def plugin_focus_changed(self):
"""Focus has changed from one plugin to another"""
self.update_edit_menu()
# def plugin_focus_changed(self):
# """Focus has changed from one plugin to another"""
# self.update_edit_menu()

def show_shortcuts(self, menu):
"""Show action shortcuts in menu."""
Expand Down Expand Up @@ -1249,45 +1265,45 @@ def get_focus_widget_properties(self):
textedit_properties = (console, not_readonly, readwrite_editor)
return widget, textedit_properties

def update_edit_menu(self):
"""Update edit menu"""
widget, textedit_properties = self.get_focus_widget_properties()
if textedit_properties is None: # widget is not an editor/console
return
# !!! Below this line, widget is expected to be a QPlainTextEdit
# instance
console, not_readonly, readwrite_editor = textedit_properties

if hasattr(self, 'editor'):
# Editor has focus and there is no file opened in it
if (not console and not_readonly and self.editor
and not self.editor.is_file_opened()):
return

# Disabling all actions to begin with
for child in self.edit_menu.actions():
child.setEnabled(False)

self.selectall_action.setEnabled(True)

# Undo, redo
self.undo_action.setEnabled(readwrite_editor
and widget.document().isUndoAvailable())
self.redo_action.setEnabled(readwrite_editor
and widget.document().isRedoAvailable())

# Copy, cut, paste, delete
has_selection = widget.has_selected_text()
self.copy_action.setEnabled(has_selection)
self.cut_action.setEnabled(has_selection and not_readonly)
self.paste_action.setEnabled(not_readonly)

# Comment, uncomment, indent, unindent...
if not console and not_readonly:
# This is the editor and current file is writable
if self.get_plugin(Plugins.Editor, error=False):
for action in self.editor.edit_menu_actions:
action.setEnabled(True)
# def update_edit_menu(self):
# """Update edit menu"""
# widget, textedit_properties = self.get_focus_widget_properties()
# if textedit_properties is None: # widget is not an editor/console
# return
# # !!! Below this line, widget is expected to be a QPlainTextEdit
# # instance
# console, not_readonly, readwrite_editor = textedit_properties

# if hasattr(self, 'editor'):
# # Editor has focus and there is no file opened in it
# if (not console and not_readonly and self.editor
# and not self.editor.is_file_opened()):
# return

# # Disabling all actions to begin with
# for child in self.edit_menu.actions():
# child.setEnabled(False)

# self.selectall_action.setEnabled(True)

# # Undo, redo
# self.undo_action.setEnabled(readwrite_editor
# and widget.document().isUndoAvailable())
# self.redo_action.setEnabled(readwrite_editor
# and widget.document().isRedoAvailable())

# # Copy, cut, paste, delete
# has_selection = widget.has_selected_text()
# self.copy_action.setEnabled(has_selection)
# self.cut_action.setEnabled(has_selection and not_readonly)
# self.paste_action.setEnabled(not_readonly)

# # Comment, uncomment, indent, unindent...
# if not console and not_readonly:
# # This is the editor and current file is writable
# if self.get_plugin(Plugins.Editor, error=False):
# for action in self.editor.edit_menu_actions:
# action.setEnabled(True)

def set_splash(self, message):
"""Set splash message"""
Expand Down Expand Up @@ -1378,18 +1394,18 @@ def add_dockwidget(self, plugin):
self.addDockWidget(location, dockwidget)
self.widgetlist.append(plugin)

def global_callback(self):
"""Global callback"""
widget = QApplication.focusWidget()
action = self.sender()
callback = from_qvariant(action.data(), to_text_string)
from spyder.plugins.editor.widgets.base import TextEditBaseWidget
from spyder.plugins.ipythonconsole.widgets import ControlWidget

if isinstance(widget, (TextEditBaseWidget, ControlWidget)):
getattr(widget, callback)()
else:
return
# def global_callback(self):
# """Global callback"""
# widget = QApplication.focusWidget()
# action = self.sender()
# callback = from_qvariant(action.data(), to_text_string)
# from spyder.plugins.editor.widgets.base import TextEditBaseWidget
# from spyder.plugins.ipythonconsole.widgets import ControlWidget

# if isinstance(widget, (TextEditBaseWidget, ControlWidget)):
# getattr(widget, callback)()
# else:
# return

def redirect_internalshell_stdio(self, state):
console = self.get_plugin(Plugins.Console, error=False)
Expand Down
Loading

0 comments on commit abeebfd

Please sign in to comment.