Skip to content

Commit

Permalink
Add SpyderFontMixin to easily get the different fonts used in Spyder
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed May 21, 2023
1 parent 6014598 commit 491c044
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 60 deletions.
27 changes: 27 additions & 0 deletions spyder/api/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

# Third party imports
from qtpy.QtCore import Qt
from qtpy.QtGui import QFont
from qtpy.QtWidgets import QSizePolicy, QToolBar, QWidget, QToolButton

# Local imports
from spyder.api.config.mixins import SpyderConfigurationObserver
from spyder.api.exceptions import SpyderAPIError
from spyder.api.widgets.menus import SpyderMenu
from spyder.config.gui import get_font
from spyder.config.fonts import SpyderFontType
from spyder.config.manager import CONF
from spyder.utils.icon_manager import ima
from spyder.utils.qthelpers import create_action, create_toolbutton
Expand Down Expand Up @@ -609,3 +612,27 @@ def update_translation(self):
change.
"""
pass


class SpyderFontMixin:
"""Mixin to get the different Spyder font types."""

@classmethod
def get_font(
cls,
font_type: Optional[str] = SpyderFontType.Plain,
font_size_delta: Optional[int] = 0
) -> QFont:
"""
Get a font type as a QFont object.
Parameters
----------
font_type: str, optional
A Spyder font type. This must be one of the `SpyderFontType` enum
values. The default is `SpyderFontType.Plain`.
font_size_delta: int, optional
Small increase or decrease over the default font size. The default
is 0.
"""
return get_font(option=font_type, font_size_delta=font_size_delta)
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
QSpinBox, QTableView, QVBoxLayout)

# Local imports
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _
from spyder.config.gui import get_font
from spyder.plugins.completion.api import SUPPORTED_LANGUAGES
from spyder.utils.misc import check_connection_port
from spyder.utils.programs import find_program
Expand Down Expand Up @@ -107,7 +107,7 @@ def delete(self):
self.remove_option(language)


class LSPServerEditor(QDialog):
class LSPServerEditor(QDialog, SpyderFontMixin):
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 2084
DEFAULT_CMD = ''
Expand Down Expand Up @@ -208,7 +208,7 @@ def __init__(self, parent, language=None, cmd='', host='127.0.0.1',
color_scheme=get_option('selected', section='appearance'),
wrap=False,
highlight_current_line=True,
font=get_font()
font=self.get_font()
)
self.conf_input.set_language('json')
self.conf_input.setToolTip(_('Additional LSP server configuration '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
QLineEdit, QTableView, QVBoxLayout)

# Local imports
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _
from spyder.config.manager import CONF
from spyder.config.gui import get_font
from spyder.plugins.completion.api import SUPPORTED_LANGUAGES
from spyder.utils.snippets.ast import build_snippet_ast
from spyder.widgets.helperwidgets import ItemDelegate
Expand Down Expand Up @@ -215,7 +214,7 @@ def delete(self):
recursive_notification=False)


class SnippetEditor(QDialog):
class SnippetEditor(QDialog, SpyderFontMixin):
SNIPPET_VALID = _('Valid snippet')
SNIPPET_INVALID = _('Invalid snippet')
INVALID_CB_CSS = "QComboBox {border: 1px solid red;}"
Expand Down Expand Up @@ -305,7 +304,7 @@ def __init__(self, parent, language=None, trigger_text='', description='',
color_scheme=get_option('selected', section='appearance'),
wrap=False,
highlight_current_line=True,
font=get_font()
font=self.get_font()
)
self.snippet_input.set_language(language)
self.snippet_input.setToolTip(_('Snippet text completion to insert'))
Expand Down
8 changes: 4 additions & 4 deletions spyder/plugins/debugger/widgets/framesbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
# Local imports
from spyder.api.config.decorators import on_conf_change
from spyder.api.config.mixins import SpyderConfigurationAccessor
from spyder.api.widgets.mixins import SpyderWidgetMixin
from spyder.api.widgets.mixins import SpyderFontMixin, SpyderWidgetMixin
from spyder.api.translations import _
from spyder.config.gui import get_font
from spyder.widgets.helperwidgets import FinderWidget


Expand Down Expand Up @@ -454,15 +453,16 @@ def sizeHint(self, option, index):
return size


class ResultsBrowser(QTreeWidget, SpyderConfigurationAccessor):
class ResultsBrowser(QTreeWidget, SpyderConfigurationAccessor,
SpyderFontMixin):
CONF_SECTION = 'debugger'
sig_edit_goto = Signal(str, int, str)
sig_activated = Signal(int)
sig_show_namespace = Signal(dict)

def __init__(self, parent, color_scheme):
super().__init__(parent)
self.font = get_font()
self.font = self.get_font()
self.data = None
self.threads = None
self.color_scheme = color_scheme
Expand Down
6 changes: 3 additions & 3 deletions spyder/plugins/editor/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# Local imports
from spyder.api.config.mixins import SpyderConfigurationAccessor
from spyder.config.gui import get_font
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.plugins.editor.api.decoration import TextDecoration, DRAW_ORDERS
from spyder.plugins.editor.utils.decoration import TextDecorationsManager
from spyder.plugins.editor.widgets.completion import CompletionWidget
Expand All @@ -37,7 +37,7 @@


class TextEditBaseWidget(
QPlainTextEdit, BaseEditMixin, SpyderConfigurationAccessor
QPlainTextEdit, BaseEditMixin, SpyderConfigurationAccessor, SpyderFontMixin
):
"""Text edit base widget"""
BRACE_MATCHING_SCOPE = ('sof', 'eof')
Expand Down Expand Up @@ -124,7 +124,7 @@ def reset_current_cell():

def setup_completion(self):
size = self.get_conf('completion/size', section='main')
font = get_font()
font = self.get_font()
self.completion_widget.setup_appearance(size, font)

def set_indent_chars(self, indent_chars):
Expand Down
8 changes: 4 additions & 4 deletions spyder/plugins/findinfiles/widgets/results_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# Local imports
from spyder.api.translations import _
from spyder.config.gui import get_font
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.plugins.findinfiles.widgets.search_thread import (
ELLIPSIS, MAX_RESULT_LENGTH)
from spyder.utils import icon_manager as ima
Expand Down Expand Up @@ -176,7 +176,7 @@ def sizeHint(self, option, index):
return size


class ResultsBrowser(OneColumnTree):
class ResultsBrowser(OneColumnTree, SpyderFontMixin):
sig_edit_goto_requested = Signal(str, int, str, int, int)
sig_max_results_reached = Signal()

Expand All @@ -189,7 +189,7 @@ def __init__(self, parent, text_color, max_results=1000):
self.error_flag = None
self.completed = None
self.sorting = {}
self.font = get_font()
self.font = self.get_font()
self.data = None
self.files = None
self.root_items = None
Expand Down Expand Up @@ -241,7 +241,7 @@ def clicked(self, item):
self.activated(item)

def clear_title(self, search_text):
self.font = get_font()
self.font = self.get_font()
self.clear()
self.setSortingEnabled(False)
self.num_files = 0
Expand Down
11 changes: 7 additions & 4 deletions spyder/plugins/onlinehelp/pydoc_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import warnings

# Local imports
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _, DEV
from spyder.config.fonts import SpyderFontType
from spyder.config.gui import is_dark_interface, get_font
from spyder.config.gui import is_dark_interface
from spyder.py3compat import to_text_string


Expand Down Expand Up @@ -628,12 +629,14 @@ def _url_handler(url, content_type="text/html"):
See https://github.com/python/cpython/blob/master/Lib/pydoc.py
"""
class _HTMLDoc(CustomHTMLDoc):
class _HTMLDoc(CustomHTMLDoc, SpyderFontMixin):

