Debian Codegraph @

fltk/test/tree_demo.py

raw symbols SCIP
1 #! /usr/bin/env python
2
3 #
4 # "$Id: tree.py 158 2006-01-11 08:00:45Z andreasheld $"
5 #
6 # Tree widget test program for pyFLTK the Python bindings
7 # for the Fast Light Tool Kit (FLTK).
8 #
9 # FLTK copyright 1998-1999 by Bill Spitzak and others.
10 # pyFLTK copyright 2003 by Andreas Held and others.
11 #
12 # This library is free software you can redistribute it and/or
13 # modify it under the terms of the GNU Library General Public
14 # License, version 2.0 as published by the Free Software Foundation.
15 #
16 # This library is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # Library General Public License for more details.
20 #
21 # You should have received a copy of the GNU Library General Public
22 # License along with this library if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 # USA.
25 #
26 # Please report all bugs and problems to "[email protected]".
27 #
28
29
30 """
31 Implements a tree widget for pyfltk version 1.1.x
32
33 Contains a demo which can be viewed by executing this file
34
35 Written Dec 2005 by David McNab <[email protected]>
36 Released under the terms of the GNU Lesser General Public License.
37
38 No warrantee, yada yada.
39 """
40
41 import traceback
42
43 from fltk import *
44
45 item_height = 20
46
47 item_indent = 25
48
49 item_box_indent = 10
50 item_box_width = 9
51 class Node:
52 """
53 a tree node contains a payload, a state, a list of children
54 and a ref to its parent
55
56 If you want to intercept node open/close events, you
57 should subclass this
58 """
59 def __init__(self, tree, parent, title, payload=None, children=None):
60 """
61 create a tree node
62
63 title is the text to be displayed
64
65 payload is an object which this node contains, can be any
66 python object
67
68 children is a list of child nodes (optional)
69 """
70 self.tree = tree
71 self.parent = parent
72 self.level = parent.level + 1
73 self.title = title
74 self.payload = payload
75 if children == None:
76 children = []
77 self.children = children
78 self.isopen = False
79
80 def append(self, title, data=None, refresh=True):
81 """
82 adds data to root node
83
84 Arguments:
85 - title - text to show in tree display
86 - data - an optional data payload
87 - refresh - default True - whether to refresh the
88 tree after adding this node
89 """
90 node = self.__class__(self.tree, self, title, data)
91 self.children.append(node)
92
93 self.tree._nodeToShow = node
94 self.tree.refresh()
95
96 return node
97
98 def refresh(self):
99 """
100 draws this node, and children (if any, and if open)
101 """
102 tree = self.tree
103
104 line = " " * (self.level * 4 + 1)
105
106 if self.children:
107 if self.isopen:
108 line += "[-] "
109 else:
110 line += "[+] "
111
112 line += self.title
113
114 tree.visibleNodes.append(self)
115 self.treeIdx = tree.nitems
116
117 # if this node was selected, mark it
118 if tree._nodeToShow == self:
119 tree._nodeToShowIdx = tree.nitems
120
121 tree.nitems += 1
122 tree.add(line)
123
124 if self.isopen:
125 for child in self.children:
126 child.refresh()
127
128 def open(self):
129 """
130 opens this node
131
132 Invokes the on_open handler, if any
133 """
134 self.isopen = True
135 self.on_open()
136 self.tree.refresh()
137
138 def close(self):
139 """
140 closes this node
141
142 Invokes the on_close handler, if any
143 """
144 self.isopen = False
145 self.on_close()
146 self.tree.refresh()
147
148 def toggle(self):
149 """
150 toggles open/close state
151 """
152 if self.isopen:
153 self.close()
154 else:
155 self.open()
156
157
158 def promote(self):
159 """
160 promotes this node up one level in hierarchy
161 """
162 parent = self.parent
163
164 if parent == self.tree:
165 # already at top - bail
166 return
167
168 grandparent = parent.parent
169
170 parentIdx = grandparent.children.index(parent)
171 selfIdx = parent.children.index(self)
172
173 parent.children.remove(self)
174 grandparent.children.insert(parentIdx, self)
175 self.parent = grandparent
176
177 self._changeLevel(-1)
178
179 self.tree._nodeToShow = self
180 self.tree.refresh()
181
182 #self.tree.value(self.tree.visibleNodes.index(self) + 1)
183
184 def demote(self):
185 """
186 demotest this item, if possible
187 """
188 selidx = self.tree.value()
189
190 siblings = self.parent.children
191
192 selfidx = siblings.index(self)
193
194 if selfidx == 0:
195 # already subordinate to previous visible node
196 return
197
198 siblings.remove(self)
199
200 prevsibling = siblings[selfidx-1]
201 if not prevsibling.isopen:
202 prevsibling.isopen = True
203
204 prevsibling.children.append(self)
205
206 self.parent = prevsibling
207 self._changeLevel(1)
208
209 self.tree._nodeToShow = self
210 self.tree.refresh()
211
212 #self.tree.value(selidx)
213
214 def moveup(self):
215 """
216 moves this node up one
217 """
218 selidx = self.tree.value()
219
220 siblings = self.parent.children
221 selfidx = siblings.index(self)
222
223 if selfidx == 0:
224 # already top child
225 return
226
227 prevnode = siblings[selfidx-1]
228 siblings[selfidx-1] = self
229 siblings[selfidx] = prevnode
230
231 self.tree._nodeToShow = self
232 self.tree.refresh()
233
234 #self.tree.value(selidx-1)
235
236 def movedown(self):
237 """
238 moves this node down one
239 """
240 selidx = self.tree.value()
241
242 siblings = self.parent.children
243 selfidx = siblings.index(self)
244
245 if selfidx >= len(siblings)-1:
246 # already top child
247 return
248
249 nextnode = siblings[selfidx+1]
250 siblings[selfidx+1] = self
251 siblings[selfidx] = nextnode
252
253 self.tree._nodeToShow = self
254 self.tree.refresh()
255
256 #self.tree.value(selidx+1)
257
258 def cut(self):
259
260 self.parent.children.remove(self)
261 self.tree.refresh()
262 return self
263
264 def _changeLevel(self, diff=0):
265
266 self.level += diff
267 for child in self.children:
268 child._changeLevel(diff)
269
270 def _setLevel(self, level):
271
272 self.level = level
273 for child in self.children:
274 child._setLevel(level)
275
276 def on_open(self):
277 """
278 handler for when this node is opened
279
280 You might want to use this, say, when using
281 the tree to browse a large hierarchy such as
282 a filesystem
283
284 Your handler should either execute the .append() method,
285 or manipulate the .children list
286
287 Override if needed
288 """
289 print("on_open: not overridden")
290
291 def on_close(self):
292 """
293 handler for when this node is closed
294
295 You might want to use this, say, when using
296 the tree to browse a large hierarchy such as
297 a filesystem
298
299 Your handler should either execute the .append() method,
300 or manipulate the .children list
301
302 Typically, you will want to do::
303
304 self.children = []
305
306 Override if needed
307 """
308 print("on_close: not overridden")
309
310
311 class Fl_Tree(Fl_Hold_Browser):
312 """
313 Implements a tree widget
314
315 If you want handlers for node open/close,
316 you should subclass this class, and override
317 the 'nodeClass' attribute
318 """
319 nodeClass = Node
320
321 def __init__(self, x, y, w, h, label=0):
322 """
323 Create the tree widget, initially empty
324
325 The label will be the text of the root node
326 """
327 Fl_Hold_Browser.__init__(self, x, y, w, h, label)
328
329 self.children = []
330
331 self.widget = self
332 self.isopen = True
333
334 self.visibleNodes = []
335 self.level = -1
336
337 self._nodeToShow = None
338 self._nodeToShowIdx = -1
339
340 # add the box deco
341 self.box(FL_DOWN_BOX)
342
343 self.callback(self._on_click)
344
345 def handle(self, evid):
346
347 ret = Fl_Hold_Browser.handle(self, evid)
348
349 return ret
350
351 def append(self, title, data = None):
352 """
353 adds data to root node
354 """
355 node = self.nodeClass(self, self, title, data)
356
357 self.children.append(node)
358
359 self.refresh()
360
361 return node
362
363 def refresh(self):
364 """
365 redraws all the contents of this tree
366 """
367 self.clear()
368
369 # enumerate out all the children, and children's children
370 self.nitems = 0
371 self._nodeToShowIdx = -1
372
373 self.visibleNodes = []
374 for child in self.children:
375 child.refresh()
376
377 if self._nodeToShowIdx >= 0:
378 self.value(self._nodeToShowIdx+1)
379
380 self._nodeToShowIdx = -1
381 self._nodeToShow = None
382
383 def _on_click(self, ev):
384
385 selidx = self.value()
386
387 if selidx <= 0:
388 return
389 thisidx = selidx - 1
390
391 node = self.visibleNodes[thisidx]
392
393 x = Fl.event_x() - self.x()
394
395 xMin = (node.level) * 16 + 8
396 xMax = xMin + 16
397
398 if x >= xMin and x <= xMax:
399 node.toggle()
400 self.value(selidx)
401 else:
402 self.on_select(node)
403
404 def on_select(self, node):
405 """
406 override in callbacks
407 """
408 def valuenode(self):
409
410 idx = self.value()
411
412 if idx <= 0:
413 return None
414
415 idx -= 1
416
417 return self.visibleNodes[idx]
418
419 def cut(self):
420 """
421 does a cut of selected node
422 """
423 node = self.valuenode()
424 if node is None:
425 return None
426
427 node.parent.children.remove(node)
428 self.refresh()
429 return node
430
431 def paste(self, node):
432 """
433 does a paste of selected node
434 """
435 parent = self.valuenode()
436 if parent is None:
437 return None
438
439 parent.children.append(node)
440 node._setLevel(parent.level+1)
441 self.refresh()
442
443
444 def demo():
445 """
446 runs a small demo program
447 """
448 xWin = 200
449 yWin = 200
450 wWin = 300
451 hWin = 400
452
453 xTree = 20
454 yTree = 20
455 wTree = 200
456 hTree = hWin - 2 * yTree
457
458 xButs = xTree + wTree + 20
459
460 class MyTree(Fl_Tree):
461 """
462 """
463 def on_select(self, node):
464 print(f"on_select: node={node.title}")
465
466 print("creating window")
467 win = Fl_Window(xWin, yWin, wWin, hWin, "Fl_Tree demo")
468
469 print("adding tree")
470 tree = MyTree(xTree, yTree, wTree, hTree, "something")
471 tree.align(FL_ALIGN_TOP)
472
473 def on_promote(ev):
474 node = tree.valuenode()
475 if node:
476 print(f"promote: {node.title}")
477 node.promote()
478
479 def on_demote(ev):
480 node = tree.valuenode()
481 if node:
482 print(f"demote: {node.title}")
483 node.demote()
484
485 def on_moveup(ev):
486 node = tree.valuenode()
487 if node:
488 print(f"moveup: {node.title}")
489 node.moveup()
490
491 def on_movedown(ev):
492 node = tree.valuenode()
493 if node:
494 print(f"movedown: {node.title}")
495 node.movedown()
496
497 but_promote = Fl_Button(xButs, 20, 20, 20, "@<-")
498 but_promote.callback(on_promote)
499
500 but_demote = Fl_Button(xButs, 50, 20, 20, "@->")
501 but_demote.callback(on_demote)
502
503 but_moveup = Fl_Button(xButs, 80, 20, 20, "up")
504 but_moveup.callback(on_moveup)
505
506 but_movedown = Fl_Button(xButs, 110, 20, 20, "dn")
507 but_movedown.callback(on_movedown)
508
509
510 print("ending window")
511 win.end()
512
513 print("showing window")
514 win.show()
515
516 # add stuff to root node
517 if 1:
518 for i in range(3):
519 node = tree.append(f"item-{i}")
520 if 1:
521 for j in range(3):
522 subnode = node.append(f"item-{i}{j}")
523 if 0:
524 for k in range(2):
525 subnode.append(f"item-{i}{j}{k}")
526
527 print("entering main loop")
528 Fl.run()
529
530 if __name__ == '__main__':
531 demo()
532
533