<s> """<STR_LIT>""" <EOL> import wx <EOL> TREE_THRESHOLD = <NUM_LIT:5> <EOL> class PreferenceEditor ( wx . Dialog ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , title , preferences , style = "<STR_LIT>" ) : <EOL> wx . Dialog . __init__ ( self , parent , wx . ID_ANY , title , size = ( <NUM_LIT> , <NUM_LIT> ) , <EOL> style = wx . RESIZE_BORDER | wx . DEFAULT_DIALOG_STYLE ) <EOL> self . Bind ( wx . EVT_CLOSE , self . OnClose ) <EOL> self . _current_panel = None <EOL> self . _panels = [ ] <EOL> self . _settings = preferences . settings <EOL> self . _closing = False <EOL> panels = preferences . preference_panels <EOL> if style not in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if style == "<STR_LIT>" or ( style == "<STR_LIT>" and len ( panels ) > TREE_THRESHOLD ) : <EOL> self . _sw = wx . SplitterWindow ( self , wx . ID_ANY , style = wx . SP_LIVE_UPDATE | wx . SP_3D ) <EOL> self . _tree = wx . TreeCtrl ( self . _sw , wx . ID_ANY , <EOL> style = wx . TR_HIDE_ROOT | wx . TR_HAS_BUTTONS ) <EOL> self . _container = PanelContainer ( self . _sw , wx . ID_ANY ) <EOL> self . _sw . SplitVertically ( self . _tree , self . _container , <NUM_LIT> ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sizer . Add ( self . _sw , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . _tree . Bind ( wx . EVT_TREE_SEL_CHANGED , self . OnTreeSelection ) <EOL> self . _populate_tree ( panels ) <EOL> self . _tree . SelectItem ( self . _tree . GetFirstChild ( self . _tree . GetRootItem ( ) ) [ <NUM_LIT:0> ] ) <EOL> self . SetSizer ( sizer ) <EOL> elif style == "<STR_LIT>" or ( style == "<STR_LIT>" and len ( panels ) > <NUM_LIT:1> ) : <EOL> self . _notebook = wx . Notebook ( self ) <EOL> for panel_class in sorted ( panels , key = lambda p : p . location ) : <EOL> container = PanelContainer ( self . _notebook ) <EOL> panel = container . AddPanel ( panel_class ) <EOL> container . ShowPanel ( panel ) <EOL> self . _notebook . AddPage ( container , panel . GetTitle ( ) ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sizer . Add ( self . _notebook , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . SetSizer ( sizer ) <EOL> else : <EOL> self . _container = PanelContainer ( self , wx . ID_ANY ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sizer . Add ( self . _container , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . SetSizer ( sizer ) <EOL> panel = self . _container . AddPanel ( panels [ <NUM_LIT:0> ] ) <EOL> self . _container . ShowPanel ( panel ) <EOL> def OnClose ( self , evt ) : <EOL> self . _closing = True <EOL> evt . Skip ( ) <EOL> def OnTreeSelection ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . _closing : <EOL> return <EOL> instance_or_class = self . _tree . GetItemPyData ( event . GetItem ( ) ) <EOL> if isinstance ( instance_or_class , wx . Panel ) : <EOL> panel = instance_or_class <EOL> else : <EOL> panel = self . _container . AddPanel ( instance_or_class , self . _settings ) <EOL> self . _panels . append ( panel ) <EOL> self . _tree . SetItemPyData ( event . GetItem ( ) , panel ) <EOL> self . _container . ShowPanel ( panel ) <EOL> def _populate_tree ( self , panels ) : <EOL> """<STR_LIT>""" <EOL> self . _tree . AddRoot ( "<STR_LIT>" ) <EOL> for panel_class in panels : <EOL> location = panel_class . location <EOL> if not isinstance ( location , tuple ) : <EOL> location = ( location , ) <EOL> item = self . _get_item ( location ) <EOL> self . _tree . SetItemPyData ( item , panel_class ) <EOL> self . _tree . ExpandAll ( ) <EOL> def _get_item ( self , location ) : <EOL> item = self . _tree . GetRootItem ( ) <EOL> for text in location : <EOL> item = self . _get_child_item ( item , text ) <EOL> return item <EOL> def _get_child_item ( self , parent , text ) : <EOL> """<STR_LIT>""" <EOL> if self . _tree . ItemHasChildren ( parent ) : <EOL> item , cookie = self . _tree . GetFirstChild ( parent ) <EOL> while item : <EOL> if self . _tree . GetItemText ( item ) . strip ( ) . lower ( ) == text . strip ( ) . lower ( ) : <EOL> return item <EOL> item , cookie = self . _tree . GetNextChild ( parent , cookie ) <EOL> item = self . _tree . AppendItem ( parent , text ) <EOL> return item <EOL> def _get_children ( self , parent ) : <EOL> if self . _tree . ItemHasChildren ( parent ) : <EOL> item , cookie = self . _tree . GetFirstChild ( parent ) <EOL> while item : <EOL> yield item <EOL> item , cookie = self . _tree . GetNextChild ( parent , cookie ) <EOL> class PanelContainer ( wx . Panel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PanelContainer , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _current_panel = None <EOL> self . title = wx . StaticText ( self , label = "<STR_LIT>" ) <EOL> self . panels_container = wx . Panel ( self ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sizer . Add ( self . title , <NUM_LIT:0> , wx . TOP | wx . LEFT | wx . EXPAND , <NUM_LIT:4> ) <EOL> sizer . Add ( wx . StaticLine ( self ) , <NUM_LIT:0> , wx . EXPAND | wx . TOP | wx . BOTTOM , <NUM_LIT:4> ) <EOL> sizer . Add ( self . panels_container , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . SetSizer ( sizer ) <EOL> self . panels_container . SetSizer ( wx . BoxSizer ( wx . VERTICAL ) ) <EOL> font = self . title . GetFont ( ) <EOL> font . SetPointSize ( font . GetPointSize ( ) + <NUM_LIT:2> ) <EOL> self . title . SetFont ( font ) <EOL> self . title . SetForegroundColour ( "<STR_LIT>" ) <EOL> def AddPanel ( self , panel_class , settings ) : <EOL> """<STR_LIT>""" <EOL> panel = panel_class ( parent = self . panels_container , settings = settings ) <EOL> self . panels_container . GetSizer ( ) . Add ( panel , <NUM_LIT:1> , wx . EXPAND ) <EOL> return panel <EOL> def ShowPanel ( self , panel ) : <EOL> """<STR_LIT>""" <EOL> if self . _current_panel is not None : <EOL> self . _current_panel . Hide ( ) <EOL> self . _current_panel = panel <EOL> panel . Show ( ) <EOL> sizer = self . panels_container . GetSizer ( ) <EOL> item = sizer . GetItem ( panel ) <EOL> title = getattr ( panel , "<STR_LIT:title>" , panel . location [ - <NUM_LIT:1> ] ) <EOL> self . title . SetLabel ( title ) <EOL> if item is None : <EOL> sizer . Add ( panel , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . Layout ( ) <EOL> def SetTitle ( self , title ) : <EOL> """<STR_LIT>""" <EOL> self . title . SetLabel ( title ) </s>
