Skip to content

Commit

Permalink
Preferences: Standardize how tabs are created in config pages
Browse files Browse the repository at this point in the history
- Now it's only necessary to call the create_tab method of
SpyderConfigPage to add tabs. Creating the tab widget and setting its
layout is done automatically by that method.
- This will allow us to easily control the properties of those tab
widgets in a single place.
  • Loading branch information
ccordoba12 committed Sep 18, 2023
1 parent f501efd commit fd694e8
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 91 deletions.
26 changes: 12 additions & 14 deletions spyder/plugins/application/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from qtpy.compat import from_qvariant
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QApplication, QButtonGroup, QGridLayout, QGroupBox,
QHBoxLayout, QLabel, QMessageBox, QTabWidget,
QVBoxLayout, QWidget)
QHBoxLayout, QLabel, QMessageBox, QVBoxLayout,
QWidget)

from spyder.config.base import (_, DISABLED_LANGUAGES, LANGUAGE_CODES,
is_conda_based_app, save_lang_conf)
Expand Down Expand Up @@ -236,21 +236,19 @@ def set_open_file(state):

screen_resolution_layout.addLayout(screen_resolution_inner_layout)
screen_resolution_group.setLayout(screen_resolution_layout)

if sys.platform == "darwin" and not is_conda_based_app():
interface_tab = self.create_tab(screen_resolution_group,
interface_group, macOS_group)
self.create_tab(
_("Interface"),
[screen_resolution_group, interface_group, macOS_group]
)
else:
interface_tab = self.create_tab(screen_resolution_group,
interface_group)

self.tabs = QTabWidget()
self.tabs.addTab(interface_tab, _("Interface"))
self.tabs.addTab(self.create_tab(advanced_widget),
_("Advanced settings"))
self.create_tab(
_("Interface"),
[screen_resolution_group, interface_group]
)

vlayout = QVBoxLayout()
vlayout.addWidget(self.tabs)
self.setLayout(vlayout)
self.create_tab(_("Advanced settings"), advanced_widget)

