<s> from PyQt4 . QtCore import * <EOL> from PyQt4 . QtGui import * <EOL> import re <EOL> class FindReplaceDialog ( QDialog ) : <EOL> def __init__ ( self , parent = None ) : <EOL> super ( FindReplaceDialog , self ) . __init__ ( parent ) <EOL> grid = QGridLayout ( self ) <EOL> self . searchInput = QLineEdit ( self ) <EOL> self . replaceInput = QLineEdit ( self ) <EOL> self . matchCase = QCheckBox ( self . tr ( '<STR_LIT>' ) , self ) <EOL> self . wholeWords = QCheckBox ( self . tr ( '<STR_LIT>' ) , self ) <EOL> self . reCheck = QCheckBox ( self . tr ( '<STR_LIT>' ) , self ) <EOL> self . nextButton = QPushButton ( self . tr ( "<STR_LIT>" ) , self ) <EOL> self . prevButton = QPushButton ( self . tr ( "<STR_LIT>" ) , self ) <EOL> self . replaceButton = QPushButton ( self . tr ( "<STR_LIT>" ) , self ) <EOL> self . replaceAllButton = QPushButton ( self . tr ( "<STR_LIT>" ) , self ) <EOL> self . nextButton . clicked . connect ( self . find ) <EOL> self . prevButton . clicked . connect ( lambda : self . find ( back = True ) ) <EOL> self . replaceButton . clicked . connect ( self . replace ) <EOL> self . replaceAllButton . clicked . connect ( self . replaceAll ) <EOL> self . searchInput . returnPressed . connect ( self . find ) <EOL> self . replaceInput . returnPressed . connect ( self . replace ) <EOL> grid . addWidget ( QLabel ( self . tr ( "<STR_LIT>" ) ) , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> grid . addWidget ( QLabel ( self . tr ( "<STR_LIT>" ) ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> grid . addWidget ( QLabel ( self . tr ( "<STR_LIT>" ) ) , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> grid . addWidget ( self . searchInput , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> grid . addWidget ( self . nextButton , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> grid . addWidget ( self . prevButton , <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> grid . addWidget ( self . matchCase , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> grid . addWidget ( self . wholeWords , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> grid . addWidget ( self . reCheck , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> grid . addWidget ( self . replaceInput , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> grid . addWidget ( self . replaceButton , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> grid . addWidget ( self . replaceAllButton , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> def replace ( self , autofind = True ) : <EOL> if autofind : <EOL> self . find ( ) <EOL> contents = self . parent ( ) . textCursor ( ) . selectedText ( ) <EOL> if self . reCheck . isChecked ( ) : <EOL> search_term = self . searchInput . text ( ) <EOL> else : <EOL> search_term = re . escape ( self . searchInput . text ( ) ) <EOL> replace_term = self . replaceInput . text ( ) <EOL> flags = <NUM_LIT:0> <EOL> if not self . matchCase . isChecked ( ) : <EOL> flags = re . IGNORECASE <EOL> if self . wholeWords . isChecked ( ) : <EOL> search_term = r'<STR_LIT>' . format ( search_term ) <EOL> contents = re . sub ( search_term , replace_term , contents , flags = flags ) <EOL> self . parent ( ) . textCursor ( ) . insertText ( contents ) <EOL> def replaceAll ( self ) : <EOL> self . parent ( ) . selectAll ( ) <EOL> self . replace ( autofind = False ) <EOL> def find ( self , back = False ) : <EOL> flags = <NUM_LIT> <EOL> if self . reCheck . isChecked ( ) : <EOL> search_term = QRegExp ( self . searchInput . text ( ) ) <EOL> else : <EOL> search_term = self . searchInput . text ( ) <EOL> if self . matchCase . isChecked ( ) : <EOL> flags |= QTextDocument . FindCaseSensitively <EOL> elif self . reCheck . isChecked ( ) : <EOL> search_term . setCaseSensitivity ( Qt . CaseInsensitive ) <EOL> if self . wholeWords . isChecked ( ) : <EOL> flags |= QTextDocument . FindWholeWords <EOL> if back : <EOL> flags |= QTextDocument . FindBackward <EOL> start_here = self . parent ( ) . textCursor ( ) <EOL> if flags : <EOL> cursor = self . parent ( ) . document ( ) . find ( search_term , start_here , options = flags ) <EOL> else : <EOL> cursor = self . parent ( ) . document ( ) . find ( search_term , start_here ) <EOL> self . parent ( ) . setTextCursor ( cursor ) </s>
