summaryrefslogtreecommitdiffstats
path: root/examples2/dirview.py
blob: 11e7eeda5cfb23c9a34f7eea4795cc1eb1b7d900 (plain)
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
#!/usr/bin/env python

import sys
from python_tqt.qt import *

class Directory(TQListViewItem):
    def __init__(self, parent, name=None):
        apply(TQListViewItem.__init__,(self,parent))
        if isinstance(parent, TQListView):
            self.p = None
            self.f = '/'
        else:
            self.p = parent
            self.f = name
        self.c = []
        self.readable = 1

    def setOpen(self, o):
        if o and not self.childCount():
            s = self.fullName()
            thisDir = TQDir(s)
            if not thisDir.isReadable():
                self.readable = 0
                return

            files = thisDir.entryInfoList()
            if files:
                for f in files:
                    fileName = str(f.fileName())
                    if fileName == '.' or fileName == '..':
                        continue
                    elif f.isSymLink():
                        d = TQListViewItem(self, fileName, 'Symbolic Link')
                    elif f.isDir():
                        d = Directory(self, fileName)
                    else:
                        if f.isFile():
                            s = 'File'
                        else:
                            s = 'Special'
                        d = TQListViewItem(self, fileName, s)
                    self.c.append(d)
    
        TQListViewItem.setOpen(self, o)

    def setup(self):
        self.setExpandable(1)
        TQListViewItem.setup(self)

    def fullName(self):
        if self.p:
            s = self.p.fullName() + self.f + '/'
        else:
            s = '/'
        return s

    def text(self, column):
        if column == 0:
            return self.f
        elif self.readable:
            return 'Directory'
        else:
            return 'Unreadable Directory'

a = TQApplication(sys.argv)
mw = TQListView()
a.setMainWidget(mw)
mw.setCaption('Directory Browser')
mw.addColumn('Name')
mw.addColumn('Type')
mw.resize(400, 400)
mw.setTreeStepSize(20)
root = Directory(mw)
root.setOpen(1)
mw.show()
a.exec_loop()