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.

tablestatistics.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: tablestatistics.py,v 1.1 2002/06/19 07:56:07 phil Exp $
  4. #**
  5. #** Copyright (C) 1992-1998 Troll Tech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for PyTQt. This example
  8. #** program may be used, distributed and modified without limitation.
  9. #**
  10. #*****************************************************************************/
  11. import sys
  12. import os
  13. from python_tqt.qt import *
  14. from python_tqt.qttable import *
  15. TRUE = 1
  16. FALSE = 0
  17. # column constants
  18. TB_FILE = 0
  19. TB_SIZE = 1
  20. TB_FLAG = 2
  21. TB_COLS = 3
  22. dirs = (
  23. "kernel",
  24. "tools",
  25. "widgets",
  26. "dialogs",
  27. "xml",
  28. "table",
  29. "network",
  30. "opengl",
  31. "canvas",
  32. )
  33. class Table(TQTable):
  34. def __init__(self):
  35. TQTable.__init__(self, 0, TB_COLS)
  36. self.setSorting(TRUE)
  37. self.horizontalHeader().setLabel(TB_FILE, self.tr("File"))
  38. self.horizontalHeader().setLabel(TB_SIZE, self.tr("Size (bytes)"))
  39. self.horizontalHeader().setLabel(TB_FLAG, self.tr("Use in Sum"))
  40. self.initTable()
  41. self.adjustColumn(TB_FILE)
  42. # if the user edited something we might need to recalculate the sum
  43. self.connect(self, SIGNAL("valueChanged(int, int)"), self.recalcSum)
  44. def initTable(self):
  45. # read all the TQt source and header files into a list
  46. all = []
  47. qtdir = os.getenv("QTDIR")
  48. for i in dirs:
  49. dir = TQDir(os.path.join(qtdir, "src", i))
  50. lst = TQStringList(dir.entryList("*.cpp; *.h"))
  51. for f in lst:
  52. if f.contains("moc"):
  53. continue
  54. all.append(os.path.join(i, str(f)))
  55. # set the number of rows we'll need for the table
  56. self.setNumRows(len(all) + 1)
  57. i = 0
  58. sum = 0L
  59. # insert the data into the table
  60. for it in all:
  61. self.setText(i, TB_FILE, it)
  62. f = TQFile(os.path.join(qtdir, "src", str(it)))
  63. self.setText(i, TB_SIZE, str(f.size()))
  64. ci = ComboItem(self, TQTableItem.WhenCurrent)
  65. self.setItem(i, TB_FLAG, ci)
  66. i = i + 1
  67. sum += f.size()
  68. self.displaySum(sum)
  69. def recalcSum(self, dummy, col):
  70. # only recalc if a value in the second or third column changed
  71. if col < TB_SIZE or col > TB_FLAG:
  72. return
  73. sum = 0L
  74. for i in range(self.numRows()-1):
  75. if str(self.text(i, TB_FLAG)) == "No":
  76. continue
  77. sum += long(str(self.text(i, TB_SIZE)))
  78. self.displaySum(sum)
  79. def displaySum(self, sum):
  80. # insert calculated data
  81. i1 = TableItem(self, TQTableItem.Never, self.tr("Sum"))
  82. self.setItem(self.numRows()-1, TB_FILE, i1)
  83. i2 = TableItem(self, TQTableItem.Never, str(sum))
  84. self.setItem(self.numRows()-1, TB_SIZE, i2)
  85. def sortColumn(self, col, ascending, wholeRows):
  86. # sum row should not be sorted, so get rid of it for now
  87. self.clearCell(self.numRows()-1, TB_FILE)
  88. self.clearCell(self.numRows()-1, TB_SIZE)
  89. # do sort
  90. TQTable.sortColumn(self, col, ascending, TRUE)
  91. # re-insert sum row
  92. self.recalcSum(0, TB_SIZE)
  93. class TableItem(TQTableItem):
  94. def __init__(self, *args):
  95. apply(TQTableItem.__init__, (self,) + args)
  96. def paint(self, p, cg, cr, selected):
  97. g = TQColorGroup(cg)
  98. # last row is the sum row - we want to make it more visible by
  99. # using a red background
  100. if self.row() == self.table().numRows()-1:
  101. g.setColor(TQColorGroup.Base, TQColor("red"))
  102. TQTableItem.paint(self, p, g, cr, selected)
  103. class ComboItem(TQTableItem):
  104. def __init__(self, t, et):
  105. TQTableItem.__init__(self, t, et, "Yes")
  106. self.cb = None
  107. # we do not want this item to be replaced
  108. self.setReplaceable(FALSE)
  109. def createEditor(self):
  110. # create an editor - a combobox in our case
  111. self.cb = TQComboBox(self.table().viewport())
  112. TQObject.connect(self.cb, SIGNAL("activated(int)"),
  113. self.table(), SLOT("doValueChanged()"))
  114. self.cb.insertItem("Yes")
  115. self.cb.insertItem("No")
  116. # and initialize it
  117. if str(self.text()) == "No":
  118. self.cb.setCurrentItem(1)
  119. else:
  120. self.cb.setCurrentItem(0)
  121. return self.cb
  122. def setContentFromEditor(self, w):
  123. # the user changed the value of the combobox, so synchronize the
  124. # value of the item (its text), with the value of the combobox
  125. if w.inherits("TQComboBox"):
  126. self.setText(w.currentText())
  127. else:
  128. TQTableItem.setContentFromEditor(self, w)
  129. def setText(self, s):
  130. # initialize the combobox from the text
  131. if self.cb:
  132. if str(s) == "No":
  133. self.cb.setCurrentItem(1)
  134. else:
  135. self.cb.setCurrentItem(0)
  136. TQTableItem.setText(self, s)
  137. if __name__ == '__main__':
  138. app = TQApplication(sys.argv)
  139. t = Table()
  140. t.setCaption("Statistics")
  141. t.show()
  142. app.setMainWidget(t)
  143. app.exec_loop()