File: PmwTreeBrowse.py

package info (click to toggle)
python-pmw 0.8.5-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,820 kB
  • ctags: 3,468
  • sloc: python: 14,898; makefile: 37; sh: 17
file content (259 lines) | stat: -rw-r--r-- 8,037 bytes parent folder | download | duplicates (4)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import Pmw
import Tkinter

class DoubleSelects:
    def __init__(self, hull):
	self.level = len(hull.treeLevel) #hasn't been added to list yet!
	self._hull = hull
	self.name = "Pane 0"
	hull.browser.add(self.name, min= .1,size=150 )
	num = len(hull.browser.panes())
	rel = 1.0/num
	print "REL", rel
	hull.browser.configure(hull_width=num*150)
	for i in range(num):
	    hull.browser.configurepane(i,size=rel)

	self.innerPane = Pmw.PanedWidget(hull.browser.pane(self.name))

	self.topName = 'Pane ' + str(num) + "t"
	self.innerPane.add(self.topName, size = .5,)

	self.topBox = Pmw.ScrolledListBox(self.innerPane.pane(self.topName),
	    listbox_selectmode='single',
	    vscrollmode = 'dynamic',
	    selectioncommand=self.selectionNodeCommand,
	    dblclickcommand=self.defNodeCmd)
        self.topBox.pack(fill= 'both', expand = 'yes', padx=0, pady=0)
        self.topBox.component('listbox').configure(exportselection=0)
	self.bottomName = 'Pane ' + str(num) + "b"
	self.innerPane.add(self.bottomName, size=.5 ,)
	self.bottomBox = Pmw.ScrolledListBox(
	    self.innerPane.pane(self.bottomName),
	    listbox_selectmode='single',
	    vscrollmode = 'dynamic',
	    selectioncommand=self.selectionLeafCommand,
	    dblclickcommand=self.defLeafCmd)
        self.bottomBox.pack(fill = 'both', expand='yes', padx = 0, pady = 0)
        self.bottomBox.component('listbox').configure(exportselection=0)
	self.innerPane.pack(expand = 1, fill='both')

    def selectionNodeCommand(self):
	sels = self.topBox.getcurselection()
	if len(sels) == 0:
	    print 'No selection'
	else:
	    print "bbb", self._hull.tree
	    nodes, leaves = self._hull.tree._selected(sels[0],self.level)
	    apply(self.topbox.insert,('end',)+tuple(nodes))
#	    print  self._hull.current
#	    print 'Selection:', sels[0]
#	    self._hull.selectedNode(self.level,sels)
#	    self.list = self.tree.getRootNodeNames()
#	    print "LIST", list
#	    apply(self.topbox.insert,('end',)+tuple(self.list))
	    

    def defNodeCmd(self):
	sels = self.topBox.getcurselection()
	if len(sels) == 0:
	    print 'No selection for double click'
	else:
	    print 'Double click:', sels[0]

    def selectionLeafCommand(self):
	sels = self.bottomBox.getcurselection()
	if len(sels) == 0:
	    print 'No selection'
	else:
	    print 'Selection:', sels[0]

	#  display the contents of the selected file in a dialog box which
	#  is appended to self.tmpwindict in order to facilitate easy
	#  deletion
	import string   # for string.join
	import os       # for os.sep
	# construct the file name given the selected directory list.
	# NOTE:  if a file is selected under the root directory, then
	#        there will be a double-slash prepended to its name.
	#        this is legal under unix, not sure if it is so under DOS.
	#        also, the slash character must be replaced with a p
#	fn = os.sep + string.join(self._hull.current + sels[0], os.sep) + os.sep + sels[0]
#        self._hull.selectedNode(self.level, self.topBox.getcurselection())
        s = self._hull.current[:self.level+1]
	s.append(sels[0])
        fn = os.sep + string.join(s, os.sep)
	print '*** file selected is \'', fn, '\'.'
	print '*** file directory is ', s
	print '*** app trees: ', self._hull.trees
	print '*** app treeLevel: ', self._hull.treeLevel
	self._hull.tmpwindict[sels[0]]= Pmw.TextDialog()
	self._hull.tmpwindict[sels[0]].component('scrolledtext').importfile(fn)
	    
    def defLeafCmd(self):
	sels = self.bottomBox.getcurselection()
	if len(sels) == 0:
	    print 'No selection for double click'
	else:
	    print 'Double click:', sels[0]    
	

