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