<s> import sys <EOL> from PyQt4 import QtGui , QtCore <EOL> from PyQt4 . QtCore import Qt <EOL> from ext import * <EOL> class Main ( QtGui . QMainWindow ) : <EOL> def __init__ ( self , parent = None ) : <EOL> QtGui . QMainWindow . __init__ ( self , parent ) <EOL> self . filename = "<STR_LIT>" <EOL> self . initUI ( ) <EOL> def initToolbar ( self ) : <EOL> self . newAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . newAction . setShortcut ( "<STR_LIT>" ) <EOL> self . newAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . newAction . triggered . connect ( self . new ) <EOL> self . openAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . openAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . openAction . setShortcut ( "<STR_LIT>" ) <EOL> self . openAction . triggered . connect ( self . open ) <EOL> self . saveAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . saveAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . saveAction . setShortcut ( "<STR_LIT>" ) <EOL> self . saveAction . triggered . connect ( self . save ) <EOL> self . printAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . printAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . printAction . setShortcut ( "<STR_LIT>" ) <EOL> self . printAction . triggered . connect ( self . printHandler ) <EOL> self . previewAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . previewAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . previewAction . setShortcut ( "<STR_LIT>" ) <EOL> self . previewAction . triggered . connect ( self . preview ) <EOL> self . findAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . findAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . findAction . setShortcut ( "<STR_LIT>" ) <EOL> self . findAction . triggered . connect ( find . Find ( self ) . show ) <EOL> self . cutAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . cutAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . cutAction . setShortcut ( "<STR_LIT>" ) <EOL> self . cutAction . triggered . connect ( self . text . cut ) <EOL> self . copyAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . copyAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . copyAction . setShortcut ( "<STR_LIT>" ) <EOL> self . copyAction . triggered . connect ( self . text . copy ) <EOL> self . pasteAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . pasteAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . pasteAction . setShortcut ( "<STR_LIT>" ) <EOL> self . pasteAction . triggered . connect ( self . text . paste ) <EOL> self . undoAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . undoAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . undoAction . setShortcut ( "<STR_LIT>" ) <EOL> self . undoAction . triggered . connect ( self . text . undo ) <EOL> self . redoAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> self . redoAction . setStatusTip ( "<STR_LIT>" ) <EOL> self . redoAction . setShortcut ( "<STR_LIT>" ) <EOL> self . redoAction . triggered . connect ( self . text . redo ) <EOL> wordCountAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> wordCountAction . setStatusTip ( "<STR_LIT>" ) <EOL> wordCountAction . setShortcut ( "<STR_LIT>" ) <EOL> wordCountAction . triggered . connect ( self . wordCount ) <EOL> imageAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> imageAction . setStatusTip ( "<STR_LIT>" ) <EOL> imageAction . setShortcut ( "<STR_LIT>" ) <EOL> imageAction . triggered . connect ( self . insertImage ) <EOL> bulletAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> bulletAction . setStatusTip ( "<STR_LIT>" ) <EOL> bulletAction . setShortcut ( "<STR_LIT>" ) <EOL> bulletAction . triggered . connect ( self . bulletList ) <EOL> numberedAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> numberedAction . setStatusTip ( "<STR_LIT>" ) <EOL> numberedAction . setShortcut ( "<STR_LIT>" ) <EOL> numberedAction . triggered . connect ( self . numberList ) <EOL> self . toolbar = self . addToolBar ( "<STR_LIT>" ) <EOL> self . toolbar . addAction ( self . newAction ) <EOL> self . toolbar . addAction ( self . openAction ) <EOL> self . toolbar . addAction ( self . saveAction ) <EOL> self . toolbar . addSeparator ( ) <EOL> self . toolbar . addAction ( self . printAction ) <EOL> self . toolbar . addAction ( self . previewAction ) <EOL> self . toolbar . addSeparator ( ) <EOL> self . toolbar . addAction ( self . cutAction ) <EOL> self . toolbar . addAction ( self . copyAction ) <EOL> self . toolbar . addAction ( self . pasteAction ) <EOL> self . toolbar . addAction ( self . undoAction ) <EOL> self . toolbar . addAction ( self . redoAction ) <EOL> self . toolbar . addSeparator ( ) <EOL> self . toolbar . addAction ( self . findAction ) <EOL> self . toolbar . addAction ( wordCountAction ) <EOL> self . toolbar . addAction ( imageAction ) <EOL> self . toolbar . addSeparator ( ) <EOL> self . toolbar . addAction ( bulletAction ) <EOL> self . toolbar . addAction ( numberedAction ) <EOL> self . addToolBarBreak ( ) <EOL> def initFormatbar ( self ) : <EOL> fontBox = QtGui . QFontComboBox ( self ) <EOL> fontBox . currentFontChanged . connect ( lambda font : self . text . setCurrentFont ( font ) ) <EOL> fontSize = QtGui . QSpinBox ( self ) <EOL> fontSize . setSuffix ( "<STR_LIT>" ) <EOL> fontSize . valueChanged . connect ( lambda size : self . text . setFontPointSize ( size ) ) <EOL> fontSize . setValue ( <NUM_LIT> ) <EOL> fontColor = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> fontColor . triggered . connect ( self . fontColorChanged ) <EOL> boldAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> boldAction . triggered . connect ( self . bold ) <EOL> italicAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> italicAction . triggered . connect ( self . italic ) <EOL> underlAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> underlAction . triggered . connect ( self . underline ) <EOL> strikeAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> strikeAction . triggered . connect ( self . strike ) <EOL> superAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> superAction . triggered . connect ( self . superScript ) <EOL> subAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> subAction . triggered . connect ( self . subScript ) <EOL> alignLeft = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> alignLeft . triggered . connect ( self . alignLeft ) <EOL> alignCenter = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> alignCenter . triggered . connect ( self . alignCenter ) <EOL> alignRight = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> alignRight . triggered . connect ( self . alignRight ) <EOL> alignJustify = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> alignJustify . triggered . connect ( self . alignJustify ) <EOL> indentAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> indentAction . setShortcut ( "<STR_LIT>" ) <EOL> indentAction . triggered . connect ( self . indent ) <EOL> dedentAction = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> dedentAction . setShortcut ( "<STR_LIT>" ) <EOL> dedentAction . triggered . connect ( self . dedent ) <EOL> backColor = QtGui . QAction ( QtGui . QIcon ( "<STR_LIT>" ) , "<STR_LIT>" , self ) <EOL> backColor . triggered . connect ( self . highlight ) <EOL> self . formatbar = self . addToolBar ( "<STR_LIT>" ) <EOL> self . formatbar . addWidget ( fontBox ) <EOL> self . formatbar . addWidget ( fontSize ) <EOL> self . formatbar . addSeparator ( ) <EOL> self . formatbar . addAction ( fontColor ) <EOL> self . formatbar . addAction ( backColor ) <EOL> self . formatbar . addSeparator ( ) <EOL> self . formatbar . addAction ( boldAction ) <EOL> self . formatbar . addAction ( italicAction ) <EOL> self . formatbar . addAction ( underlAction ) <EOL> self . formatbar . addAction ( strikeAction ) <EOL> self . formatbar . addAction ( superAction ) <EOL> self . formatbar . addAction ( subAction ) <EOL> self . formatbar . addSeparator ( ) <EOL> self . formatbar . addAction ( alignLeft ) <EOL> self . formatbar . addAction ( alignCenter ) <EOL> self . formatbar . addAction ( alignRight ) <EOL> self . formatbar . addAction ( alignJustify ) <EOL> self . formatbar . addSeparator ( ) <EOL> self . formatbar . addAction ( indentAction ) <EOL> self . formatbar . addAction ( dedentAction ) <EOL> def initMenubar ( self ) : <EOL> menubar = self . menuBar ( ) <EOL> file = menubar . addMenu ( "<STR_LIT>" ) <EOL> edit = menubar . addMenu ( "<STR_LIT>" ) <EOL> view = menubar . addMenu ( "<STR_LIT>" ) <EOL> file . addAction ( self . newAction ) <EOL> file . addAction ( self . openAction ) <EOL> file . addAction ( self . saveAction ) <EOL> file . addAction ( self . printAction ) <EOL> file . addAction ( self . previewAction ) <EOL> edit . addAction ( self . undoAction ) <EOL> edit . addAction ( self . redoAction ) <EOL> edit . addAction ( self . cutAction ) <EOL> edit . addAction ( self . copyAction ) <EOL> edit . addAction ( self . pasteAction ) <EOL> edit . addAction ( self . findAction ) <EOL> toolbarAction = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> toolbarAction . triggered . connect ( self . toggleToolbar ) <EOL> formatbarAction = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> formatbarAction . triggered . connect ( self . toggleFormatbar ) <EOL> statusbarAction = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> statusbarAction . triggered . connect ( self . toggleStatusbar ) <EOL> view . addAction ( toolbarAction ) <EOL> view . addAction ( formatbarAction ) <EOL> view . addAction ( statusbarAction ) <EOL> def initUI ( self ) : <EOL> self . text = QtGui . QTextEdit ( self ) <EOL> self . text . setTabStopWidth ( <NUM_LIT> ) <EOL> self . initToolbar ( ) <EOL> self . initFormatbar ( ) <EOL> self . initMenubar ( ) <EOL> self . setCentralWidget ( self . text ) <EOL> self . statusbar = self . statusBar ( ) <EOL> self . text . cursorPositionChanged . connect ( self . cursorPosition ) <EOL> self . setGeometry ( <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT> ) <EOL> self . setWindowTitle ( "<STR_LIT>" ) <EOL> self . setWindowIcon ( QtGui . QIcon ( "<STR_LIT>" ) ) <EOL> def toggleToolbar ( self ) : <EOL> state = self . toolbar . isVisible ( ) <EOL> self . toolbar . setVisible ( not state ) <EOL> def toggleFormatbar ( self ) : <EOL> state = self . formatbar . isVisible ( ) <EOL> self . formatbar . setVisible ( not state ) <EOL> def toggleStatusbar ( self ) : <EOL> state = self . statusbar . isVisible ( ) <EOL> self . statusbar . setVisible ( not state ) <EOL> def new ( self ) : <EOL> spawn = Main ( ) <EOL> spawn . show ( ) <EOL> def open ( self ) : <EOL> self . filename = QtGui . QFileDialog . getOpenFileName ( self , '<STR_LIT>' , "<STR_LIT:.>" , "<STR_LIT>" ) <EOL> if self . filename : <EOL> with open ( self . filename , "<STR_LIT>" ) as file : <EOL> self . text . setText ( file . read ( ) ) <EOL> def save ( self ) : <EOL> if not self . filename : <EOL> self . filename = QtGui . QFileDialog . getSaveFileName ( self , '<STR_LIT>' ) <EOL> if not self . filename . endswith ( "<STR_LIT>" ) : <EOL> self . filename += "<STR_LIT>" <EOL> with open ( self . filename , "<STR_LIT>" ) as file : <EOL> file . write ( self . text . toHtml ( ) ) <EOL> def preview ( self ) : <EOL> preview = QtGui . QPrintPreviewDialog ( ) <EOL> preview . paintRequested . connect ( lambda p : self . text . print_ ( p ) ) <EOL> preview . exec_ ( ) <EOL> def printHandler ( self ) : <EOL> dialog = QtGui . QPrintDialog ( ) <EOL> if dialog . exec_ ( ) == QtGui . QDialog . Accepted : <EOL> self . text . document ( ) . print_ ( dialog . printer ( ) ) <EOL> def cursorPosition ( self ) : <EOL> cursor = self . text . textCursor ( ) <EOL> line = cursor . blockNumber ( ) + <NUM_LIT:1> <EOL> col = cursor . columnNumber ( ) <EOL> self . statusbar . showMessage ( "<STR_LIT>" . format ( line , col ) ) <EOL> def wordCount ( self ) : <EOL> wc = wordcount . WordCount ( self ) <EOL> wc . getText ( ) <EOL> wc . show ( ) <EOL> def insertImage ( self ) : <EOL> filename = QtGui . QFileDialog . getOpenFileName ( self , '<STR_LIT>' , "<STR_LIT:.>" , "<STR_LIT>" ) <EOL> if filename : <EOL> image = QtGui . QImage ( filename ) <EOL> if image . isNull ( ) : <EOL> popup = QtGui . QMessageBox ( QtGui . QMessageBox . Critical , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> QtGui . QMessageBox . Ok , <EOL> self ) <EOL> popup . show ( ) <EOL> else : <EOL> cursor = self . text . textCursor ( ) <EOL> cursor . insertImage ( image , filename ) <EOL> def fontColorChanged ( self ) : <EOL> color = QtGui . QColorDialog . getColor ( ) <EOL> self . text . setTextColor ( color ) <EOL> def highlight ( self ) : <EOL> color = QtGui . QColorDialog . getColor ( ) <EOL> self . text . setTextBackgroundColor ( color ) <EOL> def bold ( self ) : <EOL> if self . text . fontWeight ( ) == QtGui . QFont . Bold : <EOL> self . text . setFontWeight ( QtGui . QFont . Normal ) <EOL> else : <EOL> self . text . setFontWeight ( QtGui . QFont . Bold ) <EOL> def italic ( self ) : <EOL> state = self . text . fontItalic ( ) <EOL> self . text . setFontItalic ( not state ) <EOL> def underline ( self ) : <EOL> state = self . text . fontUnderline ( ) <EOL> self . text . setFontUnderline ( not state ) <EOL> def strike ( self ) : <EOL> fmt = self . text . currentCharFormat ( ) <EOL> fmt . setFontStrikeOut ( not fmt . fontStrikeOut ( ) ) <EOL> self . text . setCurrentCharFormat ( fmt ) <EOL> def superScript ( self ) : <EOL> fmt = self . text . currentCharFormat ( ) <EOL> align = fmt . verticalAlignment ( ) <EOL> if align == QtGui . QTextCharFormat . AlignNormal : <EOL> fmt . setVerticalAlignment ( QtGui . QTextCharFormat . AlignSuperScript ) <EOL> else : <EOL> fmt . setVerticalAlignment ( QtGui . QTextCharFormat . AlignNormal ) <EOL> self . text . setCurrentCharFormat ( fmt ) <EOL> def subScript ( self ) : <EOL> fmt = self . text . currentCharFormat ( ) <EOL> align = fmt . verticalAlignment ( ) <EOL> if align == QtGui . QTextCharFormat . AlignNormal : <EOL> fmt . setVerticalAlignment ( QtGui . QTextCharFormat . AlignSubScript ) <EOL> else : <EOL> fmt . setVerticalAlignment ( QtGui . QTextCharFormat . AlignNormal ) <EOL> self . text . setCurrentCharFormat ( fmt ) <EOL> def alignLeft ( self ) : <EOL> self . text . setAlignment ( Qt . AlignLeft ) <EOL> def alignRight ( self ) : <EOL> self . text . setAlignment ( Qt . AlignRight ) <EOL> def alignCenter ( self ) : <EOL> self . text . setAlignment ( Qt . AlignCenter ) <EOL> def alignJustify ( self ) : <EOL> self . text . setAlignment ( Qt . AlignJustify ) <EOL> def indent ( self ) : <EOL> cursor = self . text . textCursor ( ) <EOL> if cursor . hasSelection ( ) : <EOL> temp = cursor . blockNumber ( ) <EOL> cursor . setPosition ( cursor . anchor ( ) ) <EOL> diff = cursor . blockNumber ( ) - temp <EOL> direction = QtGui . QTextCursor . Up if diff > <NUM_LIT:0> else QtGui . QTextCursor . Down <EOL> for n in range ( abs ( diff ) + <NUM_LIT:1> ) : <EOL> cursor . movePosition ( QtGui . QTextCursor . StartOfLine ) <EOL> cursor . insertText ( "<STR_LIT:\t>" ) <EOL> cursor . movePosition ( direction ) <EOL> else : <EOL> cursor . insertText ( "<STR_LIT:\t>" ) <EOL> def handleDedent ( self , cursor ) : <EOL> cursor . movePosition ( QtGui . QTextCursor . StartOfLine ) <EOL> line = cursor . block ( ) . text ( ) <EOL> if line . startswith ( "<STR_LIT:\t>" ) : <EOL> cursor . deleteChar ( ) <EOL> else : <EOL> for char in line [ : <NUM_LIT:8> ] : <EOL> if char != "<STR_LIT:U+0020>" : <EOL> break <EOL> cursor . deleteChar ( ) <EOL> def dedent ( self ) : <EOL> cursor = self . text . textCursor ( ) <EOL> if cursor . hasSelection ( ) : <EOL> temp = cursor . blockNumber ( ) <EOL> cursor . setPosition ( cursor . anchor ( ) ) <EOL> diff = cursor . blockNumber ( ) - temp <EOL> direction = QtGui . QTextCursor . Up if diff > <NUM_LIT:0> else QtGui . QTextCursor . Down <EOL> for n in range ( abs ( diff ) + <NUM_LIT:1> ) : <EOL> self . handleDedent ( cursor ) <EOL> cursor . movePosition ( direction ) <EOL> else : <EOL> self . handleDedent ( cursor ) <EOL> def bulletList ( self ) : <EOL> cursor = self . text . textCursor ( ) <EOL> cursor . insertList ( QtGui . QTextListFormat . ListDisc ) <EOL> def numberList ( self ) : <EOL> cursor = self . text . textCursor ( ) <EOL> cursor . insertList ( QtGui . QTextListFormat . ListDecimal ) <EOL> def main ( ) : <EOL> app = QtGui . QApplication ( sys . argv ) <EOL> main = Main ( ) <EOL> main . show ( ) <EOL> sys . exit ( app . exec_ ( ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
