<s> from pyface . qt import QtCore , QtGui <EOL> from pyface . qt . QtGui import QFont <EOL> from traits . api import Dict <EOL> from date_editor import SimpleEditor <EOL> from date_editor import CustomEditor as DateCustomEditor <EOL> class CustomEditor ( DateCustomEditor ) : <EOL> dates = Dict ( ) <EOL> styles = Dict ( ) <EOL> def init ( self , parent ) : <EOL> self . control = QtGui . QCalendarWidget ( ) <EOL> if not self . factory . allow_future : <EOL> self . control . setMaximumDate ( QtCore . QDate . currentDate ( ) ) <EOL> if not self . factory . allow_past : <EOL> self . control . setMinimumDate ( QtCore . QDate . currentDate ( ) ) <EOL> if self . factory . dates_trait and self . factory . styles_trait : <EOL> self . sync_value ( self . factory . dates_trait , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . sync_value ( self . factory . styles_trait , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . control . clicked . connect ( self . update_object ) <EOL> return <EOL> def _dates_changed ( self , old , new ) : <EOL> if old : <EOL> [ map ( self . _reset_formatting , dates ) for dates in old . values ( ) ] <EOL> if new : <EOL> styles = getattr ( self . object , self . factory . styles_trait , None ) <EOL> self . _apply_styles ( styles , new ) <EOL> def _dates_items_changed ( self , event ) : <EOL> groups_to_set = event . added <EOL> groups_to_set . update ( event . changed ) <EOL> styles = getattr ( self . object , self . factory . styles_trait , None ) <EOL> self . _apply_styles ( styles , groups_to_set ) <EOL> [ map ( self . _reset_formatting , dates ) for dates in event . removed . values ( ) ] <EOL> def _styles_changed ( self , old , new ) : <EOL> groups = getattr ( self . object , self . factory . dates_trait , { } ) <EOL> if not new : <EOL> [ map ( self . _reset_formatting , dates ) for dates in groups . values ( ) ] <EOL> else : <EOL> self . _apply_styles ( new , groups ) <EOL> return <EOL> def _styles_items_changed ( self , event ) : <EOL> groups = getattr ( self . object , self . factory . dates_trait ) <EOL> styles = getattr ( self . object , self . factory . styles_trait ) <EOL> names_to_update = event . added . keys ( ) + event . changed . keys ( ) <EOL> modified_groups = dict ( ( name , groups [ name ] ) for name in names_to_update ) <EOL> self . _apply_styles ( styles , modified_groups ) <EOL> names_to_reset = event . removed . keys ( ) <EOL> for name in names_to_reset : <EOL> self . _reset_formatting ( groups [ name ] ) <EOL> return <EOL> def _apply_style ( self , style , dates ) : <EOL> """<STR_LIT>""" <EOL> for dt in dates : <EOL> qdt = QtCore . QDate ( dt ) <EOL> textformat = self . control . dateTextFormat ( qdt ) <EOL> self . _apply_cellformat ( style , textformat ) <EOL> self . control . setDateTextFormat ( qdt , textformat ) <EOL> return <EOL> def _apply_styles ( self , style_dict , date_dict ) : <EOL> """<STR_LIT>""" <EOL> if not style_dict or not date_dict : <EOL> return <EOL> for groupname , dates in date_dict . items ( ) : <EOL> cellformat = style_dict . get ( groupname , None ) <EOL> if not cellformat : <EOL> continue <EOL> for dt in dates : <EOL> qdt = QtCore . QDate ( dt ) <EOL> textformat = self . control . dateTextFormat ( qdt ) <EOL> self . _apply_cellformat ( cellformat , textformat ) <EOL> self . control . setDateTextFormat ( qdt , textformat ) <EOL> return <EOL> def _reset_formatting ( self , dates ) : <EOL> for dt in dates : <EOL> qdt = QtCore . QDate ( dt ) <EOL> self . control . setDateTextFormat ( qdt , QtGui . QTextCharFormat ( ) ) <EOL> def _apply_cellformat ( self , cf , textformat ) : <EOL> """<STR_LIT>""" <EOL> if cf . italics is not None : <EOL> textformat . setFontItalic ( cf . italics ) <EOL> if cf . underline is not None : <EOL> textformat . setFontUnderline ( cf . underline ) <EOL> if cf . bold is not None : <EOL> if cf . bold : <EOL> weight = QFont . Bold <EOL> else : <EOL> weight = QFont . Normal <EOL> textformat . setFontWeight ( weight ) <EOL> if cf . bgcolor is not None : <EOL> textformat . setBackground ( self . _color_to_brush ( cf . bgcolor ) ) <EOL> if cf . fgcolor is not None : <EOL> textformat . setForeground ( self . _color_to_brush ( cf . fgcolor ) ) <EOL> return <EOL> def _color_to_brush ( self , color ) : <EOL> """<STR_LIT>""" <EOL> brush = QtGui . QBrush ( ) <EOL> if isinstance ( color , basestring ) and hasattr ( QtCore . Qt , color ) : <EOL> col = getattr ( QtCore . Qt , color ) <EOL> elif isinstance ( color , tuple ) and len ( color ) == <NUM_LIT:3> : <EOL> col = QtGui . QColor ( ) <EOL> col . setRgb ( * color ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % color ) <EOL> brush . setColor ( col ) <EOL> return brush </s>
