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.

tut11.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python
  2. # TQt tutorial 11.
  3. import sys
  4. import math
  5. from python_tqt import qt
  6. class LCDRange(qt.TQVBox):
  7. def __init__(self, parent=None, name=None):
  8. qt.TQVBox.__init__(self, parent, name)
  9. lcd = qt.TQLCDNumber(2, self, "lcd")
  10. self.slider = qt.TQSlider(qt.TQt.Horizontal, self, "slider")
  11. self.slider.setRange(0, 99)
  12. self.slider.setValue(0)
  13. self.connect(self.slider, qt.SIGNAL("valueChanged(int)"), lcd, qt.SLOT("display(int)"))
  14. self.connect(self.slider, qt.SIGNAL("valueChanged(int)"), self, qt.PYSIGNAL("valueChanged(int)"))
  15. self.setFocusProxy(self.slider)
  16. def value(self):
  17. return self.slider.value()
  18. def setValue(self, value):
  19. self.slider.setValue(value)
  20. def setRange(self, minVal, maxVal):
  21. if minVal < 0 or maxVal > 99 or minVal > maxVal:
  22. raise ValueError, "LCDRange.setRange(): invalid range"
  23. self.slider.setRange(minVal, maxVal)
  24. class CannonField(qt.TQWidget):
  25. def __init__(self, parent=None, name=None):
  26. qt.TQWidget.__init__(self, parent, name)
  27. self.ang = 45
  28. self.f = 0
  29. self.timerCount = 0
  30. self.autoShootTimer = qt.TQTimer(self, "movement handler")
  31. self.connect(self.autoShootTimer, qt.SIGNAL("timeout()"), self.moveShot)
  32. self.shoot_ang = 0
  33. self.shoot_f = 0
  34. self.setPalette(qt.TQPalette(qt.TQColor(250, 250, 200)))
  35. self.barrelRect = qt.TQRect(33, -4, 15, 8)
  36. def angle(self):
  37. return self.ang
  38. def setAngle(self, degrees):
  39. if degrees < 5:
  40. degrees = 5
  41. if degrees > 70:
  42. degrees = 70
  43. if self.ang == degrees:
  44. return
  45. self.ang = degrees
  46. self.repaint(self.cannonRect(), 0)
  47. self.emit(qt.PYSIGNAL("angleChanged(int)"), (self.ang, ))
  48. def force(self):
  49. return self.f
  50. def setForce(self, newton):
  51. if newton < 0:
  52. newton = 0
  53. if self.f == newton:
  54. return
  55. self.f = newton
  56. self.emit(qt.PYSIGNAL("forceChanged(int)"), (self.f, ))
  57. def shoot(self):
  58. if self.autoShootTimer.isActive():
  59. return
  60. self.timerCount = 0
  61. self.shoot_ang = self.ang
  62. self.shoot_f = self.f
  63. self.autoShootTimer.start(50)
  64. def moveShot(self):
  65. r = qt.TQRegion(self.shotRect())
  66. self.timerCount = self.timerCount + 1
  67. shotR = self.shotRect()
  68. if shotR.x() > self.width() or shotR.y() > self.height():
  69. self.autoShootTimer.stop()
  70. else:
  71. r = r.unite(qt.TQRegion(shotR))
  72. self.repaint(r)
  73. def paintEvent(self, ev):
  74. updateR = ev.rect()
  75. p = qt.TQPainter(self)
  76. if updateR.intersects(self.cannonRect()):
  77. self.paintCannon(p)
  78. if self.autoShootTimer.isActive() and updateR.intersects(self.shotRect()):
  79. self.paintShot(p)
  80. def paintShot(self, p):
  81. p.setBrush(qt.TQt.black)
  82. p.setPen(qt.TQt.NoPen)
  83. p.drawRect(self.shotRect())
  84. def paintCannon(self, p):
  85. cr = self.cannonRect()
  86. pix = qt.TQPixmap(cr.size())
  87. pix.fill(self, cr.topLeft())
  88. tmp = qt.TQPainter(pix)
  89. tmp.setBrush(qt.TQt.blue)
  90. tmp.setPen(qt.TQt.NoPen)
  91. tmp.translate(0, pix.height() - 1)
  92. tmp.drawPie(qt.TQRect(-35, -35, 70, 70), 0, 90 * 16)
  93. tmp.rotate(-self.ang)
  94. tmp.drawRect(self.barrelRect)
  95. tmp.end()
  96. p.drawPixmap(cr.topLeft(), pix)
  97. def cannonRect(self):
  98. r = qt.TQRect(0, 0, 50, 50)
  99. r.moveBottomLeft(self.rect().bottomLeft())
  100. return r
  101. def shotRect(self):
  102. gravity = 4.0
  103. time = self.timerCount / 4.0
  104. velocity = self.shoot_f
  105. radians = self.shoot_ang * 3.14159265 / 180
  106. velx = velocity * math.cos(radians)
  107. vely = velocity * math.sin(radians)
  108. x0 = (self.barrelRect.right() + 5) * math.cos(radians)
  109. y0 = (self.barrelRect.right() + 5) * math.sin(radians)
  110. x = x0 + velx * time
  111. y = y0 + vely * time - 0.5 * gravity * time * time
  112. r = qt.TQRect(0, 0, 6, 6)
  113. r.moveCenter(qt.TQPoint(x, self.height() - 1 - y))
  114. return r
  115. def sizePolicy(self):
  116. return qt.TQSizePolicy(qt.TQSizePolicy.Expanding, qt.TQSizePolicy.Expanding)
  117. class MyWidget(qt.TQWidget):
  118. def __init__(self, parent=None, name=None):
  119. qt.TQWidget.__init__(self, parent, name)
  120. quit = qt.TQPushButton("&Quit", self, "quit")
  121. quit.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  122. self.connect(quit, qt.SIGNAL("clicked()"), qt.tqApp, qt.SLOT("quit()"))
  123. self.angle = LCDRange(self, "angle")
  124. self.angle.setRange(5, 70)
  125. self.force = LCDRange(self, "force")
  126. self.force.setRange(10, 50)
  127. self.cannonField = CannonField(self, "cannonField")
  128. self.connect(self.angle, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setAngle)
  129. self.connect(self.cannonField, qt.PYSIGNAL("angleChanged(int)"), self.angle.setValue)
  130. self.connect(self.force, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setForce)
  131. self.connect(self.cannonField, qt.PYSIGNAL("forceChanged(int)"), self.force.setValue)
  132. shoot = qt.TQPushButton("&Shoot", self, "shoot")
  133. shoot.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  134. self.connect(shoot, qt.SIGNAL("clicked()"), self.cannonField.shoot)
  135. grid = qt.TQGridLayout(self, 2, 2, 10)
  136. grid.addWidget(quit, 0, 0)
  137. grid.addWidget(self.cannonField, 1, 1)
  138. grid.setColStretch(1, 10)
  139. leftBox = qt.TQVBoxLayout()
  140. grid.addLayout(leftBox, 1, 0)
  141. leftBox.addWidget(self.angle)
  142. leftBox.addWidget(self.force)
  143. topBox = qt.TQHBoxLayout()
  144. grid.addLayout(topBox, 0, 1)
  145. topBox.addWidget(shoot)
  146. topBox.addStretch(1)
  147. self.angle.setValue(60)
  148. self.force.setValue(25)
  149. self.angle.setFocus()
  150. qt.TQApplication.setColorSpec(qt.TQApplication.CustomColor)
  151. a = qt.TQApplication(sys.argv)
  152. w = MyWidget()
  153. w.setGeometry(100, 100, 500, 355)
  154. a.setMainWidget(w)
  155. w.show()
  156. sys.exit(a.exec_loop())