<s> from PyQt4 import QtGui , QtCore <EOL> from PyQt4 . QtCore import Qt <EOL> import re <EOL> class Find ( QtGui . QDialog ) : <EOL> def __init__ ( self , parent = None ) : <EOL> QtGui . QDialog . __init__ ( self , parent ) <EOL> self . parent = parent <EOL> self . lastStart = <NUM_LIT:0> <EOL> self . initUI ( ) <EOL> def initUI ( self ) : <EOL> findButton = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> findButton . clicked . connect ( self . find ) <EOL> replaceButton = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> replaceButton . clicked . connect ( self . replace ) <EOL> allButton = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> allButton . clicked . connect ( self . replaceAll ) <EOL> self . normalRadio = QtGui . QRadioButton ( "<STR_LIT>" , self ) <EOL> regexRadio = QtGui . QRadioButton ( "<STR_LIT>" , self ) <EOL> self . findField = QtGui . QTextEdit ( self ) <EOL> self . findField . resize ( <NUM_LIT> , <NUM_LIT:50> ) <EOL> self . replaceField = QtGui . QTextEdit ( self ) <EOL> self . replaceField . resize ( <NUM_LIT> , <NUM_LIT:50> ) <EOL> layout = QtGui . QGridLayout ( ) <EOL> layout . addWidget ( self . findField , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> ) <EOL> layout . addWidget ( self . normalRadio , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> layout . addWidget ( regexRadio , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> layout . addWidget ( findButton , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> layout . addWidget ( self . replaceField , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:4> ) <EOL> layout . addWidget ( replaceButton , <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> layout . addWidget ( allButton , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . setGeometry ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> self . setWindowTitle ( "<STR_LIT>" ) <EOL> self . setLayout ( layout ) <EOL> self . normalRadio . setChecked ( True ) <EOL> def find ( self ) : <EOL> text = self . parent . text . toPlainText ( ) <EOL> query = self . findField . toPlainText ( ) <EOL> if self . normalRadio . isChecked ( ) : <EOL> self . lastStart = text . find ( query , self . lastStart + <NUM_LIT:1> ) <EOL> if self . lastStart >= <NUM_LIT:0> : <EOL> end = self . lastStart + len ( query ) <EOL> self . moveCursor ( self . lastStart , end ) <EOL> else : <EOL> self . lastStart = <NUM_LIT:0> <EOL> self . parent . text . moveCursor ( QtGui . QTextCursor . End ) <EOL> else : <EOL> pattern = re . compile ( query ) <EOL> match = pattern . search ( text , self . lastStart + <NUM_LIT:1> ) <EOL> if match : <EOL> self . lastStart = match . start ( ) <EOL> self . moveCursor ( self . lastStart , match . end ( ) ) <EOL> else : <EOL> self . lastStart = <NUM_LIT:0> <EOL> self . parent . text . moveCursor ( QtGui . QTextCursor . End ) <EOL> def replace ( self ) : <EOL> cursor = self . parent . text . textCursor ( ) <EOL> if cursor . hasSelection ( ) : <EOL> cursor . insertText ( self . replaceField . toPlainText ( ) ) <EOL> self . parent . text . setTextCursor ( cursor ) <EOL> def replaceAll ( self ) : <EOL> self . lastStart = <NUM_LIT:0> <EOL> self . find ( ) <EOL> while self . lastStart : <EOL> self . replace ( ) <EOL> self . find ( ) <EOL> def moveCursor ( self , start , end ) : <EOL> cursor = self . parent . text . textCursor ( ) <EOL> cursor . setPosition ( start ) <EOL> cursor . movePosition ( QtGui . QTextCursor . Right , QtGui . QTextCursor . KeepAnchor , end - start ) <EOL> self . parent . text . setTextCursor ( cursor ) </s>
