SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application.py 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env python
  2. # A simple application.
  3. import sys, string
  4. from python_tqt.qt import *
  5. fileopen = [
  6. '16 13 5 1',
  7. '. c #040404',
  8. '# c #808304',
  9. 'a c None',
  10. 'b c #f3f704',
  11. 'c c #f3f7f3',
  12. 'aaaaaaaaa...aaaa',
  13. 'aaaaaaaa.aaa.a.a',
  14. 'aaaaaaaaaaaaa..a',
  15. 'a...aaaaaaaa...a',
  16. '.bcb.......aaaaa',
  17. '.cbcbcbcbc.aaaaa',
  18. '.bcbcbcbcb.aaaaa',
  19. '.cbcb...........',
  20. '.bcb.#########.a',
  21. '.cb.#########.aa',
  22. '.b.#########.aaa',
  23. '..#########.aaaa',
  24. '...........aaaaa'
  25. ]
  26. filesave = [
  27. '14 14 4 1',
  28. '. c #040404',
  29. '# c #808304',
  30. 'a c #bfc2bf',
  31. 'b c None',
  32. '..............',
  33. '.#.aaaaaaaa.a.',
  34. '.#.aaaaaaaa...',
  35. '.#.aaaaaaaa.#.',
  36. '.#.aaaaaaaa.#.',
  37. '.#.aaaaaaaa.#.',
  38. '.#.aaaaaaaa.#.',
  39. '.##........##.',
  40. '.############.',
  41. '.##.........#.',
  42. '.##......aa.#.',
  43. '.##......aa.#.',
  44. '.##......aa.#.',
  45. 'b.............'
  46. ]
  47. fileprint = [
  48. '16 14 6 1',
  49. '. c #000000',
  50. '# c #848284',
  51. 'a c #c6c3c6',
  52. 'b c #ffff00',
  53. 'c c #ffffff',
  54. 'd c None',
  55. 'ddddd.........dd',
  56. 'dddd.cccccccc.dd',
  57. 'dddd.c.....c.ddd',
  58. 'ddd.cccccccc.ddd',
  59. 'ddd.c.....c....d',
  60. 'dd.cccccccc.a.a.',
  61. 'd..........a.a..',
  62. '.aaaaaaaaaa.a.a.',
  63. '.............aa.',
  64. '.aaaaaa###aa.a.d',
  65. '.aaaaaabbbaa...d',
  66. '.............a.d',
  67. 'd.aaaaaaaaa.a.dd',
  68. 'dd...........ddd'
  69. ]
  70. fileOpenText = \
  71. '''<img source="fileopen">
  72. Click this button to open a <em>new file</em>.<br><br>
  73. You can also select the <b>Open</b> command from the <b>File</b> menu.'''
  74. fileSaveText = \
  75. '''Click this button to save the file you are editing.<br><br>
  76. You will be prompted for a filename.<br><br>
  77. You can also select the <b>Save</b> command from the <b>File</b> menu.'''
  78. filePrintText = \
  79. '''Click this button to print the file you are editing.<br><br>
  80. You can also select the <b>Print</b> command from the <b>File</b> menu.'''
  81. editorList = []
  82. class ApplicationWindow(TQMainWindow):
  83. def __init__(self):
  84. TQMainWindow.__init__(self,None,'example application main window',TQt.WDestructiveClose)
  85. self.filename = TQString.null
  86. self.printer = TQPrinter()
  87. self.fileTools = TQToolBar(self,'file operations')
  88. openIcon = TQPixmap(fileopen)
  89. self.fileOpen = TQToolButton(TQIconSet(openIcon),'Open File',TQString.null,self.load,self.fileTools,'open file')
  90. saveIcon = TQPixmap(filesave)
  91. self.fileSave = TQToolButton(TQIconSet(saveIcon),'Save File',TQString.null,self.save,self.fileTools,'save file')
  92. printIcon = TQPixmap(fileprint)
  93. self.filePrint = TQToolButton(TQIconSet(printIcon),'Print File',TQString.null,self.printDoc,self.fileTools,'print file')
  94. TQWhatsThis.whatsThisButton(self.fileTools)
  95. TQWhatsThis.add(self.fileOpen,fileOpenText)
  96. TQMimeSourceFactory.defaultFactory().setPixmap('fileopen',openIcon)
  97. TQWhatsThis.add(self.fileSave,fileSaveText)
  98. TQWhatsThis.add(self.filePrint,filePrintText)
  99. self.file = TQPopupMenu(self)
  100. self.menuBar().insertItem('&File',self.file)
  101. self.file.insertItem('&New',self.newDoc,TQt.CTRL + TQt.Key_N)
  102. id = self.file.insertItem(TQIconSet(openIcon),'&Open',self.load,TQt.CTRL + TQt.Key_O)
  103. self.file.setWhatsThis(id,fileOpenText)
  104. id = self.file.insertItem(TQIconSet(saveIcon),'&Save',self.save,TQt.CTRL + TQt.Key_S)
  105. self.file.setWhatsThis(id,fileSaveText)
  106. id = self.file.insertItem('Save &as',self.saveAs)
  107. self.file.setWhatsThis(id,fileSaveText)
  108. self.file.insertSeparator()
  109. id = self.file.insertItem(TQIconSet(printIcon),'&Print',self.printDoc,TQt.CTRL + TQt.Key_P)
  110. self.file.setWhatsThis(id,filePrintText)
  111. self.file.insertSeparator()
  112. self.file.insertItem('&Close',self,SLOT('close()'),TQt.CTRL + TQt.Key_W)
  113. self.file.insertItem('&Quit',tqApp,SLOT('closeAllWindows()'),TQt.CTRL + TQt.Key_Q)
  114. self.help = TQPopupMenu(self)
  115. self.menuBar().insertSeparator()
  116. self.menuBar().insertItem('&Help',self.help)
  117. self.help.insertItem('&About',self.about,TQt.Key_F1)
  118. self.help.insertItem('About &TQt',self.aboutTQt)
  119. self.e = TQMultiLineEdit(self,'editor')
  120. self.e.setFocus()
  121. self.setCentralWidget(self.e)
  122. self.statusBar().message('Ready',2000)
  123. self.resize(450,600)
  124. def newDoc(self):
  125. ed = ApplicationWindow()
  126. ed.show()
  127. editorList.append(ed)
  128. def load(self):
  129. fn = TQFileDialog.getOpenFileName(TQString.null,TQString.null,self)
  130. if fn.isEmpty():
  131. self.statusBar().message('Loading aborted',2000)
  132. return
  133. fileName = str(fn)
  134. self.e.setAutoUpdate(0)
  135. self.e.clear()
  136. try:
  137. f = open(fileName,'r')
  138. except:
  139. return
  140. for l in f.readlines():
  141. self.e.append(string.rstrip(l))
  142. f.close()
  143. self.e.setAutoUpdate(1)
  144. self.e.repaint()
  145. self.e.setEdited(0)
  146. self.setCaption(fileName)
  147. self.statusBar().message('Loaded document %s' % (fileName),2000)
  148. def save(self):
  149. if self.filename.isEmpty():
  150. self.saveAs()
  151. return
  152. try:
  153. f = open(str(self.filename),'w+')
  154. except:
  155. self.statusBar().message('Could not write to %s' % (self.filename),2000)
  156. return
  157. f.write(str(self.e.text()))
  158. f.close()
  159. self.e.setEdited(0)
  160. self.setCaption(self.filename)
  161. self.statusBar().message('File %s saved' % (self.filename),2000)
  162. def saveAs(self):
  163. fn = TQFileDialog.getSaveFileName(TQString.null,TQString.null,self)
  164. if not fn.isEmpty():
  165. self.filename = fn
  166. self.save()
  167. else:
  168. self.statusBar().message('Saving aborted',2000)
  169. def printDoc(self):
  170. Margin = 10
  171. pageNo = 1
  172. if self.printer.setup(self):
  173. self.statusBar().message('Printing...')
  174. p = TQPainter()
  175. p.begin(self.printer)
  176. p.setFont(self.e.font())
  177. yPos = 0
  178. fm = p.fontMetrics()
  179. metrics = TQPaintDeviceMetrics(self.printer)
  180. for i in range(self.e.numLines):
  181. if Margin + yPos > metrics.height() - Margin:
  182. pageNo = pageNo + 1
  183. self.statusBar().message('Printing (page %d)...' % (pageNo))
  184. self.printer.newPage()
  185. yPos = 0
  186. p.drawText(Margin,Margin + yPos,metrics.width(),fm.lineSpacing(),TQt.ExpandTabs | TQt.DontClip,self.e.textLine(i))
  187. yPos = yPos + fm.lineSpacing()
  188. p.end()
  189. self.statusBar().message('Printing completed',2000)
  190. else:
  191. self.statusBar().message('Printing aborted',2000)
  192. def closeEvent(self,ce):
  193. if not self.e.edited():
  194. ce.accept()
  195. return
  196. rc = TQMessageBox.information(self,'TQt Application Example',
  197. 'The document has been changed since the last save.',
  198. 'Save Now','Cancel','Leave Anyway',0,1)
  199. if rc == 0:
  200. self.save()
  201. ce.accept()
  202. elif rc == 2:
  203. ce.accept()
  204. else:
  205. ce.ignore()
  206. def about(self):
  207. TQMessageBox.about(self,'TQt Application Example',
  208. 'This example demonstrates simple use of TQMainWindow,\nTQMenuBar and TQToolBar.')
  209. def aboutTQt(self):
  210. TQMessageBox.aboutTQt(self,'TQt Application Example')
  211. a = TQApplication(sys.argv)
  212. mw = ApplicationWindow()
  213. mw.setCaption('Document 1')
  214. mw.show()
  215. a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
  216. a.exec_loop()