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.

menu.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: menu.py,v 1.1.1.1 2002/06/04 23:04:42 phil Exp $
  4. #**
  5. #** Copyright (C) 1992-1998 Troll Tech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for PyTQt. This example
  8. #** program may be used, distributed and modified without limitation.
  9. #**
  10. #*****************************************************************************/
  11. import sys, string
  12. from python_tqt.qt import *
  13. TRUE = 1
  14. FALSE = 0
  15. # XPM
  16. p1_xpm = [
  17. "16 16 3 1",
  18. " c None",
  19. ". c #000000000000",
  20. "X c #FFFFFFFF0000",
  21. " ",
  22. " ",
  23. " .... ",
  24. " .XXXX. ",
  25. " .............. ",
  26. " .XXXXXXXXXXXX. ",
  27. " .XXXXXXXXXXXX. ",
  28. " .XXXXXXXXXXXX. ",
  29. " .XXXXXXXXXXXX. ",
  30. " .XXXXXXXXXXXX. ",
  31. " .XXXXXXXXXXXX. ",
  32. " .XXXXXXXXXXXX. ",
  33. " .XXXXXXXXXXXX. ",
  34. " .XXXXXXXXXXXX. ",
  35. " .............. ",
  36. " "
  37. ]
  38. # XPM
  39. p2_xpm = [
  40. "16 16 3 1",
  41. " c None",
  42. ". c #000000000000",
  43. "X c #FFFFFFFFFFFF",
  44. " ",
  45. " ...... ",
  46. " .XXX.X. ",
  47. " .XXX.XX. ",
  48. " .XXX.XXX. ",
  49. " .XXX..... ",
  50. " .XXXXXXX. ",
  51. " .XXXXXXX. ",
  52. " .XXXXXXX. ",
  53. " .XXXXXXX. ",
  54. " .XXXXXXX. ",
  55. " .XXXXXXX. ",
  56. " .XXXXXXX. ",
  57. " ......... ",
  58. " ",
  59. " "
  60. ]
  61. # XPM
  62. p3_xpm = [
  63. "16 16 3 1",
  64. " c None",
  65. ". c #000000000000",
  66. "X c #FFFFFFFFFFFF",
  67. " ",
  68. " ",
  69. " ......... ",
  70. " ........... ",
  71. " ........ .. ",
  72. " ........... ",
  73. " ........... ",
  74. " ........... ",
  75. " ........... ",
  76. " ...XXXXX... ",
  77. " ...XXXXX... ",
  78. " ...XXXXX... ",
  79. " ...XXXXX... ",
  80. " ......... ",
  81. " ",
  82. " "
  83. ]
  84. p4_xpm = [
  85. ' 16 14 5 1',
  86. '. c #000000',
  87. '# c #848284',
  88. 'a c #c6c3c6',
  89. 'b c #ffff00',
  90. 'c c #ffffff',
  91. 'aaaaa.........aa',
  92. 'aaaa.cccccccc.aa',
  93. 'aaaa.c.....c.aaa',
  94. 'aaa.cccccccc.aaa',
  95. 'aaa.c.....c....a',
  96. 'aa.cccccccc.a.a.',
  97. 'a..........a.a..',
  98. '.aaaaaaaaaa.a.a.',
  99. '.............aa.',
  100. '.aaaaaa###aa.a.a',
  101. '.aaaaaabbbaa...a',
  102. '.............a.a',
  103. 'a.aaaaaaaaa.a.aa',
  104. 'aa...........aaa'
  105. ]
  106. # Auxiliary class to provide fancy menu items with different fonts.
  107. # Used for the "bold" and "underline" menu items in the options menu.
  108. #class MyMenuItem( TQCustomMenuItem ):
  109. # def __init__( self, s=None, f=None ):
  110. # apply( TQCustomMenuItem.__init__,( self, s, f ) )
  111. # string = TQString( s )
  112. # font = TQFont( f )
  113. # def paint( self, p, TRUE, FALSE, x, y, w, h ) :
  114. # p.setFont ( font )
  115. # p.drawText( x, y, w, h, TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip, string )
  116. # def sizeHint( self ):
  117. # return TQFontMetrics( font ).size( TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip, string )
  118. #
  119. ### Implementation of MenuExample class
  120. #
  121. class MenuExample( TQWidget ):
  122. def __init__( self, parent=None, name=None ):
  123. apply( TQWidget.__init__,(self, parent, name) )
  124. self.p1 = TQIconSet( TQPixmap ( p1_xpm ) )
  125. self.p2 = TQIconSet( TQPixmap ( p2_xpm ) )
  126. self.p3 = TQIconSet( TQPixmap ( p3_xpm ) )
  127. self.p4 = TQIconSet( TQPixmap ( p4_xpm ) )
  128. #openIcon = TQPixmap()
  129. #saveIcon = TQPixmap()
  130. #printIcon = TQPixmap()
  131. self.printer = TQPopupMenu( self )
  132. #TQ_CHECK_PTR( self.printer )
  133. self.printer.insertTearOffHandle()
  134. self.printer.insertItem( "&Print to printer", self.printDoc )
  135. self.printer.insertItem( "Print to &file", self.file )
  136. self.printer.insertItem( "Print to fa&x", self.fax )
  137. self.printer.insertSeparator()
  138. self.printer.insertItem( "Printer &Setup", self.printerSetup )
  139. self.file = TQPopupMenu( self )
  140. #TQ_CHECK_PTR( self.file );
  141. self.file.insertItem( self.p1, "&Open", self.open, TQt.CTRL+TQt.Key_O )
  142. self.file.insertItem( self.p2, "&New", self.news, TQt.CTRL+TQt.Key_N )
  143. self.file.insertItem( self.p3, "&Save", self.save, TQt.CTRL+TQt.Key_S )
  144. self.file.insertItem( "&Close", self.closeDoc, TQt.CTRL+TQt.Key_W )
  145. self.file.insertSeparator()
  146. self.file.insertItem( self.p4, "&Print", self.printer, TQt.CTRL+TQt.Key_P )
  147. self.file.insertSeparator()
  148. self.file.insertItem( "E&xit", tqApp, SLOT( "quit()" ), TQt.CTRL+TQt.Key_Q )
  149. self.edit = TQPopupMenu( self )
  150. #TQ_CHECK_PTR( self.edit )
  151. undoID = self.edit.insertItem( "&Undo", self.undo )
  152. redoID = self.edit.insertItem( "&Redo", self.redo )
  153. self.edit.setItemEnabled( undoID, TRUE )
  154. self.edit.setItemEnabled( redoID, FALSE )
  155. self.options = TQPopupMenu( self )
  156. #TQ_CHECK_PTR( self.options )
  157. self.options.insertTearOffHandle()
  158. self.options.setCaption( 'Options' )
  159. self.options.insertItem( "&Normal Font", self.normal )
  160. self.options.insertSeparator()
  161. self.options.polish() # adjust system settings
  162. self.f = TQFont( self.options.font() )
  163. self.f.setBold( TRUE )
  164. self.boldID = self.options.insertItem( "&Bold" )
  165. self.options.setAccel( TQt.CTRL+TQt.Key_B, self.boldID )
  166. self.options.connectItem( self.boldID, self.bold )
  167. self.f = TQFont( self.options.font() )
  168. self.f.setUnderline( TRUE )
  169. self.underlineID = self.options.insertItem( "&Underline" )
  170. self.options.setAccel( TQt.CTRL+TQt.Key_U, self.underlineID )
  171. self.options.connectItem( self.underlineID, self.underline )
  172. self.isBold = FALSE
  173. self.isUnderline = FALSE
  174. self.options.setCheckable( TRUE )
  175. self.options = TQPopupMenu()
  176. #TQ_CHECK_PTR( self.options )
  177. self.options.insertItem( "&Normal Font", self.normal )
  178. self.options.insertSeparator()
  179. self.boldID = self.options.insertItem( "&Bold", self.bold )
  180. self.underlineID = self.options.insertItem( "&Underline", self.underline )
  181. self.isBold = FALSE
  182. self.isUnderline = FALSE
  183. self.options.setCheckable( TRUE )
  184. self.help = TQPopupMenu( self )
  185. #TQ_CHECK_PTR( self.help )
  186. self.help.insertItem( "&About", self.about, TQt.CTRL+TQt.Key_H )
  187. self.help.insertItem( "About &TQt", self.aboutTQt )
  188. self.menu = TQMenuBar( self )
  189. #TQ_CHECK_PTR( self.menu );
  190. self.menu.insertItem( "&File", self.file )
  191. self.menu.insertItem( "&Edit", self.edit )
  192. self.menu.insertItem( "&Options", self.options )
  193. self.menu.insertSeparator()
  194. self.menu.insertItem( "&Help", self.help )
  195. self.menu.setSeparator( TQMenuBar.InWindowsStyle )
  196. self.label = TQLabel( self )
  197. #TQ_CHECK_PTR( self.label )
  198. self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )
  199. self.label.setFrameStyle( TQFrame.Box | TQFrame.Raised )
  200. self.label.setLineWidth( 1 )
  201. self.label.setAlignment( TQt.AlignCenter )
  202. self.label.setFont( TQFont( "times", 12, TQFont.Bold ) )
  203. self.connect( self, PYSIGNAL( "explain" ), self.label.setText )
  204. #self.connect( self, PYSIGNAL( "explain(const char *)" ),
  205. # self.label, SLOT( "setText(const char *)" ) )
  206. self.setMinimumSize( 100, 80 )
  207. def open( self ):
  208. self.emit ( PYSIGNAL( "explain" ), ( "File/Open selected", ) )
  209. def news( self ):
  210. self.emit ( PYSIGNAL( "explain" ), ( "File/New selected", ) )
  211. def save( self ):
  212. self.emit ( PYSIGNAL( "explain" ), ( "File/Save selected", ) )
  213. def closeDoc( self ):
  214. self.emit ( PYSIGNAL( "explain" ), ( "File/Close selected", ) )
  215. def undo( self ):
  216. self.emit ( PYSIGNAL( "explain" ), ( "Edit/Undo selected", ) )
  217. def redo( self ):
  218. self.emit ( PYSIGNAL( "explain" ), ( "Edit/Redo selected", ) )
  219. def normal( self ):
  220. self.isBold = FALSE
  221. self.isUnderline = FALSE
  222. self.options.setItemChecked( self.boldID, self.isBold )
  223. self.options.setItemChecked( self.underlineID, self.isUnderline )
  224. self.emit(PYSIGNAL("explain"), ("Options/Normal selected",))
  225. def bold( self ):
  226. self.isBold = not self.isBold
  227. self.options.setItemChecked( self.boldID, self.isBold )
  228. self.emit ( PYSIGNAL( "explain" ), ( "Options/Bold selected", ) )
  229. def underline( self ):
  230. self.isUnderline = not self.isUnderline
  231. self.options.setItemChecked( self.underlineID, self.isUnderline )
  232. self.emit(PYSIGNAL("explain"), ("Options/Underline selected",))
  233. def about( self ):
  234. TQMessageBox.about( self, "TQt Menu Example",
  235. "This example demonstrates simple use of TQt menus.\n"
  236. "You can cut and paste lines from it to your own\n"
  237. "programs." )
  238. def aboutTQt( self ):
  239. TQMessageBox.aboutTQt( self, "TQt Menu Example" )
  240. def printDoc( self ):
  241. self.emit ( PYSIGNAL( "explain" ), ( "File/Printer/Print selected", ) )
  242. def file( self ):
  243. self.emit ( PYSIGNAL( "explain" ), ( "File/Printer/Print To File selected", ) )
  244. def fax( self ):
  245. self.emit ( PYSIGNAL( "explain" ), ( "File/Printer/Print To Fax selected", ) )
  246. def printerSetup( self ):
  247. self.emit ( PYSIGNAL( "explain" ), ( "File/Printer/Printer Setup selected", ) )
  248. def resizeEvent( self, ev ):
  249. self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )
  250. a = TQApplication( sys.argv )
  251. m = MenuExample()
  252. a.setMainWidget( m )
  253. m.setCaption( 'MenuExample' )
  254. m.show()
  255. #a.connect( a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()') )
  256. a.exec_loop()