Skip to content

Commit

Permalink
remove the style sheets from user control.
Browse files Browse the repository at this point in the history
expose a backend registration API for the adventurous.
  • Loading branch information
sccolbert committed May 28, 2013
1 parent cd90fa4 commit 947760e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,61 @@
#------------------------------------------------------------------------------


#: The global dict of registered stylesheets. This dict should never be
#: modified directly. Use the functions 'register_style_sheet' and
#: 'get_style_sheet' instead.
_style_sheets = {}


def get_style_sheet(name):
""" Get the style sheet for the given style id.
Parameters
----------
name : basestring
The string name of the style previously registered with the
'register_style_sheet' function.
Returns
-------
result : unicode
The unicode stylesheet for the style name, or an empty string
if the style is not found.
"""
return _style_sheets.get(name, u'')


def register_style_sheet(name, sheet):
""" Register a dock area style sheet.
Parameters
----------
name : basestring
The name to associate with the stylesheet for later lookup.
sheet : unicode
A unicode string containing the style sheet for the dock area.
Raises
------
ValueError
If the given name is already registered, a value error will be
raised.
"""
assert isinstance(sheet, unicode), 'style sheet must a unicode string'
if name in _style_sheets:
raise ValueError("'%s' style is already registered" % name)
_style_sheets[name] = sheet


#: A null stylesheet.
register_style_sheet(u'', u'')


#: A stylesheet inspired by Visual Studio 2010
VS_2010_STYLE = u"""
register_style_sheet(u'vs-2010', u"""
QDockArea {
padding: 5px;
background: rgb(49, 67, 98);
Expand Down Expand Up @@ -125,11 +178,12 @@
QBitmapButton#docktab-close-button:selected {
color: black;
}
"""
""")


#: A mild grey and brown stylesheet.
#: Inspired by http://www.colourlovers.com/palette/2866138/Grey_Wind
GREY_WIND_STYLE = u"""
register_style_sheet(u'grey-wind', u"""
QDockArea {
padding: 5px;
background: rgb(175, 178, 183);
Expand Down Expand Up @@ -247,12 +301,12 @@
QBitmapButton#dockwindow-restore-button:pressed {
background: rgb(144, 144, 152);
}
"""
""")


#: A yellow, brown, and grey stylesheet.
#: Inspired by http://www.colourlovers.com/palette/90734/Newly_Risen_Moon
NEW_MOON_STYLE = u"""
register_style_sheet(u'new-moon', u"""
QDockArea {
padding: 5px;
background: rgb(54, 57, 59);
Expand Down Expand Up @@ -370,11 +424,11 @@
QBitmapButton#dockwindow-restore-button:pressed {
background: rgb(105, 103, 88);
}
"""
""")


#: A stylesheet inspired by Windows Metro.
METRO_STYLE = u"""
register_style_sheet(u'metro', u"""
QDockArea {
padding: 5px;
background: #C0C0C0;
Expand Down Expand Up @@ -490,4 +544,4 @@
QBitmapButton#dockwindow-restore-button:pressed {
background: #3D6099;
}
"""
""")
11 changes: 6 additions & 5 deletions enaml/qt/qt_dock_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .docking.dock_manager import DockManager
from .docking.q_dock_area import QDockArea
from .docking.style_sheets import get_style_sheet
from .qt_constraints_widget import QtConstraintsWidget
from .qt_dock_item import QtDockItem

Expand Down Expand Up @@ -91,8 +92,8 @@ def init_widget(self):
super(QtDockArea, self).init_widget()
d = self.declaration
self.set_tab_position(d.tab_position)
if d.style_sheet:
self.set_style_sheet(d.style_sheet)
if d.style:
self.set_style(d.style)

def init_layout(self):
""" Initialize the layout of the underlying control.
Expand Down Expand Up @@ -162,11 +163,11 @@ def set_tab_position(self, position):
"""
self.widget.setTabPosition(TAB_POSITIONS[position])

def set_style_sheet(self, style_sheet):
""" Set the style sheet for the underlying widget.
def set_style(self, style):
""" Set the style for the underlying widget.
"""
self.widget.setStyleSheet(style_sheet)
self.widget.setStyleSheet(get_style_sheet(style))

def save_layout(self):
""" Save the current layout on the underlying widget.
Expand Down
3 changes: 0 additions & 3 deletions enaml/widgets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
from .date_selector import DateSelector
from .datetime_selector import DatetimeSelector
from .dock_area import DockArea
from .dock_area_styles import (
VS_2010_STYLE, GREY_WIND_STYLE, NEW_MOON_STYLE, METRO_STYLE
)
from .dock_item import DockItem
from .dock_pane import DockPane
from .dual_slider import DualSlider
Expand Down
10 changes: 5 additions & 5 deletions enaml/widgets/dock_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)

from .constraints_widget import ConstraintsWidget, ProxyConstraintsWidget
from .dock_area_styles import VS_2010_STYLE
from .dock_item import DockItem


Expand Down Expand Up @@ -50,7 +49,7 @@ class ProxyDockArea(ProxyConstraintsWidget):
def set_tab_position(self, position):
raise NotImplementedError

def set_style_sheet(self, style_sheet):
def set_style(self, style):
raise NotImplementedError

def save_layout(self):
Expand Down Expand Up @@ -78,8 +77,9 @@ class DockArea(ConstraintsWidget):
tab_position = d_(Enum('top', 'bottom', 'left', 'right'))

#: The style to apply to the dock area. The default style resembles
#: Visual Studio 2010.
style_sheet = d_(Unicode(VS_2010_STYLE))
#: Visual Studio 2010. The builtin styles are: 'vs-2010', 'grey-wind',
#: 'new-moon', and 'metro'.
style = d_(Unicode('vs-2010'))

#: A Stack expands freely in height and width by default
hug_width = set_default('ignore')
Expand Down Expand Up @@ -204,7 +204,7 @@ def _update_layout(self, change):
if change['type'] == 'update':
self.apply_layout(change['value'])

@observe(('tab_position', 'style_sheet'))
@observe(('tab_position', 'style'))
def _update_proxy(self, change):
""" Update the proxy when the area state changes.
Expand Down
12 changes: 6 additions & 6 deletions examples/widgets/dock_area.enaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ from enaml.layout.api import (
)
from enaml.widgets.api import (
Window, Container, DockArea, DockItem, PushButton, Field, Html, Slider,
ObjectCombo, VS_2010_STYLE, GREY_WIND_STYLE, NEW_MOON_STYLE, METRO_STYLE
ObjectCombo
)


STYLES = {
'VS 2010': VS_2010_STYLE,
'Grey Wind': GREY_WIND_STYLE,
'New Moon': NEW_MOON_STYLE,
'Metro': METRO_STYLE,
'VS 2010': 'vs-2010',
'Grey Wind': 'grey-wind',
'New Moon': 'new-moon',
'Metro': 'metro',
}


Expand Down Expand Up @@ -139,4 +139,4 @@ enamldef Main(Window):
items = STYLES.keys()
selected = 'VS 2010'
MyDockArea: area:
style_sheet << STYLES[style_c.selected]
style << STYLES[style_c.selected]

0 comments on commit 947760e

Please sign in to comment.