File: PmwTreeBrowse.py.html

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (273 lines) | stat: -rw-r--r-- 11,465 bytes parent folder | download | duplicates (2)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>

<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
  <META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
        <TITLE>PmwTreeBrowse.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
import Pmw
import Tkinter

<STRONG><FONT COLOR="#CC6600">class DoubleSelects</FONT></STRONG>:
<STRONG>    def __init__</STRONG>(self, hull):
	self.level = len(hull.treeLevel) <FONT COLOR="#DD0000">#hasn't been added to list yet!</FONT>
	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 = <FONT COLOR="#009900">'Pane '</FONT> + str(num) + <FONT COLOR="#009900">"t"</FONT>
	self.innerPane.add(self.topName, size = .5,)

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

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

<STRONG>    def defNodeCmd</STRONG>(self):
	sels = self.topBox.getcurselection()
	if len(sels) == 0:
	    print <FONT COLOR="#009900">'No selection for double click'</FONT>
	else:
	    print <FONT COLOR="#009900">'Double click:'</FONT>, sels[0]

<STRONG>    def selectionLeafCommand</STRONG>(self):
	sels = self.bottomBox.getcurselection()
	if len(sels) == 0:
	    print <FONT COLOR="#009900">'No selection'</FONT>
	else:
	    print <FONT COLOR="#009900">'Selection:'</FONT>, sels[0]

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

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

	<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
        Pmw.MegaWidget.__init__(self, parent)
	oldInterior = Pmw.MegaWidget.interior(self)
	print "OLDINTER", oldInterior

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

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

<STRONG>    def addRoot</STRONG>(self, tree):

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



<STRONG>    def addPane</STRONG>(self):
	self.treeLevel.append(DoubleSelects(self))

<STRONG>    def clearPanes</STRONG>(self,thislevel):
	for i in range(thislevel,len(self.treeLevel)):
	    print "clear attributes from level ",i, self.treeLevel
	    self.treeLevel[i].bottomBox.delete(0, <FONT COLOR="#009900">'end'</FONT>)
	for i in range(thislevel+1,len(self.treeLevel)):
	    print "clear nodes from level ",i, self.treeLevel
	    self.treeLevel[i].topBox.delete(0, <FONT COLOR="#009900">'end'</FONT>)
<STRONG>    def selectedNode</STRONG>(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)<FONT COLOR="#DD0000">#, self.current</FONT>
	nodes = self.trees[0].getNodeNames(tuple(self.current))
<FONT COLOR="#DD0000">#	nnodes = []</FONT>
<FONT COLOR="#DD0000">#	for i in nodes:</FONT>
<FONT COLOR="#DD0000">#	    nnodes.append(i[-1])</FONT>
<FONT COLOR="#DD0000">#	nodes = nnodes</FONT>
<FONT COLOR="#DD0000">#	print "NODES", nodes</FONT>
	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, (<FONT COLOR="#009900">'end'</FONT>,)+tuple(attributes))

	if len(nodes) > 0:
	    topbox = self.treeLevel[nextlevel].topBox
	    apply(topbox.insert, (<FONT COLOR="#009900">'end'</FONT>,)+tuple(nodes))
<STRONG>    def interior</STRONG>(self):
	return self.__dialogChildSite


<STRONG>def test</STRONG>():
    import os
<STRONG><FONT COLOR="#CC6600">    class Tree</FONT></STRONG>:
<STRONG>	def __init__</STRONG>(self, root):
	    self.root = root
	    self.current = []

<STRONG>	def _selected</STRONG>(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,<FONT COLOR="#009900">"/"</FONT>))
	    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
<FONT COLOR="#DD0000"># 	def mkdict(self,arg,dirname,names):</FONT>
# 	    pieces = tuple(string.splitfields(dirname,<FONT COLOR="#009900">"/"</FONT>)[1:])
<FONT COLOR="#DD0000"># 	    nodes = self.dict[pieces,"nodes"] = []</FONT>
<FONT COLOR="#DD0000"># 	    leaves = self.dict[pieces,"leaves"] = []</FONT>
<FONT COLOR="#DD0000"># 	    for name in names:</FONT>
<FONT COLOR="#DD0000"># 		if os.path.isdir(os.path.join(dirname,name)):</FONT>
<FONT COLOR="#DD0000"># 		    nodes.append(name)</FONT>
<FONT COLOR="#DD0000"># 		else:</FONT>
<FONT COLOR="#DD0000"># 		    leaves.append(name)</FONT>

# 	    depth = len(string.splitfields(dirname,<FONT COLOR="#009900">"/"</FONT>))-1

<STRONG>	def getRootKey</STRONG>(self):
	    return ((self.root,))

<STRONG>	def getRootName</STRONG>(self):
	    return self.root

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

<STRONG>	def getLeafNames</STRONG>(self, key):
	    return self.dict[key,"nodes"]

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

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

if __name__ == <FONT COLOR="#009900">'__main__'</FONT>:

    root = Pmw.initialise()
    root.resizable(width=0, height=0)
    tree = test()
    widget = TreeBrowser(root, tree)
    widget.pack(anchor="nw", expand=<FONT COLOR="#009900">'yes'</FONT>)


</PRE>

 </BODY> </HTML>