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ů.

116 lines
3.1KB

  1. #!/usr/bin/env python
  2. import sys
  3. from python_tqt.qt import *
  4. class Table(TQTableView):
  5. def __init__(self, numRows, numCols, parent=None, name=''):
  6. TQTableView.__init__(self, parent, name)
  7. self.curRow = self.curCol = 0
  8. self.setFocusPolicy(TQWidget.StrongFocus)
  9. self.setBackgroundMode(TQWidget.PaletteBase)
  10. self.setNumCols(numCols)
  11. self.setNumRows(numRows)
  12. self.setCellWidth(100)
  13. self.setCellHeight(30)
  14. self.setTableFlags(Tbl_vScrollBar |
  15. Tbl_hScrollBar |
  16. Tbl_clipCellPainting)
  17. self.resize(400,200)
  18. self.contents = [''] * (numRows * numCols)
  19. def cellContent(self, row, col):
  20. return self.contents[self.indexOf(row,col)]
  21. def setCellContent(self, row, col, c):
  22. self.contents[self.indexOf(row,col)] = c
  23. self.updateCell(row, col)
  24. def paintCell(self, p, row, col):
  25. w = self.cellWidth(col)
  26. h = self.cellHeight(row)
  27. x2 = w-1
  28. y2 = h-1
  29. p.drawLine(x2,0,x2,y2)
  30. p.drawLine(0,y2,x2,y2)
  31. if row == self.curRow and col == self.curCol:
  32. if self.hasFocus():
  33. p.drawRect(0, 0, x2, y2)
  34. else:
  35. p.setPen(TQt.DotLine)
  36. p.drawRect(0, 0, x2, y2)
  37. p.setPen(TQt.SolidLine)
  38. p.drawText(0,0,w,h,TQt.AlignCenter,self.contents[self.indexOf(row,col)])
  39. def mousePressEvent(self, me):
  40. oldRow = self.curRow
  41. oldCol = self.curCol
  42. clickedPos = me.pos()
  43. self.curRow = self.findRow(clickedPos.y())
  44. self.curCol = self.findCol(clickedPos.x())
  45. if self.curRow != oldRow or \
  46. self.curCol != oldCol:
  47. self.updateCell(oldRow, oldCol)
  48. self.updateCell(self.curRow, self.curCol)
  49. def keyPressEvent(self, ke):
  50. oldRow = self.curRow
  51. oldCol = self.curCol
  52. edge = 0
  53. key = ke.key()
  54. if key == Key_Left:
  55. if self.curCol > 0:
  56. self.curCol = self.curCol - 1
  57. edge = self.leftCell()
  58. if self.curCol < edge:
  59. self.setLeftCell(edge-1)
  60. elif key == Key_Right:
  61. if self.curCol < self.numCols()-1:
  62. self.curCol = self.curCol + 1
  63. edge = self.lastColVisible()
  64. if self.curCol >= edge:
  65. self.setLeftCell(self.leftCell()+1)
  66. elif key == Key_Up:
  67. if self.curRow > 0:
  68. self.curRow = self.curRow - 1
  69. edge = self.topCell()
  70. if self.curRow < edge:
  71. self.setTopCell(edge-1)
  72. elif key == Key_Down:
  73. if self.curRow < self.numRows()-1:
  74. self.curRow = self.curRow + 1
  75. edge = self.lastRowVisible()
  76. if self.curRow >= edge:
  77. self.setTopCell(self.topCell()+1)
  78. else:
  79. ke.ignore()
  80. return
  81. if self.curRow != oldRow or \
  82. self.curCol != oldCol:
  83. self.updateCell(oldRow, oldCol)
  84. self.updateCell(self.curRow, self.curCol)
  85. def focusInEvnet(self, fie):
  86. self.updateCell(self.curRow, self.curCol)
  87. def focusOutEvent(self, foe):
  88. self.updateCell(self.curRow, self.curCol)
  89. def indexOf(self, row, col):
  90. return (row * self.numCols()) + col
  91. numRows = 20
  92. numCols = 20
  93. a = TQApplication(sys.argv)
  94. v = Table(numRows, numCols)
  95. for i in range(numRows):
  96. for j in range(numCols):
  97. v.setCellContent(i,j,'%d %c' % (j, 65+(i%26)))
  98. a.setMainWidget(v)
  99. v.show()
  100. a.exec_loop()