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
171
172
|
#!/usr/bin/python
# Next look at switching to a selected directory from the path (DONE),
# switching to a directory in the list, and removing/changing toolbar
# buttons (and their actions).
# Have a checkbutton for hidden files / directories somewhere.
import os
from PyQt4 import QtGui, QtCore
def clicked(r, c):
print r, c
def iclicked(item):
print item
class DirListing(QtGui.QTreeWidget): #qt
# Only using top-level items of the tree
def __init__(self):
QtGui.QTreeWidget.__init__(self) #qt
self._hcompact = False # used for scheduling header-compaction
self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
self.headers(['Name']) #qt
self.setRootIsDecorated(False) #qt
self.connect(self, QtCore.SIGNAL('itemSelectionChanged()'),
self.s_select)
self.connect(self, QtCore.SIGNAL('itemClicked(QTreeWidgetItem *,int)'),
self.s_clicked)
def s_select(self):
# Signal a selection change, passing the new selection list (indexes)
s = [self.indexOfTopLevelItem(i) for i in self.selectedItems()] #qt
print "Sel", s
def s_clicked(self, item, col): #qt
# I guess I should use this for selection if using single
# click actions, because setting a list up might cause the
# first item to be selected (it doesn't, actually, so select
# could be used), and it should
# only change directory if actually clicked.
"""This is intended for activating a user-defined editing function.
Tests showed that this is called after the selection is changed, so
if using this signal, use it only in 'Single' selection mode and
use this, not 'select' to record selection changes. Clicking on the
selected row should start editing the cell, otherwise just change
the selection.
"""
ix = self.indexOfTopLevelItem(item) #qt
print ix, col
def headers(self, headers): #qt
self.setHeaderLabels(headers) #qt
if self._hcompact:
self._compact()
def set(self, items, index=-1): #qt
# Note that each item must be a tuple/list containing
# entries for each column.
self.clear() #qt
c = 0
for i in items:
item = QtGui.QTreeWidgetItem(self, i) #qt
self.addTopLevelItem(item) #qt
if c == index:
self.setCurrentItem(item)
c += 1
if self._hcompact:
self._compact()
def x__compact(self, on=True):
self._hcompact = on
if on:
self._compact()
def _compact(self):
for i in range(self.columnCount()): #qt
self.resizeColumnToContents(i) #qt
def dirsel(action):
print action.xtag
i = 0
if action.xindex == 0:
print '/'
else:
path = ''
while i < action.xindex:
i += 1
path += '/' + dirs[i]
print path
setlisting(path)
# the toolbuttons should stay the same until a different lower directory
# is chosen (one not in the old list?)
def setlisting(path):
dlist = os.listdir(path)
dldir = []
dlfile = []
for f in dlist:
if os.path.isdir(path + '/' + f):
dldir.append('d:' + f)
else:
dlfile.append('f:' + f)
dldir.sort()
dlfile.sort()
listing.set([d] for d in (dldir + dlfile))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
app.setStyleSheet("""
QToolButton {
border: 2px solid #8f8f91;
border-radius: 6px;
background-color: yellow;
}
QToolButton:checked {
background-color: #f0c080;
}
""")
window = QtGui.QWidget()
listing = DirListing()
bar = QtGui.QToolBar()
bar.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
actg = QtGui.QActionGroup(bar)
QtCore.QObject.connect(actg, QtCore.SIGNAL('triggered (QAction *)'), dirsel)
actg.setExclusive(True)
layout = QtGui.QVBoxLayout()
layout.addWidget(bar)
layout.addWidget(listing)
window.setLayout(layout)
window.resize(600, 480)
path = '/home/mt/DATA/pyjamas'
dirs = path.split('/')
# dirs = ['', 'home', 'mt', 'DATA', 'software-verylong', 'DOCS', 'python_qt']
butix = 0
for but in dirs:
bw = bar.addAction(but+'/')
bw.setCheckable(True)
actw = actg.addAction(bw)
actw.xtag = but
actw.xindex = butix
butix += 1
setlisting(path)
window.show()
sys.exit(app.exec_())
|