#!/usr/bin/env python # A simple application. import sys, string from qt import * fileopen = [ '16 13 5 1', '. c #040404', '# c #808304', 'a c None', 'b c #f3f704', 'c c #f3f7f3', 'aaaaaaaaa...aaaa', 'aaaaaaaa.aaa.a.a', 'aaaaaaaaaaaaa..a', 'a...aaaaaaaa...a', '.bcb.......aaaaa', '.cbcbcbcbc.aaaaa', '.bcbcbcbcb.aaaaa', '.cbcb...........', '.bcb.#########.a', '.cb.#########.aa', '.b.#########.aaa', '..#########.aaaa', '...........aaaaa' ] filesave = [ '14 14 4 1', '. c #040404', '# c #808304', 'a c #bfc2bf', 'b c None', '..............', '.#.aaaaaaaa.a.', '.#.aaaaaaaa...', '.#.aaaaaaaa.#.', '.#.aaaaaaaa.#.', '.#.aaaaaaaa.#.', '.#.aaaaaaaa.#.', '.##........##.', '.############.', '.##.........#.', '.##......aa.#.', '.##......aa.#.', '.##......aa.#.', 'b.............' ] fileprint = [ '16 14 6 1', '. c #000000', '# c #848284', 'a c #c6c3c6', 'b c #ffff00', 'c c #ffffff', 'd c None', 'ddddd.........dd', 'dddd.cccccccc.dd', 'dddd.c.....c.ddd', 'ddd.cccccccc.ddd', 'ddd.c.....c....d', 'dd.cccccccc.a.a.', 'd..........a.a..', '.aaaaaaaaaa.a.a.', '.............aa.', '.aaaaaa###aa.a.d', '.aaaaaabbbaa...d', '.............a.d', 'd.aaaaaaaaa.a.dd', 'dd...........ddd' ] fileOpenText = \ ''' Click this button to open a new file.

You can also select the Open command from the File menu.''' fileSaveText = \ '''Click this button to save the file you are editing.

You will be prompted for a filename.

You can also select the Save command from the File menu.''' filePrintText = \ '''Click this button to print the file you are editing.

You can also select the Print command from the File menu.''' editorList = [] class ApplicationWindow(TQMainWindow): def __init__(self): TQMainWindow.__init__(self,None,'example application main window',TQt.WDestructiveClose) self.filename = TQString.null self.printer = TQPrinter() self.fileTools = TQToolBar(self,'file operations') openIcon = TQPixmap(fileopen) self.fileOpen = TQToolButton(TQIconSet(openIcon),'Open File',TQString.null,self.load,self.fileTools,'open file') saveIcon = TQPixmap(filesave) self.fileSave = TQToolButton(TQIconSet(saveIcon),'Save File',TQString.null,self.save,self.fileTools,'save file') printIcon = TQPixmap(fileprint) self.filePrint = TQToolButton(TQIconSet(printIcon),'Print File',TQString.null,self.printDoc,self.fileTools,'print file') TQWhatsThis.whatsThisButton(self.fileTools) TQWhatsThis.add(self.fileOpen,fileOpenText) TQMimeSourceFactory.defaultFactory().setPixmap('fileopen',openIcon) TQWhatsThis.add(self.fileSave,fileSaveText) TQWhatsThis.add(self.filePrint,filePrintText) self.file = TQPopupMenu(self) self.menuBar().insertItem('&File',self.file) self.file.insertItem('&New',self.newDoc,TQt.CTRL + TQt.Key_N) id = self.file.insertItem(TQIconSet(openIcon),'&Open',self.load,TQt.CTRL + TQt.Key_O) self.file.setWhatsThis(id,fileOpenText) id = self.file.insertItem(TQIconSet(saveIcon),'&Save',self.save,TQt.CTRL + TQt.Key_S) self.file.setWhatsThis(id,fileSaveText) id = self.file.insertItem('Save &as',self.saveAs) self.file.setWhatsThis(id,fileSaveText) self.file.insertSeparator() id = self.file.insertItem(TQIconSet(printIcon),'&Print',self.printDoc,TQt.CTRL + TQt.Key_P) self.file.setWhatsThis(id,filePrintText) self.file.insertSeparator() self.file.insertItem('&Close',self,SLOT('close()'),TQt.CTRL + TQt.Key_W) self.file.insertItem('&Quit',qApp,SLOT('closeAllWindows()'),TQt.CTRL + TQt.Key_Q) self.help = TQPopupMenu(self) self.menuBar().insertSeparator() self.menuBar().insertItem('&Help',self.help) self.help.insertItem('&About',self.about,TQt.Key_F1) self.help.insertItem('About &TQt',self.aboutTQt) self.e = TQMultiLineEdit(self,'editor') self.e.setFocus() self.setCentralWidget(self.e) self.statusBar().message('Ready',2000) self.resize(450,600) def newDoc(self): ed = ApplicationWindow() ed.show() editorList.append(ed) def load(self): fn = TQFileDialog.getOpenFileName(TQString.null,TQString.null,self) if fn.isEmpty(): self.statusBar().message('Loading aborted',2000) return fileName = str(fn) self.e.setAutoUpdate(0) self.e.clear() try: f = open(fileName,'r') except: return for l in f.readlines(): self.e.append(string.rstrip(l)) f.close() self.e.setAutoUpdate(1) self.e.repaint() self.e.setEdited(0) self.setCaption(fileName) self.statusBar().message('Loaded document %s' % (fileName),2000) def save(self): if self.filename.isEmpty(): self.saveAs() return try: f = open(str(self.filename),'w+') except: self.statusBar().message('Could not write to %s' % (self.filename),2000) return f.write(str(self.e.text())) f.close() self.e.setEdited(0) self.setCaption(self.filename) self.statusBar().message('File %s saved' % (self.filename),2000) def saveAs(self): fn = TQFileDialog.getSaveFileName(TQString.null,TQString.null,self) if not fn.isEmpty(): self.filename = fn self.save() else: self.statusBar().message('Saving aborted',2000) def printDoc(self): Margin = 10 pageNo = 1 if self.printer.setup(self): self.statusBar().message('Printing...') p = TQPainter() p.begin(self.printer) p.setFont(self.e.font()) yPos = 0 fm = p.fontMetrics() metrics = TQPaintDeviceMetrics(self.printer) for i in range(self.e.numLines): if Margin + yPos > metrics.height() - Margin: pageNo = pageNo + 1 self.statusBar().message('Printing (page %d)...' % (pageNo)) self.printer.newPage() yPos = 0 p.drawText(Margin,Margin + yPos,metrics.width(),fm.lineSpacing(),TQt.ExpandTabs | TQt.DontClip,self.e.textLine(i)) yPos = yPos + fm.lineSpacing() p.end() self.statusBar().message('Printing completed',2000) else: self.statusBar().message('Printing aborted',2000) def closeEvent(self,ce): if not self.e.edited(): ce.accept() return rc = TQMessageBox.information(self,'TQt Application Example', 'The document has been changed since the last save.', 'Save Now','Cancel','Leave Anyway',0,1) if rc == 0: self.save() ce.accept() elif rc == 2: ce.accept() else: ce.ignore() def about(self): TQMessageBox.about(self,'TQt Application Example', 'This example demonstrates simple use of TQMainWindow,\nTQMenuBar and TQToolBar.') def aboutTQt(self): TQMessageBox.aboutTQt(self,'TQt Application Example') a = TQApplication(sys.argv) mw = ApplicationWindow() mw.setCaption('Document 1') mw.show() a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()')) a.exec_loop()