<s> from atom . api import Typed <EOL> from enaml . styling import StyleCache <EOL> from enaml . widgets . dock_area import ProxyDockArea <EOL> from enaml . widgets . dock_events import DockItemEvent <EOL> from . QtCore import QObject , QEvent , QSize , QTimer <EOL> from . QtGui import QTabWidget <EOL> from . docking . dock_manager import DockManager <EOL> from . docking . event_types import ( <EOL> DockItemDocked , DockItemUndocked , DockItemExtended , DockItemRetracted , <EOL> DockItemShown , DockItemHidden , DockItemClosed , DockTabSelected <EOL> ) <EOL> from . docking . q_dock_area import QDockArea <EOL> from . docking . style_sheets import get_style_sheet <EOL> from . qt_constraints_widget import QtConstraintsWidget <EOL> from . qt_dock_item import QtDockItem <EOL> from . styleutil import translate_dock_area_style <EOL> TAB_POSITIONS = { <EOL> '<STR_LIT>' : QTabWidget . North , <EOL> '<STR_LIT>' : QTabWidget . South , <EOL> '<STR_LIT:left>' : QTabWidget . West , <EOL> '<STR_LIT:right>' : QTabWidget . East , <EOL> } <EOL> EVENT_TYPES = { <EOL> DockItemDocked : DockItemEvent . Docked , <EOL> DockItemUndocked : DockItemEvent . Undocked , <EOL> DockItemExtended : DockItemEvent . Extended , <EOL> DockItemRetracted : DockItemEvent . Retracted , <EOL> DockItemShown : DockItemEvent . Shown , <EOL> DockItemHidden : DockItemEvent . Hidden , <EOL> DockItemClosed : DockItemEvent . Closed , <EOL> DockTabSelected : DockItemEvent . TabSelected , <EOL> } <EOL> class DockLayoutFilter ( QObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , owner ) : <EOL> super ( DockLayoutFilter , self ) . __init__ ( ) <EOL> self . _owner = owner <EOL> self . _size_hint = QSize ( ) <EOL> self . _pending = False <EOL> self . _timer = timer = QTimer ( ) <EOL> timer . setSingleShot ( True ) <EOL> timer . timeout . connect ( self . onNotify ) <EOL> def onNotify ( self ) : <EOL> self . _owner . geometry_updated ( ) <EOL> self . _pending = False <EOL> def eventFilter ( self , obj , event ) : <EOL> if not self . _pending and event . type ( ) == QEvent . LayoutRequest : <EOL> hint = obj . sizeHint ( ) <EOL> if hint != self . _size_hint : <EOL> self . _size_hint = hint <EOL> self . _timer . start ( <NUM_LIT:0> ) <EOL> self . _pending = True <EOL> return False <EOL> class DockEventFilter ( QObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , owner ) : <EOL> super ( DockEventFilter , self ) . __init__ ( ) <EOL> self . _owner = owner <EOL> def eventFilter ( self , obj , event ) : <EOL> e_type = EVENT_TYPES . get ( event . type ( ) ) <EOL> if e_type is not None : <EOL> d = self . _owner . declaration <EOL> if d is not None : <EOL> d . dock_event ( DockItemEvent ( type = e_type , name = event . name ( ) ) ) <EOL> return False <EOL> class QtDockArea ( QtConstraintsWidget , ProxyDockArea ) : <EOL> """<STR_LIT>""" <EOL> widget = Typed ( QDockArea ) <EOL> manager = Typed ( DockManager ) <EOL> dock_layout_filter = Typed ( DockLayoutFilter ) <EOL> dock_event_filter = Typed ( DockEventFilter ) <EOL> def create_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> self . widget = QDockArea ( self . parent_widget ( ) ) <EOL> self . manager = DockManager ( self . widget ) <EOL> self . dock_event_filter = DockEventFilter ( self ) <EOL> self . dock_layout_filter = DockLayoutFilter ( self ) <EOL> def init_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QtDockArea , self ) . init_widget ( ) <EOL> d = self . declaration <EOL> self . set_tab_position ( d . tab_position ) <EOL> self . set_live_drag ( d . live_drag ) <EOL> if d . style : <EOL> self . set_style ( d . style ) <EOL> self . set_dock_events_enabled ( d . dock_events_enabled ) <EOL> def init_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QtDockArea , self ) . init_layout ( ) <EOL> manager = self . manager <EOL> for item in self . dock_items ( ) : <EOL> manager . add_item ( item ) <EOL> d = self . declaration <EOL> self . apply_layout ( d . layout ) <EOL> self . widget . installEventFilter ( self . dock_layout_filter ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> self . widget . removeEventFilter ( self . dock_layout_filter ) <EOL> self . widget . removeEventFilter ( self . dock_event_filter ) <EOL> del self . dock_layout_filter <EOL> del self . dock_event_filter <EOL> self . manager . destroy ( ) <EOL> super ( QtDockArea , self ) . destroy ( ) <EOL> def refresh_style_sheet ( self ) : <EOL> """<STR_LIT>""" <EOL> parts = [ u'<STR_LIT>' ] <EOL> name = self . widget . objectName ( ) <EOL> for style in StyleCache . styles ( self . declaration ) : <EOL> t = translate_dock_area_style ( name , style ) <EOL> if t : <EOL> parts . append ( t ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> stylesheet = u'<STR_LIT>' . join ( parts ) <EOL> else : <EOL> stylesheet = u'<STR_LIT>' <EOL> self . widget . setStyleSheet ( stylesheet ) <EOL> def dock_items ( self ) : <EOL> """<STR_LIT>""" <EOL> for d in self . declaration . dock_items ( ) : <EOL> w = d . proxy . widget <EOL> if w is not None : <EOL> yield w <EOL> def child_added ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( QtDockArea , self ) . child_added ( child ) <EOL> if isinstance ( child , QtDockItem ) : <EOL> w = child . widget <EOL> if w is not None : <EOL> self . manager . add_item ( w ) <EOL> def child_removed ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( QtDockArea , self ) . child_removed ( child ) <EOL> if isinstance ( child , QtDockItem ) : <EOL> w = child . widget <EOL> if w is not None : <EOL> self . manager . remove_item ( w ) <EOL> def set_tab_position ( self , position ) : <EOL> """<STR_LIT>""" <EOL> self . widget . setTabPosition ( TAB_POSITIONS [ position ] ) <EOL> def set_live_drag ( self , live_drag ) : <EOL> """<STR_LIT>""" <EOL> self . widget . setOpaqueItemResize ( live_drag ) <EOL> def set_style ( self , style ) : <EOL> """<STR_LIT>""" <EOL> sheet = get_style_sheet ( style ) <EOL> if sheet : <EOL> self . widget . setStyleSheet ( sheet ) <EOL> def set_dock_events_enabled ( self , enabled ) : <EOL> """<STR_LIT>""" <EOL> widget = self . widget <EOL> widget . setDockEventsEnabled ( enabled ) <EOL> if enabled : <EOL> widget . installEventFilter ( self . dock_event_filter ) <EOL> else : <EOL> widget . removeEventFilter ( self . dock_event_filter ) <EOL> def save_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . manager . save_layout ( ) <EOL> def apply_layout ( self , layout ) : <EOL> """<STR_LIT>""" <EOL> self . manager . apply_layout ( layout ) <EOL> def update_layout ( self , ops ) : <EOL> """<STR_LIT>""" <EOL> self . manager . update_layout ( ops ) </s>
