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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: menu.py,v 1.3 2003/06/06 12:59:26 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. # Auxiliary class to provide fancy menu items with different fonts.
  85. # Used for the "bold" and "underline" menu items in the options menu.
  86. class MyMenuItem( TQCustomMenuItem ):
  87. def __init__( self, s, f ):
  88. TQCustomMenuItem.__init__( self )
  89. self.string = s
  90. self.font = f
  91. def paint( self, p, cg, act, enabled, x, y, w, h ) :
  92. p.setFont ( self.font )
  93. p.drawText( x, y, w, h, TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip, self.string )
  94. def sizeHint( self ):
  95. return TQFontMetrics( self.font ).size( TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip, self.string )
  96. # Implementation of MenuExample class
  97. class MenuExample( TQWidget ):
  98. def __init__( self, parent=None, name=None ):
  99. TQWidget.__init__( self, parent, name )
  100. self.p1 = TQIconSet( TQPixmap ( p1_xpm ) )
  101. self.p2 = TQIconSet( TQPixmap ( p2_xpm ) )
  102. self.p3 = TQIconSet( TQPixmap ( p3_xpm ) )
  103. self.printer = TQPopupMenu( self )
  104. self.printer.insertTearOffHandle()
  105. self.printer.insertItem( "&Print to printer", self.printDoc )
  106. self.printer.insertItem( "Print to &file", self.printFile )
  107. self.printer.insertItem( "Print to fa&x", self.fax )
  108. self.printer.insertSeparator()
  109. self.printer.insertItem( "Printer &Setup", self.printerSetup )
  110. self.file = TQPopupMenu( self )
  111. self.file.insertItem( self.p1, "&Open", self.open, TQt.CTRL+TQt.Key_O )
  112. self.file.insertItem( self.p2, "&New", self.news, TQt.CTRL+TQt.Key_N )
  113. self.file.insertItem( self.p3, "&Save", self.save, TQt.CTRL+TQt.Key_S )
  114. self.file.insertItem( "&Close", self.closeDoc, TQt.CTRL+TQt.Key_W )
  115. self.file.insertSeparator()
  116. self.file.insertItem( "&Print", self.printer, TQt.CTRL+TQt.Key_P )
  117. self.file.insertSeparator()
  118. self.file.insertItem( "E&xit", tqApp, SLOT( "quit()" ), TQt.CTRL+TQt.Key_Q )
  119. self.edit = TQPopupMenu( self )
  120. undoID = self.edit.insertItem( "&Undo", self.undo )
  121. redoID = self.edit.insertItem( "&Redo", self.redo )
  122. self.edit.setItemEnabled( undoID, FALSE )
  123. self.edit.setItemEnabled( redoID, FALSE )
  124. self.options = TQPopupMenu( self )
  125. self.options.insertTearOffHandle()
  126. self.options.setCaption( "Options" )
  127. self.options.insertItem( "&Normal Font", self.normal )
  128. self.options.insertSeparator()
  129. self.options.polish() # adjust system settings
  130. f = self.options.font()
  131. f.setBold( TRUE )
  132. self.boldID = self.options.insertItem( MyMenuItem( "Bold", f ) )
  133. self.options.setAccel( TQt.CTRL+TQt.Key_B, self.boldID )
  134. self.options.connectItem( self.boldID, self.bold )
  135. f = self.options.font()
  136. f.setUnderline( TRUE )
  137. self.underlineID = self.options.insertItem( MyMenuItem( "Underline", f ) )
  138. self.options.setAccel( TQt.CTRL+TQt.Key_U, self.underlineID )
  139. self.options.connectItem( self.underlineID, self.underline )
  140. self.isBold = FALSE
  141. self.isUnderline = FALSE
  142. self.options.setCheckable( TRUE )
  143. self.help = TQPopupMenu( self )
  144. self.help.insertItem( "&About", self.about, TQt.CTRL+TQt.Key_H )
  145. self.help.insertItem( "About &TQt", self.aboutTQt )
  146. self.menu = TQMenuBar( self )
  147. self.menu.insertItem( "&File", self.file )
  148. self.menu.insertItem( "&Edit", self.edit )
  149. self.menu.insertItem( "&Options", self.options )
  150. self.menu.insertSeparator()
  151. self.menu.insertItem( "&Help", self.help )
  152. self.menu.setSeparator( TQMenuBar.InWindowsStyle )
  153. msg = TQLabel( self )
  154. msg.setText( "A context menu is available.\n"
  155. "Invoke it by right-clicking or by"
  156. " pressing the 'context' button." )
  157. msg.setGeometry( 0, self.height() - 60, self.width(), 60 )
  158. msg.setAlignment( TQt.AlignCenter )
  159. self.label = TQLabel( self )
  160. self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )
  161. self.label.setFrameStyle( TQFrame.Box | TQFrame.Raised )
  162. self.label.setLineWidth( 1 )
  163. self.label.setAlignment( TQt.AlignCenter )
  164. self.connect( self, PYSIGNAL( "explain" ), self.label, SLOT( "setText(const TQString &" ) )
  165. self.setMinimumSize( 100, 80 )
  166. self.setFocusPolicy( TQWidget.ClickFocus )
  167. def contextMenuEvent(self, cmev):
  168. contextMenu = TQPopupMenu( self )
  169. caption = TQLabel( "<font color=darkblue><u><b>Context Menu</b></u></font>", self )
  170. caption.setAlignment( TQt.AlignCenter )
  171. contextMenu.insertItem( caption )
  172. contextMenu.insertItem( "&New", self.news, TQt.CTRL+TQt.Key_N )
  173. contextMenu.insertItem( "&Open...", self.open, TQt.CTRL+TQt.Key_O )
  174. contextMenu.insertItem( "&Save", self.save, TQt.CTRL+TQt.Key_S )
  175. submenu = TQPopupMenu( self )
  176. submenu.insertItem( "&Print to printer", self.printDoc )
  177. submenu.insertItem( "Print to &file", self.printFile )
  178. submenu.insertItem( "Print to fa&x", self.fax )
  179. contextMenu.insertItem( "&Print", submenu )
  180. contextMenu.exec_loop( TQCursor.pos() )
  181. def open( self ):
  182. self.emit( PYSIGNAL( "explain" ), ( "File/Open selected", ) )
  183. def news( self ):
  184. self.emit( PYSIGNAL( "explain" ), ( "File/New selected", ) )
  185. def save( self ):
  186. self.emit( PYSIGNAL( "explain" ), ( "File/Save selected", ) )
  187. def closeDoc( self ):
  188. self.emit( PYSIGNAL( "explain" ), ( "File/Close selected", ) )
  189. def undo( self ):
  190. self.emit( PYSIGNAL( "explain" ), ( "Edit/Undo selected", ) )
  191. def redo( self ):
  192. self.emit( PYSIGNAL( "explain" ), ( "Edit/Redo selected", ) )
  193. def normal( self ):
  194. self.isBold = FALSE
  195. self.isUnderline = FALSE
  196. font = TQFont()
  197. self.label.setFont( font )
  198. self.options.setItemChecked( self.boldID, self.isBold )
  199. self.options.setItemChecked( self.underlineID, self.isUnderline )
  200. self.emit( PYSIGNAL( "explain" ), ( "Options/Normal selected", ) )
  201. def bold( self ):
  202. self.isBold = not self.isBold
  203. font = TQFont()
  204. font.setBold( self.isBold )
  205. font.setUnderline( self.isUnderline )
  206. self.label.setFont( font )
  207. self.options.setItemChecked( self.boldID, self.isBold )
  208. self.emit( PYSIGNAL( "explain" ), ( "Options/Bold selected", ) )
  209. def underline( self ):
  210. self.isUnderline = not self.isUnderline
  211. font = TQFont()
  212. font.setBold( self.isBold )
  213. font.setUnderline( self.isUnderline )
  214. self.label.setFont( font )
  215. self.options.setItemChecked( self.underlineID, self.isUnderline )
  216. self.emit( PYSIGNAL("explain"), ( "Options/Underline selected", ))
  217. def about( self ):
  218. TQMessageBox.about( self, "TQt Menu Example",
  219. "This example demonstrates simple use of TQt menus.\n"
  220. "You can cut and paste lines from it to your own\n"
  221. "programs." )
  222. def aboutTQt( self ):
  223. TQMessageBox.aboutTQt( self, "TQt Menu Example" )
  224. def printDoc( self ):
  225. self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print selected", ) )
  226. def printFile( self ):
  227. self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print To File selected", ) )
  228. def fax( self ):
  229. self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print To Fax selected", ) )
  230. def printerSetup( self ):
  231. self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Printer Setup selected", ) )
  232. def resizeEvent( self, ev ):
  233. self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )
  234. a = TQApplication( sys.argv )
  235. m = MenuExample()
  236. m.setCaption( "TQt Examples - Menus" )
  237. a.setMainWidget( m )
  238. m.show()
  239. a.exec_loop()