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.

desktop.py 4.9KB

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