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.

513 lines
19KB

  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. self.menubar.setSeparator( 1 )
  123. # Create an easter egg
  124. TQToolTip.add( self.menubar, TQRect( 0, 0, 2, 2 ), "easter egg" )
  125. self.popup = TQPopupMenu()
  126. self.id = self.popup.insertItem( "&New" )
  127. self.popup.setItemEnabled( self.id, FALSE )
  128. self.id = self.popup.insertItem( "&Open" )
  129. self.popup.setItemEnabled( self.id, FALSE )
  130. self.popup.insertSeparator()
  131. self.popup.insertItem( "&Quit", tqApp, SLOT("quit()"), TQt.CTRL+TQt.Key_Q )
  132. self.menubar.insertItem( "&File", self.popup )
  133. # Must tell the layout about a menubar in a widget
  134. self.topLayout.setMenuBar( self.menubar )
  135. # Create an analog and a digital clock
  136. self.aclock = AnalogClock( self )
  137. self.aclock.resize( 50, 50 )
  138. self.dclock = DigitalClock( self )
  139. self.dclock.setMaximumWidth( 200 )
  140. self.grid.addWidget( self.aclock, 0, 2 )
  141. self.grid.addWidget( self.dclock, 1, 2 )
  142. # Give the dclock widget a blue palette
  143. col = TQColor()
  144. col.setRgb( 0xaa, 0xbe, 0xff )
  145. self.dclock.setPalette( TQPalette( col ) )
  146. # make tool tips for both of them
  147. TQToolTip.add( self.aclock, "custom widget: analog clock" )
  148. TQToolTip.add( self.dclock, "custom widget: digital clock" )
  149. # Create a push button.
  150. self.pb = TQPushButton( self, "button1" ) # create button 1
  151. self.pb.setText( "Push button 1" )
  152. self.pb.setFixedHeight( self.pb.sizeHint().height() )
  153. self.grid.addWidget( self.pb, 0, 0, TQt.AlignVCenter )
  154. self.connect( self.pb, SIGNAL("clicked()"), self.button1Clicked )
  155. TQToolTip.add( self.pb, "push button 1" )
  156. self.pm = TQPixmap()
  157. self.pix = self.pm.load( "qt.png" ) # load pixmap for button 2
  158. if not self.pix:
  159. TQMessageBox.information( None, "TQt Widgets Example",
  160. "Could not load the file \"qt.png\", which\n"
  161. "contains an icon used...\n\n"
  162. "The text \"line 42\" will be substituted.",
  163. TQMessageBox.Ok + TQMessageBox.Default )
  164. # Create a label containing a TQMovie
  165. self.movielabel = TQLabel( self, "label0" )
  166. self.movie = TQMovie( MOVIEFILENAME )
  167. self.movie.connectStatus( self.movieStatus )
  168. self.movie.connectUpdate( self.movieUpdate )
  169. self.movielabel.setFrameStyle( TQFrame.Box | TQFrame.Plain )
  170. self.movielabel.setMovie( self.movie )
  171. self.movielabel.setMargin( 0 )
  172. self.movielabel.setFixedSize( 128 + self.movielabel.frameWidth() * 2,
  173. 64 + self.movielabel.frameWidth() * 2 )
  174. self.grid.addWidget( self.movielabel, 0, 1, TQt.AlignCenter )
  175. TQToolTip.add( self.movielabel, "movie" )
  176. # Create a group of check boxes
  177. self.bg = TQButtonGroup( self, "checkGroup" )
  178. self.bg.setTitle( "Check Boxes" )
  179. self.grid.addWidget( self.bg, 1, 0 )
  180. # Create a layout for the check boxes
  181. self.vbox = TQVBoxLayout(self.bg, 10)
  182. self.vbox.addSpacing( self.bg.fontMetrics().height() )
  183. self.cb = range(3)
  184. self.cb[0] = TQCheckBox( self.bg )
  185. self.cb[0].setText( "Read" )
  186. self.vbox.addWidget( self.cb[0] )
  187. self.cb[0].setMinimumSize( self.cb[0].sizeHint() )
  188. self.cb[1] = TQCheckBox( self.bg )
  189. self.cb[1].setText( "Write" )
  190. self.vbox.addWidget( self.cb[1] )
  191. self.cb[1].setMinimumSize( self.cb[1].sizeHint() )
  192. self.cb[2] = TQCheckBox( self.bg )
  193. self.cb[2].setText( "Execute" )
  194. self.cb[2].setMinimumSize( self.cb[2].sizeHint() )
  195. self.vbox.addWidget( self.cb[2] )
  196. self.bg.setMinimumSize( self.bg.childrenRect().size() )
  197. self.vbox.activate()
  198. self.connect( self.bg, SIGNAL("clicked(int)"), self.checkBoxClicked )
  199. TQToolTip.add( self.cb[0], "check box 1" )
  200. TQToolTip.add( self.cb[1], "check box 2" )
  201. TQToolTip.add( self.cb[2], "check box 3" )
  202. # Create a group of radio buttons
  203. self.bg = TQButtonGroup( self, "radioGroup" )
  204. self.bg.setTitle( "Radio buttons" )
  205. self.grid.addWidget( self.bg, 1, 1 )
  206. # Create a layout for the radio buttons
  207. self.vbox = TQVBoxLayout( self.bg, 10 )
  208. self.vbox.addSpacing( self.bg.fontMetrics().height() )
  209. self.rb = TQRadioButton( self.bg )
  210. self.rb.setText( "&AM" )
  211. self.rb.setChecked( TRUE )
  212. self.vbox.addWidget( self.rb )
  213. self.rb.setMinimumSize( self.rb.sizeHint() )
  214. TQToolTip.add( self.rb, "radio button 1" )
  215. self.rb = TQRadioButton( self.bg )
  216. self.rb.setText( "&FM" )
  217. self.vbox.addWidget( self.rb )
  218. self.rb.setMinimumSize( self.rb.sizeHint() )
  219. TQToolTip.add( self.rb, "radio button 2" )
  220. self.rb = TQRadioButton( self.bg )
  221. self.rb.setText( "&Short Wave" )
  222. self.vbox.addWidget( self.rb )
  223. self.rb.setMinimumSize( self.rb.sizeHint() )
  224. self.vbox.activate()
  225. self.connect( self.bg, SIGNAL("clicked(int)"), self.radioButtonClicked )
  226. TQToolTip.add( self.rb, "radio button 3" )
  227. # Create a list box
  228. self.lb = TQListBox( self, "listBox" )
  229. for i in range( 0, 100, 1 ): # fill list box
  230. txt = TQString()
  231. txt = "line %d" % i
  232. if i == 42 and self.pix:
  233. self.lb.insertItem( self.pm )
  234. else:
  235. self.lb.insertItem( txt )
  236. self.grid.addMultiCellWidget( self.lb, 2, 4, 0, 0 )
  237. self.connect( self.lb, SIGNAL("selected(int)"), self.listBoxItemSelected )
  238. TQToolTip.add( self.lb, "list box" )
  239. self.vbox = TQVBoxLayout( 8 )
  240. self.grid.addLayout( self.vbox, 2, 1 )
  241. # Create a slider
  242. self.sb = TQSlider( 0, 300, 1, 100, TQSlider.Horizontal, self, "Slider" )
  243. #self.sb.setTickmarks( TQSlider.Below )
  244. self.sb.setTickmarks( 1 )
  245. self.sb.setTickInterval( 10 )
  246. #self.sb.setFocusPolicy( TQWidget.TabFocus )
  247. self.sb.setFocusPolicy( 1 )
  248. self.sb.setFixedHeight( self.sb.sizeHint().height() )
  249. self.vbox.addWidget( self.sb )
  250. self.connect( self.sb, SIGNAL("valueChanged(int)"), self.sliderValueChanged )
  251. TQToolTip.add( self.sb, "slider" )
  252. # Create a combo box
  253. self.combo = TQComboBox( FALSE, self, "comboBox" )
  254. self.combo.insertItem( "darkBlue" )
  255. self.combo.insertItem( "darkRed" )
  256. self.combo.insertItem( "darkGreen" )
  257. self.combo.insertItem( "blue" )
  258. self.combo.insertItem( "red" )
  259. self.combo.setFixedHeight( self.combo.sizeHint().height() )
  260. self.vbox.addWidget( self.combo )
  261. self.connect( self.combo, SIGNAL("activated(int)"), self.comboBoxItemActivated )
  262. TQToolTip.add( self.combo, "read-only combo box" )
  263. # Create an editable combo box
  264. self.edCombo = TQComboBox( TRUE, self, "edComboBox" )
  265. self.edCombo.insertItem( "Permutable" )
  266. self.edCombo.insertItem( "Malleable" )
  267. self.edCombo.insertItem( "Adaptable" )
  268. self.edCombo.insertItem( "Alterable" )
  269. self.edCombo.insertItem( "Inconstant" )
  270. self.edCombo.setFixedHeight( self.edCombo.sizeHint().height() )
  271. self.vbox.addWidget( self.edCombo )
  272. self.connect( self.edCombo, SIGNAL("activated(const TQString &)"), self.edComboBoxItemActivated)
  273. TQToolTip.add( self.edCombo, "editable combo box" )
  274. self.edCombo.setAutoCompletion( TRUE )
  275. self.vbox.addStretch( 1 )
  276. self.vbox = TQVBoxLayout( 8 )
  277. self.grid.addLayout( self.vbox, 2, 2 )
  278. # Create a spin box
  279. self.spin = TQSpinBox( 0, 10, 1, self, "spin" )
  280. self.spin.setSuffix( " mm" )
  281. self.spin.setSpecialValueText( "Auto" )
  282. self.spin.setMinimumSize( self.spin.sizeHint() )
  283. self.connect( self.spin, SIGNAL( "valueChanged(const TQString &)" ), self.spinBoxValueChanged )
  284. TQToolTip.add( self.spin, "spin box" )
  285. self.vbox.addWidget( self.spin )
  286. self.vbox.addStretch( 1 )
  287. # Create a multi line edit
  288. self.mle = TQMultiLineEdit( self, "multiLineEdit" )
  289. self.grid.addMultiCellWidget( self.mle, 3, 3, 1, 2 )
  290. self.mle.setMinimumHeight( self.mle.fontMetrics().height() * 3 )
  291. self.mle.setText("This is a TQMultiLineEdit widget,\n"
  292. "useful for small multi-line\n"
  293. "input fields.")
  294. TQToolTip.add( self.mle, "multi line editor" )
  295. # Create a single line edit
  296. self.le = TQLineEdit( self, "lineEdit" )
  297. self.grid.addMultiCellWidget( self.le, 4, 4, 1, 2 )
  298. self.le.setFixedHeight( self.le.sizeHint().height() )
  299. self.connect( self.le, SIGNAL("textChanged(const TQString &)"), self.lineEditTextChanged )
  300. TQToolTip.add( self.le, "single line editor" )
  301. # Create a horizontal line (sort of TQFrame) above the message line
  302. self.separator = TQFrame( self, "separatorLine" )
  303. self.separator.setFrameStyle( TQFrame.HLine | TQFrame.Sunken )
  304. self.separator.setFixedHeight( self.separator.sizeHint().height() )
  305. self.grid.addMultiCellWidget( self.separator, 5, 5, 0, 2 )
  306. TQToolTip.add( self.separator, "tool tips on a separator! wow!" )
  307. self.grid.setRowStretch( 0, 0 )
  308. self.grid.setRowStretch( 1, 0 )
  309. self.grid.setRowStretch( 2, 0 )
  310. self.grid.setRowStretch( 3, 1 )
  311. self.grid.setRowStretch( 4, 1 )
  312. self.grid.setRowStretch( 5, 0 )
  313. self.grid.setColStretch( 0, 1 )
  314. self.grid.setColStretch( 1, 1 )
  315. self.grid.setColStretch( 2, 1 )
  316. # Create an label and a message in a plain widget
  317. # The message is updated when buttons are clicked etc.
  318. self.hbox = TQHBoxLayout()
  319. self.topLayout.addLayout( self.hbox )
  320. self.msgLabel = TQLabel( self, "msgLabel" )
  321. self.msgLabel.setText( "Message:" )
  322. self.msgLabel.setAlignment( TQt.AlignHCenter | TQt.AlignVCenter )
  323. self.msgLabel.setFixedSize( self.msgLabel.sizeHint() )
  324. self.hbox.addWidget( self.msgLabel )
  325. TQToolTip.add( self.msgLabel, "label 1" )
  326. self.msg = TQLabel( self, "message" )
  327. self.msg.setFrameStyle( TQFrame.Panel | TQFrame.Sunken )
  328. self.msg.setAlignment( TQt.AlignCenter )
  329. self.msg.setFont( TQFont( "times", 12, TQFont.Bold ) )
  330. self.msg.setText( "Message" )
  331. self.msg.setFixedHeight( self.msg.sizeHint().height() )
  332. self.msg.setText( "" )
  333. self.hbox.addWidget( self.msg, 5 )
  334. TQToolTip.add( self.msg, "label 2" )
  335. self.topLayout.activate()
  336. def setStatus(self, text):
  337. self.msg.setText( text )
  338. def movieUpdate( self, r ):
  339. # Uncomment this to test animated icons on your window manager
  340. self.setIcon( self.movie.framePixmap() )
  341. def movieStatus( self, s ):
  342. if s == TQMovie.SourceEmpty or s == TQMovie.UnrecognizedFormat:
  343. pm = TQPixmap('tt-logo.png')
  344. self.movielabel.setPixmap(pm)
  345. self.movielabel.setFixedSize(pm.size())
  346. else:
  347. if ( self.movielabel.movie() ): # for flicker-free animation:
  348. self.movielabel.setBackgroundMode( TQWidget.NoBackground )
  349. def button1Clicked( self ):
  350. self.msg.setText( "The first push button was clicked" )
  351. def checkBoxClicked( self, id ):
  352. txt = TQString()
  353. txt = "Check box %s clicked : " % str(id)
  354. chk = ["-","-","-"]
  355. if self.cb[0].isChecked():
  356. chk[0] = "r"
  357. if self.cb[1].isChecked():
  358. chk[1] = "w"
  359. if self.cb[2].isChecked():
  360. chk[2] = "x"
  361. txt = txt + str(chk[0]+chk[1]+chk[2])
  362. self.msg.setText( txt )
  363. def edComboBoxItemActivated( self, text):
  364. txt = TQString()
  365. txt = "Editable Combo Box set to %s" % text
  366. self.msg.setText( txt )
  367. def radioButtonClicked( self, id ):
  368. txt = TQString()
  369. txt = "Radio button #%d clicked" % id
  370. self.msg.setText( txt )
  371. def listBoxItemSelected( self, index ):
  372. txt = TQString()
  373. txt = "List box item %d selected" % index
  374. self.msg.setText( txt )
  375. def sliderValueChanged( self, value ):
  376. txt = TQString()
  377. txt = "Movie set to %d%% of normal speed" % value
  378. self.msg.setText( txt )
  379. self.movie.setSpeed( value )
  380. def comboBoxItemActivated( self, index ):
  381. txt = TQString()
  382. txt = "Comboxo box item %d activated" % index
  383. self.msg.setText( txt )
  384. if index == 0:
  385. TQApplication.setWinStyleHighlightColor( TQt.darkBlue )
  386. elif index == 1:
  387. TQApplication.setWinStyleHighlightColor( TQt.darkRed )
  388. elif index == 2:
  389. TQApplication.setWinStyleHighlightColor( TQt.darkGreen )
  390. elif index == 3:
  391. TQApplication.setWinStyleHighlightColor( TQt.blue )
  392. elif index == 4:
  393. TQApplication.setWinStyleHighlightColor( TQt.red )
  394. def lineEditTextChanged( self, newText ):
  395. self.msg.setText("Line edit text: " + unicode(newText))
  396. def spinBoxValueChanged( self, valueText ):
  397. self.msg.setText("Spin box value: " + unicode(valueText))
  398. # All application events are passed throught this event filter.
  399. # We're using it to display some information about a clicked
  400. # widget (right mouse button + CTRL).
  401. #def eventFilter( self, event ):
  402. # identify_now = TRUE
  403. # if event.type() == Event_MouseButtonPress and identify_now:
  404. # e = TQMouseEvent( event )
  405. # if (e.button() == TQt.RightButton) and (e.state() & TQt.ControlButton) != 0:
  406. # txt = TQString( "The clicked widget is a\n" )
  407. # txt = txt + TQObect.className()
  408. # txt = txt + "\nThe widget's name is\n"
  409. # if TQObject.name():
  410. # txt = txt + TQObject.name()
  411. # else:
  412. # txt = txt + "<no name>"
  413. # identify_now = FALSE # don't do it in message box
  414. # TQMessageBox.message( "Identify Widget", txt, 0, TQObject )
  415. # identify_now = TRUE; # allow it again
  416. # return FALSE # don't eat event
  417. ################################################################################################
  418. #TQApplication.setColourSpec( TQApplication.CustomColor )
  419. a = TQApplication( sys.argv )
  420. w = WidgetView()
  421. a.setMainWidget( w )
  422. w.show()
  423. a.exec_loop()