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.

canvas.py 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #!/usr/bin/env python
  2. import sys
  3. from python_tqt.qt import *
  4. from python_tqt.qtcanvas import *
  5. import random
  6. True = 1
  7. False = 0
  8. butterfly_fn = TQString.null
  9. butterflyimg = []
  10. logo_fn = TQString.null
  11. logoimg = []
  12. bouncy_logo = None
  13. views = []
  14. class ImageItem(TQCanvasRectangle):
  15. def __init__(self,img,canvas):
  16. TQCanvasRectangle.__init__(self,canvas)
  17. self.imageRTTI=984376
  18. self.image=img
  19. self.pixmap=TQPixmap()
  20. self.setSize(self.image.width(), self.image.height())
  21. self.pixmap.convertFromImage(self.image, TQt.OrderedAlphaDither);
  22. def rtti(self):
  23. return self.imageRTTI
  24. def hit(self,p):
  25. ix = p.x()-self.x()
  26. iy = p.y()-self.y()
  27. if not self.image.valid( ix , iy ):
  28. return False
  29. self.pixel = self.image.pixel( ix, iy )
  30. return (tqAlpha( self.pixel ) != 0)
  31. def drawShape(self,p):
  32. p.drawPixmap( self.x(), self.y(), self.pixmap )
  33. class NodeItem(TQCanvasEllipse):
  34. def __init__(self,canvas):
  35. TQCanvasEllipse.__init__(self,6,6,canvas)
  36. self.__inList=[]
  37. self.__outList=[]
  38. self.setPen(TQPen(TQt.black))
  39. self.setBrush(TQBrush(TQt.red))
  40. self.setZ(128)
  41. def addInEdge(self,edge):
  42. self.__inList.append(edge)
  43. def addOutEdge(self,edge):
  44. self.__outList.append(edge)
  45. def moveBy(self,dx,dy):
  46. TQCanvasEllipse.moveBy(self,dx,dy)
  47. for each_edge in self.__inList:
  48. each_edge.setToPoint( int(self.x()), int(self.y()) )
  49. for each_edge in self.__outList:
  50. each_edge.setFromPoint( int(self.x()), int(self.y()) )
  51. class EdgeItem(TQCanvasLine):
  52. __c=0
  53. def __init__(self,fromNode, toNode,canvas):
  54. TQCanvasLine.__init__(self,canvas)
  55. self.__c=self.__c+1
  56. self.setPen(TQPen(TQt.black))
  57. self.setBrush(TQBrush(TQt.red))
  58. fromNode.addOutEdge(self)
  59. toNode.addInEdge(self)
  60. self.setPoints(int(fromNode.x()),int(fromNode.y()), int(toNode.x()), int(toNode.y()))
  61. self.setZ(127)
  62. def setFromPoint(self,x,y):
  63. self.setPoints(x,y,self.endPoint().x(),self.endPoint().y())
  64. def setToPoint(self,x,y):
  65. self.setPoints(self.startPoint().x(), self.startPoint().y(),x,y)
  66. def count(self):
  67. return self.__c
  68. def moveBy(self,dx,dy):
  69. pass
  70. class FigureEditor(TQCanvasView):
  71. def __init__(self,c,parent,name,f):
  72. TQCanvasView.__init__(self,c,parent,name,f)
  73. self.__moving=0
  74. self.__moving_start= 0
  75. def contentsMousePressEvent(self,e): # TQMouseEvent e
  76. point = self.inverseWorldMatrix().map(e.pos())
  77. ilist = self.canvas().collisions(point) #TQCanvasItemList ilist
  78. for each_item in ilist:
  79. if each_item.rtti()==984376:
  80. if not each_item.hit(point):
  81. continue
  82. self.__moving=each_item
  83. self.__moving_start=point
  84. return
  85. self.__moving=0
  86. def clear(self):
  87. ilist = self.canvas().allItems()
  88. for each_item in ilist:
  89. if each_item:
  90. each_item.setCanvas(None)
  91. del each_item
  92. self.canvas().update()
  93. def contentsMouseMoveEvent(self,e):
  94. if self.__moving :
  95. point = self.inverseWorldMatrix().map(e.pos());
  96. self.__moving.moveBy(point.x() - self.__moving_start.x(),point.y() - self.__moving_start.y())
  97. self.__moving_start = point
  98. self.canvas().update()
  99. class BouncyLogo(TQCanvasSprite):
  100. def __init__(self,canvas):
  101. # Make sure the logo exists.
  102. global bouncy_logo
  103. if bouncy_logo is None:
  104. bouncy_logo=TQCanvasPixmapArray("qt-trans.xpm")
  105. TQCanvasSprite.__init__(self,None,canvas)
  106. self.setSequence(bouncy_logo)
  107. self.setAnimated(True)
  108. self.initPos()
  109. self.logo_rtti=1234
  110. def rtti(self):
  111. return self.logo_rtti
  112. def initPos(self):
  113. self.initSpeed()
  114. trial=1000
  115. self.move(random.random()%self.canvas().width(), random.random()%self.canvas().height())
  116. self.advance(0)
  117. trial=trial-1
  118. while (trial & (self.xVelocity()==0 )& (self.yVelocity()==0)):
  119. elf.move(random.random()%self.canvas().width(), random.random()%self.canvas().height())
  120. self.advance(0)
  121. trial=trial-1
  122. def initSpeed(self):
  123. speed=4.0
  124. d=random.random()%1024/1024.0
  125. self.setVelocity(d*speed*2-speed, (1-d)*speed*2-speed)
  126. def advance(self,stage):
  127. if stage==0:
  128. vx=self.xVelocity()
  129. vy=self.yVelocity()
  130. if (vx==0.0) & (vy==0.0):
  131. self.initSpeed()
  132. vx=self.xVelocity()
  133. vy=self.yVelocity()
  134. nx=self.x()+vx
  135. ny=self.y()+vy
  136. if (nx<0) | (nx >= self.canvas().width()):
  137. vx=-vx
  138. if (ny<0) | (ny >= self.canvas().height()):
  139. vy=-vy
  140. for bounce in [0,1,2,3]:
  141. l=self.collisions(False)
  142. for hit in l:
  143. if (hit.rtti()==1234) & (hit.collidesWith(self)):
  144. if bounce==0:
  145. vx=-vx
  146. elif bounce==1:
  147. vy=-vy
  148. vx=-vx
  149. elif bounce==2:
  150. vx=-vx
  151. elif bounce==3:
  152. vx=0
  153. vy=0
  154. self.setVelocity(vx,vy)
  155. break
  156. if (self.x()+vx < 0) | (self.x()+vx >= self.canvas().width()):
  157. vx=0
  158. if (self.y()+vy < 0) | (self.y()+vy >= self.canvas().height()):
  159. vy=0
  160. self.setVelocity(vx,vy)
  161. elif stage==1:
  162. TQCanvasItem.advance(self,stage)
  163. class Main (TQMainWindow):
  164. def __init__(self,c,parent,name,f=0):
  165. TQMainWindow.__init__(self,parent,name,f)
  166. self.editor=FigureEditor(c,self,name,f)
  167. self.printer=TQPrinter()
  168. self.dbf_id=0
  169. self.canvas=c
  170. self.mainCount=0
  171. file=TQPopupMenu(self.menuBar())
  172. file.insertItem("&Fill canvas", self.init, TQt.CTRL+TQt.Key_F)
  173. file.insertItem("&Erase canvas", self.clear, TQt.CTRL+TQt.Key_E)
  174. file.insertItem("&New view", self.newView, TQt.CTRL+TQt.Key_N)
  175. file.insertSeparator();
  176. file.insertItem("&Print", self._print, TQt.CTRL+TQt.Key_P)
  177. file.insertSeparator()
  178. file.insertItem("E&xit", tqApp, SLOT("quit()"), TQt.CTRL+TQt.Key_Q)
  179. self.menuBar().insertItem("&File", file)
  180. edit = TQPopupMenu(self.menuBar() )
  181. edit.insertItem("Add &Circle", self.addCircle, TQt.ALT+TQt.Key_C)
  182. edit.insertItem("Add &Hexagon", self.addHexagon, TQt.ALT+TQt.Key_H)
  183. edit.insertItem("Add &Polygon", self.addPolygon, TQt.ALT+TQt.Key_P)
  184. edit.insertItem("Add Spl&ine", self.addSpline, TQt.ALT+TQt.Key_I)
  185. edit.insertItem("Add &Text", self.addText, TQt.ALT+TQt.Key_T)
  186. edit.insertItem("Add &Line", self.addLine, TQt.ALT+TQt.Key_L)
  187. edit.insertItem("Add &Rectangle", self.addRectangle, TQt.ALT+TQt.Key_R)
  188. edit.insertItem("Add &Sprite", self.addSprite, TQt.ALT+TQt.Key_S)
  189. edit.insertItem("Create &Mesh", self.addMesh, TQt.ALT+TQt.Key_M )
  190. edit.insertItem("Add &Alpha-blended image", self.addButterfly, TQt.ALT+TQt.Key_A)
  191. self.menuBar().insertItem("&Edit", edit)
  192. view = TQPopupMenu(self.menuBar() );
  193. view.insertItem("&Enlarge", self.enlarge, TQt.SHIFT+TQt.CTRL+TQt.Key_Plus);
  194. view.insertItem("Shr&ink", self.shrink, TQt.SHIFT+TQt.CTRL+TQt.Key_Minus);
  195. view.insertSeparator();
  196. view.insertItem("&Rotate clockwise", self.rotateClockwise, TQt.CTRL+TQt.Key_PageDown);
  197. view.insertItem("Rotate &counterclockwise", self.rotateCounterClockwise, TQt.CTRL+TQt.Key_PageUp);
  198. view.insertItem("&Zoom in", self.zoomIn, TQt.CTRL+TQt.Key_Plus);
  199. view.insertItem("Zoom &out", self.zoomOut, TQt.CTRL+TQt.Key_Minus);
  200. view.insertItem("Translate left", self.moveL, TQt.CTRL+TQt.Key_Left);
  201. view.insertItem("Translate right", self.moveR, TQt.CTRL+TQt.Key_Right);
  202. view.insertItem("Translate up", self.moveU, TQt.CTRL+TQt.Key_Up);
  203. view.insertItem("Translate down", self.moveD, TQt.CTRL+TQt.Key_Down);
  204. view.insertItem("&Mirror", self.mirror, TQt.CTRL+TQt.Key_Home);
  205. self.menuBar().insertItem("&View", view)
  206. self.options = TQPopupMenu( self.menuBar() );
  207. self.dbf_id = self.options.insertItem("Double buffer", self.toggleDoubleBuffer)
  208. self.options.setItemChecked(self.dbf_id, True)
  209. self.menuBar().insertItem("&Options",self.options)
  210. self.menuBar().insertSeparator();
  211. help = TQPopupMenu( self.menuBar() )
  212. help.insertItem("&About", self.help, TQt.Key_F1)
  213. help.insertItem("&About TQt", self.aboutTQt, TQt.Key_F2)
  214. help.setItemChecked(self.dbf_id, True)
  215. self.menuBar().insertItem("&Help",help)
  216. self.statusBar()
  217. self.setCentralWidget(self.editor)
  218. self.printer = 0
  219. self.tb=0
  220. self.tp=0
  221. self.init()
  222. def init(self):
  223. self.clear()
  224. r=24
  225. r=r+1
  226. random.seed(r)
  227. for i in range(self.canvas.width()/56):
  228. self.addButterfly()
  229. for j in range(self.canvas.width()/85):
  230. self.addHexagon()
  231. for k in range(self.canvas.width()/128):
  232. self.addLogo()
  233. def newView(self):
  234. m=Main(self.canvas,None,"new windiw",TQt.WDestructiveClose)
  235. tqApp.setMainWidget(m)
  236. m.show()
  237. tqApp.setMainWidget(None)
  238. views.append(m)
  239. def clear(self):
  240. self.editor.clear()
  241. def help(self):
  242. TQMessageBox.information(None, "PyTQt Canvas Example",
  243. "<h3>The PyTQt TQCanvas classes example</h3><hr>"
  244. "<p>This is the PyTQt implementation of "
  245. "TQt canvas example.</p> by Sadi Kose "
  246. "<i>(kose@nuvox.net)</i><hr>"
  247. "<ul>"
  248. "<li> Press ALT-S for some sprites."
  249. "<li> Press ALT-C for some circles."
  250. "<li> Press ALT-L for some lines."
  251. "<li> Drag the objects around."
  252. "<li> Read the code!"
  253. "</ul>","Dismiss")
  254. def aboutTQt(self):
  255. TQMessageBox.aboutTQt(self,"PyTQt Canvas Example")
  256. def toggleDoubleBuffer(self):
  257. s = not self.options.isItemChecked(self.dbf_id)
  258. self.options.setItemChecked(self.dbf_id,s)
  259. self.canvas.setDoubleBuffering(s)
  260. def enlarge(self):
  261. self.canvas.resize(self.canvas.width()*4/3, self.canvas.height()*4/3)
  262. def shrink(self):
  263. self.canvas.resize(self.canvas.width()*3/4, self.canvas.height()*3/4)
  264. def rotateClockwise(self):
  265. m = self.editor.worldMatrix()
  266. m.rotate( 22.5 )
  267. self.editor.setWorldMatrix( m )
  268. def rotateCounterClockwise(self):
  269. m = self.editor.worldMatrix()
  270. m.rotate( -22.5 )
  271. self.editor.setWorldMatrix( m )
  272. def zoomIn(self):
  273. m = self.editor.worldMatrix()
  274. m.scale( 2.0, 2.0 )
  275. self.editor.setWorldMatrix( m )
  276. def zoomOut(self):
  277. m = self.editor.worldMatrix()
  278. m.scale( 0.5, 0.5 )
  279. self.editor.setWorldMatrix( m )
  280. def mirror(self):
  281. m = self.editor.worldMatrix()
  282. m.scale( -1, 1 )
  283. self.editor.setWorldMatrix( m )
  284. def moveL(self):
  285. m = self.editor.worldMatrix()
  286. m.translate( -16, 0 )
  287. self.editor.setWorldMatrix( m )
  288. def moveR(self):
  289. m = self.editor.worldMatrix()
  290. m.translate( +16, 0 )
  291. self.editor.setWorldMatrix( m )
  292. def moveU(self):
  293. m = self.editor.worldMatrix()
  294. m.translate( 0, -16 )
  295. self.editor.setWorldMatrix( m )
  296. def moveD(self):
  297. m = self.editor.worldMatrix();
  298. m.translate( 0, +16 );
  299. self.editor.setWorldMatrix( m )
  300. def _print(self):
  301. if not self.printer:
  302. self.printer = TQPrinter()
  303. if self.printer.setup(self) :
  304. pp=TQPainter(self.printer)
  305. self.canvas.drawArea(TQRect(0,0,self.canvas.width(),self.canvas.height()),pp,False)
  306. def addSprite(self):
  307. i = BouncyLogo(self.canvas)
  308. i.setZ(256*random.random()%256);
  309. i.show();
  310. def addButterfly(self):
  311. if butterfly_fn.isEmpty():
  312. return
  313. if not butterflyimg:
  314. butterflyimg.append(TQImage())
  315. butterflyimg[0].load(butterfly_fn)
  316. butterflyimg.append(TQImage())
  317. butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
  318. int(butterflyimg[0].height()*0.75) )
  319. butterflyimg.append(TQImage())
  320. butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
  321. int(butterflyimg[0].height()*0.5) )
  322. butterflyimg.append(TQImage())
  323. butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
  324. int(butterflyimg[0].height()*0.25) )
  325. i = ImageItem(butterflyimg[int(4*random.random()%4)],self.canvas)
  326. i.move((self.canvas.width()-butterflyimg[0].width())*random.random()%(self.canvas.width()-butterflyimg[0].width()),
  327. (self.canvas.height()-butterflyimg[0].height())*random.random()%(self.canvas.height()-butterflyimg[0].height()))
  328. i.setZ(256*random.random()%256+250);
  329. i.show()
  330. def addLogo(self):
  331. if logo_fn.isEmpty():
  332. return;
  333. if not logoimg:
  334. logoimg.append(TQImage())
  335. logoimg[0].load( logo_fn )
  336. logoimg.append(TQImage())
  337. logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
  338. int(logoimg[0].height()*0.75) )
  339. logoimg.append(TQImage())
  340. logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
  341. int(logoimg[0].height()*0.5) )
  342. logoimg.append(TQImage())
  343. logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
  344. int(logoimg[0].height()*0.25) );
  345. i = ImageItem(logoimg[int(4*random.random()%4)],self.canvas)
  346. i.move((self.canvas.width()-logoimg[0].width())*random.random()%(self.canvas.width()-logoimg[0].width()),
  347. (self.canvas.height()-logoimg[0].width())*random.random()%(self.canvas.height()-logoimg[0].width()))
  348. i.setZ(256*random.random()%256+256)
  349. i.show()
  350. def addCircle(self):
  351. i = TQCanvasEllipse(50,50,self.canvas)
  352. i.setBrush( TQBrush(TQColor(256*random.random()%32*8,256*random.random()%32*8,256*random.random()%32*8) ))
  353. i.move(self.canvas.width()*random.random()%self.canvas.width(),self.canvas.width()*random.random()%self.canvas.height())
  354. i.setZ(256*random.random()%256)
  355. i.show()
  356. def addHexagon(self):
  357. i = TQCanvasPolygon(self.canvas)
  358. size = canvas.width() / 25
  359. pa=TQPointArray(6)
  360. pa.setPoint(0,TQPoint(2*size,0))
  361. pa.setPoint(1,TQPoint(size,-size*173/100))
  362. pa.setPoint(2,TQPoint(-size,-size*173/100))
  363. pa.setPoint(3,TQPoint(-2*size,0))
  364. pa.setPoint(4,TQPoint(-size,size*173/100))
  365. pa.setPoint(5,TQPoint(size,size*173/100))
  366. i.setPoints(pa)
  367. i.setBrush( TQBrush(TQColor(256*random.random()%32*8,256*random.random()%32*8,256*random.random()%32*8) ))
  368. i.move(self.canvas.width()*random.random()%self.canvas.width(),self.canvas.width()*random.random()%self.canvas.height())
  369. i.setZ(256*random.random()%256)
  370. i.show()
  371. def addPolygon(self):
  372. i = TQCanvasPolygon(self.canvas)
  373. size = self.canvas.width()/2
  374. pa=TQPointArray(6)
  375. pa.setPoint(0, TQPoint(0,0))
  376. pa.setPoint(1, TQPoint(size,size/5))
  377. pa.setPoint(2, TQPoint(size*4/5,size))
  378. pa.setPoint(3, TQPoint(size/6,size*5/4))
  379. pa.setPoint(4, TQPoint(size*3/4,size*3/4))
  380. pa.setPoint(5, TQPoint(size*3/4,size/4))
  381. i.setPoints(pa)
  382. i.setBrush(TQBrush( TQColor(256*random.random()%32*8,256*random.random()%32*8,256*random.random()%32*8)) )
  383. i.move(self.canvas.width()*random.random()%self.canvas.width(),self.canvas.width()*random.random()%self.canvas.height())
  384. i.setZ(256*random.random()%256)
  385. i.show()
  386. def addSpline(self):
  387. i = TQCanvasSpline(self.canvas)
  388. size = canvas.width()/6
  389. pa=TQPointArray(12)
  390. pa.setPoint(0,TQPoint(0,0))
  391. pa.setPoint(1,TQPoint(size/2,0))
  392. pa.setPoint(2,TQPoint(size,size/2))
  393. pa.setPoint(3,TQPoint(size,size))
  394. pa.setPoint(4,TQPoint(size,size*3/2))
  395. pa.setPoint(5,TQPoint(size/2,size*2))
  396. pa.setPoint(6,TQPoint(0,size*2))
  397. pa.setPoint(7,TQPoint(-size/2,size*2))
  398. pa.setPoint(8,TQPoint(size/4,size*3/2))
  399. pa.setPoint(9,TQPoint(0,size))
  400. pa.setPoint(10,TQPoint(-size/4,size/2))
  401. pa.setPoint(11,TQPoint(-size/2,0))
  402. i.setControlPoints(pa)
  403. i.setBrush( TQBrush(TQColor(256*random.random()%32*8,256*random.random()%32*8,256*random.random()%32*8) ))
  404. i.move(self.canvas.width()*random.random()%self.canvas.width(),self.canvas.width()*random.random()%self.canvas.height())
  405. i.setZ(256*random.random()%256)
  406. i.show()
  407. def addText(self):
  408. i = TQCanvasText(self.canvas)
  409. i.setText("TQCanvasText")
  410. i.move(self.canvas.width()*random.random()%self.canvas.width(),self.canvas.width()*random.random()%self.canvas.height())
  411. i.setZ(256*random.random()%256)
  412. i.show()
  413. def addLine(self):
  414. i = TQCanvasLine(self.canvas);
  415. i.setPoints( self.canvas.width()*random.random()%self.canvas.width(), self.canvas.width()*random.random()%self.canvas.height(),
  416. self.canvas.width()*random.random()%self.canvas.width(), self.canvas.width()*random.random()%self.canvas.height() )
  417. i.setPen( TQPen(TQColor(256*random.random()%32*8,256*random.random()%32*8,256*random.random()%32*8), 6) )
  418. i.setZ(256*random.random()%256)
  419. i.show()
  420. def ternary(self,exp,x,y):
  421. if exp:
  422. return x
  423. else:
  424. return y
  425. def addMesh(self):
  426. x0 = 0;
  427. y0 = 0;
  428. if not self.tb:
  429. self.tb = TQBrush( TQt.red )
  430. if not self.tp:
  431. self.tp = TQPen( TQt.black )
  432. nodecount = 0;
  433. w = self.canvas.width()
  434. h = self.canvas.height()
  435. dist = 30
  436. rows = h / dist
  437. cols = w / dist
  438. #ifndef QT_NO_PROGRESSDIALOG
  439. #progress=TQProgressDialog( "Creating mesh...", "Abort", rows,
  440. # self, "progress", True );
  441. #endif
  442. lastRow=[]
  443. for c in range(cols):
  444. lastRow.append(NodeItem(self.canvas))
  445. for j in range(rows):
  446. n = self.ternary(j%2 , cols-1 , cols)
  447. prev = 0;
  448. for i in range(n):
  449. el = NodeItem( self.canvas )
  450. nodecount=nodecount+1
  451. r = 20*20*random.random()
  452. xrand = r %20
  453. yrand = (r/20) %20
  454. el.move( xrand + x0 + i*dist + self.ternary(j%2 , dist/2 , 0 ),
  455. yrand + y0 + j*dist );
  456. if j > 0 :
  457. if i < cols-1 :
  458. EdgeItem( lastRow[i], el, self.canvas ).show()
  459. if j%2 :
  460. EdgeItem( lastRow[i+1], el, self.canvas ).show()
  461. elif i > 0 :
  462. EdgeItem( lastRow[i-1], el, self.canvas ).show()
  463. if prev:
  464. EdgeItem( prev, el, self.canvas ).show()
  465. if i > 0 :
  466. lastRow[i-1] = prev
  467. prev = el
  468. el.show()
  469. lastRow[n-1]=prev
  470. #ifndef QT_NO_PROGRESSDIALOG
  471. #progress.setProgress( j )
  472. #if progress.wasCancelled() :
  473. # break
  474. #endif
  475. #ifndef QT_NO_PROGRESSDIALOG
  476. #progress.setProgress( rows )
  477. #endif
  478. #// tqDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() );
  479. def addRectangle(self):
  480. i = TQCanvasRectangle( self.canvas.width()*random.random()%self.canvas.width(),
  481. self.canvas.width()*random.random()%self.canvas.height(),
  482. self.canvas.width()/5,self.canvas.width()/5,self.canvas)
  483. z = 256*random.random()%256
  484. i.setBrush( TQBrush(TQColor(z,z,z) ))
  485. i.setPen( TQPen(TQColor(self.canvas.width()*random.random()%32*8,
  486. self.canvas.width()*random.random()%32*8,
  487. self.canvas.width()*random.random()%32*8), 6) )
  488. i.setZ(z)
  489. i.show()
  490. if __name__=='__main__':
  491. app=TQApplication(sys.argv)
  492. if len(sys.argv) > 1:
  493. butterfly_fn=TQString(sys.argv[1])
  494. else:
  495. butterfly_fn=TQString("butterfly.png")
  496. if len(sys.argv) > 2:
  497. logo_fn = TQString(sys.argv[2])
  498. else:
  499. logo_fn=TQString("qtlogo.png")
  500. canvas=TQCanvas(800,600)
  501. canvas.setAdvancePeriod(30)
  502. m=Main(canvas,None,"pyqt canvas example")
  503. m.resize(m.sizeHint())
  504. tqApp.setMainWidget(m)
  505. m.setCaption("TQt Canvas Example ported to PyTQt")
  506. if TQApplication.desktop().width() > m.width() + 10 and TQApplication.desktop().height() > m.height() + 30:
  507. m.show()
  508. else:
  509. m.showMaximized()
  510. m.show();
  511. #// m.help();
  512. tqApp.setMainWidget(None);
  513. TQObject.connect( tqApp, SIGNAL("lastWindowClosed()"), tqApp, SLOT("quit()") )
  514. app.exec_loop()
  515. # We need to explicitly delete the canvas now (and, therefore, the main
  516. # window beforehand) to make sure that the sprite logo doesn't get garbage
  517. # collected first.
  518. views = []
  519. del m
  520. del canvas