<s> """<STR_LIT>""" <EOL> from pyqode . core . api . panel import Panel <EOL> from pyqode . core . api . decoration import TextDecoration <EOL> from pyqode . qt import QtCore , QtGui , QtWidgets <EOL> class EncodingPanel ( Panel ) : <EOL> """<STR_LIT>""" <EOL> cancel_requested = QtCore . Signal ( object ) <EOL> _description = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ property <EOL> def color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _color <EOL> @ color . setter <EOL> def color ( self , value ) : <EOL> self . _color = value <EOL> self . _refresh_stylesheet ( ) <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . color = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def foreground ( self ) : <EOL> return self . _foreground <EOL> @ foreground . setter <EOL> def foreground ( self , value ) : <EOL> self . _foreground = value <EOL> self . _refresh_stylesheet ( ) <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . foreground = value <EOL> except KeyError : <EOL> pass <EOL> def _refresh_stylesheet ( self ) : <EOL> try : <EOL> self . _lbl_stylesheet = ( '<STR_LIT>' % <EOL> ( self . _foreground . name ( ) , <EOL> self . _color . name ( ) ) ) <EOL> for lbl in self . _labels : <EOL> lbl . setStyleSheet ( self . _lbl_stylesheet ) <EOL> except AttributeError : <EOL> pass <EOL> def __init__ ( self , add_context_menu = True ) : <EOL> super ( EncodingPanel , self ) . __init__ ( dynamic = True ) <EOL> from pyqode . core . _forms . pnl_encoding_ui import Ui_Form <EOL> self . ui = Ui_Form ( ) <EOL> self . ui . setupUi ( self ) <EOL> self . __add_ctx_mnu = add_context_menu <EOL> self . _labels = [ self . ui . label , self . ui . lblDescription ] <EOL> self . _color = None <EOL> self . color = QtGui . QColor ( '<STR_LIT>' ) <EOL> self . _foreground = None <EOL> self . foreground = QtGui . QColor ( '<STR_LIT>' ) <EOL> self . _deco = None <EOL> self . ui . pushButtonRetry . clicked . connect ( self . _reload ) <EOL> self . ui . pushButtonEdit . clicked . connect ( self . _edit_anyway ) <EOL> self . ui . pushButtonCancel . clicked . connect ( self . cancel ) <EOL> self . hide ( ) <EOL> def enable_caret_line ( self , value = True ) : <EOL> try : <EOL> from pyqode . core . modes import CaretLineHighlighterMode <EOL> mode = self . editor . modes . get ( CaretLineHighlighterMode ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> mode . enabled = value <EOL> def on_open_failed ( self , path , encoding ) : <EOL> self . enable_caret_line ( False ) <EOL> self . ui . comboBoxEncodings . current_encoding = encoding <EOL> self . ui . lblDescription . setText ( <EOL> self . _description % ( _ ( '<STR_LIT>' ) % <EOL> path ) ) <EOL> try : <EOL> with open ( path , '<STR_LIT:rb>' ) as file : <EOL> content = str ( file . read ( <NUM_LIT:16> ) ) <EOL> except OSError : <EOL> content = '<STR_LIT>' <EOL> self . editor . setPlainText ( <EOL> content , self . editor . file . get_mimetype ( path ) , <EOL> self . editor . file . encoding ) <EOL> self . editor . setDocumentTitle ( self . editor . file . name ) <EOL> self . editor . setWindowTitle ( self . editor . file . name ) <EOL> QtCore . QTimer . singleShot ( <NUM_LIT:1> , self . show ) <EOL> def show ( self ) : <EOL> super ( EncodingPanel , self ) . show ( ) <EOL> self . editor . selectAll ( ) <EOL> self . _deco = TextDecoration ( self . editor . textCursor ( ) ) <EOL> self . _deco . set_background ( QtCore . Qt . red ) <EOL> self . _deco . set_foreground ( QtCore . Qt . black ) <EOL> self . editor . decorations . append ( self . _deco ) <EOL> cursor = self . editor . textCursor ( ) <EOL> cursor . clearSelection ( ) <EOL> cursor . setPosition ( <NUM_LIT:0> ) <EOL> self . editor . setTextCursor ( cursor ) <EOL> self . editor . setReadOnly ( True ) <EOL> def paintEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( EncodingPanel , self ) . paintEvent ( event ) <EOL> if self . isVisible ( ) : <EOL> painter = QtGui . QPainter ( self ) <EOL> self . _background_brush = QtGui . QBrush ( self . _color ) <EOL> painter . fillRect ( event . rect ( ) , self . _background_brush ) <EOL> def on_install ( self , editor ) : <EOL> super ( EncodingPanel , self ) . on_install ( editor ) <EOL> if self . __add_ctx_mnu : <EOL> from pyqode . core . widgets import EncodingsContextMenu <EOL> EncodingsContextMenu ( parent = editor ) <EOL> def _reload ( self ) : <EOL> self . hide ( ) <EOL> self . _rm_deco ( ) <EOL> self . editor . setReadOnly ( False ) <EOL> self . enable_caret_line ( True ) <EOL> self . editor . file . reload ( self . ui . comboBoxEncodings . current_encoding ) <EOL> def _edit_anyway ( self ) : <EOL> self . _rm_deco ( ) <EOL> self . editor . setReadOnly ( False ) <EOL> self . enable_caret_line ( True ) <EOL> self . hide ( ) <EOL> def _rm_deco ( self ) : <EOL> if self . _deco : <EOL> self . editor . decorations . remove ( self . _deco ) <EOL> self . _deco = None <EOL> def cancel ( self ) : <EOL> if self . sender ( ) : <EOL> self . editor . clear ( ) <EOL> self . _rm_deco ( ) <EOL> self . enable_caret_line ( True ) <EOL> self . cancel_requested . emit ( self . editor ) <EOL> self . hide ( ) <EOL> def clone_settings ( self , original ) : <EOL> self . color = original . color <EOL> self . foreground = original . foreground <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import locale <EOL> import sys <EOL> from pyqode . core . api import CodeEdit <EOL> def simulate_open ( ) : <EOL> pnl . on_open_failed ( __file__ , locale . getpreferredencoding ( ) ) <EOL> def simulate_save ( ) : <EOL> pnl . on_save_failed ( __file__ , locale . getpreferredencoding ( ) ) <EOL> app = QtWidgets . QApplication ( sys . argv ) <EOL> edit = CodeEdit ( ) <EOL> edit . setMinimumSize ( <NUM_LIT> , <NUM_LIT> ) <EOL> edit . file . open ( __file__ ) <EOL> pnl = EncodingPanel ( ) <EOL> edit . panels . append ( pnl , pnl . Position . TOP ) <EOL> edit . show ( ) <EOL> simulate_open ( ) <EOL> QtCore . QTimer . singleShot ( <NUM_LIT> , simulate_save ) <EOL> app . exec_ ( ) </s>
