Skip to content

Commit

Permalink
Fix errors on Variable Explorer and Debugger when current widget is e…
Browse files Browse the repository at this point in the history
…mpty

Also, introduce a new method to ShellConnectMainWidget to easily
check if the current widget is an empty one.
  • Loading branch information
ccordoba12 committed Jul 21, 2023
1 parent b6f4a36 commit 60cb321
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
36 changes: 22 additions & 14 deletions spyder/api/shellconnect/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand All @@ -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)
Expand Down Expand Up @@ -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)
15 changes: 9 additions & 6 deletions spyder/plugins/debugger/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -490,22 +493,22 @@ 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

@Slot(bool)
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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/plots/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions spyder/plugins/variableexplorer/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -483,27 +489,27 @@ 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()

@Slot(bool)
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)

Expand All @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 60cb321

Please sign in to comment.