class  TreeBrowser(Pmw.MegaWidget):
    def __init__(self, parent=None, tree=None, **kw):
	self.tree = tree
	self.tmpwindict = {}  #  temporary window list implemented as a dict
	                      #  format:  { 'name', <object> }
	self.trees= []
	self.treeLevel = []
	self.current = []
	optiondefs = ()
	self.defineoptions(kw, optiondefs)
	def pane_conf(sizes):
	    print "pane_conf", sizes

	# Initialise the base class (after defining the options).
        Pmw.MegaWidget.__init__(self, parent)
	oldInterior = Pmw.MegaWidget.interior(self)
	print "OLDINTER", oldInterior

	self.browser =self.createcomponent('browserPane',(),None,
		Pmw.PanedWidget, (oldInterior,),
		hull_width=200, hull_height=300,
			        orient='horizontal',
				command=pane_conf,
#		hull_relief = 'raised',
#		hull_borderwidth = 1,
		)
	self.browser.pack(anchor="nw")# fill = 'x', expand = 'yes' )

	self.initialiseoptions(TreeBrowser)
	self.addPane()
	if tree:
	    self.addRoot(tree)

    def addRoot(self, tree):

#	try:	    
	    rootName = tree.getRootKey()
	    print "ROOTNAME",rootName, self.treeLevel
	    apply(self.treeLevel[0].topBox.insert,('end',)+tuple(rootName))
	    self.trees.append(tree)
#	except:
	    print "Won't work, need data"



    def addPane(self):
	self.treeLevel.append(DoubleSelects(self))

    def clearPanes(self,thislevel):
	for i in range(thislevel,len(self.treeLevel)):
	    print "clear attributes from level ",i, self.treeLevel
	    self.treeLevel[i].bottomBox.delete(0, 'end')
	for i in range(thislevel+1,len(self.treeLevel)):
	    print "clear nodes from level ",i, self.treeLevel
	    self.treeLevel[i].topBox.delete(0, 'end')
    def selectedNode(self, level,sels):
	thislevel = level
	nextlevel = level+1
	self.current = self.current[:thislevel]
	print "CURRENT", self.current, sels
	self.current.append(sels[0])
	print "selectedNode", level,sels, len(self.treeLevel)#, self.current
	nodes = self.trees[0].getNodeNames(tuple(self.current))
#	nnodes = []
#	for i in nodes:
#	    nnodes.append(i[-1])
#	nodes = nnodes
#	print "NODES", nodes
	if len(self.treeLevel) <= nextlevel and len(nodes) > 0:
	    self.addPane()
	self.clearPanes(thislevel)
	attributes = self.trees[0].getLeafKeys(tuple(self.current))
	bottombox = self.treeLevel[thislevel].bottomBox
	print "ATTRIBUTES", attributes, bottombox
	apply(bottombox.insert, ('end',)+tuple(attributes))

	if len(nodes) > 0:
	    topbox = self.treeLevel[nextlevel].topBox
	    apply(topbox.insert, ('end',)+tuple(nodes))
    def interior(self):
	return self.__dialogChildSite


def test():
    import os
    class Tree:
	def __init__(self, root):
	    self.root = root
	    self.current = []

	def _selected(self, word,level):
	    if len(self.current) > level:
		self.current[level-1] = word
		self.current = self.current[:level-1]
	    if len(self.current) == level:
		self.current.append(word)
	    names = os.listdir(string.joinfields(self.current,"/"))
	    print names
	    node = []
	    leaves = []
	    for name in names:
		if os.path.isdir(os.path.join(dirname,name)):
 		    nodes.append(name)
 		else:
 		    leaves.append(name)
	    return nodes, leaves
# 	def mkdict(self,arg,dirname,names):
# 	    pieces = tuple(string.splitfields(dirname,"/")[1:])
# 	    nodes = self.dict[pieces,"nodes"] = []
# 	    leaves = self.dict[pieces,"leaves"] = []
# 	    for name in names:
# 		if os.path.isdir(os.path.join(dirname,name)):
# 		    nodes.append(name)
# 		else:
# 		    leaves.append(name)

# 	    depth = len(string.splitfields(dirname,"/"))-1

	def getRootKey(self):
	    return ((self.root,))

	def getRootName(self):
	    return self.root

# 	def getRootNodeNames(self):
# 	    ret = []
# 	    nodelist = self.getNodeKeys(self.getRootKey())
# 	    print "NODES", nodelist
# 	    for node in nodelist:
# 		ret.append(node[1])
# 	    return ret
	def getNodeNames(self, key):
	    return self.dict[key,"nodes"]

	def getLeafNames(self, key):
	    return self.dict[key,"nodes"]

	def getNodeKeys(self, key):
	    ret = []
	    for name in self.dict[key,"nodes"]:
		ret.append(tuple(list(key)+[name]))
	    return ret

	def getLeafKeys(self, key):
	    ret = []
	    for name in self.dict[key,"leaves"]:
		ret.append(name)
	    return ret
    
    return Tree("etc")	

if __name__ == '__main__':

    root = Pmw.initialise()
    root.resizable(width=0, height=0)
    tree = test()
    widget = TreeBrowser(root, tree)
    widget.pack(anchor="nw", expand='yes')