<s> from enaml . qt . QtCore import Qt , QRect , QSize , QPoint , QTimer , Signal <EOL> from enaml . qt . QtGui import QApplication , QFrame , QLayout <EOL> from . event_types import ( <EOL> QDockItemEvent , DockItemShown , DockItemHidden , DockItemClosed <EOL> ) <EOL> from . q_dock_tab_widget import QDockTabWidget <EOL> from . q_dock_title_bar import QDockTitleBar <EOL> from . utils import repolish <EOL> class _AlertData ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , timer , level , on , off , repeat , persist ) : <EOL> self . timer = timer <EOL> self . level = level <EOL> self . on = on <EOL> self . off = off <EOL> self . repeat = repeat <EOL> self . persist = persist <EOL> self . remaining = repeat <EOL> self . active = False <EOL> class QDockItemLayout ( QLayout ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent = None ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItemLayout , self ) . __init__ ( parent ) <EOL> self . _size_hint = QSize ( ) <EOL> self . _min_size = QSize ( ) <EOL> self . _max_size = QSize ( ) <EOL> self . _title_bar = None <EOL> self . _dock_widget = None <EOL> def titleBarWidget ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _title_bar <EOL> def setTitleBarWidget ( self , title_bar ) : <EOL> """<STR_LIT>""" <EOL> old_bar = self . _title_bar <EOL> if old_bar is not None : <EOL> old_bar . hide ( ) <EOL> old_bar . setParent ( None ) <EOL> self . _title_bar = title_bar <EOL> if title_bar is not None : <EOL> title_bar . setParent ( self . parentWidget ( ) ) <EOL> self . invalidate ( ) <EOL> def dockWidget ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dock_widget <EOL> def setDockWidget ( self , widget ) : <EOL> """<STR_LIT>""" <EOL> old_widget = self . _dock_widget <EOL> if widget is old_widget : <EOL> return <EOL> if old_widget is not None : <EOL> old_widget . hide ( ) <EOL> old_widget . setParent ( None ) <EOL> self . _dock_widget = widget <EOL> if widget is not None : <EOL> widget . setParent ( self . parentWidget ( ) ) <EOL> self . invalidate ( ) <EOL> def invalidate ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItemLayout , self ) . invalidate ( ) <EOL> self . _size_hint = QSize ( ) <EOL> self . _min_size = QSize ( ) <EOL> self . _max_size = QSize ( ) <EOL> def setGeometry ( self , rect ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItemLayout , self ) . setGeometry ( rect ) <EOL> title = self . _title_bar <EOL> widget = self . _dock_widget <EOL> title_rect = QRect ( rect ) <EOL> widget_rect = QRect ( rect ) <EOL> if title is not None and not title . isHidden ( ) : <EOL> msh = title . minimumSizeHint ( ) <EOL> title_rect . setHeight ( msh . height ( ) ) <EOL> widget_rect . setTop ( title_rect . bottom ( ) + <NUM_LIT:1> ) <EOL> title . setGeometry ( title_rect ) <EOL> if widget is not None and not widget . isHidden ( ) : <EOL> widget . setGeometry ( widget_rect ) <EOL> def sizeHint ( self ) : <EOL> """<STR_LIT>""" <EOL> sh = self . _size_hint <EOL> if not sh . isValid ( ) : <EOL> width = height = <NUM_LIT:0> <EOL> title = self . _title_bar <EOL> widget = self . _dock_widget <EOL> if title is not None and not title . isHidden ( ) : <EOL> hint = title . sizeHint ( ) <EOL> width += hint . width ( ) <EOL> height += hint . height ( ) <EOL> if widget is not None and not widget . isHidden ( ) : <EOL> hint = widget . sizeHint ( ) <EOL> width = max ( width , hint . width ( ) ) <EOL> height += hint . height ( ) <EOL> sh = self . _size_hint = QSize ( width , height ) <EOL> return sh <EOL> def minimumSize ( self ) : <EOL> """<STR_LIT>""" <EOL> ms = self . _min_size <EOL> if not ms . isValid ( ) : <EOL> width = height = <NUM_LIT:0> <EOL> title = self . _title_bar <EOL> widget = self . _dock_widget <EOL> if title is not None and not title . isHidden ( ) : <EOL> hint = title . minimumSizeHint ( ) <EOL> width += hint . width ( ) <EOL> height += hint . height ( ) <EOL> if widget is not None and not widget . isHidden ( ) : <EOL> hint = widget . minimumSizeHint ( ) <EOL> width = max ( width , hint . width ( ) ) <EOL> height += hint . height ( ) <EOL> ms = self . _min_size = QSize ( width , height ) <EOL> return ms <EOL> def maximumSize ( self ) : <EOL> """<STR_LIT>""" <EOL> ms = self . _max_size <EOL> if not ms . isValid ( ) : <EOL> widget = self . _dock_widget <EOL> parent = self . parentWidget ( ) <EOL> if widget is not None and parent . isFloating ( ) : <EOL> ms = widget . maximumSize ( ) <EOL> title = self . _title_bar <EOL> if title is not None and not title . isHidden ( ) : <EOL> height = ms . height ( ) + title . minimumSizeHint ( ) . height ( ) <EOL> ms . setHeight ( min ( <NUM_LIT> , height ) ) <EOL> else : <EOL> ms = QSize ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . _max_size = ms <EOL> return ms <EOL> def addItem ( self , item ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> raise NotImplementedError ( msg ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:0> <EOL> def itemAt ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def takeAt ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> class QDockItem ( QFrame ) : <EOL> """<STR_LIT>""" <EOL> maximizeButtonClicked = Signal ( bool ) <EOL> restoreButtonClicked = Signal ( bool ) <EOL> closeButtonClicked = Signal ( bool ) <EOL> linkButtonToggled = Signal ( bool ) <EOL> pinButtonToggled = Signal ( bool ) <EOL> titleEdited = Signal ( unicode ) <EOL> titleBarLeftDoubleClicked = Signal ( QPoint ) <EOL> titleBarRightClicked = Signal ( QPoint ) <EOL> alerted = Signal ( unicode ) <EOL> def __init__ ( self , parent = None ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItem , self ) . __init__ ( parent ) <EOL> layout = QDockItemLayout ( ) <EOL> layout . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> layout . setSizeConstraint ( QLayout . SetMinAndMaxSize ) <EOL> self . setLayout ( layout ) <EOL> self . setTitleBarWidget ( QDockTitleBar ( ) ) <EOL> self . alerted . connect ( self . _onAlerted ) <EOL> self . _manager = None <EOL> self . _alert_data = None <EOL> self . _vis_changed = None <EOL> self . _closable = True <EOL> self . _closing = False <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _closing = True <EOL> try : <EOL> super ( QDockItem , self ) . close ( ) <EOL> finally : <EOL> self . _closing = False <EOL> def closeEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> event . ignore ( ) <EOL> if self . _closable : <EOL> event . accept ( ) <EOL> area = self . rootDockArea ( ) <EOL> if area is not None and area . dockEventsEnabled ( ) : <EOL> event = QDockItemEvent ( DockItemClosed , self . objectName ( ) ) <EOL> QApplication . postEvent ( area , event ) <EOL> def showEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItem , self ) . showEvent ( event ) <EOL> self . _postVisibilityChange ( True ) <EOL> def hideEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockItem , self ) . hideEvent ( event ) <EOL> if not self . _closing : <EOL> self . _postVisibilityChange ( False ) <EOL> def mousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . button ( ) == Qt . LeftButton : <EOL> self . clearAlert ( ) <EOL> super ( QDockItem , self ) . mousePressEvent ( event ) <EOL> def manager ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _manager <EOL> def rootDockArea ( self ) : <EOL> """<STR_LIT>""" <EOL> manager = self . _manager <EOL> if manager is not None : <EOL> return manager . dock_area ( ) <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . title ( ) <EOL> def setTitle ( self , title ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setTitle ( title ) <EOL> container = self . parent ( ) <EOL> if container is not None : <EOL> stacked = container . parent ( ) <EOL> if stacked is not None : <EOL> tabs = stacked . parent ( ) <EOL> if isinstance ( tabs , QDockTabWidget ) : <EOL> index = tabs . indexOf ( container ) <EOL> tabs . setTabText ( index , title ) <EOL> def icon ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . icon ( ) <EOL> def setIcon ( self , icon ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setIcon ( icon ) <EOL> container = self . parent ( ) <EOL> if container is not None : <EOL> stacked = container . parent ( ) <EOL> if stacked is not None : <EOL> tabs = stacked . parent ( ) <EOL> if isinstance ( tabs , QDockTabWidget ) : <EOL> index = tabs . indexOf ( container ) <EOL> tabs . setTabIcon ( index , icon ) <EOL> def iconSize ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . iconSize ( ) <EOL> def setIconSize ( self , size ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setIconSize ( size ) <EOL> def isLinked ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . isLinked ( ) <EOL> def setLinked ( self , linked ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setLinked ( linked ) <EOL> def isPinned ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . isPinned ( ) <EOL> def setPinned ( self , pinned , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setPinned ( pinned , quiet ) <EOL> def isFloating ( self ) : <EOL> """<STR_LIT>""" <EOL> container = self . parent ( ) <EOL> if container is not None : <EOL> return container . isWindow ( ) <EOL> return self . isWindow ( ) <EOL> def titleEditable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . isEditable ( ) <EOL> def setTitleEditable ( self , editable ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setEditable ( editable ) <EOL> def titleBarForceHidden ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . titleBarWidget ( ) . isForceHidden ( ) <EOL> def setTitleBarForceHidden ( self , hidden ) : <EOL> """<STR_LIT>""" <EOL> self . titleBarWidget ( ) . setForceHidden ( hidden ) <EOL> def closable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _closable <EOL> def setClosable ( self , closable ) : <EOL> """<STR_LIT>""" <EOL> if closable != self . _closable : <EOL> self . _closable = closable <EOL> bar = self . titleBarWidget ( ) <EOL> buttons = bar . buttons ( ) <EOL> if closable : <EOL> buttons |= bar . CloseButton <EOL> else : <EOL> buttons &= ~ bar . CloseButton <EOL> bar . setButtons ( buttons ) <EOL> container = self . parent ( ) <EOL> if container is not None : <EOL> stacked = container . parent ( ) <EOL> if stacked is not None : <EOL> tabs = stacked . parent ( ) <EOL> if isinstance ( tabs , QDockTabWidget ) : <EOL> index = tabs . indexOf ( container ) <EOL> tabs . setCloseButtonVisible ( index , closable ) <EOL> def titleBarWidget ( self ) : <EOL> """<STR_LIT>""" <EOL> layout = self . layout ( ) <EOL> bar = layout . titleBarWidget ( ) <EOL> if bar is None : <EOL> bar = QDockTitleBar ( ) <EOL> self . setTitleBarWidget ( bar ) <EOL> return bar <EOL> def setTitleBarWidget ( self , title_bar ) : <EOL> """<STR_LIT>""" <EOL> layout = self . layout ( ) <EOL> old = layout . titleBarWidget ( ) <EOL> if old is not None : <EOL> old . maximizeButtonClicked . disconnect ( self . maximizeButtonClicked ) <EOL> old . restoreButtonClicked . disconnect ( self . restoreButtonClicked ) <EOL> old . closeButtonClicked . disconnect ( self . closeButtonClicked ) <EOL> old . linkButtonToggled . disconnect ( self . linkButtonToggled ) <EOL> old . pinButtonToggled . disconnect ( self . pinButtonToggled ) <EOL> old . titleEdited . disconnect ( self . titleEdited ) <EOL> old . leftDoubleClicked . disconnect ( self . titleBarLeftDoubleClicked ) <EOL> old . rightClicked . disconnect ( self . titleBarRightClicked ) <EOL> title_bar = title_bar or QDockTitleBar ( ) <EOL> title_bar . maximizeButtonClicked . connect ( self . maximizeButtonClicked ) <EOL> title_bar . restoreButtonClicked . connect ( self . restoreButtonClicked ) <EOL> title_bar . closeButtonClicked . connect ( self . closeButtonClicked ) <EOL> title_bar . linkButtonToggled . connect ( self . linkButtonToggled ) <EOL> title_bar . pinButtonToggled . connect ( self . pinButtonToggled ) <EOL> title_bar . titleEdited . connect ( self . titleEdited ) <EOL> title_bar . leftDoubleClicked . connect ( self . titleBarLeftDoubleClicked ) <EOL> title_bar . rightClicked . connect ( self . titleBarRightClicked ) <EOL> layout . setTitleBarWidget ( title_bar ) <EOL> def dockWidget ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . layout ( ) . dockWidget ( ) <EOL> def setDockWidget ( self , widget ) : <EOL> """<STR_LIT>""" <EOL> self . layout ( ) . setDockWidget ( widget ) <EOL> def alert ( self , level , on = <NUM_LIT> , off = <NUM_LIT> , repeat = <NUM_LIT:4> , persist = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _alert_data is not None : <EOL> self . clearAlert ( ) <EOL> app = QApplication . instance ( ) <EOL> app . focusChanged . connect ( self . _onAppFocusChanged ) <EOL> timer = QTimer ( ) <EOL> timer . setSingleShot ( True ) <EOL> timer . timeout . connect ( self . _onAlertTimer ) <EOL> on , off , repeat = max ( - <NUM_LIT:1> , on ) , max ( <NUM_LIT:0> , off ) , max ( <NUM_LIT:1> , repeat ) <EOL> self . _alert_data = _AlertData ( timer , level , on , off , repeat , persist ) <EOL> if on < <NUM_LIT:0> : <EOL> self . alerted . emit ( level ) <EOL> else : <EOL> self . _onAlertTimer ( ) <EOL> def clearAlert ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _alert_data is not None : <EOL> self . _alert_data . timer . stop ( ) <EOL> self . _alert_data = None <EOL> app = QApplication . instance ( ) <EOL> app . focusChanged . disconnect ( self . _onAppFocusChanged ) <EOL> self . alerted . emit ( u'<STR_LIT>' ) <EOL> def _onAlertTimer ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _alert_data <EOL> if data is not None : <EOL> if not data . active : <EOL> data . active = True <EOL> data . timer . start ( data . on ) <EOL> self . alerted . emit ( data . level ) <EOL> else : <EOL> data . active = False <EOL> data . remaining -= <NUM_LIT:1> <EOL> if data . remaining > <NUM_LIT:0> : <EOL> data . timer . start ( data . off ) <EOL> self . alerted . emit ( u'<STR_LIT>' ) <EOL> elif data . persist : <EOL> data . timer . stop ( ) <EOL> self . alerted . emit ( data . level ) <EOL> else : <EOL> self . clearAlert ( ) <EOL> def _onAlerted ( self , level ) : <EOL> """<STR_LIT>""" <EOL> level = level or None <EOL> title_bar = self . titleBarWidget ( ) <EOL> label = title_bar . label ( ) <EOL> self . setProperty ( u'<STR_LIT>' , level ) <EOL> title_bar . setProperty ( u'<STR_LIT>' , level ) <EOL> label . setProperty ( u'<STR_LIT>' , level ) <EOL> repolish ( label ) <EOL> repolish ( title_bar ) <EOL> repolish ( self ) <EOL> def _onAppFocusChanged ( self , old , new ) : <EOL> """<STR_LIT>""" <EOL> while new is not None : <EOL> if new is self : <EOL> self . clearAlert ( ) <EOL> break <EOL> new = new . parent ( ) <EOL> def _onVisibilityTimer ( self ) : <EOL> """<STR_LIT>""" <EOL> area = self . rootDockArea ( ) <EOL> if area is not None and area . dockEventsEnabled ( ) : <EOL> timer , visible = self . _vis_changed <EOL> evt_type = DockItemShown if visible else DockItemHidden <EOL> event = QDockItemEvent ( evt_type , self . objectName ( ) ) <EOL> QApplication . postEvent ( area , event ) <EOL> self . _vis_changed = None <EOL> def _postVisibilityChange ( self , visible ) : <EOL> """<STR_LIT>""" <EOL> area = self . rootDockArea ( ) <EOL> if area is not None and area . dockEventsEnabled ( ) : <EOL> vis_changed = self . _vis_changed <EOL> if vis_changed is None : <EOL> timer = QTimer ( ) <EOL> timer . setSingleShot ( True ) <EOL> timer . timeout . connect ( self . _onVisibilityTimer ) <EOL> self . _vis_changed = ( timer , visible ) <EOL> timer . start ( ) <EOL> else : <EOL> timer , old_visible = vis_changed <EOL> if old_visible != visible : <EOL> self . _vis_changed = None <EOL> timer . stop ( ) </s>
