summaryrefslogtreecommitdiffstats
path: root/python/pyqt/examples2/dirview.py
blob: ae232e98cc243181256c2b6b6835205b1ea6d77a (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 qt import *

class Directory(QListViewItem):
    def __init__(self, parent, name=None):
        apply(QListViewItem.__init__,(self,parent))
        if isinstance(parent, QListView):
            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 = QDir(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 = QListViewItem(self, fileName, 'Symbolic Link')
                    elif f.isDir():
                        d = Directory(self, fileName)
                    else:
                        if f.isFile():
                            s = 'File'
                        else:
                            s = 'Special'
                        d = QListViewItem(self, fileName, s)
                    self.c.append(d)
    
        QListViewItem.setOpen(self, o)

    def setup(self):
        self.setExpandable(1)
        QListViewItem.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 = QApplication(sys.argv)
mw = QListView()
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()