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.

widgets.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #!/usr/bin/env python
  2. import sys, string
  3. from python_tqt.qt import *
  4. #
  5. ## Constructs an analog clock widget that uses an internal TQTimer.
  6. #
  7. def TQMIN( x, y ):
  8. if y > x:
  9. return y
  10. return x
  11. #
  12. ## Constructs an analog clock widget that uses an internal TQTimer.
  13. #
  14. class AnalogClock( TQWidget ):
  15. def __init__( self, *args ):
  16. apply( TQWidget.__init__, (self,) + args )
  17. self.time = TQTime.currentTime() # get current time
  18. internalTimer = TQTimer( self ) # create internal timer
  19. self.connect( internalTimer, SIGNAL("timeout()"), self.timeout )
  20. internalTimer.start( 5000 ) # emit signal every 5 seconds
  21. #
  22. ## The TQTimer::timeout() signal is received by this slot.
  23. #
  24. def timeout( self ):
  25. new_time = TQTime.currentTime() # get the current time
  26. if new_time.minute() != self.time.minute(): # minute has changed
  27. self.update()
  28. #
  29. ## The clock is painted using a 1000x1000 square coordinate system.
  30. #
  31. def paintEvent( self, qe ): # paint clock
  32. if not self.isVisible(): # is is invisible
  33. return
  34. self.time = TQTime.currentTime() # save current time
  35. pts = TQPointArray()
  36. paint = TQPainter( self )
  37. paint.setBrush( self.foregroundColor() ) # fill with foreground color
  38. cp = TQPoint( self.rect().center() ) # widget center point
  39. d = TQMIN( self.width(), self.height() ) # we want a circular clock
  40. matrix = TQWMatrix() # setup transformation matrix
  41. matrix.translate( cp.x(), cp.y() ) # origin at widget center
  42. matrix.scale( d / 1000.0, d / 1000.0 ) # scale coordinate system
  43. h_angle = 30 * ( self.time.hour() % 12 - 3 ) + self.time.minute() / 2
  44. matrix.rotate( h_angle ) # rotate to draw hour hand
  45. paint.setWorldMatrix( matrix )
  46. pts.setPoints( [ -20,0, 0,-20, 300,0, 0,20 ] )
  47. paint.drawPolygon( pts ) # draw hour hand
  48. matrix.rotate( -h_angle ) # rotate back to zero
  49. m_angle = ( self.time.minute() - 15 ) * 6
  50. matrix.rotate( m_angle ) # rotate to draw minute hand
  51. paint.setWorldMatrix( matrix )
  52. pts.setPoints( [ -10,0, 0,-10, 400,0, 0,10 ] )
  53. paint.drawPolygon( pts ) # draw minute hand
  54. matrix.rotate( -m_angle ) # rotate back to zero
  55. for i in range( 0, 12 ): # draw hour lines
  56. paint.setWorldMatrix( matrix )
  57. paint.drawLine( 450,0, 500,0 )
  58. matrix.rotate( 30 )
  59. class DigitalClock( TQLCDNumber ):
  60. def __init__( self, *args ):
  61. apply( TQLCDNumber.__init__,(self,) + args )
  62. self.showingColon = 0
  63. self.setFrameStyle(TQFrame.Panel | TQFrame.Raised)
  64. self.setLineWidth( 2 )
  65. self.showTime()
  66. self.normalTimer = self.startTimer( 500 )
  67. self.showDateTimer = -1
  68. def timerEvent( self, e ):
  69. if e.timerId() == self.showDateTimer:
  70. self.stopDate()
  71. else:
  72. if self.showDateTimer == -1:
  73. self.showTime()
  74. def mousePressEvent( self, e ):
  75. if e.button() == TQt.LeftButton:
  76. self.showDate()
  77. def showDate( self ):
  78. if self.showDateTimer != -1:
  79. return
  80. d = TQDate.currentDate()
  81. self.display('%2d %2d' % (d.month(), d.day()))
  82. self.showDateTimer = self.startTimer(2000)
  83. def stopDate( self ):
  84. self.killTimer(self.showDateTimer)
  85. self.showDateTimer = -1
  86. self.showTime()
  87. def showTime( self ):
  88. self.showingColon = not self.showingColon
  89. s = list(str(TQTime.currentTime().toString())[:5]) #.left(5)
  90. if not self.showingColon:
  91. s[2] = ' '
  92. if s[0] == '0':
  93. s[0] = ' '
  94. s = string.join(s,'')
  95. self.display( s )
  96. def TQMIN( x, y ):
  97. if y > x:
  98. return y
  99. return x
  100. TRUE = 1
  101. FALSE = 0
  102. MOVIEFILENAME = "trolltech.gif"
  103. #
  104. # WidgetView contains lots of TQt widgets.
  105. #
  106. class WidgetView ( TQWidget ):
  107. def __init__( self, *args ):
  108. apply( TQWidget.__init__, (self,) + args )
  109. # Set the window caption/title
  110. self.setCaption( "TQt Widgets Demo Application" )
  111. # Install an application-global event filter
  112. tqApp.installEventFilter( self )
  113. # Create a layout to position the widgets
  114. self.topLayout = TQVBoxLayout( self, 10 )
  115. # Create a grid layout to hold most of the widgets
  116. self.grid = TQGridLayout( 6, 3 )
  117. # This layout will get all of the stretch
  118. self.topLayout.addLayout( self.grid, 10 )
  119. # Create a menubar
  120. self.menubar = TQMenuBar( self )
  121. self.menubar.setSeparator( TQMenuBar.InWindowsStyle )
  122. # Create an easter egg
  123. TQToolTip.add( self.menubar, TQRect( 0, 0, 2, 2 ), "easter egg" )
  124. self.popup = TQPopupMenu()
  125. self.id = self.popup.insertItem( "&New" )
  126. self.popup.setItemEnabled( self.id, FALSE )
  127. self.id = self.popup.insertItem( "&Open" )
  128. self.popup.setItemEnabled( self.id, FALSE )
  129. self.popup.insertSeparator()
  130. self.popup.insertItem( "&Quit", tqApp, SLOT("quit()"), TQt.CTRL+TQt.Key_Q )
  131. self.menubar.insertItem( "&File", self.popup )
  132. # Must tell the layout about a menubar in a widget
  133. self.topLayout.setMenuBar( self.menubar )
  134. # Create an analog and a digital clock
  135. self.aclock = AnalogClock( self )
  136. self.aclock.resize( 50, 50 )
  137. self.dclock = DigitalClock( self )
  138. self.dclock.setMaximumWidth( 200 )
  139. self.grid.addWidget( self.aclock, 0, 2 )
  140. self.grid.addWidget( self.dclock, 1, 2 )
  141. # Give the dclock widget a blue palette
  142. col = TQColor()
  143. col.setRgb( 0xaa, 0xbe, 0xff )
  144. self.dclock.setPalette( TQPalette( col ) )
  145. # make tool tips for both of them
  146. TQToolTip.add( self.aclock, "custom widget: analog clock" )
  147. TQToolTip.add( self.dclock, "custom widget: digital clock" )
  148. # Create a push button.
  149. self.pb = TQPushButton( self, "button1" ) # create button 1
  150. self.pb.setText( "Push button 1" )
  151. self.pb.setFixedHeight( self.pb.sizeHint().height() )
  152. self.grid.addWidget( self.pb, 0, 0, TQt.AlignVCenter )
  153. self.connect( self.pb, SIGNAL("clicked()"), self.button1Clicked )
  154. TQToolTip.add( self.pb, "push button 1" )
  155. self.pm = TQPixmap()
  156. self.pix = self.pm.load( "qt.png" ) # load pixmap for button 2
  157. if not self.pix:
  158. TQMessageBox.information( None, "TQt Widgets Example",
  159. "Could not load the file \"qt.png\", which\n"
  160. "contains an icon used...\n\n"
  161. "The text \"line 42\" will be substituted.",
  162. TQMessageBox.Ok + TQMessageBox.Default )
  163. # Create a label containing a TQMovie
  164. self.movielabel = TQLabel( self, "label0" )
  165. self.movie = TQMovie( MOVIEFILENAME )
  166. self.movie.connectStatus( self.movieStatus )
  167. self.movie.connectUpdate( self.movieUpdate )
  168. self.movielabel.setFrameStyle( TQFrame.Box | TQFrame.Plain )
  169. self.movielabel.setMovie( self.movie )
  170. self.movielabel.setMargin( 0 )
  171. self.movielabel.setFixedSize( 128 + self.movielabel.frameWidth() * 2,
  172. 64 + self.movielabel.frameWidth() * 2 )
  173. self.grid.addWidget( self.movielabel, 0, 1, TQt.AlignCenter )
  174. TQToolTip.add( self.movielabel, "movie" )
  175. # Create a group of check boxes
  176. self.bg = TQButtonGroup( self, "checkGroup" )
  177. self.bg.setTitle( "Check Boxes" )
  178. self.grid.addWidget( self.bg, 1, 0 )
  179. # Create a layout for the check boxes
  180. self.vbox = TQVBoxLayout(self.bg, 10)
  181. self.vbox.addSpacing( self.bg.fontMetrics().height() )
  182. self.cb = range(3)
  183. self.cb[0] = TQCheckBox( self.bg )
  184. self.cb[0].setText( "Read" )
  185. self.vbox.addWidget( self.cb[0] )
  186. self.cb[0].setMinimumSize( self.cb[0].sizeHint() )
  187. self.cb[1] = TQCheckBox( self.bg )
  188. self.cb[1].setText( "Write" )
  189. self.vbox.addWidget( self.cb[1] )
  190. self.cb[1].setMinimumSize( self.cb[1].sizeHint() )
  191. self.cb[2] = TQCheckBox( self.bg )
  192. self.cb[2].setText( "Execute" )
  193. self.cb[2].setMinimumSize( self.cb[2].sizeHint() )
  194. self.vbox.addWidget( self.cb[2] )
  195. self.bg.setMinimumSize( self.bg.childrenRect().size() )
  196. self.vbox.activate()
  197. self.connect( self.bg, SIGNAL("clicked(int)"), self.checkBoxClicked )
  198. TQToolTip.add( self.cb[0], "check box 1" )
  199. TQToolTip.add( self.cb[1], "check box 2" )
  200. TQToolTip.add( self.cb[2], "check box 3" )
  201. # Create a group of radio buttons
  202. self.bg = TQButtonGroup( self, "radioGroup" )
  203. self.bg.setTitle( "Radio buttons" )
  204. self.grid.addWidget( self.bg, 1, 1 )
  205. # Create a layout for the radio buttons
  206. self.vbox = TQVBoxLayout( self.bg, 10 )
  207. self.vbox.addSpacing( self.bg.fontMetrics().height() )
  208. self.rb = TQRadioButton( self.bg )
  209. self.rb.setText( "&AM" )
  210. self.rb.setChecked( TRUE )
  211. self.vbox.addWidget( self.rb )
  212. self.rb.setMinimumSize( self.rb.sizeHint() )
  213. TQToolTip.add( self.rb, "radio button 1" )
  214. self.rb = TQRadioButton( self.bg )
  215. self.rb.setText( "&FM" )
  216. self.vbox.addWidget( self.rb )
  217. self.rb.setMinimumSize( self.rb.sizeHint() )
  218. TQToolTip.add( self.rb, "radio button 2" )
  219. self.rb = TQRadioButton( self.bg )
  220. self.rb.setText( "&Short Wave" )
  221. self.vbox.addWidget( self.rb )
  222. self.rb.setMinimumSize( self.rb.sizeHint() )
  223. self.vbox.activate()
  224. self.connect( self.bg, SIGNAL("clicked(int)"), self.radioButtonClicked )
  225. TQToolTip.add( self.rb, "radio button 3" )
  226. # Create a list box
  227. self.lb = TQListBox( self, "listBox" )
  228. for i in range( 0, 100, 1 ): # fill list box
  229. txt = TQString()
  230. txt = "line %d" % i
  231. if i == 42 and self.pix:
  232. self.lb.insertItem( self.pm )
  233. else:
  234. self.lb.insertItem( txt )
  235. self.grid.addMultiCellWidget( self.lb, 2, 4, 0, 0 )
  236. self.connect( self.lb, SIGNAL("selected(int)"), self.listBoxItemSelected )
  237. TQToolTip.add( self.lb, "list box" )
  238. self.vbox = TQVBoxLayout( 8 )
  239. self.grid.addLayout( self.vbox, 2, 1 )
  240. # Create a slider
  241. self.sb = TQSlider( 0, 300, 1, 100, TQSlider.Horizontal, self, "Slider" )
  242. self.sb.setTickmarks( TQSlider.Below )
  243. self.sb.setTickInterval( 10 )
  244. self.sb.setFocusPolicy( TQWidget.TabFocus )
  245. self.sb.setFixedHeight( self.sb.sizeHint().height() )
  246. self.vbox.addWidget( self.sb )
  247. self.connect( self.sb, SIGNAL("valueChanged(int)"), self.sliderValueChanged )
  248. TQToolTip.add( self.sb, "slider" )
  249. # Create a combo box
  250. self.combo = TQComboBox( FALSE, self, "comboBox" )
  251. self.combo.insertItem( "darkBlue" )
  252. self.combo.insertItem( "darkRed" )
  253. self.combo.insertItem( "darkGreen" )
  254. self.combo.insertItem( "blue" )
  255. self.combo.insertItem( "red" )
  256. self.combo.setFixedHeight( self.combo.sizeHint().height() )
  257. self.vbox.addWidget( self.combo )
  258. self.connect( self.combo, SIGNAL("activated(int)"), self.comboBoxItemActivated )
  259. TQToolTip.add( self.combo, "read-only combo box" )
  260. # Create an editable combo box
  261. self.edCombo = TQComboBox( TRUE, self, "edComboBox" )
  262. self.edCombo.insertItem( "Permutable" )
  263. self.edCombo.insertItem( "Malleable" )
  264. self.edCombo.insertItem( "Adaptable" )
  265. self.edCombo.insertItem( "Alterable" )
  266. self.edCombo.insertItem( "Inconstant" )
  267. self.edCombo.setFixedHeight( self.edCombo.sizeHint().height() )
  268. self.vbox.addWidget( self.edCombo )
  269. self.connect( self.edCombo, SIGNAL("activated(const TQString &)"), self.edComboBoxItemActivated)
  270. TQToolTip.add( self.edCombo, "editable combo box" )
  271. self.edCombo.setAutoCompletion( TRUE )
  272. self.vbox.addStretch( 1 )
  273. self.vbox = TQVBoxLayout( 8 )
  274. self.grid.addLayout( self.vbox, 2, 2 )
  275. # Create a spin box
  276. self.spin = TQSpinBox( 0, 10, 1, self, "spin" )
  277. self.spin.setSuffix( " mm" )
  278. self.spin.setSpecialValueText( "Auto" )
  279. self.spin.setMinimumSize( self.spin.sizeHint() )
  280. self.connect( self.spin, SIGNAL( "valueChanged(const TQString &)" ), self.spinBoxValueChanged )
  281. TQToolTip.add( self.spin, "spin box" )
  282. self.vbox.addWidget( self.spin )
  283. self.vbox.addStretch( 1 )
  284. # Create a multi line edit
  285. self.mle = TQMultiLineEdit( self, "multiLineEdit" )
  286. self.grid.addMultiCellWidget( self.mle, 3, 3, 1, 2 )
  287. self.mle.setMinimumHeight( self.mle.fontMetrics().height() * 3 )
  288. self.mle.setText("This is a TQMultiLineEdit widget,\n"
  289. "useful for small multi-line\n"
  290. "input fields.")
  291. TQToolTip.add( self.mle, "multi line editor" )
  292. # Create a single line edit
  293. self.le = TQLineEdit( self, "lineEdit" )
  294. self.grid.addMultiCellWidget( self.le, 4, 4, 1, 2 )
  295. self.le.setFixedHeight( self.le.sizeHint().height() )
  296. self.connect( self.le, SIGNAL("textChanged(const TQString &)"), self.lineEditTextChanged )
  297. TQToolTip.add( self.le, "single line editor" )
  298. # Create a horizontal line (sort of TQFrame) above the message line
  299. self.separator = TQFrame( self, "separatorLine" )
  300. self.separator.setFrameStyle( TQFrame.HLine | TQFrame.Sunken )
  301. self.separator.setFixedHeight( self.separator.sizeHint().height() )
  302. self.grid.addMultiCellWidget( self.separator, 5, 5, 0, 2 )
  303. TQToolTip.add( self.separator, "tool tips on a separator! wow!" )
  304. self.grid.setRowStretch( 0, 0 )
  305. self.grid.setRowStretch( 1, 0 )
  306. self.grid.setRowStretch( 2, 0 )
  307. self.grid.setRowStretch( 3, 1 )
  308. self.grid.setRowStretch( 4, 1 )
  309. self.grid.setRowStretch( 5, 0 )
  310. self.grid.setColStretch( 0, 1 )
  311. self.grid.setColStretch( 1, 1 )
  312. self.grid.setColStretch( 2, 1 )
  313. # Create an label and a message in a plain widget
  314. # The message is updated when buttons are clicked etc.
  315. self.hbox = TQHBoxLayout()
  316. self.topLayout.addLayout( self.hbox )
  317. self.msgLabel = TQLabel( self, "msgLabel" )
  318. self.msgLabel.setText( "Message:" )
  319. self.msgLabel.setAlignment( TQt.AlignHCenter | TQt.AlignVCenter )
  320. self.msgLabel.setFixedSize( self.msgLabel.sizeHint() )
  321. self.hbox.addWidget( self.msgLabel )
  322. TQToolTip.add( self.msgLabel, "label 1" )
  323. self.msg = TQLabel( self, "message" )
  324. self.msg.setFrameStyle( TQFrame.Panel | TQFrame.Sunken )
  325. self.msg.setAlignment( TQt.AlignCenter )
  326. self.msg.setFont( TQFont( "times", 12, TQFont.Bold ) )
  327. self.msg.setText( "Message" )
  328. self.msg.setFixedHeight( self.msg.sizeHint().height() )
  329. self.msg.setText( "" )
  330. self.hbox.addWidget( self.msg, 5 )
  331. TQToolTip.add( self.msg, "label 2" )
  332. self.topLayout.activate()
  333. def setStatus(self, text):
  334. self.msg.setText( text )
  335. def movieUpdate( self, r ):
  336. # Uncomment this to test animated icons on your window manager
  337. self.setIcon( self.movie.framePixmap() )
  338. def movieStatus( self, s ):
  339. if s == TQMovie.SourceEmpty or s == TQMovie.UnrecognizedFormat:
  340. pm = TQPixmap('tt-logo.png')
  341. self.movielabel.setPixmap(pm)
  342. self.movielabel.setFixedSize(pm.size())
  343. else:
  344. if ( self.movielabel.movie() ): # for flicker-free animation:
  345. self.movielabel.setBackgroundMode( TQWidget.NoBackground )
  346. def button1Clicked( self ):
  347. self.msg.setText( "The first push button was clicked" )
  348. def checkBoxClicked( self, id ):
  349. txt = "Check box %s clicked : " % str(id)
  350. chk = ["-","-","-"]
  351. if self.cb[0].isChecked():
  352. chk[0] = "r"
  353. if self.cb[1].isChecked():
  354. chk[1] = "w"
  355. if self.cb[2].isChecked():
  356. chk[2] = "x"
  357. txt = txt + str(chk[0]+chk[1]+chk[2])
  358. self.msg.setText( txt )
  359. def edComboBoxItemActivated( self, text):
  360. self.msg.setText( "Editable Combo Box set to %s" % text )
  361. def radioButtonClicked( self, id ):
  362. self.msg.setText( "Radio button #%d clicked" % id )
  363. def listBoxItemSelected( self, index ):
  364. self.msg.setText( "List box item %d selected" % index )
  365. def sliderValueChanged( self, value ):
  366. self.msg.setText( "Movie set to %d%% of normal speed" % value )
  367. self.movie.setSpeed( value )
  368. def comboBoxItemActivated( self, index ):
  369. self.msg.setText( "Combo box item %d activated" % index )
  370. p = TQApplication.palette()
  371. if index == 0:
  372. p.setColor( TQColorGroup.Highlight, TQt.darkBlue )
  373. elif index == 1:
  374. p.setColor( TQColorGroup.Highlight, TQt.darkRed )
  375. elif index == 2:
  376. p.setColor( TQColorGroup.Highlight, TQt.darkGreen )
  377. elif index == 3:
  378. p.setColor( TQColorGroup.Highlight, TQt.blue )
  379. elif index == 4:
  380. p.setColor( TQColorGroup.Highlight, TQt.red )
  381. TQApplication.setPalette( p, TRUE )
  382. def lineEditTextChanged( self, newText ):
  383. self.msg.setText("Line edit text: " + unicode(newText))
  384. def spinBoxValueChanged( self, valueText ):
  385. self.msg.setText("Spin box value: " + unicode(valueText))
  386. # All application events are passed throught this event filter.
  387. # We're using it to display some information about a clicked
  388. # widget (right mouse button + CTRL).
  389. #def eventFilter( self, event ):
  390. # identify_now = TRUE
  391. # if event.type() == Event_MouseButtonPress and identify_now:
  392. # e = TQMouseEvent( event )
  393. # if (e.button() == TQt.RightButton) and (e.state() & TQt.ControlButton) != 0:
  394. # txt = TQString( "The clicked widget is a\n" )
  395. # txt = txt + TQObect.className()
  396. # txt = txt + "\nThe widget's name is\n"
  397. # if TQObject.name():
  398. # txt = txt + TQObject.name()
  399. # else:
  400. # txt = txt + "<no name>"
  401. # identify_now = FALSE # don't do it in message box
  402. # TQMessageBox.message( "Identify Widget", txt, 0, TQObject )
  403. # identify_now = TRUE; # allow it again
  404. # return FALSE # don't eat event
  405. ################################################################################################
  406. #TQApplication.setColourSpec( TQApplication.CustomColor )
  407. a = TQApplication( sys.argv )
  408. w = WidgetView()
  409. a.setMainWidget( w )
  410. w.show()
  411. a.exec_loop()