def page(self, title, contents):
"""Format an HTML page."""
rich_text_font = get_font(option=SpyderFontType.Rich).family()
plain_text_font = get_font(option=SpyderFontType.Plain).family()
rich_text_font = self.get_font(
font_type=SpyderFontType.Rich).family()
plain_text_font = self.get_font(
font_type=SpyderFontType.Plain).family()

if is_dark_interface():
css_path = "static/css/dark_pydoc.css"
Expand Down
12 changes: 8 additions & 4 deletions spyder/plugins/variableexplorer/widgets/arrayeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from spyder_kernels.utils.lazymodules import numpy as np

# Local imports
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _
from spyder.config.fonts import DEFAULT_SMALL_DELTA
from spyder.config.gui import get_font
from spyder.config.manager import CONF
from spyder.py3compat import (is_binary_string, is_string, is_text_string,
to_binary_string, to_text_string)
Expand Down Expand Up @@ -307,7 +307,9 @@ def data(self, index, role=Qt.DisplayRole):
except (TypeError, ValueError):
return to_qvariant()
elif role == Qt.FontRole:
return to_qvariant(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
return to_qvariant(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
return to_qvariant()

def setData(self, index, value, role=Qt.EditRole):
Expand Down Expand Up @@ -384,7 +386,7 @@ def reset(self):
self.endResetModel()


class ArrayDelegate(QItemDelegate):
class ArrayDelegate(QItemDelegate, SpyderFontMixin):
"""Array Editor Item Delegate"""
def __init__(self, dtype, parent=None):
QItemDelegate.__init__(self, parent)
Expand All @@ -403,7 +405,9 @@ def createEditor(self, parent, option, index):
return
elif value is not np.ma.masked:
editor = QLineEdit(parent)
editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
editor.setFont(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
editor.setAlignment(Qt.AlignCenter)
if is_number(self.dtype):
validator = QDoubleValidator(editor)
Expand Down
20 changes: 14 additions & 6 deletions spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
is_known_type)

# Local imports
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _, is_conda_based_app
from spyder.config.fonts import DEFAULT_SMALL_DELTA
from spyder.config.gui import get_font
from spyder.py3compat import is_binary_string, is_text_string, to_text_string
from spyder.plugins.variableexplorer.widgets.arrayeditor import ArrayEditor
from spyder.plugins.variableexplorer.widgets.dataframeeditor import (
Expand All @@ -38,7 +38,7 @@
LARGE_ARRAY = 5e6


class CollectionsDelegate(QItemDelegate):
class CollectionsDelegate(QItemDelegate, SpyderFontMixin):
"""CollectionsEditor Item Delegate"""
sig_free_memory_requested = Signal()
sig_editor_creation_started = Signal()
Expand Down Expand Up @@ -229,7 +229,9 @@ def createEditor(self, parent, option, index, object_explorer=False):
else:
editor = QDateEdit(value, parent=parent)
editor.setCalendarPopup(True)
editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
editor.setFont(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
self.sig_editor_shown.emit()
return editor
# TextEditor for a long string
Expand All @@ -249,7 +251,9 @@ def createEditor(self, parent, option, index, object_explorer=False):
return None
else:
editor = QLineEdit(parent=parent)
editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
editor.setFont(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
editor.setAlignment(Qt.AlignLeft)
# This is making Spyder crash because the QLineEdit that it's
# been modified is removed and a new one is created after
Expand Down Expand Up @@ -510,7 +514,9 @@ def createEditor(self, parent, option, index):
else:
editor = QDateEdit(value, parent=parent)
editor.setCalendarPopup(True)
editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
editor.setFont(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
return editor
# TextEditor for a long string
elif is_text_string(value) and len(value) > 40:
Expand All @@ -528,7 +534,9 @@ def createEditor(self, parent, option, index):
return None
else:
editor = QLineEdit(parent=parent)
editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
editor.setFont(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
editor.setAlignment(Qt.AlignLeft)
# This is making Spyder crash because the QLineEdit that it's
# been modified is removed and a new one is created after
Expand Down
8 changes: 5 additions & 3 deletions spyder/plugins/variableexplorer/widgets/dataframeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@

# Local imports
from spyder.api.config.mixins import SpyderConfigurationAccessor
from spyder.api.widgets.mixins import SpyderFontMixin
from spyder.config.base import _
from spyder.config.fonts import DEFAULT_SMALL_DELTA
from spyder.config.gui import get_font
from spyder.py3compat import (is_text_string, is_type_text_string,
to_text_string)
from spyder.utils.icon_manager import ima
Expand Down Expand Up @@ -108,7 +108,7 @@ def global_max(col_vals, index):
return max(max_col), min(min_col)


class DataFrameModel(QAbstractTableModel):
class DataFrameModel(QAbstractTableModel, SpyderFontMixin):
"""
DataFrame Table Model.
Expand Down Expand Up @@ -375,7 +375,9 @@ def data(self, index, role=Qt.DisplayRole):
elif role == Qt.BackgroundColorRole:
return to_qvariant(self.get_bgcolor(index))
elif role == Qt.FontRole:
return to_qvariant(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
return to_qvariant(
self.get_font(font_size_delta=DEFAULT_SMALL_DELTA)
)
elif role == Qt.ToolTipRole:
if index in self.display_error_idxs:
return _("It is not possible to display this value because\n"
Expand Down
Loading

0 comments on commit 491c044

Please sign in to comment.