SIP4 python bindings for TQt
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

desktop.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python
  2. import sys
  3. from python_tqt.qt import *
  4. seed = 0.353535353535
  5. KINDA_RAND_MAX = 32767
  6. def kindaRand():
  7. global seed
  8. seed = seed * 147
  9. seed = seed - int(seed)
  10. return int(seed*(KINDA_RAND_MAX + 1))
  11. velmax = 15
  12. velmin = 4
  13. def velocity(i):
  14. if i == 1 or i == 2:
  15. i = (kindaRand()&0x7fff % velmax)/3 + velmin
  16. else:
  17. i = (kindaRand()&0x7fff % velmax) + velmin
  18. return i
  19. maxpoints = 5
  20. maxcurves = 8
  21. def poly():
  22. d = TQApplication.desktop()
  23. d.setEraseColor(TQt.white)
  24. xvel = [ 0 ] * maxpoints
  25. yvel = [ 0 ] * maxpoints
  26. head = 0
  27. tail = -maxcurves + 2
  28. a = [ TQPointArray() ] * maxcurves
  29. r = d.rect()
  30. for i in range(maxcurves):
  31. a[i].resize(maxpoints)
  32. p = a[0]
  33. for i in range(maxpoints):
  34. p.setPoint(i, (kindaRand()&0x7fff) % r.width(),
  35. (kindaRand()&0x7fff) % r.height() )
  36. xvel[i] = velocity(i)
  37. yvel[i] = velocity(i)
  38. paint = TQPainter()
  39. paint.begin(d)
  40. for ntimes in range(2000):
  41. paint.setBrush(TQColor(kindaRand()%360,180,255, TQColor.Hsv))
  42. paint.drawPolygon(a[head])
  43. tail = tail + 1
  44. if tail >= maxcurves:
  45. tail = 0
  46. minx = r.left()
  47. maxx = r.right()
  48. miny = r.top()
  49. maxy = r.bottom()
  50. p = a[head]
  51. head = head + 1
  52. if head >= maxcurves:
  53. head = 0
  54. for i in range(maxpoints):
  55. x, y = p.point(i)
  56. x = x + xvel[i]
  57. y = y + yvel[i]
  58. if x >= maxx:
  59. x = maxx - (x - maxx + 1)
  60. xvel[i] = -velocity(i)
  61. if x <= minx:
  62. x = minx + (minx - x + 1)
  63. xvel[i] = velocity(i)
  64. if y >= maxy:
  65. y = maxy - (y - maxy + 1)
  66. yvel[i] = -velocity(i)
  67. if y <= miny:
  68. y = miny + (miny - y + 1)
  69. yvel[i] = velocity(i)
  70. a[head].setPoint(i, x, y)
  71. paint.end()
  72. def rotate():
  73. w = 64
  74. h = 64
  75. image = TQImage(w, h, 8, 128)
  76. for i in range(128):
  77. image.setColor(i, tqRgb(i,0,0))
  78. for y in range(h):
  79. for x in range(w):
  80. image.setPixel(x,y,(x+y)%128)
  81. pm = TQPixmap()
  82. pm.convertFromImage(image)
  83. pm.setOptimization(TQPixmap.BestOptim)
  84. d = TQApplication.desktop()
  85. for i in range(0,361,2):
  86. m = TQWMatrix()
  87. m.rotate(i)
  88. rpm = pm.xForm(m)
  89. d.setErasePixmap(rpm)
  90. d.update()
  91. def generateStone(pm, c1, c2, c3):
  92. p = TQPainter()
  93. p1 = TQPen(c1, 0)
  94. p2 = TQPen(c2, 0)
  95. p3 = TQPen(c3, 0)
  96. p.begin(pm)
  97. for i in range(pm.width()):
  98. for j in range(pm.height()):
  99. r = kindaRand()
  100. if r < KINDA_RAND_MAX / 3:
  101. p.setPen(p1)
  102. elif r < KINDA_RAND_MAX / 3 * 2:
  103. p.setPen(p2)
  104. else:
  105. p.setPen(p3)
  106. p.drawPoint(i, j)
  107. p.end()
  108. def drawShadeText(p, x, y, text, topColor, bottomColor, sw=2):
  109. if not p.isActive():
  110. return
  111. p.setPen(bottomColor)
  112. p.drawText(x+sw, y+sw, text)
  113. p.setPen(topColor)
  114. p.drawText(x, y, text)
  115. class DesktopWidget(TQWidget):
  116. def __init__(self, s, parent=None, name=''):
  117. TQWidget.__init__(self, parent, name, TQt.WType_Desktop | TQt.WPaintDesktop)
  118. self.text = s
  119. self.pm = None
  120. def paintEvent(self, pe):
  121. c1 = self.eraseColor()
  122. c2 = c1.light(104)
  123. c3 = c1.dark(106)
  124. if not self.pm:
  125. self.pm = TQPixmap(64, 64)
  126. generateStone(self.pm, c1, c2, c3)
  127. self.setErasePixmap(self.pm)
  128. self.update()
  129. br = self.fontMetrics().boundingRect(self.text)
  130. offscreen = TQPixmap(br.width(), br.height())
  131. x = self.width()/2 - br.width()/2
  132. y = self.height()/2 - br.height()/2
  133. offscreen.fill(self, x, y)
  134. p = TQPainter()
  135. p.begin(offscreen)
  136. drawShadeText(p, -br.x(), -br.y(), self.text, c2, c3, 3)
  137. p.end()
  138. bitBlt(self, x, y, offscreen)
  139. def desktopWidget(s='Trolltech'):
  140. t = DesktopWidget(s)
  141. t.update()
  142. tqApp.exec_loop()
  143. def desktopText(s='Trolltech'):
  144. border = 20
  145. c1 = tqApp.palette().normal().background()
  146. c2 = c1.light(104)
  147. c3 = c1.dark(106)
  148. pm = TQPixmap(10, 10)
  149. p = TQPainter()
  150. p.begin(pm)
  151. r = p.fontMetrics().boundingRect(s)
  152. p.end()
  153. appWidth = tqApp.desktop().width()
  154. appHeight = tqApp.desktop().height()
  155. if r.width() > appWidth - border*2:
  156. r.setWidth(appWidth - border*2)
  157. if r.height() > appHeight - border*2:
  158. r.setHeight(appHeight - border*2)
  159. pm.resize(r.size().width()+border*2,r.size().height()+border*2)
  160. generateStone(pm, c1, c2, c3)
  161. p.begin(pm)
  162. drawShadeText(p, -r.x()+border, -r.y()+border, s, c2, c3)
  163. p.end()
  164. tqApp.desktop().setErasePixmap(pm)
  165. a = TQApplication(sys.argv)
  166. if len(sys.argv) > 1:
  167. f = TQFont('charter', 96, TQFont.Black)
  168. f.setStyleHint(TQFont.Times)
  169. a.setFont(f)
  170. validOptions = 0
  171. if len(sys.argv) == 2:
  172. validOptions = 1
  173. if sys.argv[1] == '-poly':
  174. poly()
  175. elif sys.argv[1] == '-rotate':
  176. rotate()
  177. elif sys.argv[1] == '-troll':
  178. desktopText()
  179. elif sys.argv[1] == '-trollwidget':
  180. desktopWidget()
  181. else:
  182. validOptions = 0
  183. if len(sys.argv) == 3:
  184. validOptions = 1
  185. if sys.argv[1] == '-shadetext':
  186. desktopText(sys.argv[2])
  187. elif sys.argv[1] == '-shadewidget':
  188. desktopWidget(sys.argv[2])
  189. else:
  190. validOptions = 0
  191. if not validOptions:
  192. print """Usage:
  193. \tdesktop -poly
  194. \tdesktop -rotate
  195. \tdesktop -troll
  196. \tdesktop -trollwidget
  197. \tdesktop -shadetext <text>
  198. \tdesktop -shadewidget <text>"""
  199. rotate()