Mercurial > fife-parpg
comparison engine/extensions/filebrowser.py @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | f55979111ba1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 # coding: utf-8 | |
2 | |
3 import pychan | |
4 import pychan.widgets as widgets | |
5 | |
6 class FileBrowser(object): | |
7 """ | |
8 FileBrowser displays directory and file listings from the vfs. | |
9 The fileSelected parameter is a callback invoked when a file selection has been made; its | |
10 signature must be fileSelected(path,filename). If selectdir is set, fileSelected's | |
11 filename parameter should be optional. | |
12 The savefile option provides a box for supplying a new filename that doesn't exist yet. | |
13 The selectdir option allows directories to be selected as well as files. | |
14 """ | |
15 def __init__(self, engine, fileSelected, savefile=False, selectdir=False, extensions=('xml',)): | |
16 self.engine = engine | |
17 self.fileSelected = fileSelected | |
18 | |
19 self._widget = None | |
20 self.savefile = savefile | |
21 self.selectdir = selectdir | |
22 | |
23 self.extensions = extensions | |
24 self.path = './..' | |
25 self.dir_list = ('..',) + self.engine.getVFS().listDirectories(self.path) | |
26 self.file_list = self.engine.getVFS().listFiles(self.path) | |
27 | |
28 def showBrowser(self): | |
29 if self._widget: | |
30 self._widget.show() | |
31 return | |
32 self._widget = pychan.loadXML('content/gui/filebrowser.xml') | |
33 self._widget.mapEvents({ | |
34 'dirList' : self._setDirectory, | |
35 'selectButton' : self._selectFile, | |
36 'closeButton' : self._widget.hide | |
37 }) | |
38 self._setDirectory() | |
39 if self.savefile: | |
40 self._file_entry = widgets.TextField(name='saveField', text='') | |
41 self._widget.findChild(name="fileColumn").addChild(self._file_entry) | |
42 self._widget.show() | |
43 | |
44 def _setDirectory(self): | |
45 selection = self._widget.collectData('dirList') | |
46 if not (selection < 0): | |
47 new_dir = self.dir_list[selection] | |
48 lst = self.path.split('/') | |
49 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': | |
50 lst.pop() | |
51 else: | |
52 lst.append(new_dir) | |
53 self.path = '/'.join(lst) | |
54 | |
55 self.dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path)) | |
56 self.file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path)) | |
57 self._widget.distributeInitialData({ | |
58 'dirList' : self.dir_list, | |
59 'fileList' : self.file_list | |
60 }) | |
61 | |
62 def _selectFile(self): | |
63 self._widget.hide() | |
64 selection = self._widget.collectData('fileList') | |
65 | |
66 if self.savefile: | |
67 if self._widget.collectData('saveField'): | |
68 self.fileSelected(self.path,self._widget.collectData('saveField')) | |
69 return | |
70 | |
71 if selection >= 0: | |
72 self.fileSelected(self.path,self.file_list[selection]) | |
73 return | |
74 | |
75 if self.selectdir: | |
76 self.fileSelected(self.path) | |
77 return | |
78 | |
79 print 'FileBrowser: error, no selection.' |