Skip to content

Commit

Permalink
add a 'selected_tab' attribute to the notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Sep 30, 2013
1 parent b3199f3 commit 45ca092
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
76 changes: 75 additions & 1 deletion enaml/qt/qt_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from weakref import WeakKeyDictionary

from atom.api import Typed
from atom.api import Int, Typed

from enaml.widgets.notebook import ProxyNotebook

Expand Down Expand Up @@ -261,13 +261,20 @@ def setTabsClosable(self, closable):
self._refreshTabBar()


#: A guard flag for the tab change
CHANGE_GUARD = 0x01


class QtNotebook(QtConstraintsWidget, ProxyNotebook):
""" A Qt implementation of an Enaml ProxyNotebook.
"""
#: A reference to the widget created by the proxy.
widget = Typed(QNotebook)

#: A bitfield of guard flags for the object.
_guard = Int(0)

#--------------------------------------------------------------------------
# Initialization API
#--------------------------------------------------------------------------
Expand All @@ -293,6 +300,7 @@ def init_widget(self):
self.set_tab_position(d.tab_position)
self.set_tabs_closable(d.tabs_closable)
self.set_tabs_movable(d.tabs_movable)
# the selected tab is synchronized during init_layout

def init_layout(self):
""" Handle the layout initialization for the notebook.
Expand All @@ -302,7 +310,9 @@ def init_layout(self):
widget = self.widget
for page in self.pages():
widget.addPage(page)
self.init_selected_tab()
widget.layoutRequested.connect(self.on_layout_requested)
widget.currentChanged.connect(self.on_current_changed)

#--------------------------------------------------------------------------
# Utility Methods
Expand All @@ -316,6 +326,40 @@ def pages(self):
if w is not None:
yield w

def find_page(self, name):
""" Find the page with the given name.
Parameters
----------
name : unicode
The object name for the page of interest.
Returns
-------
result : QPage or None
The target page or None if one is not found.
"""
for page in self.pages():
if page.objectName() == name:
return page

def init_selected_tab(self):
""" Initialize the selected tab.
This should be called only during widget initialization.
"""
d = self.declaration
if d.selected_tab:
self.set_selected_tab(d.selected_tab)
else:
self._guard |= CHANGE_GUARD
try:
d.selected_tab = self.widget.currentWidget().objectName()
finally:
self._guard &= ~CHANGE_GUARD

#--------------------------------------------------------------------------
# Child Events
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -346,6 +390,19 @@ def on_layout_requested(self):
"""
self.size_hint_updated()

def on_current_changed(self):
""" Handle the 'currentChanged' signal from the QNotebook.
"""
if not self._guard & CHANGE_GUARD:
self._guard |= CHANGE_GUARD
try:
page = self.widget.currentWidget()
name = page.objectName() if page is not None else u''
self.declaration.selected_tab = name
finally:
self._guard &= ~CHANGE_GUARD

#--------------------------------------------------------------------------
# ProxyNotebook API
#--------------------------------------------------------------------------
Expand All @@ -372,3 +429,20 @@ def set_tabs_movable(self, movable):
"""
self.widget.setMovable(movable)

def set_selected_tab(self, name):
""" Set the selected tab of the widget.
"""
if not self._guard & CHANGE_GUARD:
page = self.find_page(name)
if page is None:
import warnings
msg = "cannot select tab '%s' - tab not found"
warnings.warn(msg % name, UserWarning)
return
self._guard |= CHANGE_GUARD
try:
self.widget.setCurrentWidget(page)
finally:
self._guard &= ~CHANGE_GUARD
13 changes: 11 additions & 2 deletions enaml/widgets/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Enum, Bool, Typed, ForwardTyped, observe, set_default
from atom.api import (
Enum, Bool, Typed, ForwardTyped, Unicode, observe, set_default
)

from enaml.core.declarative import d_

Expand All @@ -32,6 +34,9 @@ def set_tabs_closable(self, closable):
def set_tabs_movable(self, movable):
raise NotImplementedError

def set_selected_tab(self, name):
raise NotImplementedError


class Notebook(ConstraintsWidget):
""" A component which displays its children as tabbed pages.
Expand All @@ -52,6 +57,9 @@ class Notebook(ConstraintsWidget):
#: Whether or not the tabs in the notebook should be movable.
tabs_movable = d_(Bool(True))

#: The object name for the selected tab in the notebook.
selected_tab = d_(Unicode())

#: A notebook expands freely in height and width by default.
hug_width = set_default('ignore')
hug_height = set_default('ignore')
Expand All @@ -68,7 +76,8 @@ def pages(self):
#--------------------------------------------------------------------------
# Observers
#--------------------------------------------------------------------------
@observe('tab_style', 'tab_position', 'tabs_closable', 'tabs_movable')
@observe('tab_style', 'tab_position', 'tabs_closable', 'tabs_movable',
'selected_tab')
def _update_proxy(self, change):
""" Send the state change to the proxy.
Expand Down

0 comments on commit 45ca092

Please sign in to comment.