Skip to content

Commit

Permalink
Stylesheet: Create a new class for tab bars used in Preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Oct 25, 2023
1 parent 66700f0 commit 69fa4ba
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
31 changes: 4 additions & 27 deletions spyder/plugins/preferences/widgets/configdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from spyder.utils.icon_manager import ima
from spyder.utils.palette import QStylePalette
from spyder.utils.stylesheet import (
AppStyle, MAC, SPECIAL_TABBAR_STYLESHEET, WIN)
AppStyle, MAC, PREFERENCES_TABBAR_STYLESHEET, WIN)


class PageScrollArea(QScrollArea):
Expand Down Expand Up @@ -396,34 +396,11 @@ def _adjust_separators_width(self):
sep.setFixedWidth(204)

def _generate_stylesheet(self):
"""Generate stylesheet for this widget as qstylizer object."""
# Use special tabbar stylesheet for as the base one and then extend it.
tabs_stylesheet = SPECIAL_TABBAR_STYLESHEET.get_copy()
"""Generate stylesheet for this widget as a qstylizer object."""
# Use the tabbar stylesheet as the base one and extend it.
tabs_stylesheet = PREFERENCES_TABBAR_STYLESHEET.get_copy()
css = tabs_stylesheet.get_stylesheet()

# Set tabs font size to be the same as the one for items
css.QTabBar.setValues(
fontSize=f'{self.items_font.pointSize()}pt',
)

# Make tabbar scroll button a bit bigger on Windows and Mac (this has
# no effect on Linux).
if WIN or MAC:
css['QTabBar QToolButton'].setValues(
padding=f'{tabs_stylesheet.SCROLL_BUTTONS_PADDING - 1}px',
)

# Increase padding around text a bit because we're using an larger font
css['QTabBar::tab'].setValues(
padding='6px 10px',
)

# Remove border and add padding for content inside tabs
css['QTabWidget::pane'].setValues(
border='0px',
padding='15px',
)