def apply_settings(self, options):
if 'high_dpi_custom_scale_factors' in options:
Expand Down
26 changes: 9 additions & 17 deletions spyder/plugins/editor/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""Editor config page."""

from qtpy.QtWidgets import (QGridLayout, QGroupBox, QHBoxLayout, QLabel,
QTabWidget, QVBoxLayout)
QVBoxLayout)

from spyder.api.config.decorators import on_conf_change
from spyder.api.config.mixins import SpyderConfigurationObserver
Expand Down Expand Up @@ -333,27 +333,19 @@ def enable_tabwidth_spin(index):
eol_group.setLayout(eol_layout)

# --- Tabs ---
self.tabs = QTabWidget(self)
self.tabs.addTab(
self.create_tab(display_group, highlight_group, other_group),
_("Interface")
self.create_tab(
_("Interface"),
[display_group, highlight_group, other_group]
)

self.tabs.addTab(
self.create_tab(automatic_group, indentation_group),
_("Source code")
)
self.create_tab(_("Source code"), [automatic_group, indentation_group])

self.tabs.addTab(
self.create_tab(templates_group, autosave_group, docstring_group,
annotations_group, eol_group),
_("Advanced settings")
self.create_tab(
_("Advanced settings"),
[templates_group, autosave_group, docstring_group,
annotations_group, eol_group]
)

vlayout = QVBoxLayout()
vlayout.addWidget(self.tabs)
self.setLayout(vlayout)

@on_conf_change(
option=('provider_configuration', 'lsp', 'values', 'format_on_save'),
section='completions'
Expand Down
15 changes: 4 additions & 11 deletions spyder/plugins/explorer/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"""File explorer configuration page."""

# Third party imports
from qtpy.QtWidgets import (QTabWidget, QVBoxLayout, QWidget, QGroupBox,
QLabel, QPushButton)
from qtpy.QtWidgets import (QGroupBox, QLabel, QPushButton, QVBoxLayout,
QWidget)

# Local imports
from spyder.api.preferences import PluginConfigPage
Expand Down Expand Up @@ -86,15 +86,8 @@ def setup_page(self):
layout_file.addWidget(self.edit_file_associations)
associations_widget.setLayout(layout_file)

self.tabs = QTabWidget()
self.tabs.addTab(self.create_tab(general_widget), _("General"))
self.tabs.addTab(self.create_tab(associations_widget),
_("File associations"))

tab_layout = QVBoxLayout()
tab_layout.addWidget(self.tabs)

self.setLayout(tab_layout)
self.create_tab(_("General"), general_widget)
self.create_tab(_("File associations"), associations_widget)

# Signals
file_associations.sig_data_changed.connect(self.update_associations)
Expand Down
34 changes: 14 additions & 20 deletions spyder/plugins/ipythonconsole/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Third party imports
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QGridLayout, QGroupBox, QHBoxLayout, QLabel,
QTabWidget, QVBoxLayout)
QVBoxLayout)

# Local imports
from spyder.api.translations import _
Expand Down Expand Up @@ -368,29 +368,23 @@ def setup_page(self):
windows_group.setLayout(windows_layout)

# --- Tabs organization ---
self.tabs = QTabWidget(self)
self.tabs.addTab(
self.create_tab(display_group, confirmations_group, comp_group,
source_code_group),
_("Interface")
self.create_tab(
_("Interface"),
[display_group, confirmations_group, comp_group, source_code_group]
)

self.tabs.addTab(
self.create_tab(pylab_group, backend_group, inline_group),
_("Graphics")
self.create_tab(
_("Graphics"),
[pylab_group, backend_group, inline_group]
)

self.tabs.addTab(
self.create_tab(run_lines_group, run_file_group),
_("Startup")
self.create_tab(
_("Startup"),
[run_lines_group, run_file_group]
)

self.tabs.addTab(
self.create_tab(jedi_group, greedy_group, autocall_group,
sympy_group, prompts_group, windows_group),
_("Advanced settings")
self.create_tab(
_("Advanced settings"),
[jedi_group, greedy_group, autocall_group, sympy_group,
prompts_group, windows_group]
)

vlayout = QVBoxLayout()
vlayout.addWidget(self.tabs)
self.setLayout(vlayout)
59 changes: 40 additions & 19 deletions spyder/plugins/preferences/widgets/config_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,16 +889,41 @@ def create_button(self, text, callback):
self.CONF_SECTION, opt))
return btn

def create_tab(self, *widgets):
"""Create simple tab widget page: widgets added in a vertical layout"""
widget = QWidget(self)
def create_tab(self, name, widgets):
"""
Create a tab widget page.
Parameters
----------
name: str
Name of the tab
widgets: list or QWidget
List of widgets to add to the tab. This can be also a single
widget.
Notes
-----
* Widgets are added in a vertical layout.
"""
if self.tabs is None:
self.tabs = QTabWidget(self)

vlayout = QVBoxLayout()
vlayout.addWidget(self.tabs)
self.setLayout(vlayout)

if not isinstance(widgets, list):
widgets = [widgets]

tab = QWidget(self)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
for widg in widgets:
layout.addWidget(widg)
for w in widgets:
layout.addWidget(w)
layout.addStretch(1)
widget.setLayout(layout)
return widget
tab.setLayout(layout)

self.tabs.addTab(tab, name)

def prompt_restart_required(self):
"""Prompt the user with a request to restart."""
Expand All @@ -922,24 +947,20 @@ def restart(self):
"""Restart Spyder."""
self.main.restart(close_immediately=True)

def add_tab(self, Widget):
def _add_tab(self, Widget):
widget = Widget(self)

if self.tabs is None:
# In case a preference page does not have any tabs, we need to
# add a tab with the widgets that already exist and then add the
# new tab.
self.tabs = QTabWidget()
layout = self.layout()
main_widget = QWidget()
main_widget = QWidget(self)
main_widget.setLayout(layout)
self.tabs.addTab(self.create_tab(main_widget),
_('General'))
self.tabs.addTab(self.create_tab(widget),
Widget.TITLE)
vlayout = QVBoxLayout()
vlayout.addWidget(self.tabs)
self.setLayout(vlayout)

self.create_tab(_('General'), main_widget)
self.create_tab(Widget.TITLE, widget)
else:
self.tabs.addTab(self.create_tab(widget),
Widget.TITLE)
self.create_tab(Widget.TITLE, widget)

self.load_from_conf()
4 changes: 2 additions & 2 deletions spyder/plugins/preferences/widgets/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def _dialog_finished(result_code):
page = ConfigPage(plugin, dlg)
page.initialize()
for Tab in config_tabs.get(page_name, []):
page.add_tab(Tab)
page._add_tab(Tab)
dlg.add_page(page)
else:
page = plugin._create_configwidget(dlg, main_window)
for Tab in config_tabs.get(page_name, []):
page.add_tab(Tab)
page._add_tab(Tab)
dlg.add_page(page)

# Add separator after the last element of the most important
Expand Down
11 changes: 3 additions & 8 deletions spyder/plugins/run/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QGroupBox, QLabel, QVBoxLayout, QComboBox,
QTableView, QAbstractItemView, QPushButton,
QGridLayout, QHeaderView, QTabWidget, QWidget)
QGridLayout, QHeaderView, QWidget)

# Local imports
from spyder.api.preferences import PluginConfigPage
Expand Down Expand Up @@ -239,13 +239,8 @@ def setup_page(self):
executor_widget = QWidget()
executor_widget.setLayout(vlayout)

self.tabs = QTabWidget()
self.tabs.addTab(self.create_tab(executor_widget), _("Run executors"))
self.tabs.addTab(
self.create_tab(run_widget), _("Editor interactions"))
main_layout = QVBoxLayout()
main_layout.addWidget(self.tabs)
self.setLayout(main_layout)
self.create_tab(_("Run executors"), executor_widget)
self.create_tab(_("Editor interactions"), run_widget)

def executor_index_changed(self, index: int):
# Save previous executor configuration
Expand Down

0 comments on commit fd694e8

Please sign in to comment.