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.

361 lines
10KB

  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. document = [
  71. "12 16 6 1",
  72. " c #040404",
  73. ". c None",
  74. "X c white",
  75. "o c #808304",
  76. "O c black",
  77. "+ c #f3f7f3",
  78. " .....",
  79. " XXXXX ....",
  80. " XXXXX X ...",
  81. " XXXXX XX ..",
  82. " XooXX O.",
  83. " X+XXX+XXXO.",
  84. " XXXXXXXXXO.",
  85. " XoooXooXXO.",
  86. " XXXXXXXXXO.",
  87. " XXXXXXXXXO.",
  88. " XoXXoooXXO.",
  89. " XXXXXXXXXO.",
  90. "OXXXXXXXXXO.",
  91. "OXXXXXXXXXO.",
  92. "OOOOOOOOOOO.",
  93. "............"
  94. ]
  95. fileOpenText = \
  96. '''<img source="fileopen">
  97. Click this button to open a <em>new file</em>.<br><br>
  98. You can also select the <b>Open</b> command from the <b>File</b> menu.'''
  99. fileSaveText = \
  100. '''Click this button to save the file you are editing.<br><br>
  101. You will be prompted for a filename.<br><br>
  102. You can also select the <b>Save</b> command from the <b>File</b> menu.'''
  103. filePrintText = \
  104. '''Click this button to print the file you are editing.<br><br>
  105. You can also select the <b>Print</b> command from the <b>File</b> menu.'''
  106. True=1
  107. False=0
  108. class ApplicationWindow(TQMainWindow):
  109. def __init__(self):
  110. TQMainWindow.__init__(self,None,'example application main window',TQt.WDestructiveClose)
  111. self.filename = TQString.null
  112. self.printer = TQPrinter()
  113. self.fileTools = TQToolBar(self,'file operations')
  114. openIcon = TQPixmap(fileopen)
  115. self.fileOpen = TQToolButton(TQIconSet(openIcon),'Open File',TQString.null,self.load,self.fileTools,'open file')
  116. saveIcon = TQPixmap(filesave)
  117. self.fileSave = TQToolButton(TQIconSet(saveIcon),'Save File',TQString.null,self.save,self.fileTools,'save file')
  118. printIcon = TQPixmap(fileprint)
  119. self.filePrint = TQToolButton(TQIconSet(printIcon),'Print File',TQString.null,self.printDoc,self.fileTools,'print file')
  120. TQWhatsThis.whatsThisButton(self.fileTools)
  121. TQWhatsThis.add(self.fileOpen,fileOpenText)
  122. TQMimeSourceFactory.defaultFactory().setPixmap('fileopen',openIcon)
  123. TQWhatsThis.add(self.fileSave,fileSaveText)
  124. TQWhatsThis.add(self.filePrint,filePrintText)
  125. self.file = TQPopupMenu(self)
  126. self.menuBar().insertItem('&File',self.file)
  127. self.file.insertItem('&New',self.newDoc,TQt.CTRL + TQt.Key_N)
  128. id = self.file.insertItem(TQIconSet(openIcon),'&Open',self.load,TQt.CTRL + TQt.Key_O)
  129. self.file.setWhatsThis(id,fileOpenText)
  130. id = self.file.insertItem(TQIconSet(saveIcon),'&Save',self.save,TQt.CTRL + TQt.Key_S)
  131. self.file.setWhatsThis(id,fileSaveText)
  132. id = self.file.insertItem('Save &as',self.saveAs)
  133. self.file.setWhatsThis(id,fileSaveText)
  134. self.file.insertSeparator()
  135. id = self.file.insertItem(TQIconSet(printIcon),'&Print',self.printDoc,TQt.CTRL + TQt.Key_P)
  136. self.file.setWhatsThis(id,filePrintText)
  137. self.file.insertSeparator()
  138. self.file.insertItem('&Close',self,SLOT('close()'),TQt.CTRL + TQt.Key_W)
  139. self.file.insertItem('&Quit',tqApp,SLOT('closeAllWindows()'),TQt.CTRL + TQt.Key_Q)
  140. self.menuBar().insertSeparator()
  141. self.windows = TQPopupMenu( self )
  142. self.windows.setCheckable( True )
  143. self.connect( self.windows, SIGNAL( "aboutToShow()" ),
  144. self.windowsMenuAboutToShow )
  145. self.menuBar().insertItem( "&Windows", self.windows )
  146. self.help = TQPopupMenu(self)
  147. self.menuBar().insertSeparator()
  148. self.menuBar().insertItem('&Help',self.help)
  149. self.help.insertItem('&About',self.about,TQt.Key_F1)
  150. self.help.insertItem('About &TQt',self.aboutTQt)
  151. self.help.insertSeparator()
  152. self.help.insertItem( "What's &This", self, SLOT("whatsThis()"), TQt.SHIFT+TQt.Key_F1)
  153. self.menuBar().insertSeparator()
  154. self.vb = TQVBox( self )
  155. self.vb.setFrameStyle( TQFrame.StyledPanel | TQFrame.Sunken )
  156. self.ws = TQWorkspace( self.vb )
  157. self.ws.setScrollBarsEnabled( True )
  158. self.setCentralWidget( self.vb )
  159. self.statusBar().message('Ready',2000)
  160. #self.resize(450,600)
  161. def newDoc(self):
  162. w = MDIWindow( self.ws, "", TQt.WDestructiveClose )
  163. self.connect( w, PYSIGNAL( "message" ), self.statusBar(), SLOT( "message(const TQString&, int )") )
  164. w.setCaption("unnamed document")
  165. w.setIcon( TQPixmap(document) )
  166. # show the very first window in maximized mode
  167. if len(self.ws.windowList())==0:
  168. w.showMaximized()
  169. else:
  170. w.show()
  171. return w
  172. def load(self):
  173. fn = TQFileDialog.getOpenFileName( TQString.null, TQString.null, self )
  174. if not fn.isEmpty():
  175. w = self.newDoc()
  176. w.load( fn )
  177. else:
  178. self.statusBar().message( "Loading aborted", 2000 )
  179. def save(self):
  180. m = self.ws.activeWindow()
  181. if m:
  182. m.save()
  183. def saveAs(self):
  184. m = self.ws.activeWindow()
  185. if m:
  186. m.saveAs()
  187. def printDoc(self):
  188. m = self.ws.activeWindow()
  189. if m:
  190. m.printDoc( self.printer )
  191. def closeWindow(self):
  192. m = self.ws.activeWindow()
  193. if m:
  194. m.close()
  195. def about(self):
  196. TQMessageBox.about( self, "TQt Application Example",
  197. "This example demonstrates simple use of\n "
  198. "TQt's Multiple Document Interface (MDI).")
  199. def aboutTQt(self):
  200. TQMessageBox.aboutTQt( self, "TQt Application Example" )
  201. def windowsMenuAboutToShow(self):
  202. self.windows.clear()
  203. cascadeId = self.windows.insertItem("&Cascade", self.ws, SLOT("cascade() ") )
  204. tileId = self.windows.insertItem("&Tile", self.ws, SLOT("tile()" ) )
  205. if len(self.ws.windowList())==0 :
  206. self.windows.setItemEnabled( cascadeId, False )
  207. self.windows.setItemEnabled( tileId, False )
  208. self.windows.insertSeparator()
  209. windows = self.ws.windowList()
  210. cnt=0
  211. for i in windows:
  212. id =self.windows.insertItem(i.caption(),self.windowsMenuActivated )
  213. self.windows.setItemParameter( id, cnt );
  214. self.windows.setItemChecked( id, self.ws.activeWindow() == i )
  215. cnt=cnt+1
  216. def windowsMenuActivated(self,sid ):
  217. w = self.ws.windowList().at( id )
  218. if w:
  219. w.showNormal()
  220. w.setFocus()
  221. class MDIWindow( TQMainWindow):
  222. def __init__(self,parent, name, wflags ):
  223. TQMainWindow.__init__(self,parent, name, wflags )
  224. self.mmovie = 0
  225. self.medit = TQMultiLineEdit( self )
  226. self.setFocusProxy( self.medit )
  227. self.setCentralWidget( self.medit );
  228. def load(self, fn ):
  229. self.filename = fn
  230. self.f=TQFile( self.filename )
  231. if not self.f.open( IO_ReadOnly ):
  232. return
  233. if fn.contains(".gif"):
  234. tmp=TQWidget(self)
  235. self.setFocusProxy(tmp)
  236. self.setCentralWidget(tmp)
  237. self.medit.hide()
  238. del self.medit
  239. qm=TQMovie(fn)
  240. #ifdef Q_WS_QWS // temporary speed-test hack
  241. #qm->setDisplayWidget(tmp);
  242. #endif
  243. tmp.setBackgroundMode(TQWidget.NoBackground)
  244. tmp.show()
  245. self.mmovie=qm
  246. else :
  247. self.mmovie = 0
  248. t=TQTextStream(self.f)
  249. s = t.read()
  250. self.medit.setText( s )
  251. self.f.close()
  252. self.setCaption( self.filename )
  253. self.emit(PYSIGNAL( "message"),(TQString("Loaded document %1").arg(self.filename),2000 ))
  254. def save(self):
  255. if self.filename.isEmpty():
  256. self.saveAs()
  257. return
  258. text = self.medit.text()
  259. output=open(str(self.filename),'w')
  260. output.write(str(text))
  261. #emit message( TQString("Could not write to %1").arg(filename), 2000 );
  262. #return
  263. output.close()
  264. self.setCaption(self.filename)
  265. self.emit(PYSIGNAL( "message"),(TQString("File %1 saved").arg(self.filename),2000 ))
  266. def saveAs(self):
  267. fn = TQFileDialog.getSaveFileName( self.filename, TQString.null, self )
  268. if not fn.isEmpty():
  269. self.filename = fn
  270. self.save()
  271. else :
  272. self.emit(PYSIGNAL( "message"),(TQString("Saving aborted"),2000 ))
  273. def printDoc(self,printer):
  274. Margin = 10
  275. pageNo = 1
  276. if printer.setup(self):
  277. self.emit(PYSIGNAL( "message"),(TQString("Printing..."),2000 ))
  278. p = TQPainter()
  279. p.begin(printer)
  280. p.setFont(self.medit.font())
  281. yPos = 0
  282. fm = p.fontMetrics()
  283. metrics = TQPaintDeviceMetrics(printer)
  284. for i in range(self.medit.numLines()):
  285. if Margin + yPos > metrics.height() - Margin:
  286. pageNo = pageNo + 1
  287. self.emit(PYSIGNAL( "message"),(TQString("Printing (page %1) ...").arg(pageNo),2000 ))
  288. printer.newPage()
  289. yPos = 0
  290. p.drawText(Margin,Margin + yPos,metrics.width(),fm.lineSpacing(),TQt.ExpandTabs | TQt.DontClip,self.medit.textLine(i))
  291. yPos = yPos + fm.lineSpacing()
  292. p.end()
  293. self.emit(PYSIGNAL( "message"),(TQString("Printing completed"),2000 ))
  294. else:
  295. self.emit(PYSIGNAL( "message"),(TQString("Printing aborted"),2000 ))
  296. if __name__=='__main__':
  297. a = TQApplication(sys.argv)
  298. mw = ApplicationWindow()
  299. mw.setCaption("PyTQt Example - Multiple Documents Interface (MDI)")
  300. mw.show()
  301. a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
  302. a.exec_loop()