Skip to content

Commit

Permalink
Merge pull request #21734 from ccordoba12/sidebar-dialog
Browse files Browse the repository at this point in the history
PR: Create a base class for sidebar dialogs (Widgets)
  • Loading branch information
ccordoba12 authored Jan 26, 2024
2 parents 40fbe52 + bdc913e commit 582251b
Show file tree
Hide file tree
Showing 6 changed files with 680 additions and 482 deletions.
8 changes: 6 additions & 2 deletions spyder/config/appearance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import sys

from spyder.config.base import running_under_pytest
from spyder.config.fonts import MEDIUM, MONOSPACE
from spyder.plugins.help.utils.sphinxify import CSS_PATH

Expand All @@ -28,8 +29,11 @@
'font/bold': False,
# We set the app font used in the system when Spyder starts, so we don't
# need to do it here.
'app_font/family': '',
'app_font/size': 0,
'app_font/family': 'Arial' if running_under_pytest() else '',
# This default value helps to do visual checks in our tests when run
# independently and avoids Qt warnings related to a null font size. It can
# also be useful in case we fail to detect the interface font.
'app_font/size': 10,
'app_font/italic': False,
'app_font/bold': False,
'use_system_font': True,
Expand Down
4 changes: 2 additions & 2 deletions spyder/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@


# =============================================================================
# Config instance
# Config version
# =============================================================================
# IMPORTANT NOTES:
# 1. If you want to *change* the default value of a current option, you need to
Expand All @@ -670,4 +670,4 @@
# or if you want to *rename* options, then you need to do a MAJOR update in
# version, e.g. from 3.0.0 to 4.0.0
# 3. You don't need to touch this value if you're just adding a new option
CONF_VERSION = '82.0.0'
CONF_VERSION = '82.1.0'
136 changes: 44 additions & 92 deletions spyder/plugins/preferences/widgets/config_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from spyder.utils.misc import getcwd_or_home
from spyder.widgets.colors import ColorLayout
from spyder.widgets.comboboxes import FileComboBox
from spyder.widgets.sidebardialog import SidebarPage


class BaseConfigTab(QWidget):
Expand All @@ -60,99 +61,26 @@ def remove_option(self, option, section=None):
CONF.remove_option(section, option)


class ConfigPage(QWidget):
"""Base class for configuration page in Preferences"""
class SpyderConfigPage(SidebarPage, ConfigAccessMixin):
"""
Page that can display graphical elements connected to our config system.
"""

# Signals
apply_button_enabled = Signal(bool)
show_this_page = Signal()

def __init__(self, parent, apply_callback=None):
QWidget.__init__(self, parent)
# Constants
CONF_SECTION = None

def __init__(self, parent):
SidebarPage.__init__(self, parent)

# Callback to call before saving settings to disk
self.pre_apply_callback = None

# Callback to call after saving settings to disk
self.apply_callback = apply_callback

self.is_modified = False

def initialize(self):
"""
Initialize configuration page:
* setup GUI widgets
* load settings and change widgets accordingly
"""
self.setup_page()
self.load_from_conf()

def get_name(self):
"""Return configuration page name"""
raise NotImplementedError

def get_icon(self):
"""Return configuration page icon (24x24)"""
raise NotImplementedError

def setup_page(self):
"""Setup configuration page widget"""
raise NotImplementedError

def set_modified(self, state):
self.is_modified = state
self.apply_button_enabled.emit(state)

def is_valid(self):
"""Return True if all widget contents are valid"""
raise NotImplementedError

def apply_changes(self):
"""Apply changes callback"""
if self.is_modified:
if self.pre_apply_callback is not None:
self.pre_apply_callback()

self.save_to_conf()

if self.apply_callback is not None:
self.apply_callback()

# Since the language cannot be retrieved by CONF and the language
# is needed before loading CONF, this is an extra method needed to
# ensure that when changes are applied, they are copied to a
# specific file storing the language value. This only applies to
# the main section config.
if self.CONF_SECTION == u'main':
self._save_lang()

for restart_option in self.restart_options:
if restart_option in self.changed_options:
self.prompt_restart_required()
break # Ensure a single popup is displayed
self.set_modified(False)

def load_from_conf(self):
"""Load settings from configuration file"""
raise NotImplementedError

def save_to_conf(self):
"""Save settings to configuration file"""
raise NotImplementedError


class SpyderConfigPage(ConfigPage, ConfigAccessMixin):
"""Plugin configuration dialog box page widget"""
CONF_SECTION = None
MAX_WIDTH = 620
MIN_HEIGHT = 550

def __init__(self, parent):
ConfigPage.__init__(
self,
parent,
apply_callback=lambda: self._apply_settings_tabs(
self.changed_options)
self.apply_callback = lambda: self._apply_settings_tabs(
self.changed_options
)

self.checkboxes = {}
Expand All @@ -171,14 +99,12 @@ def __init__(self, parent):
self.default_button_group = None
self.main = parent.main
self.tabs = None
self.is_modified = False

# Set dimensions
self.setMaximumWidth(self.MAX_WIDTH)
self.setMinimumHeight(self.MIN_HEIGHT)

def sizeHint(self):
"""Default page size."""
return QSize(self.MAX_WIDTH, self.MIN_HEIGHT)
def initialize(self):
"""Initialize configuration page."""
self.setup_page()
self.load_from_conf()

def _apply_settings_tabs(self, options):
if self.tabs is not None:
Expand All @@ -195,13 +121,39 @@ def _apply_settings_tabs(self, options):
def apply_settings(self, options):
raise NotImplementedError

def apply_changes(self):
"""Apply changes callback"""
if self.is_modified:
if self.pre_apply_callback is not None:
self.pre_apply_callback()

self.save_to_conf()

if self.apply_callback is not None:
self.apply_callback()

# Since the language cannot be retrieved by CONF and the language
# is needed before loading CONF, this is an extra method needed to
# ensure that when changes are applied, they are copied to a
# specific file storing the language value. This only applies to
# the main section config.
if self.CONF_SECTION == 'main':
self._save_lang()

for restart_option in self.restart_options:
if restart_option in self.changed_options:
self.prompt_restart_required()
break # Ensure a single popup is displayed
self.set_modified(False)

def check_settings(self):
"""This method is called to check settings after configuration
dialog has been shown"""
pass

def set_modified(self, state):
ConfigPage.set_modified(self, state)
self.is_modified = state
self.apply_button_enabled.emit(state)
if not state:
self.changed_options = set()

Expand Down
Loading

0 comments on commit 582251b

Please sign in to comment.