From 60cb321668add43bcf4aaed353157bbfc779611f Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Fri, 21 Jul 2023 12:22:47 -0500 Subject: [PATCH] Fix errors on Variable Explorer and Debugger when current widget is empty Also, introduce a new method to ShellConnectMainWidget to easily check if the current widget is an empty one. --- spyder/api/shellconnect/main_widget.py | 36 +++++++++++-------- .../plugins/debugger/widgets/main_widget.py | 15 ++++---- spyder/plugins/plots/widgets/main_widget.py | 2 +- .../variableexplorer/widgets/main_widget.py | 26 +++++++++----- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/spyder/api/shellconnect/main_widget.py b/spyder/api/shellconnect/main_widget.py index b3ef523b757..8cc6d373445 100644 --- a/spyder/api/shellconnect/main_widget.py +++ b/spyder/api/shellconnect/main_widget.py @@ -43,6 +43,24 @@ def __init__(self, *args, **kwargs): layout.addWidget(self._stack) self.setLayout(layout) + # ---- PluginMainWidget API + # ------------------------------------------------------------------------ + def current_widget(self): + """ + Return the current figure browser widget in the stack. + + Returns + ------- + QWidget + The current widget. + """ + return self._stack.currentWidget() + + def get_focus_widget(self): + return self.current_widget() + + # ---- SpyderWidgetMixin API + # ------------------------------------------------------------------------ def update_style(self): self._stack.setStyleSheet("QStackedWidget {padding: 0px; border: 0px}") @@ -59,20 +77,6 @@ def count(self): """ return self._stack.count() - def current_widget(self): - """ - Return the current figure browser widget in the stack. - - Returns - ------- - QWidget - The current widget. - """ - return self._stack.currentWidget() - - def get_focus_widget(self): - return self.current_widget() - def get_widget_for_shellwidget(self, shellwidget): """return widget corresponding to shellwidget.""" shellwidget_id = id(shellwidget) @@ -163,3 +167,7 @@ def refresh(self): if self.count(): widget = self.current_widget() widget.refresh() + + def is_current_widget_empty(self): + """Check if the current widget is a PaneEmptyWidget.""" + return isinstance(self.current_widget(), PaneEmptyWidget) diff --git a/spyder/plugins/debugger/widgets/main_widget.py b/spyder/plugins/debugger/widgets/main_widget.py index 82eabdcc2d8..00028a70190 100644 --- a/spyder/plugins/debugger/widgets/main_widget.py +++ b/spyder/plugins/debugger/widgets/main_widget.py @@ -341,6 +341,9 @@ def setup(self): def update_actions(self): """Update actions.""" widget = self.current_widget() + if self.is_current_widget_empty(): + return + search_action = self.get_action(DebuggerWidgetActions.Search) enter_debug_action = self.get_action( DebuggerWidgetActions.EnterDebug) @@ -361,6 +364,7 @@ def update_actions(self): show_enter_debugger = post_mortem or executing is_inspecting = widget.state == FramesBrowserState.Inspect pdb_prompt = sw.is_waiting_pdb_input() + search_action.setChecked(search) enter_debug_action.setEnabled(show_enter_debugger) inspect_action.setEnabled(executing) @@ -372,8 +376,7 @@ def update_actions(self): DebuggerWidgetActions.Step, DebuggerWidgetActions.Return, DebuggerWidgetActions.Stop, - DebuggerWidgetActions.GotoCursor, - ]: + DebuggerWidgetActions.GotoCursor]: action = self.get_action(action_name) action.setEnabled(pdb_prompt) @@ -490,7 +493,7 @@ def set_pdb_take_focus(self, take_focus): next call. """ widget = self.current_widget() - if widget is None: + if widget is None or self.is_current_widget_empty(): return False widget.shellwidget._pdb_take_focus = take_focus @@ -498,14 +501,14 @@ def set_pdb_take_focus(self, take_focus): def toggle_finder(self, checked): """Show or hide finder.""" widget = self.current_widget() - if widget is None: + if widget is None or self.is_current_widget_empty(): return widget.toggle_finder(checked) def get_pdb_state(self): """Get debugging state of the current console.""" widget = self.current_widget() - if widget is None or not hasattr(widget, 'shellwidget'): + if widget is None or self.is_current_widget_empty(): return False sw = widget.shellwidget if sw is not None: @@ -515,7 +518,7 @@ def get_pdb_state(self): def get_pdb_last_step(self): """Get last pdb step of the current console.""" widget = self.current_widget() - if widget is None or not hasattr(widget, 'shellwidget'): + if widget is None or self.is_current_widget_empty(): return None, None sw = widget.shellwidget if sw is not None: diff --git a/spyder/plugins/plots/widgets/main_widget.py b/spyder/plugins/plots/widgets/main_widget.py index 81ab276d7fe..9caf486b02c 100644 --- a/spyder/plugins/plots/widgets/main_widget.py +++ b/spyder/plugins/plots/widgets/main_widget.py @@ -216,7 +216,7 @@ def update_actions(self): value = False widget = self.current_widget() figviewer = None - if widget and hasattr(widget, 'figviewer'): + if widget and not self.is_current_widget_empty(): figviewer = widget.figviewer thumbnails_sb = widget.thumbnails_sb value = figviewer.figcanvas.fig is not None diff --git a/spyder/plugins/variableexplorer/widgets/main_widget.py b/spyder/plugins/variableexplorer/widgets/main_widget.py index 15728cfca76..acaef4ccae1 100644 --- a/spyder/plugins/variableexplorer/widgets/main_widget.py +++ b/spyder/plugins/variableexplorer/widgets/main_widget.py @@ -420,18 +420,24 @@ def setup(self): def update_actions(self): """Update the actions.""" + nsb = self.current_widget() + if self.is_current_widget_empty(): + return + action = self.get_action(VariableExplorerWidgetActions.ToggleMinMax) action.setEnabled(is_module_installed('numpy')) - nsb = self.current_widget() + if nsb: save_data_action = self.get_action( VariableExplorerWidgetActions.SaveData) save_data_action.setEnabled(nsb.filename is not None) search_action = self.get_action(VariableExplorerWidgetActions.Search) + if nsb is None: checked = False else: checked = nsb.finder_is_visible() + search_action.setChecked(checked) @on_conf_change @@ -483,19 +489,19 @@ def import_data(self, filenames=None): """ Import data in current namespace. """ - if self.count(): + if not self.is_current_widget_empty(): nsb = self.current_widget() nsb.refresh_table() nsb.import_data(filenames=filenames) def save_data(self): - if self.count(): + if not self.is_current_widget_empty(): nsb = self.current_widget() nsb.save_data() self.update_actions() def reset_namespace(self): - if self.count(): + if not self.is_current_widget_empty(): nsb = self.current_widget() nsb.reset_namespace() @@ -503,7 +509,7 @@ def reset_namespace(self): def toggle_finder(self, checked): """Hide or show the finder.""" widget = self.current_widget() - if widget is None: + if widget is None or self.is_current_widget_empty(): return widget.toggle_finder(checked) @@ -514,7 +520,7 @@ def hide_finder(self): action.setChecked(False) def refresh_table(self): - if self.count(): + if not self.is_current_widget_empty(): nsb = self.current_widget() nsb.refresh_table() @@ -530,10 +536,12 @@ def free_memory(self): self.sig_free_memory_requested) def resize_rows(self): - self._current_editor.resizeRowsToContents() + if self._current_editor is not None: + self._current_editor.resizeRowsToContents() def resize_columns(self): - self._current_editor.resize_column_contents() + if self._current_editor is not None: + self._current_editor.resize_column_contents() def paste(self): self._current_editor.paste() @@ -576,7 +584,7 @@ def view_item(self): @property def _current_editor(self): editor = None - if self.count(): + if not self.is_current_widget_empty(): nsb = self.current_widget() editor = nsb.editor return editor