Schedulator summary now predicts release dates based on your bounce goals,
[wvapps.git] / photoreider / dirctrl.py
blob27732e5bad786d1eb24cf34389af2f6744285c30
1 # dirctrl.py: extended version of the GenericDirCtrl
2 # arch-tag: extended version of the GenericDirCtrl
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
4 # license: GPL
6 import wx
7 import os, glob
9 import vfs
10 import zipfile
13 class DirCtrl(wx.GenericDirCtrl):
14 def __init__(self, *args, **kwds):
15 wx.GenericDirCtrl.__init__(self, *args, **kwds)
16 self._handler = wx.EvtHandler()
17 self.PushEventHandler(self._handler)
18 self._added_items = {}
19 self._virtual_tree_done = {}
20 self.tree = wx.PyTypeCast(self.GetTreeCtrl(), 'wxTreeCtrl')
21 imglist = self.tree.GetImageList()
22 if wx.Platform == '__WXGTK__': bmpfile = 'zip.xpm'
23 else: bmpfile = 'zip_win.xpm'
24 bmp = wx.Bitmap(os.path.join('icons', bmpfile), wx.BITMAP_TYPE_XPM)
25 self.zip_image = imglist.Add(bmp)
26 # events
27 wx.EVT_TREE_ITEM_EXPANDING(self._handler, self.tree.GetId(),
28 self.on_expand)
29 wx.EVT_TREE_ITEM_COLLAPSED(self._handler, self.tree.GetId(),
30 self.on_collapse)
31 wx.EVT_TREE_SEL_CHANGED(self._handler, self.tree.GetId(),
32 self.on_sel_changed)
34 def build_path(self, node):
35 if self.tree.GetItemParent(node) == self.tree.GetRootItem():
36 return self.tree.GetItemText(node)
37 return os.path.join(self.build_path(self.tree.GetItemParent(node)),
38 self.tree.GetItemText(node))
40 def add_virtual_tree(self, node, path, zipfilename):
41 zvfs = vfs.get_dirtree(zipfilename)
42 def add_rec(n, p, vfsn):
43 for vfschild in vfsn.children:
44 label = vfschild.name.replace('/', '')
45 child = self.tree.AppendItem(n, label, 0, 1)
46 p1 = p.replace('#zip:', '/')
47 if not p1.endswith('/'): p1 += '/'
48 p1 += label
49 if p.endswith('#zip:'): p2 = p + label
50 else: p2 = p + '/' + label
51 self._added_items[p1] = (p2, child)
52 add_rec(child, p2, vfschild)
53 add_rec(node, path, zvfs.root)
54 self._virtual_tree_done[path] = 1
56 def on_expand(self, event):
57 item = event.GetItem()
58 if item.Ok():
59 path = self.build_path(item)
60 if path in self._added_items:
61 return
62 def go():
63 # first search the subdirs without children, in case there
64 # are some zipfiles there aswell (otherwise it will be
65 # impossible to select them)
66 child, cookie = self.tree.GetFirstChild(item, 0)
67 while True:
68 if not child.Ok(): break
69 if not self.tree.ItemHasChildren(child):
70 if glob.glob(os.path.join(
71 path, self.tree.GetItemText(child), '*.zip')):
72 self.tree.SetItemHasChildren(child)
73 child, cookie = self.tree.GetNextChild(item, cookie)
74 # then add the zipfiles on this directory
75 for name in glob.glob(os.path.join(path, '*.zip')):
76 i = self.tree.AppendItem(item, os.path.basename(name),
77 self.zip_image)
78 self._added_items[name] = (name + '#zip:', i)
80 wx.CallAfter(go)
81 event.Skip()
83 def on_collapse(self, event):
84 item = event.GetItem()
85 if self.build_path(item) in self._added_items:
86 return
87 event.Skip()
89 def on_sel_changed(self, event):
90 item = event.GetItem()
91 path = self.build_path(item)
92 if path in self._added_items:
93 p, n = self._added_items[path]
94 if p.endswith('#zip:') and p not in self._virtual_tree_done:
95 self.add_virtual_tree(n, p, path)
96 event.Skip()
98 def GetPath(self):
99 item = self.tree.GetSelection()
100 if item != self.tree.GetRootItem():
101 path = self.build_path(item)
102 if path in self._added_items:
103 return self._added_items[path][0]
104 return wx.GenericDirCtrl.GetPath(self)
106 def SetPath(self, path):
107 index = path.find('#zip:')
108 if index != -1:
109 if index == len(path)-5: path = path[:-5]
110 else: path = path.replace('#zip:', '/')
111 if path in self._added_items:
112 item = self._added_items[path][1]
113 self.tree.SelectItem(item)
114 self.tree.EnsureVisible(item)
115 return
116 else:
117 if vfs.is_virtual(path):
118 # this is a zipfile, but not added to the tree yet:
119 # add it now...
120 wx.GenericDirCtrl.SetPath(self, path)
121 self.tree.Expand(self.tree.GetSelection())
122 wx.CallAfter(self.SetPath, path + '#zip:')
123 return
124 wx.GenericDirCtrl.SetPath(self, path)
126 def up_dir(self):
127 sel = self.tree.GetSelection()
128 if not sel.Ok(): return
129 sel = self.tree.GetItemParent(sel)
130 if not sel.Ok() or sel == self.tree.GetRootItem(): return
131 self.tree.SelectItem(sel)
133 # end of class DirCtrl