<s> from . import QT_API <EOL> if QT_API != '<STR_LIT>' : <EOL> msg = '<STR_LIT>' <EOL> raise ImportError ( msg ) <EOL> import logging <EOL> import sys <EOL> import weakref <EOL> from atom . api import Typed <EOL> from enaml . colors import parse_color <EOL> from enaml . fonts import parse_font <EOL> from enaml . scintilla . scintilla import ProxyScintilla <EOL> from PyQt4 import Qsci <EOL> from . QtGui import QColor , QFont <EOL> from . q_resource_helpers import QColor_from_Color , QFont_from_Font <EOL> from . qt_control import QtControl <EOL> from . scintilla_lexers import LEXERS , LEXERS_INV <EOL> from . scintilla_tokens import TOKENS <EOL> logger = logging . getLogger ( __name__ ) <EOL> Base = Qsci . QsciScintillaBase <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> DEFAULT_EOL = '<STR_LIT>' <EOL> elif sys . platform == '<STR_LIT>' : <EOL> DEFAULT_EOL = '<STR_LIT>' <EOL> else : <EOL> DEFAULT_EOL = '<STR_LIT>' <EOL> EOL_MODE = { <EOL> '<STR_LIT>' : Base . SC_EOL_CRLF , <EOL> '<STR_LIT>' : Base . SC_EOL_CR , <EOL> '<STR_LIT>' : Base . SC_EOL_LF , <EOL> } <EOL> EDGE_MODE = { <EOL> '<STR_LIT:none>' : Base . EDGE_NONE , <EOL> '<STR_LIT>' : Base . EDGE_LINE , <EOL> '<STR_LIT>' : Base . EDGE_BACKGROUND , <EOL> } <EOL> INDENTATION_GUIDES = { <EOL> '<STR_LIT:none>' : Base . SC_IV_NONE , <EOL> '<STR_LIT>' : Base . SC_IV_REAL , <EOL> '<STR_LIT>' : Base . SC_IV_LOOKFORWARD , <EOL> '<STR_LIT>' : Base . SC_IV_LOOKBOTH , <EOL> } <EOL> WHITE_SPACE = { <EOL> '<STR_LIT>' : Base . SCWS_VISIBLEALWAYS , <EOL> '<STR_LIT>' : Base . SCWS_VISIBLEAFTERINDENT , <EOL> '<STR_LIT>' : Base . SCWS_INVISIBLE , <EOL> } <EOL> def _make_color ( color_str ) : <EOL> """<STR_LIT>""" <EOL> color = parse_color ( color_str ) <EOL> if color is not None : <EOL> return QColor_from_Color ( color ) <EOL> return QColor ( ) <EOL> def _make_font ( font_str ) : <EOL> """<STR_LIT>""" <EOL> font = parse_font ( font_str ) <EOL> if font is not None : <EOL> return QFont_from_Font ( font ) <EOL> return QFont ( ) <EOL> class QtScintilla ( QtControl , ProxyScintilla ) : <EOL> """<STR_LIT>""" <EOL> qsci_doc_cache = weakref . WeakValueDictionary ( ) <EOL> widget = Typed ( Qsci . QsciScintilla ) <EOL> qsci_doc = Typed ( Qsci . QsciDocument ) <EOL> def create_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> self . widget = Qsci . QsciScintilla ( self . parent_widget ( ) ) <EOL> def init_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QtScintilla , self ) . init_widget ( ) <EOL> d = self . declaration <EOL> self . set_document ( d . document ) <EOL> self . set_syntax ( d . syntax , refresh_style = False ) <EOL> self . set_settings ( d . settings ) <EOL> self . set_zoom ( d . zoom ) <EOL> self . refresh_style ( ) <EOL> self . widget . textChanged . connect ( self . on_text_changed ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . qsci_doc <EOL> super ( QtScintilla , self ) . destroy ( ) <EOL> def on_text_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . declaration <EOL> if d is not None : <EOL> d . text_changed ( ) <EOL> def refresh_style ( self ) : <EOL> """<STR_LIT>""" <EOL> colorcache = { } <EOL> fontcache = { } <EOL> def get_color ( color_str ) : <EOL> if color_str in colorcache : <EOL> return colorcache [ color_str ] <EOL> color = colorcache [ color_str ] = _make_color ( color_str ) <EOL> return color <EOL> def get_font ( font_str ) : <EOL> if font_str in fontcache : <EOL> return fontcache [ font_str ] <EOL> font = fontcache [ font_str ] = _make_font ( font_str ) <EOL> return font <EOL> def pull_color ( dct , key , default ) : <EOL> color = dct . get ( key ) <EOL> if color is None : <EOL> return default <EOL> return get_color ( color ) <EOL> def pull_font ( dct , key , default ) : <EOL> font = dct . get ( key ) <EOL> if font is None : <EOL> return default <EOL> return get_font ( font ) <EOL> caret_color = QColor ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> default_color = QColor ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> default_paper = QColor ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> default_font = QFont ( ) <EOL> theme = self . declaration . theme <EOL> settings = theme . get ( '<STR_LIT>' ) <EOL> if settings is not None : <EOL> caret_color = pull_color ( settings , '<STR_LIT>' , caret_color ) <EOL> default_color = pull_color ( settings , '<STR_LIT>' , default_color ) <EOL> default_paper = pull_color ( settings , '<STR_LIT>' , default_paper ) <EOL> default_font = pull_font ( settings , '<STR_LIT>' , default_font ) <EOL> widget = self . widget <EOL> widget . setCaretForegroundColor ( caret_color ) <EOL> widget . setColor ( default_color ) <EOL> widget . setPaper ( default_paper ) <EOL> widget . setFont ( default_font ) <EOL> lexer = widget . lexer ( ) <EOL> if lexer is None : <EOL> return <EOL> syntax = LEXERS_INV . get ( type ( lexer ) ) <EOL> if syntax is None : <EOL> return <EOL> syntax_rules = theme . get ( syntax , { } ) <EOL> syntax_tokens = TOKENS . get ( syntax , { } ) <EOL> syntax_default = syntax_rules . get ( '<STR_LIT:default>' , { } ) <EOL> default_color = pull_color ( syntax_default , '<STR_LIT>' , default_color ) <EOL> default_paper = pull_color ( syntax_default , '<STR_LIT>' , default_paper ) <EOL> default_font = pull_font ( syntax_default , '<STR_LIT>' , default_font ) <EOL> lexer . setColor ( default_color ) <EOL> lexer . setPaper ( default_paper ) <EOL> lexer . setFont ( default_font ) <EOL> for token , rule in syntax_rules . iteritems ( ) : <EOL> if token == '<STR_LIT:default>' : <EOL> continue <EOL> qtoken = getattr ( lexer , syntax_tokens . get ( token , '<STR_LIT>' ) , None ) <EOL> if qtoken is not None : <EOL> color = pull_color ( rule , '<STR_LIT>' , default_color ) <EOL> paper = pull_color ( rule , '<STR_LIT>' , default_paper ) <EOL> font = pull_font ( rule , '<STR_LIT>' , default_font ) <EOL> lexer . setColor ( color , qtoken ) <EOL> lexer . setPaper ( paper , qtoken ) <EOL> lexer . setFont ( font , qtoken ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> logger . warn ( msg % ( token , syntax ) ) <EOL> def set_document ( self , document ) : <EOL> """<STR_LIT>""" <EOL> qdoc = self . qsci_doc_cache . get ( document . uuid ) <EOL> if qdoc is None : <EOL> qdoc = self . qsci_doc_cache [ document . uuid ] = Qsci . QsciDocument ( ) <EOL> self . qsci_doc = qdoc <EOL> self . widget . setDocument ( qdoc ) <EOL> def set_syntax ( self , syntax , refresh_style = True ) : <EOL> """<STR_LIT>""" <EOL> old = self . widget . lexer ( ) <EOL> if old is not None : <EOL> old . deleteLater ( ) <EOL> lexer_cls = LEXERS . get ( syntax ) or ( lambda w : None ) <EOL> self . widget . setLexer ( lexer_cls ( self . widget ) ) <EOL> if refresh_style : <EOL> self . refresh_style ( ) <EOL> def set_theme ( self , theme ) : <EOL> """<STR_LIT>""" <EOL> self . refresh_style ( ) <EOL> def set_settings ( self , settings ) : <EOL> """<STR_LIT>""" <EOL> w = self . widget <EOL> send = w . SendScintilla <EOL> get = settings . get <EOL> def pull ( kind , name , default ) : <EOL> value = get ( name , default ) <EOL> if not isinstance ( value , kind ) : <EOL> msg = '<STR_LIT>' <EOL> logger . warn ( msg % ( value , name ) ) <EOL> value = default <EOL> return value <EOL> pull_int = lambda name , default : pull ( int , name , default ) <EOL> pull_bool = lambda name , default : pull ( bool , name , default ) <EOL> pull_str = lambda name , default : pull ( str , name , default ) <EOL> def pull_enum ( options , name , default ) : <EOL> value = pull_str ( name , default ) <EOL> if value in options : <EOL> return options [ value ] <EOL> message = '<STR_LIT>' <EOL> logger . warn ( message % ( value , name ) ) <EOL> return options [ default ] <EOL> def pull_color ( name , default ) : <EOL> color = pull_str ( name , default ) <EOL> return _make_color ( color ) <EOL> send ( w . SCI_SETEOLMODE , pull_enum ( EOL_MODE , '<STR_LIT>' , DEFAULT_EOL ) ) <EOL> send ( w . SCI_SETVIEWEOL , pull_bool ( '<STR_LIT>' , False ) ) <EOL> send ( w . SCI_SETEDGEMODE , pull_enum ( EDGE_MODE , '<STR_LIT>' , '<STR_LIT:none>' ) ) <EOL> send ( w . SCI_SETEDGECOLUMN , pull_int ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> send ( w . SCI_SETEDGECOLOUR , pull_color ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> send ( w . SCI_SETTABWIDTH , pull_int ( '<STR_LIT>' , <NUM_LIT:8> ) ) <EOL> send ( w . SCI_SETUSETABS , pull_bool ( '<STR_LIT>' , True ) ) <EOL> send ( w . SCI_SETINDENT , pull_int ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> send ( w . SCI_SETTABINDENTS , pull_bool ( '<STR_LIT>' , False ) ) <EOL> send ( w . SCI_SETBACKSPACEUNINDENTS , <EOL> pull_bool ( '<STR_LIT>' , False ) ) <EOL> send ( w . SCI_SETINDENTATIONGUIDES , <EOL> pull_enum ( INDENTATION_GUIDES , '<STR_LIT>' , '<STR_LIT:none>' ) ) <EOL> send ( w . SCI_SETVIEWWS , pull_enum ( WHITE_SPACE , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> send ( w . SCI_SETWHITESPACESIZE , pull_int ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> send ( w . SCI_SETEXTRAASCENT , pull_int ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> send ( w . SCI_SETEXTRADESCENT , pull_int ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> def set_zoom ( self , zoom ) : <EOL> """<STR_LIT>""" <EOL> self . widget . zoomTo ( zoom ) <EOL> def get_text ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . widget . text ( ) <EOL> def set_text ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . widget . setText ( text ) <EOL> def set_foreground ( self , foreground ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_background ( self , background ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_font ( self , font ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
