Skip to content

Commit

Permalink
Main Window: Remove attributes related to the Run menu and toolbar
Browse files Browse the repository at this point in the history
- They are no longer needed because those widgets are now populated by
the Run plugin.
- Also, fix adding the Run toolbar to new Editor windows.
  • Loading branch information
ccordoba12 committed Feb 6, 2023
1 parent 58525e3 commit 4d42430
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 81 deletions.
12 changes: 0 additions & 12 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class MainWindow(QMainWindow, SpyderConfigurationAccessor):
# Signals
restore_scrollbar_position = Signal()
sig_setup_finished = Signal()
all_actions_defined = Signal()
sig_open_external_file = Signal(str)
sig_resized = Signal("QResizeEvent")
sig_moved = Signal("QMoveEvent")
Expand Down Expand Up @@ -249,14 +248,10 @@ def signal_handler(signum, frame=None):
self.search_menu_actions = []
self.source_menu = None
self.source_menu_actions = []
self.run_menu = None
self.run_menu_actions = []

# TODO: Move to corresponding Plugins
self.file_toolbar = None
self.file_toolbar_actions = []
self.run_toolbar = None
self.run_toolbar_actions = []

self.menus = []

Expand Down Expand Up @@ -831,7 +826,6 @@ def setup(self):
self.search_menu = mainmenu.get_application_menu("search_menu")
self.source_menu = mainmenu.get_application_menu("source_menu")
self.source_menu.aboutToShow.connect(self.update_source_menu)
self.run_menu = mainmenu.get_application_menu("run_menu")

