<s> from atom . api import Typed , Bool <EOL> from enaml . qt . QtCore import Qt , QMargins , QPoint , QRect , QEvent , Signal <EOL> from enaml . qt . QtGui import QApplication , QLayout , QIcon <EOL> from . event_types import QDockItemEvent , DockItemUndocked <EOL> from . q_dock_area import QDockArea <EOL> from . q_dock_bar import QDockBar <EOL> from . q_dock_frame import QDockFrame <EOL> from . q_dock_frame_layout import QDockFrameLayout <EOL> from . q_dock_tab_widget import QDockTabWidget <EOL> from . q_guide_rose import QGuideRose <EOL> from . utils import repolish <EOL> class QDockContainerLayout ( QDockFrameLayout ) : <EOL> """<STR_LIT>""" <EOL> def invalidate ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockContainerLayout , self ) . invalidate ( ) <EOL> widget = self . getWidget ( ) <EOL> if widget is not None : <EOL> self . parentWidget ( ) . setSizePolicy ( widget . sizePolicy ( ) ) <EOL> def _computePressPos ( container , coeff ) : <EOL> """<STR_LIT>""" <EOL> margins = container . layout ( ) . contentsMargins ( ) <EOL> button_width = <NUM_LIT:50> <EOL> max_x = container . width ( ) - margins . right ( ) - button_width <EOL> test_x = int ( coeff * container . width ( ) ) <EOL> new_x = max ( margins . left ( ) + <NUM_LIT:5> , min ( test_x , max_x ) ) <EOL> title_bar = container . dockItem ( ) . titleBarWidget ( ) <EOL> title_height = title_bar . height ( ) / <NUM_LIT:2> <EOL> mid_title = title_bar . mapTo ( container , QPoint ( <NUM_LIT:0> , title_height ) ) <EOL> return QPoint ( new_x , mid_title . y ( ) ) <EOL> def _closestDockBar ( container ) : <EOL> """<STR_LIT>""" <EOL> area = container . parentDockArea ( ) <EOL> if area is None : <EOL> return QDockBar . North <EOL> pane = area . centralPane ( ) <EOL> c_width = container . width ( ) <EOL> c_height = container . height ( ) <EOL> p_width = pane . width ( ) <EOL> p_height = pane . height ( ) <EOL> p1 = container . mapTo ( pane , QPoint ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> p2 = container . mapTo ( pane , QPoint ( c_width , c_height ) ) <EOL> edge_ranks = ( p1 . y ( ) , p_width - p2 . x ( ) , p_height - p2 . y ( ) , p1 . x ( ) ) <EOL> f_width = <NUM_LIT:1.0> - float ( c_width ) / p_width <EOL> f_height = <NUM_LIT:1.0> - float ( c_height ) / p_height <EOL> edge_weights = ( f_width , f_height ) * <NUM_LIT:2> <EOL> tie_breakers = ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> borders = ( QDockBar . North , QDockBar . East , QDockBar . South , QDockBar . West ) <EOL> values = zip ( edge_ranks , edge_weights , tie_breakers , borders ) <EOL> return sorted ( values ) [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] <EOL> class QDockContainer ( QDockFrame ) : <EOL> """<STR_LIT>""" <EOL> topLevelChanged = Signal ( bool ) <EOL> class FrameState ( QDockFrame . FrameState ) : <EOL> """<STR_LIT>""" <EOL> press_pos = Typed ( QPoint ) <EOL> dragging = Bool ( False ) <EOL> item_is_maximized = Bool ( False ) <EOL> in_dock_bar = Bool ( False ) <EOL> def __init__ ( self , manager , parent = None ) : <EOL> """<STR_LIT>""" <EOL> super ( QDockContainer , self ) . __init__ ( manager , parent ) <EOL> layout = QDockContainerLayout ( ) <EOL> layout . setSizeConstraint ( QLayout . SetMinAndMaxSize ) <EOL> self . setLayout ( layout ) <EOL> self . setProperty ( '<STR_LIT>' , False ) <EOL> self . _dock_item = None <EOL> def titleBarGeometry ( self ) : <EOL> """<STR_LIT>""" <EOL> title_bar = self . dockItem ( ) . titleBarWidget ( ) <EOL> if title_bar . isHidden ( ) : <EOL> return QRect ( ) <EOL> pt = title_bar . mapTo ( self , QPoint ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> return QRect ( pt , title_bar . size ( ) ) <EOL> def resizeMargins ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . isMaximized ( ) : <EOL> return QMargins ( ) <EOL> return self . layout ( ) . contentsMargins ( ) <EOL> def showMaximized ( self ) : <EOL> """<STR_LIT>""" <EOL> def update_buttons ( bar , link = False , pin = False ) : <EOL> buttons = bar . buttons ( ) <EOL> buttons |= bar . RestoreButton <EOL> buttons &= ~ bar . MaximizeButton <EOL> if link : <EOL> buttons &= ~ bar . LinkButton <EOL> if pin : <EOL> buttons &= ~ bar . PinButton <EOL> bar . setButtons ( buttons ) <EOL> if self . isWindow ( ) : <EOL> super ( QDockContainer , self ) . showMaximized ( ) <EOL> self . setLinked ( False ) <EOL> update_buttons ( self . dockItem ( ) . titleBarWidget ( ) , link = True ) <EOL> else : <EOL> area = self . parentDockArea ( ) <EOL> if area is not None : <EOL> item = self . dockItem ( ) <EOL> update_buttons ( item . titleBarWidget ( ) , pin = True ) <EOL> area . setMaximizedWidget ( item ) <EOL> self . frame_state . item_is_maximized = True <EOL> item . installEventFilter ( self ) <EOL> def showNormal ( self ) : <EOL> """<STR_LIT>""" <EOL> def update_buttons ( bar , link = False , pin = False ) : <EOL> buttons = bar . buttons ( ) <EOL> buttons |= bar . MaximizeButton <EOL> buttons &= ~ bar . RestoreButton <EOL> if link : <EOL> buttons |= bar . LinkButton <EOL> if pin : <EOL> buttons |= bar . PinButton <EOL> bar . setButtons ( buttons ) <EOL> if self . isWindow ( ) : <EOL> super ( QDockContainer , self ) . showNormal ( ) <EOL> self . setLinked ( False ) <EOL> update_buttons ( self . dockItem ( ) . titleBarWidget ( ) , link = True ) <EOL> elif self . frame_state . item_is_maximized : <EOL> item = self . dockItem ( ) <EOL> update_buttons ( item . titleBarWidget ( ) , pin = True ) <EOL> self . layout ( ) . setWidget ( item ) <EOL> self . frame_state . item_is_maximized = False <EOL> item . removeEventFilter ( self ) <EOL> def dockItem ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dock_item <EOL> def setDockItem ( self , dock_item ) : <EOL> """<STR_LIT>""" <EOL> layout = self . layout ( ) <EOL> old = layout . getWidget ( ) <EOL> if old is not None : <EOL> old . maximizeButtonClicked . disconnect ( self . showMaximized ) <EOL> old . restoreButtonClicked . disconnect ( self . showNormal ) <EOL> old . closeButtonClicked . disconnect ( self . close ) <EOL> old . linkButtonToggled . disconnect ( self . linkButtonToggled ) <EOL> old . pinButtonToggled . disconnect ( self . onPinButtonToggled ) <EOL> old . titleBarLeftDoubleClicked . disconnect ( self . toggleMaximized ) <EOL> if dock_item is not None : <EOL> dock_item . maximizeButtonClicked . connect ( self . showMaximized ) <EOL> dock_item . restoreButtonClicked . connect ( self . showNormal ) <EOL> dock_item . closeButtonClicked . connect ( self . close ) <EOL> dock_item . linkButtonToggled . connect ( self . linkButtonToggled ) <EOL> dock_item . pinButtonToggled . connect ( self . onPinButtonToggled ) <EOL> dock_item . titleBarLeftDoubleClicked . connect ( self . toggleMaximized ) <EOL> layout . setWidget ( dock_item ) <EOL> self . _dock_item = dock_item <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> return item . title ( ) <EOL> return u'<STR_LIT>' <EOL> def icon ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> return item . icon ( ) <EOL> return QIcon ( ) <EOL> def closable ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> return item . closable ( ) <EOL> return True <EOL> def isLinked ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> return item . isLinked ( ) <EOL> return False <EOL> def setLinked ( self , linked ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> item . setLinked ( linked ) <EOL> def isPinned ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> return item . isPinned ( ) <EOL> return False <EOL> def setPinned ( self , pinned , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> item . setPinned ( pinned , quiet ) <EOL> def showTitleBar ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> item . titleBarWidget ( ) . show ( ) <EOL> def hideTitleBar ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> item . titleBarWidget ( ) . hide ( ) <EOL> def showLinkButton ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> bar = item . titleBarWidget ( ) <EOL> bar . setButtons ( bar . buttons ( ) | bar . LinkButton ) <EOL> def hideLinkButton ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> bar = item . titleBarWidget ( ) <EOL> bar . setButtons ( bar . buttons ( ) & ~ bar . LinkButton ) <EOL> def showPinButton ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> bar = item . titleBarWidget ( ) <EOL> bar . setButtons ( bar . buttons ( ) | bar . PinButton ) <EOL> def hidePinButton ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . dockItem ( ) <EOL> if item is not None : <EOL> bar = item . titleBarWidget ( ) <EOL> bar . setButtons ( bar . buttons ( ) & ~ bar . PinButton ) <EOL> def toggleMaximized ( self ) : <EOL> """<STR_LIT>""" <EOL> is_win = self . isWindow ( ) <EOL> is_maxed = self . isMaximized ( ) <EOL> item_maxed = self . frame_state . item_is_maximized <EOL> if is_win and is_maxed or item_maxed : <EOL> self . showNormal ( ) <EOL> else : <EOL> self . showMaximized ( ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> state = self . frame_state <EOL> state . dragging = False <EOL> state . press_pos = None <EOL> state . in_dock_bar = False <EOL> self . showNormal ( ) <EOL> self . unfloat ( ) <EOL> self . hideLinkButton ( ) <EOL> self . setLinked ( False ) <EOL> self . showTitleBar ( ) <EOL> self . setAttribute ( Qt . WA_WState_ExplicitShowHide , False ) <EOL> self . setAttribute ( Qt . WA_WState_Hidden , False ) <EOL> def float ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hide ( ) <EOL> self . setAttribute ( Qt . WA_Hover , True ) <EOL> flags = Qt . Tool | Qt . FramelessWindowHint <EOL> self . setParent ( self . manager ( ) . dock_area ( ) , flags ) <EOL> self . layout ( ) . setContentsMargins ( QMargins ( <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> self . setProperty ( '<STR_LIT>' , True ) <EOL> self . setLinked ( False ) <EOL> self . showLinkButton ( ) <EOL> self . hidePinButton ( ) <EOL> repolish ( self ) <EOL> self . topLevelChanged . emit ( True ) <EOL> def unfloat ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hide ( ) <EOL> self . setAttribute ( Qt . WA_Hover , False ) <EOL> self . setParent ( self . manager ( ) . dock_area ( ) , Qt . Widget ) <EOL> self . layout ( ) . setContentsMargins ( QMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . unsetCursor ( ) <EOL> self . setProperty ( '<STR_LIT>' , False ) <EOL> self . setLinked ( False ) <EOL> self . hideLinkButton ( ) <EOL> self . showPinButton ( ) <EOL> repolish ( self ) <EOL> self . topLevelChanged . emit ( False ) <EOL> def parentDockArea ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent ( ) <EOL> while parent is not None : <EOL> if isinstance ( parent , QDockArea ) : <EOL> return parent <EOL> parent = parent . parent ( ) <EOL> def parentDockTabWidget ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent ( ) <EOL> while parent is not None : <EOL> if isinstance ( parent , QDockTabWidget ) : <EOL> return parent <EOL> parent = parent . parent ( ) <EOL> def unplug ( self ) : <EOL> """<STR_LIT>""" <EOL> dock_area = self . parentDockArea ( ) <EOL> if dock_area is None : <EOL> return False <EOL> if self . frame_state . in_dock_bar : <EOL> dock_area . removeFromDockBar ( self ) <EOL> return True <EOL> from . layout_handling import unplug_container <EOL> return unplug_container ( dock_area , self ) <EOL> def untab ( self , pos ) : <EOL> """<STR_LIT>""" <EOL> if not self . unplug ( ) : <EOL> return <EOL> self . postUndockedEvent ( ) <EOL> state = self . frame_state <EOL> state . mouse_title = True <EOL> state . dragging = True <EOL> self . float ( ) <EOL> self . raiseFrame ( ) <EOL> title_bar = self . dockItem ( ) . titleBarWidget ( ) <EOL> title_pos = QPoint ( title_bar . width ( ) / <NUM_LIT:2> , title_bar . height ( ) / <NUM_LIT:2> ) <EOL> margins = self . layout ( ) . contentsMargins ( ) <EOL> offset = QPoint ( margins . left ( ) , margins . top ( ) ) <EOL> state . press_pos = title_bar . mapTo ( self , title_pos ) + offset <EOL> self . move ( pos - state . press_pos ) <EOL> self . show ( ) <EOL> self . grabMouse ( ) <EOL> self . activateWindow ( ) <EOL> self . raise_ ( ) <EOL> def postUndockedEvent ( self ) : <EOL> """<STR_LIT>""" <EOL> root_area = self . manager ( ) . dock_area ( ) <EOL> if root_area . dockEventsEnabled ( ) : <EOL> event = QDockItemEvent ( DockItemUndocked , self . objectName ( ) ) <EOL> QApplication . postEvent ( root_area , event ) <EOL> def onPinButtonToggled ( self , pinned ) : <EOL> """<STR_LIT>""" <EOL> area = self . parentDockArea ( ) <EOL> if area is not None : <EOL> if pinned : <EOL> if not self . frame_state . in_dock_bar : <EOL> position = _closestDockBar ( self ) <EOL> self . unplug ( ) <EOL> area . addToDockBar ( self , position ) <EOL> else : <EOL> position = area . dockBarPosition ( self ) <EOL> if position is not None : <EOL> from . layout_handling import plug_frame <EOL> area . removeFromDockBar ( self ) <EOL> if area . centralWidget ( ) is None : <EOL> guide = QGuideRose . Guide . AreaCenter <EOL> else : <EOL> guide = ( <EOL> QGuideRose . Guide . BorderNorth , <EOL> QGuideRose . Guide . BorderEast , <EOL> QGuideRose . Guide . BorderSouth , <EOL> QGuideRose . Guide . BorderWest <EOL> ) [ position ] <EOL> plug_frame ( area , None , self , guide ) <EOL> def eventFilter ( self , obj , event ) : <EOL> """<STR_LIT>""" <EOL> if obj is not self . _dock_item : <EOL> return False <EOL> if event . type ( ) == QEvent . MouseButtonPress : <EOL> return self . filteredMousePressEvent ( event ) <EOL> elif event . type ( ) == QEvent . MouseMove : <EOL> return self . filteredMouseMoveEvent ( event ) <EOL> elif event . type ( ) == QEvent . MouseButtonRelease : <EOL> return self . filteredMouseReleaseEvent ( event ) <EOL> return False <EOL> def filteredMousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> bar = self . dockItem ( ) . titleBarWidget ( ) <EOL> if bar . isVisible ( ) and bar . geometry ( ) . contains ( event . pos ( ) ) : <EOL> self . frame_state . mouse_title = True <EOL> return self . titleBarMousePressEvent ( event ) <EOL> return False <EOL> def filteredMouseMoveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . frame_state . mouse_title : <EOL> return self . titleBarMouseMoveEvent ( event ) <EOL> return False <EOL> def filteredMouseReleaseEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . frame_state . mouse_title : <EOL> self . frame_state . mouse_title = False <EOL> return self . titleBarMouseReleaseEvent ( event ) <EOL> return False <EOL> def closeEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . manager ( ) . close_container ( self , event ) <EOL> def titleBarMousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . button ( ) == Qt . LeftButton : <EOL> state = self . frame_state <EOL> if state . press_pos is None : <EOL> state . press_pos = event . pos ( ) <EOL> return True <EOL> return False <EOL> def titleBarMouseMoveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> state = self . frame_state <EOL> if state . press_pos is None : <EOL> return False <EOL> global_pos = event . globalPos ( ) <EOL> if state . dragging : <EOL> if self . isWindow ( ) : <EOL> target_pos = global_pos - state . press_pos <EOL> self . manager ( ) . drag_move_frame ( self , target_pos , global_pos ) <EOL> return True <EOL> dist = ( event . pos ( ) - state . press_pos ) . manhattanLength ( ) <EOL> if dist <= QApplication . startDragDistance ( ) : <EOL> return True <EOL> state . dragging = True <EOL> if self . isWindow ( ) : <EOL> if self . isMaximized ( ) : <EOL> coeff = state . press_pos . x ( ) / float ( self . width ( ) ) <EOL> self . showNormal ( ) <EOL> state . press_pos = _computePressPos ( self , coeff ) <EOL> return True <EOL> if state . item_is_maximized : <EOL> bar = self . dockItem ( ) . titleBarWidget ( ) <EOL> coeff = state . press_pos . x ( ) / float ( bar . width ( ) ) <EOL> self . showNormal ( ) <EOL> state . press_pos = _computePressPos ( self , coeff ) <EOL> if not self . unplug ( ) : <EOL> return False <EOL> self . postUndockedEvent ( ) <EOL> self . float ( ) <EOL> self . raiseFrame ( ) <EOL> margins = self . layout ( ) . contentsMargins ( ) <EOL> state . press_pos += QPoint ( <NUM_LIT:0> , margins . top ( ) ) <EOL> self . move ( global_pos - state . press_pos ) <EOL> self . show ( ) <EOL> self . grabMouse ( ) <EOL> self . activateWindow ( ) <EOL> self . raise_ ( ) <EOL> return True <EOL> def titleBarMouseReleaseEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . button ( ) == Qt . LeftButton : <EOL> state = self . frame_state <EOL> if state . press_pos is not None : <EOL> self . releaseMouse ( ) <EOL> if self . isWindow ( ) : <EOL> self . manager ( ) . drag_release_frame ( self , event . globalPos ( ) ) <EOL> state . dragging = False <EOL> state . press_pos = None <EOL> return True <EOL> return False </s>