# Set style of contents area
css['QListView#configdialog-contents'].setValues(
padding=f'{self.ITEMS_MARGIN}px 0px',
Expand Down
95 changes: 75 additions & 20 deletions spyder/utils/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,27 @@ class AppStyle:
class SpyderStyleSheet:
"""Base class for Spyder stylesheets."""

def __init__(self, set_stylesheet=True):
SET_STYLESHEET_AT_INIT = True
"""
Decide if the stylesheet must be set when the class is initialized.
Notes
-----
There are some stylesheets for which this is not possible (e.g. the ones
that need to access our fonts).
"""

def __init__(self):
self._stylesheet = qstylizer.style.StyleSheet()
if set_stylesheet:
if self.SET_STYLESHEET_AT_INIT:
self.set_stylesheet()

def get_stylesheet(self):
return self._stylesheet

def to_string(self):
if self._stylesheet.toString() == "":
self.set_stylesheet()
return self._stylesheet.toString()

def get_copy(self):
Expand All @@ -74,6 +86,8 @@ def get_copy(self):
This allows it to be modified for specific widgets.
"""
if self._stylesheet.toString() == "":
self.set_stylesheet()
return copy.deepcopy(self)

def set_stylesheet(self):
Expand All @@ -99,17 +113,19 @@ class AppStylesheet(SpyderStyleSheet, SpyderConfigurationAccessor):
application.
"""

# Don't create the stylesheet here so that Spyder gets the app font from
# the system when it starts for the first time. This also allows us to
# display the splash screen more quickly because the stylesheet is then
# computed only when it's going to be applied to the app, not when this
# object is imported.
SET_STYLESHEET_AT_INIT = False

def __init__(self):
# Don't create the stylesheet here so that Spyder gets the app font
# from the system when it starts for the first time. This also allows
# us to display the splash screen more quickly because the stylesheet
# is then computed only when it's going to be applied to the app, not
# when this object is imported.
super().__init__(set_stylesheet=False)
super().__init__()
self._stylesheet_as_string = None

def to_string(self):
"Save stylesheet as a string for quick access."
"""Save stylesheet as a string for quick access."""
if self._stylesheet_as_string is None:
self.set_stylesheet()
self._stylesheet_as_string = self._stylesheet.toString()
Expand Down Expand Up @@ -280,7 +296,7 @@ class ApplicationToolbarStylesheet(SpyderStyleSheet):
BUTTON_MARGIN_RIGHT = '3px'

def set_stylesheet(self):
css = self._stylesheet
css = self.get_stylesheet()

# Main background color
css.QToolBar.setValues(
Expand Down Expand Up @@ -320,7 +336,7 @@ class PanesToolbarStyleSheet(SpyderStyleSheet):
BUTTON_HEIGHT = '37px'

def set_stylesheet(self):
css = self._stylesheet
css = self.get_stylesheet()

css.QToolBar.setValues(
spacing='4px'
Expand Down Expand Up @@ -531,7 +547,14 @@ def set_stylesheet(self):


class SpecialTabBarStyleSheet(BaseDockTabBarStyleSheet):
"""Style for special horizontal tabbars."""
"""
Style for special tab bars.
Notes
-----
This is the base class for horizontal tab bars that follow the design
discussed on issue spyder-ide/ux-improvements#4.
"""

SCROLL_BUTTONS_BORDER_POS = 'right'

Expand Down Expand Up @@ -599,11 +622,45 @@ def set_stylesheet(self):
)


class PreferencesTabBarStyleSheet(SpecialTabBarStyleSheet, SpyderFontsMixin):
"""Style for tab bars in our Preferences dialog."""

# This is necessary because this class needs to access fonts
SET_STYLESHEET_AT_INIT = False

def set_stylesheet(self):
super().set_stylesheet()

# Main constants
css = self.get_stylesheet()
font = self.get_font(SpyderFontType.Interface, font_size_delta=1)

# Set font size to be one point bigger than the regular text.
css.QTabBar.setValues(
fontSize=f'{font.pointSize()}pt',
)

# Make scroll buttons a bit bigger on Windows and Mac (this has no
# effect on Linux).
if WIN or MAC:
css['QTabBar QToolButton'].setValues(
padding=f'{self.SCROLL_BUTTONS_PADDING - 1}px',
)

# Increase padding around text because we're using a larger font.
css['QTabBar::tab'].setValues(
padding='6px 10px',
)

# Remove border and add padding for content inside tabs
css['QTabWidget::pane'].setValues(
border='0px',
padding='15px',
)


class HorizontalDockTabBarStyleSheet(SpecialTabBarStyleSheet):
"""
This implements the design for dockwidget tabs discussed on issue
spyder-ide/ux-improvements#4.
"""
"""Style for horizontal dockwidget tab bars."""

def set_stylesheet(self):
super().set_stylesheet()
Expand Down Expand Up @@ -646,9 +703,7 @@ def set_stylesheet(self):


class VerticalDockTabBarStyleSheet(BaseDockTabBarStyleSheet):
"""
Vertical implementation for the design on spyder-ide/ux-improvements#4.
"""
"""Style for vertical dockwidget tab bars."""

SCROLL_BUTTONS_BORDER_POS = 'bottom'

Expand Down Expand Up @@ -723,7 +778,7 @@ def set_stylesheet(self):
PANES_TABBAR_STYLESHEET = PanesTabBarStyleSheet()
HORIZONTAL_DOCK_TABBAR_STYLESHEET = HorizontalDockTabBarStyleSheet()
VERTICAL_DOCK_TABBAR_STYLESHEET = VerticalDockTabBarStyleSheet()
SPECIAL_TABBAR_STYLESHEET = SpecialTabBarStyleSheet()
PREFERENCES_TABBAR_STYLESHEET = PreferencesTabBarStyleSheet()


# =============================================================================
Expand Down

0 comments on commit 69fa4ba

Please sign in to comment.