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.

progress.py 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/usr/bin/env python
  2. #/****************************************************************************
  3. #** $Id: progress.py 3.3.7 edited Aug 31 2005 $
  4. #**
  5. #** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for TQt. This example
  8. #** program may be used, distributed and modified without limitation.
  9. #**
  10. #** Converted to PyTQt3 by Hans-Peter Jansen <hpj@urpla.net>
  11. #**
  12. #*****************************************************************************/
  13. import sys
  14. from python_tqt.qt import *
  15. from random import randint
  16. class AnimatedThingy(TQLabel):
  17. def __init__(self, parent, s):
  18. TQLabel.__init__(self, parent)
  19. self.label = TQString(s)
  20. self.setEraseColor(TQt.white)
  21. self.label.append("\n... and wasting CPU\nwith this animation!\n")
  22. self.step = 0
  23. self.nqix = 10
  24. self.stepoff = 2
  25. self.stepmax = 7
  26. self.lwidth = 2
  27. self.tdelay = 100
  28. self.ox, self.oy = [], []
  29. for i in range(self.nqix):
  30. self.ox.append((0, 0))
  31. self.oy.append((0, 0))
  32. self.x0, self.y0, self.x1, self.y1 = 0, 0, 0, 0
  33. self.dx0 = randint(0, self.stepmax) + self.stepoff
  34. self.dy0 = randint(0, self.stepmax) + self.stepoff
  35. self.dx1 = randint(0, self.stepmax) + self.stepoff
  36. self.dy1 = randint(0, self.stepmax) + self.stepoff
  37. def show(self):
  38. if not self.isVisible():
  39. self.startTimer(self.tdelay)
  40. TQWidget.show(self)
  41. def hide(self):
  42. TQWidget.hide(self)
  43. self.killTimers()
  44. def sizeHint(self):
  45. return TQSize(120, 100)
  46. def timerEvent(self, e):
  47. p, pn = self._painter()
  48. pn.setColor(self.eraseColor())
  49. p.setPen(pn)
  50. self.step = step = (self.step + 1) % self.nqix
  51. # erase line
  52. p.drawLine(self.ox[step][0], self.oy[step][0],
  53. self.ox[step][1], self.oy[step][1])
  54. # calc new line coordinates
  55. ww, wh = self.width(), self.height()
  56. self.x0, self.dx0 = self._inc(self.x0, self.dx0, ww)
  57. self.y0, self.dy0 = self._inc(self.y0, self.dy0, wh)
  58. self.x1, self.dx1 = self._inc(self.x1, self.dx1, ww)
  59. self.y1, self.dy1 = self._inc(self.y1, self.dy1, wh)
  60. self.ox[step] = (self.x0, self.x1)
  61. self.oy[step] = (self.y0, self.y1)
  62. self._drawqix(p, pn, step)
  63. self._drawtxt(p)
  64. def paintEvent(self, event):
  65. p, pn = self._painter()
  66. p.setClipRect(event.rect())
  67. for i in range(self.nqix):
  68. self._drawqix(p, pn, i)
  69. self._drawtxt(p)
  70. def _painter(self):
  71. p = TQPainter(self)
  72. # we need to create a new pen from p.pen() with TQPen() in order
  73. # to make it fully functional (changes are discarded otherwise)
  74. pn = TQPen(p.pen())
  75. pn.setWidth(self.lwidth)
  76. p.setPen(pn)
  77. return p, pn
  78. def _drawqix(self, p, pn, step):
  79. # rainbow effect
  80. pn.setColor(TQColor((step * 255)/self.nqix, 255, 255, TQColor.Hsv))
  81. p.setPen(pn)
  82. p.drawLine(self.ox[step][0], self.oy[step][0],
  83. self.ox[step][1], self.oy[step][1])
  84. def _drawtxt(self, p):
  85. p.setPen(self.colorGroup().text())
  86. p.drawText(self.rect(), TQt.AlignCenter, self.label)
  87. def _inc(self, x, dx, b):
  88. x += dx
  89. if x < 0:
  90. x = 0
  91. dx = randint(0, self.stepmax) + self.stepoff;
  92. elif x >= b:
  93. x = b - 1
  94. dx = -(randint(0, self.stepmax) + self.stepoff)
  95. return x, dx
  96. class CPUWaster(TQWidget):
  97. def __init__(self):
  98. TQWidget.__init__(self)
  99. self.first_draw_item = 1000
  100. self.last_draw_item = 1006
  101. self.rects = 0
  102. self.timer_driven = 0
  103. self.default_label = 0
  104. self.got_stop = False
  105. self.pb = None # non modal progress dialog
  106. self.menubar = menubar = TQMenuBar(self, "menu")
  107. self.file_ = file_ = TQPopupMenu()
  108. menubar.insertItem("&File", file_)
  109. for i in range(self.first_draw_item, self.last_draw_item + 1):
  110. file_.insertItem(self.drawItemText(i), i)
  111. file_.setAccel(TQKeySequence(
  112. "Ctrl+%s" % (i - self.first_draw_item)), i)
  113. self.connect(menubar, SIGNAL("activated(int)"), self.doMenuItem)
  114. file_.insertSeparator()
  115. file_.insertItem("Quit", tqApp, SLOT("quit()"), TQKeySequence("Ctrl+Q"))
  116. self.options = options = TQPopupMenu()
  117. menubar.insertItem("&Options", options)
  118. self.ld_id = options.insertItem("Loop driven", self.loopDriven,
  119. TQKeySequence("Alt+L"))
  120. self.td_id = options.insertItem("Timer driven", self.timerDriven,
  121. TQKeySequence("Alt+T"))
  122. options.insertSeparator()
  123. self.dl_id = options.insertItem("Default label", self.defaultLabel,
  124. TQKeySequence("Alt+D"))
  125. self.cl_id = options.insertItem("Custom label", self.customLabel,
  126. TQKeySequence("Alt+C"))
  127. options.insertSeparator()
  128. self.md_id = options.insertItem("No minimum duration",
  129. self.toggleMinimumDuration,
  130. TQKeySequence("Alt+M"))
  131. options.setCheckable(True)
  132. # default option settings
  133. self.timerDriven()
  134. self.customLabel()
  135. self.toggleMinimumDuration()
  136. self.resize(400, 300)
  137. self.setEraseColor(TQt.black)
  138. def drawItemRects(self, id_):
  139. r = 100
  140. for n in range(id_ - self.first_draw_item):
  141. if not n % 3:
  142. r *= 4
  143. else:
  144. r *= 5
  145. return r
  146. def drawItemText(self, id_):
  147. return TQString("%d Rectangles" % self.drawItemRects(id_))
  148. def enableDrawingItems(self, yes):
  149. for i in range(self.first_draw_item, self.last_draw_item + 1):
  150. self.menubar.setItemEnabled(i, yes)
  151. # slots
  152. def doMenuItem(self, id_):
  153. if id_ >= self.first_draw_item and id_ <= self.last_draw_item:
  154. self.draw(self.drawItemRects(id_))
  155. def timerDriven(self):
  156. self.timer_driven = True
  157. self.options.setItemChecked(self.td_id, True)
  158. self.options.setItemChecked(self.ld_id, False)
  159. def loopDriven(self):
  160. self.timer_driven = False
  161. self.options.setItemChecked(self.ld_id, True)
  162. self.options.setItemChecked(self.td_id, False)
  163. def defaultLabel(self):
  164. self.default_label = True
  165. self.options.setItemChecked(self.dl_id, True)
  166. self.options.setItemChecked(self.cl_id, False)
  167. def customLabel(self):
  168. self.default_label = False
  169. self.options.setItemChecked(self.dl_id, False)
  170. self.options.setItemChecked(self.cl_id, True)
  171. def toggleMinimumDuration(self):
  172. self.options.setItemChecked(self.md_id,
  173. not self.options.isItemChecked(self.md_id))
  174. def stopDrawing(self):
  175. self.got_stop = True
  176. def newProgressDialog(self, label, steps, modal):
  177. d = TQProgressDialog(label, "Cancel", steps, self, "progress", modal)
  178. if self.options.isItemChecked(self.md_id):
  179. d.setMinimumDuration(0)
  180. if not self.default_label:
  181. d.setLabel(AnimatedThingy(d, label))
  182. d.setCaption("Please Wait")
  183. return d
  184. def draw(self, n):
  185. if self.timer_driven:
  186. if self.pb:
  187. tqWarning("This cannot happen!")
  188. return
  189. self.rects = n
  190. self.pb = self.newProgressDialog("Drawing rectangles.\n"
  191. "Using timer event.", n, False)
  192. self.connect(self.pb, SIGNAL("cancelled()"), self.stopDrawing)
  193. self.enableDrawingItems(False)
  194. self.startTimer(0)
  195. self.got_stop = False
  196. else: # loop driven with modal progress dialog
  197. lpb = self.newProgressDialog("Drawing rectangles.\n"
  198. "Using loop.", n, True)
  199. p = TQPainter(self)
  200. for i in range(n):
  201. lpb.setProgress(i)
  202. if lpb.wasCancelled():
  203. break
  204. self._draw(p)
  205. self._clear(p)
  206. lpb.cancel()
  207. del lpb
  208. def timerEvent(self, e):
  209. if not self.got_stop:
  210. self.pb.setProgress(self.pb.totalSteps() - self.rects)
  211. self.rects -= 1
  212. p = TQPainter(self)
  213. self._draw(p)
  214. if not self.rects or self.got_stop:
  215. if not self.got_stop:
  216. self.pb.setProgress(self.pb.totalSteps())
  217. self._clear(p)
  218. self.enableDrawingItems(True)
  219. self.killTimers()
  220. self.pb.cancel()
  221. del self.pb
  222. self.pb = None
  223. def _draw(self, p):
  224. ww = self.width()
  225. wh = self.height()
  226. if ww > 8 and wh > 8:
  227. c = TQColor(randint(0, 255), randint(0, 255), randint(0, 255))
  228. x = randint(0, ww - 8)
  229. y = randint(0, wh - 8)
  230. w = randint(0, ww - x)
  231. h = randint(0, wh - y)
  232. p.fillRect(x, y, w, h, TQBrush(c))
  233. def _clear(self, p):
  234. p.fillRect(0, 0, self.width(), self.height(), TQBrush(self.eraseColor()))
  235. if __name__ == "__main__":
  236. app = TQApplication(sys.argv)
  237. try:
  238. n = int(sys.argv[1])
  239. except:
  240. n = 1
  241. wlst = [] # keep a ref around
  242. for i in range(n):
  243. cpuw = CPUWaster()
  244. if i == 0:
  245. app.setMainWidget(cpuw)
  246. cpuw.show()
  247. wlst.append(cpuw)
  248. app.exec_loop()