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.

addressbook.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #!/usr/bin/env python
  2. # A port to PyTQt of the application example from TQt v2.x.
  3. import sys, string
  4. from python_tqt.qt import *
  5. TRUE = 1
  6. FALSE = 0
  7. fileopen = [
  8. ' 16 13 5 1',
  9. '. c #040404',
  10. '# c #808304',
  11. 'a c None',
  12. 'b c #f3f704',
  13. 'c c #f3f7f3',
  14. 'aaaaaaaaa...aaaa',
  15. 'aaaaaaaa.aaa.a.a',
  16. 'aaaaaaaaaaaaa..a',
  17. 'a...aaaaaaaa...a',
  18. '.bcb.......aaaaa',
  19. '.cbcbcbcbc.aaaaa',
  20. '.bcbcbcbcb.aaaaa',
  21. '.cbcb...........',
  22. '.bcb.#########.a',
  23. '.cb.#########.aa',
  24. '.b.#########.aaa',
  25. '..#########.aaaa',
  26. '...........aaaaa'
  27. ]
  28. filesave = [
  29. ' 14 14 4 1',
  30. '. c #040404',
  31. '# c #808304',
  32. 'a c #bfc2bf',
  33. 'b c None',
  34. '..............',
  35. '.#.aaaaaaaa.a.',
  36. '.#.aaaaaaaa...',
  37. '.#.aaaaaaaa.#.',
  38. '.#.aaaaaaaa.#.',
  39. '.#.aaaaaaaa.#.',
  40. '.#.aaaaaaaa.#.',
  41. '.##........##.',
  42. '.############.',
  43. '.##.........#.',
  44. '.##......aa.#.',
  45. '.##......aa.#.',
  46. '.##......aa.#.',
  47. 'b.............'
  48. ]
  49. fileprint = [
  50. ' 16 14 6 1',
  51. '. c #000000',
  52. '# c #848284',
  53. 'a c #c6c3c6',
  54. 'b c #ffff00',
  55. 'c c #ffffff',
  56. 'd c None',
  57. 'ddddd.........dd',
  58. 'dddd.cccccccc.dd',
  59. 'dddd.c.....c.ddd',
  60. 'ddd.cccccccc.ddd',
  61. 'ddd.c.....c....d',
  62. 'dd.cccccccc.a.a.',
  63. 'd..........a.a..',
  64. '.aaaaaaaaaa.a.a.',
  65. '.............aa.',
  66. '.aaaaaa###aa.a.d',
  67. '.aaaaaabbbaa...d',
  68. '.............a.d',
  69. 'd.aaaaaaaaa.a.dd',
  70. 'dd...........ddd'
  71. ]
  72. class ABCentralWidget( TQWidget ):
  73. def __init__( self, *args ):
  74. apply( TQWidget.__init__, (self, ) + args )
  75. self.mainGrid = TQGridLayout( self, 2, 1, 5, 5 )
  76. self.setupTabWidget()
  77. self.setupListView()
  78. self.mainGrid.setRowStretch( 0, 0 )
  79. self.mainGrid.setRowStretch( 1, 1 )
  80. def save( self, filename ):
  81. if not self.listView.firstChild():
  82. return
  83. f = TQFile( filename )
  84. if not f.open( IO_WriteOnly ):
  85. return
  86. t = TQTextStream( f )
  87. #it = TQListViewItemIterator( self.listView )
  88. while not t.eof():
  89. item = TQListViewItem( self.listView )
  90. for i in range (0, 4):
  91. t << item.text( i ) << '\n'
  92. #for it in it.current():
  93. #for i in range (0, 4):
  94. #t << it.current().text[ i ] << "\n"
  95. f.close()
  96. def load( self, filename ):
  97. self.listView.clear()
  98. f = TQFile( filename )
  99. if not f.open( IO_ReadOnly ):
  100. return
  101. t = TQTextStream( f )
  102. while not t.eof():
  103. item = TQListViewItem( self.listView )
  104. for i in range (0, 4):
  105. item.setText( i, t.readLine() )
  106. f.close();
  107. def setupTabWidget( self ):
  108. self.tabWidget = TQTabWidget( self )
  109. self.input = TQWidget( self.tabWidget )
  110. self.grid1 = TQGridLayout( self.input, 2, 5, 5, 5 )
  111. self.liFirstName = TQLabel( 'First &Name', self.input )
  112. self.liFirstName.resize( self.liFirstName.sizeHint() )
  113. self.grid1.addWidget( self.liFirstName, 0, 0 )
  114. self.liLastName = TQLabel( '&Last Name', self.input )
  115. self.liLastName.resize( self.liLastName.sizeHint() )
  116. self.grid1.addWidget( self.liLastName, 0, 1 )
  117. self.liAddress = TQLabel( '&Address', self.input )
  118. self.liAddress.resize( self.liAddress.sizeHint() )
  119. self.grid1.addWidget( self.liAddress, 0, 2 )
  120. self.liEMail = TQLabel( '&E-Mail', self.input )
  121. self.liEMail.resize( self.liEMail.sizeHint() )
  122. self.grid1.addWidget( self.liEMail, 0, 3 )
  123. self.add = TQPushButton( '&Add', self.input )
  124. self.add.resize( self.add.sizeHint() )
  125. self.grid1.addWidget( self.add, 0, 4 )
  126. self.connect( self.add, SIGNAL( 'clicked()' ), self.addEntry )
  127. self.iFirstName = TQLineEdit( self.input )
  128. self.iFirstName.resize( self.iFirstName.sizeHint() )
  129. self.grid1.addWidget( self.iFirstName, 1, 0 )
  130. self.liFirstName.setBuddy( self.iFirstName )
  131. self.iLastName = TQLineEdit( self.input )
  132. self.iLastName.resize( self.iLastName.sizeHint() )
  133. self.grid1.addWidget( self.iLastName, 1, 1 )
  134. self.liLastName.setBuddy( self.iLastName )
  135. self.iAddress = TQLineEdit( self.input )
  136. self.iAddress.resize( self.iAddress.sizeHint() )
  137. self.grid1.addWidget( self.iAddress, 1, 2 )
  138. self.liAddress.setBuddy( self.iAddress )
  139. self.iEMail = TQLineEdit( self.input )
  140. self.iEMail.resize( self.iEMail.sizeHint() )
  141. self.grid1.addWidget( self.iEMail, 1, 3 )
  142. self.liEMail.setBuddy( self.iEMail )
  143. self.change = TQPushButton( '&Change', self.input )
  144. self.change.resize( self.change.sizeHint() )
  145. self.grid1.addWidget( self.change, 1, 4 )
  146. self.connect( self.change, SIGNAL( 'clicked()' ), self.changeEntry )
  147. self.tabWidget.addTab( self.input, '&Add/Change Entry' )
  148. # --------------------------------------
  149. self.search = TQWidget( self )
  150. self.grid2 = TQGridLayout( self.search, 2, 5, 5, 5 )
  151. self.cFirstName = TQCheckBox( 'First &Name', self.search )
  152. self.cFirstName.resize( self.cFirstName.sizeHint() )
  153. self.grid2.addWidget( self.cFirstName, 0, 0 )
  154. self.connect( self.cFirstName, SIGNAL( 'clicked()' ), self.toggleFirstName )
  155. self.cLastName = TQCheckBox( '&Last Name', self.search )
  156. self.cLastName.resize( self.cLastName.sizeHint() )
  157. self.grid2.addWidget( self.cLastName, 0, 1 )
  158. self.connect( self.cLastName, SIGNAL( 'clicked()' ), self.toggleLastName )
  159. self.cAddress = TQCheckBox( '&Address', self.search )
  160. self.cAddress.resize( self.cAddress.sizeHint() )
  161. self.grid2.addWidget( self.cAddress, 0, 2 )
  162. self.connect( self.cAddress, SIGNAL( 'clicked()' ), self.toggleAddress )
  163. self.cEMail = TQCheckBox( '&E-Mail', self.search )
  164. self.cEMail.resize( self.cEMail.sizeHint() )
  165. self.grid2.addWidget( self.cEMail, 0, 3 )
  166. self.connect( self.cEMail, SIGNAL( 'clicked()' ), self.toggleEMail )
  167. self.sFirstName = TQLineEdit( self.search )
  168. self.sFirstName.resize( self.sFirstName.sizeHint() )
  169. self.grid2.addWidget( self.sFirstName, 1, 0 )
  170. self.sLastName = TQLineEdit( self.search )
  171. self.sLastName.resize( self.sLastName.sizeHint() )
  172. self.grid2.addWidget( self.sLastName, 1, 1 )
  173. self.sAddress = TQLineEdit( self.search )
  174. self.sAddress.resize( self.sAddress.sizeHint() )
  175. self.grid2.addWidget( self.sAddress, 1, 2 )
  176. self.sEMail = TQLineEdit( self.search )
  177. self.sEMail.resize( self.sEMail.sizeHint() )
  178. self.grid2.addWidget( self.sEMail, 1, 3 )
  179. self.find = TQPushButton( '&Find', self.search )
  180. self.find.resize( self.find.sizeHint() )
  181. self.grid2.addWidget( self.find, 1, 4 )
  182. self.connect( self.find, SIGNAL( 'clicked()' ), self.findEntries )
  183. self.cFirstName.setChecked( TRUE )
  184. self.sFirstName.setEnabled( TRUE )
  185. self.sLastName.setEnabled( FALSE )
  186. self.sAddress.setEnabled( FALSE )
  187. self.sEMail.setEnabled( FALSE )
  188. self.tabWidget.addTab( self.search, "&Search" )
  189. self.mainGrid.addWidget( self.tabWidget, 0, 0 )
  190. def setupListView( self ):
  191. self.listView = TQListView( self )
  192. self.listView.addColumn( 'First Name' )
  193. self.listView.addColumn( 'Last Name' )
  194. self.listView.addColumn( 'Address' )
  195. self.listView.addColumn( 'E-Mail' )
  196. self.listView.setSelectionMode( TQListView.Extended )
  197. self.connect( self.listView, SIGNAL( 'clicked( TQListViewItem* )' ), self.itemSelected )
  198. self.mainGrid.addWidget( self.listView, 1, 0 )
  199. self.listView.setAllColumnsShowFocus( TRUE )
  200. def addEntry( self ):
  201. if not self.iFirstName.text().isEmpty() or not self.iLastName.text().isEmpty() or \
  202. not self.iAddress.text().isEmpty() or not self.iEMail.text().isEmpty() :
  203. self.item = TQListViewItem( self.listView )
  204. self.item.setText( 0, self.iFirstName.text() )
  205. self.item.setText( 1, self.iLastName.text() )
  206. self.item.setText( 2, self.iAddress.text() )
  207. self.item.setText( 3, self.iEMail.text() )
  208. self.iFirstName.setText( '' )
  209. self.iLastName.setText( '' )
  210. self.iAddress.setText( '' )
  211. self.iEMail.setText( '' )
  212. def changeEntry( self ):
  213. self.item = self.listView.currentItem()
  214. if self.item and ( not self.iFirstName.text().isEmpty() or not self.iLastName.text().isEmpty() or \
  215. not self.iAddress.text().isEmpty() or not self.iEMail.text().isEmpty() ) :
  216. self.item.setText( 0, self.iFirstName.text() )
  217. self.item.setText( 1, self.iLastName.text() )
  218. self.item.setText( 2, self.iAddress.text() )
  219. self.item.setText( 3, self.iEMail.text() )
  220. def selectionChanged( self ):
  221. self.iFirstName.setText( '' )
  222. self.iLastName.setText( '' )
  223. self.iAddress.setText( '' )
  224. self.iEMail.setText( '' )
  225. def itemSelected( self, item ):
  226. self.item.setSelected( TRUE )
  227. self.item.repaint()
  228. self.iFirstName.setText( item.text( 0 ) )
  229. self.iLastName.setText( item.text( 1 ) )
  230. self.iAddress.setText( item.text( 2 ) )
  231. self.iEMail.setText( item.text( 3 ) )
  232. def toggleFirstName( self ):
  233. self.sFirstName.setText( '' )
  234. if self.cFirstName.isChecked():
  235. self.sFirstName.setEnabled( TRUE )
  236. self.sFirstName.setFocus()
  237. else:
  238. self.sFirstName.setEnabled( FALSE )
  239. def toggleLastName( self ):
  240. self.sLastName.setText( '' )
  241. if self.cLastName.isChecked():
  242. self.sLastName.setEnabled( TRUE )
  243. self.sLastName.setFocus()
  244. else:
  245. self.sLastName.setEnabled( FALSE )
  246. def toggleAddress( self ):
  247. self.sAddress.setText( '' )
  248. if self.cAddress.isChecked():
  249. self.sAddress.setEnabled( TRUE )
  250. self.sAddress.setFocus()
  251. else:
  252. self.sAddress.setEnabled( FALSE )
  253. def toggleEMail( self ):
  254. self.sEMail.setText( '' )
  255. if self.cEMail.isChecked():
  256. self.sEMail.setEnabled( TRUE )
  257. self.sEMail.setFocus()
  258. else:
  259. self.sEMail.setEnabled( FALSE )
  260. def findEntries( self ):
  261. if not self.cFirstName.isChecked() and not self.cLastName.isChecked() and \
  262. not self.cAddress.isChecked() and not self.cEMail.isChecked():
  263. self.listView.clearSelection()
  264. return
  265. it = TQListViewItemIterator( self.listView )
  266. for it in it.current() :
  267. select = TRUE
  268. if self.cFirstName.isChecked():
  269. if select and it.current().text( 0 ).contains( self.sFirstName.text() ):
  270. select = TRUE
  271. else:
  272. select = FALSE
  273. if self.cLastName.isChecked():
  274. if select and it.current().text( 1 ).contains( self.sLastName.text() ):
  275. select = TRUE
  276. else:
  277. select = FALSE
  278. if self.cAddress.isChecked():
  279. if select and it.current().text( 2 ).contains( self.sAddress.text() ):
  280. select = TRUE
  281. else:
  282. select = FALSE
  283. if self.cEMail.isChecked():
  284. if select and it.current().text( 3 ).contains( self.sEMail.text() ):
  285. select = TRUE
  286. else:
  287. select = FALSE
  288. if select:
  289. it.current().setSelected( TRUE )
  290. else:
  291. it.current().setSelected( FALSE )
  292. it.current().repaint()
  293. class ABMainWindow(TQMainWindow):
  294. def __init__( self ):
  295. TQMainWindow.__init__( self, None, 'example addressbook application' )
  296. self.filename = TQString.null
  297. self.setupMenuBar()
  298. self.setupFileTools()
  299. self.setupStatusBar()
  300. self.setupCentralWidget()
  301. def setupMenuBar( self ):
  302. self.file = TQPopupMenu( self )
  303. self.menuBar().insertItem( '&File', self.file )
  304. openIcon = TQIconSet( TQPixmap( fileopen ) )
  305. self.file.insertItem( 'New', self.fileNew, TQt.CTRL + TQt.Key_N )
  306. self.file.insertItem( openIcon, 'Open', self.fileOpen, TQt.CTRL + TQt.Key_O )
  307. self.file.insertSeparator()
  308. saveIcon = TQIconSet( TQPixmap( filesave ) )
  309. self.file.insertItem( saveIcon, 'Save', self.fileSave, TQt.CTRL + TQt.Key_S )
  310. self.file.insertItem( 'Save As...', self.fileSaveAs )
  311. self.file.insertSeparator()
  312. printIcon = TQIconSet( TQPixmap( fileprint ) )
  313. self.file.insertItem( printIcon, 'Print...', self.filePrint, TQt.CTRL + TQt.Key_P )
  314. self.file.insertSeparator()
  315. #self.file.insertItem( 'Close', self.closeWindow, TQt.CTRL + TQt.Key_W )
  316. self.file.insertItem('Close', self, SLOT('close()'), TQt.CTRL+TQt.Key_W)
  317. self.file.insertItem( 'Quit', tqApp, SLOT( 'quit()' ), TQt.CTRL + TQt.Key_Q )
  318. def setupFileTools( self ):
  319. pass
  320. #self.fileTools = TQToolBar( self, 'file operations' )
  321. def setupStatusBar( self ):
  322. self.statusBar().message( "Ready", 2000 )
  323. def setupCentralWidget( self ):
  324. self.view = ABCentralWidget( self )
  325. #self.view.show()
  326. self.setCentralWidget( self.view )
  327. def closeWindow( self ):
  328. close()
  329. def fileNew( self ):
  330. pass
  331. def fileOpen( self ):
  332. fn = TQFileDialog.getOpenFileName( TQString.null, TQString.null, self )
  333. if not fn.isEmpty():
  334. self.filename = fn
  335. self.view.load( self.filename )
  336. def fileSave( self ):
  337. if self.filename.isEmpty():
  338. self.fileSaveAs()
  339. return
  340. self.view.save( self.filename )
  341. def fileSaveAs( self ):
  342. fn = TQFileDialog.getSaveFileName( TQString.null, TQString.null, self )
  343. if not fn.isEmpty():
  344. self.filename = fn
  345. self.fileSave
  346. def filePrint( self ):
  347. pass
  348. a = TQApplication( sys.argv )
  349. mw = ABMainWindow()
  350. #mw.setupMenuBar()
  351. #mw.setupFileTools
  352. #mw.setupStatusBar
  353. #mw.setupCentralWidget
  354. #view = ABCentralWidget()
  355. #mw.setCentralWidget( view )
  356. mw.setCaption( 'Addressbook 1' )
  357. a.setMainWidget( mw )
  358. mw.show()
  359. a.connect( a, SIGNAL( 'lastWindowClosed()' ), a, SLOT( 'quit()' ) )
  360. a.exec_loop()