# Switcher shortcuts
self.file_switcher_action = create_action(
Expand Down Expand Up @@ -906,7 +900,6 @@ def create_edit_action(text, tr_text, icon):
logger.info("Creating toolbars...")
toolbar = self.toolbar
self.file_toolbar = toolbar.get_application_toolbar("file_toolbar")
self.run_toolbar = toolbar.get_application_toolbar("run_toolbar")

self.set_splash(_("Setting up main window..."))

Expand All @@ -915,11 +908,6 @@ def create_edit_action(text, tr_text, icon):
add_actions(self.edit_menu, self.edit_menu_actions)
add_actions(self.search_menu, self.search_menu_actions)
add_actions(self.source_menu, self.source_menu_actions)
add_actions(self.run_menu, self.run_menu_actions)

# Emitting the signal notifying plugins that main window menu and
# toolbar actions are all defined:
self.all_actions_defined.emit()

def __getattr__(self, attr):
"""
Expand Down
9 changes: 8 additions & 1 deletion spyder/app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from spyder.plugins.debugger.api import DebuggerToolbarActions
from spyder.plugins.ipythonconsole.utils.kernelspec import SpyderKernelSpec
from spyder.plugins.projects.api import EmptyProject
from spyder.plugins.run.api import StoredRunConfigurationExecutor
from spyder.plugins.run.api import RunActions, StoredRunConfigurationExecutor
from spyder.plugins.toolbar.api import ApplicationToolbars
from spyder.utils import encoding
from spyder.utils.environ import (get_user_env, set_user_env,
Expand Down Expand Up @@ -399,6 +399,13 @@ def main_window(request, tmpdir, qtbot):
debug_button = debug_toolbar.widgetForAction(debug_action)
window.debug_button = debug_button

# Add a handle to the "Run file" button to access it quickly because
# it's used a lot.
run_toolbar = toolbar.get_application_toolbar(ApplicationToolbars.Run)
run_action = window.run.get_action(RunActions.Run)
run_button = run_toolbar.widgetForAction(run_action)
window.run_button = run_button

QApplication.processEvents()

if os.name != 'nt':
Expand Down
59 changes: 19 additions & 40 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,10 +974,6 @@ def test_connection_to_external_kernel(main_window, qtbot):
qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 1)
assert nsb.editor.source_model.rowCount() == 1

# Test runfile in external_kernel
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)

# create new file
main_window.editor.new()
code_editor = main_window.editor.get_focus_widget()
Expand All @@ -991,7 +987,7 @@ def test_connection_to_external_kernel(main_window, qtbot):

# Start running
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

assert "runfile" in shell._control.toPlainText()
assert "3" in shell._control.toPlainText()
Expand Down Expand Up @@ -1789,8 +1785,6 @@ def get_random_plugin():
assert main_window.editor._ismaximized
ipyconsole = main_window.get_plugin(Plugins.IPythonConsole)
ipyconsole.create_window()
run_action = main_window.run_toolbar_actions[0]
run_button = main_window.run_toolbar.widgetForAction(run_action)
assert main_window.editor._ismaximized

# Unmaximize when docking back the IPython console
Expand Down Expand Up @@ -1836,16 +1830,21 @@ def get_random_plugin():
# Maximize a plugin and check that it's unmaximized after running a file
plugin_3 = get_random_plugin()
qtbot.mouseClick(max_button, Qt.LeftButton)
qtbot.mouseClick(run_button, Qt.LeftButton)

run_parameters = generate_run_parameters(main_window, test_file)
CONF.set('run', 'last_used_parameters', run_parameters)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

assert not plugin_3.get_widget().get_maximized_state()
assert not max_action.isChecked()
if hasattr(plugin_3, '_hide_after_test'):
plugin_3.toggle_view(False)

# Maximize a plugin and check that it's unmaximized after running a cell
plugin_4 = get_random_plugin()
run_cell_action = main_window.run_toolbar_actions[1]
run_cell_button = main_window.run_toolbar.widgetForAction(run_cell_action)
run_cell_action = main_window.run.get_action('run cell')
run_toolbar = toolbar.get_application_toolbar(ApplicationToolbars.Run)
run_cell_button = run_toolbar.widgetForAction(run_cell_action)
qtbot.mouseClick(max_button, Qt.LeftButton)
qtbot.mouseClick(run_cell_button, Qt.LeftButton)
assert not plugin_4.get_widget().get_maximized_state()
Expand All @@ -1856,9 +1855,8 @@ def get_random_plugin():
# Maximize a plugin and check that it's unmaximized after running a
# selection
plugin_5 = get_random_plugin()
run_selection_action = main_window.run_toolbar_actions[3]
run_selection_button = main_window.run_toolbar.widgetForAction(
run_selection_action)
run_selection_action = main_window.run.get_action('run selection')
run_selection_button = run_toolbar.widgetForAction(run_selection_action)
qtbot.mouseClick(max_button, Qt.LeftButton)
qtbot.mouseClick(run_selection_button, Qt.LeftButton)
assert not plugin_5.get_widget().get_maximized_state()
Expand Down Expand Up @@ -3323,11 +3321,8 @@ def test_varexp_rename(main_window, qtbot, tmpdir):
CONF.set('run', 'last_used_parameters', run_parameters)

# ---- Run file ----
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)

with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 4,
Expand All @@ -3350,7 +3345,7 @@ def data(cm, i, j):

# ---- Run file again ----
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 5,
Expand Down Expand Up @@ -3396,12 +3391,9 @@ def test_varexp_remove(main_window, qtbot, tmpdir):
run_parameters = generate_run_parameters(main_window, filepath)
CONF.set('run', 'last_used_parameters', run_parameters)

run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)

# ---- Run file ----
with qtbot.waitSignal(shell.executed, timeout=SHELL_TIMEOUT):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 4,
Expand Down Expand Up @@ -4111,10 +4103,6 @@ def test_run_unsaved_file_multiprocessing(main_window, qtbot):
lambda: shell.spyder_kernel_ready and shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)

# Main variables
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)

# create new file
main_window.editor.new()
code_editor = main_window.editor.get_focus_widget()
Expand Down Expand Up @@ -4144,7 +4132,7 @@ def test_run_unsaved_file_multiprocessing(main_window, qtbot):
CONF.set('run', 'last_used_parameters', run_parameters)

# Start running
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

# Because multiprocessing is behaving strangly on windows, only some
# situations will work. This is one of these situations so it shouldn't
Expand Down Expand Up @@ -5130,10 +5118,8 @@ def test_func():
qtbot.wait(2000)

# Click the run button
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)
with qtbot.waitSignal(shell.executed, timeout=SHELL_TIMEOUT):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)
qtbot.wait(1000)

assert 'Test stdout' in control.toPlainText()
Expand Down Expand Up @@ -5171,9 +5157,7 @@ def crash_func():
qtbot.wait(2000)

# Click the run button
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

qtbot.waitUntil(lambda: 'Segmentation fault' in control.toPlainText(),
timeout=SHELL_TIMEOUT)
Expand Down Expand Up @@ -5307,9 +5291,6 @@ def test_debug_unsaved_function(main_window, qtbot):
# Main variables
shell = main_window.ipyconsole.get_current_shellwidget()
control = shell._control
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(
run_action)

# Clear all breakpoints
main_window.debugger.clear_all_breakpoints()
Expand All @@ -5331,7 +5312,7 @@ def test_debug_unsaved_function(main_window, qtbot):

# run file
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)

# debug foo
with qtbot.waitSignal(shell.executed):
Expand Down Expand Up @@ -5425,10 +5406,8 @@ def test_print_frames(main_window, qtbot, tmpdir, thread):
CONF.set('run', 'last_used_parameters', run_parameters)

# Click the run button
run_action = main_window.run.get_action('run')
run_button = main_window.run_toolbar.widgetForAction(run_action)
with qtbot.waitSignal(shell.executed):
qtbot.mouseClick(run_button, Qt.LeftButton)
qtbot.mouseClick(main_window.run_button, Qt.LeftButton)
qtbot.wait(1000)

# Check we are blocked
Expand Down
32 changes: 12 additions & 20 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@
from spyder.plugins.editor.widgets.status import (CursorPositionStatus,
EncodingStatus, EOLStatus,
ReadWriteStatus, VCSStatus)
from spyder.plugins.mainmenu.api import ApplicationMenus
from spyder.plugins.run.api import (
RunContext, RunConfigurationMetadata, RunConfiguration,
SupportedExtensionContexts, ExtendedContext)
from spyder.plugins.mainmenu.api import ApplicationMenus
from spyder.plugins.toolbar.api import ApplicationToolbars
from spyder.widgets.simplecodeeditor import SimpleCodeEditor


Expand Down Expand Up @@ -358,7 +359,7 @@ def __init__(self, parent, ignore_last_opened_files=False):
self.completion_capabilities = {}

# Setup new windows:
self.main.all_actions_defined.connect(self.setup_other_windows)
self.main.sig_setup_finished.connect(self.setup_other_windows)

# Find widget
self.find_widget = FindReplace(self, enable_replace=True)
Expand Down Expand Up @@ -1127,13 +1128,6 @@ def get_plugin_actions(self):
self.main.search_menu_actions = (
search_menu_actions + self.main.search_menu_actions)

# ---- Run menu/toolbar construction ----
run_menu_actions = []
self.main.run_menu_actions = (
run_menu_actions + self.main.run_menu_actions)
run_toolbar_actions = []
self.main.run_toolbar_actions += run_toolbar_actions

# ---- Source menu/toolbar construction ----
source_menu_actions = [
showblanks_action,
Expand Down Expand Up @@ -1165,8 +1159,6 @@ def get_plugin_actions(self):
# ---- Dock widget and file dependent actions ----
self.dock_toolbar_actions = (
file_toolbar_actions +
[MENU_SEPARATOR] +
run_toolbar_actions +
[MENU_SEPARATOR]
)
self.pythonfile_dependent_actions = [
Expand Down Expand Up @@ -1638,27 +1630,27 @@ def setup_other_windows(self):
ApplicationMenus.Tools).get_actions()
help_menu_actions = self.main.mainmenu.get_application_menu(
ApplicationMenus.Help).get_actions()
run_menu_actions = self.main.mainmenu.get_application_menu(
ApplicationMenus.Run).get_actions()

# TODO: Rewrite when the editor is moved to the new API
from spyder.plugins.debugger.api import DebuggerToolbarActions
debug_toolbar_actions = []
for action_name in [DebuggerToolbarActions.DebugCurrentFile,
DebuggerToolbarActions.DebugCurrentCell]:
action = self.main.debugger.get_action(action_name)
debug_toolbar_actions.append(action)
# --- TODO: Rewrite when the editor is moved to the new API
debug_toolbar_actions = self.main.toolbar.get_application_toolbar(
ApplicationToolbars.Debug).actions()
run_toolbar_actions = self.main.toolbar.get_application_toolbar(
ApplicationToolbars.Run).actions()

self.toolbar_list = ((_("File toolbar"), "file_toolbar",
self.main.file_toolbar_actions),
(_("Run toolbar"), "run_toolbar",
self.main.run_toolbar_actions),
run_toolbar_actions),
(_("Debug toolbar"), "debug_toolbar",
debug_toolbar_actions))

self.menu_list = ((_("&File"), file_menu_actions),
(_("&Edit"), self.main.edit_menu_actions),
(_("&Search"), self.main.search_menu_actions),
(_("Sour&ce"), self.main.source_menu_actions),
(_("&Run"), self.main.run_menu_actions),
(_("&Run"), run_menu_actions),
(_("&Tools"), tools_menu_actions),
(_("&View"), []),
(_("&Help"), help_menu_actions))
Expand Down
4 changes: 1 addition & 3 deletions spyder/plugins/editor/tests/test_editor_config_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ class MainWindowMock(QMainWindow):
file_toolbar_actions = []
edit_menu_actions = []
edit_toolbar_actions = []
run_menu_actions = []
run_toolbar_actions = []
source_menu_actions = []
source_toolbar_actions = []
search_menu_actions = []
statusbar = Mock()
all_actions_defined = Mock()
new_instance = Mock()
plugin_focus_changed = Mock()
fallback_completions = Mock()
ipyconsole = Mock()
mainmenu = Mock()
sig_setup_finished = Mock()


@pytest.mark.parametrize(
Expand Down
3 changes: 0 additions & 3 deletions spyder/plugins/mainmenu/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,12 @@ def remove_item_from_application_menu(self, item_id: str,
self._main.search_menu_actions, self._main.search_menu),
ApplicationMenus.Source: (
self._main.source_menu_actions, self._main.source_menu),
ApplicationMenus.Run: (
self._main.run_menu_actions, self._main.run_menu),
}

app_menus = {
ApplicationMenus.Edit: self._main.edit_menu,
ApplicationMenus.Search: self._main.search_menu,
ApplicationMenus.Source: self._main.source_menu,
ApplicationMenus.Run: self._main.run_menu,
}

menu = self.get_application_menu(menu_id)
Expand Down
3 changes: 1 addition & 2 deletions spyder/plugins/toolbar/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def on_mainwindow_visible(self):

# TODO: Until all core plugins are migrated, this is needed.
ACTION_MAP = {
ApplicationToolbars.File: self._main.file_toolbar_actions,
ApplicationToolbars.Run: self._main.run_toolbar_actions,
ApplicationToolbars.File: self._main.file_toolbar_actions
}
for toolbar in container.get_application_toolbars():
toolbar_id = toolbar.ID
Expand Down

0 comments on commit 4d42430

Please sign in to comment.