Mercurial > fife-parpg
comparison engine/python/fife/extensions/filebrowser.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children | 293e812316c0 |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 # #################################################################### | |
4 # Copyright (C) 2005-2009 by the FIFE team | |
5 # http://www.fifengine.de | |
6 # This file is part of FIFE. | |
7 # | |
8 # FIFE is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Lesser General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2.1 of the License, or (at your option) any later version. | |
12 # | |
13 # This library is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # Lesser General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Lesser General Public | |
19 # License along with this library; if not, write to the | |
20 # Free Software Foundation, Inc., | |
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 # #################################################################### | |
23 | |
24 import pychan | |
25 import pychan.widgets as widgets | |
26 import sys | |
27 | |
28 def u2s(string): | |
29 return string.encode(sys.getfilesystemencoding()) | |
30 | |
31 class FileBrowser(object): | |
32 """ | |
33 FileBrowser displays directory and file listings from the vfs. | |
34 The fileSelected parameter is a callback invoked when a file selection has been made; its | |
35 signature must be fileSelected(path,filename). If selectdir is set, fileSelected's | |
36 filename parameter should be optional. | |
37 The savefile option provides a box for supplying a new filename that doesn't exist yet. | |
38 The selectdir option allows directories to be selected as well as files. | |
39 """ | |
40 def __init__(self, engine, fileSelected, savefile=False, selectdir=False, extensions=('xml',), guixmlpath="gui/filebrowser.xml"): | |
41 self.engine = engine | |
42 self.fileSelected = fileSelected | |
43 | |
44 self._widget = None | |
45 self.savefile = savefile | |
46 self.selectdir = selectdir | |
47 | |
48 self.guixmlpath = guixmlpath | |
49 | |
50 self.extensions = extensions | |
51 self.path = './..' | |
52 self.dir_list = [] | |
53 self.file_list = [] | |
54 | |
55 def showBrowser(self): | |
56 if self._widget: | |
57 self.setDirectory(self.path) | |
58 self._widget.show() | |
59 return | |
60 | |
61 self._widget = pychan.loadXML(self.guixmlpath) | |
62 self._widget.mapEvents({ | |
63 'dirList' : self._selectDir, | |
64 'selectButton' : self._selectFile, | |
65 'closeButton' : self._widget.hide | |
66 }) | |
67 if self.savefile: | |
68 self._file_entry = widgets.TextField(name='saveField', text=u'') | |
69 self._widget.findChild(name="fileColumn").addChild(self._file_entry) | |
70 | |
71 self.setDirectory(self.path) | |
72 self._widget.show() | |
73 | |
74 def setDirectory(self, path): | |
75 self.path = path | |
76 if not self._widget: return | |
77 | |
78 def decodeList(list): | |
79 fs_encoding = sys.getfilesystemencoding() | |
80 if fs_encoding is None: fs_encoding = "ascii" | |
81 | |
82 newList = [] | |
83 for i in list: | |
84 try: newList.append(unicode(i, fs_encoding)) | |
85 except: | |
86 newList.append(unicode(i, fs_encoding, 'replace')) | |
87 print "WARNING: Could not decode item:", i | |
88 return newList | |
89 | |
90 self.dir_list = [] | |
91 self.file_list = [] | |
92 | |
93 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path)) | |
94 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path)) | |
95 | |
96 self.dir_list = decodeList(dir_list) | |
97 self.file_list = decodeList(file_list) | |
98 self._widget.distributeInitialData({ | |
99 'dirList' : self.dir_list, | |
100 'fileList' : self.file_list | |
101 }) | |
102 | |
103 self._widget.adaptLayout() | |
104 | |
105 def _selectDir(self): | |
106 selection = self._widget.collectData('dirList') | |
107 if selection >= 0 and selection < len(self.dir_list): | |
108 new_dir = u2s(self.dir_list[selection]) | |
109 lst = self.path.split('/') | |
110 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': | |
111 lst.pop() | |
112 else: | |
113 lst.append(new_dir) | |
114 | |
115 path = '/'.join(lst) | |
116 self.setDirectory(path) | |
117 | |
118 def _selectFile(self): | |
119 self._widget.hide() | |
120 selection = self._widget.collectData('fileList') | |
121 | |
122 if self.savefile: | |
123 if self._widget.collectData('saveField'): | |
124 self.fileSelected(self.path, u2s(self._widget.collectData('saveField'))) | |
125 return | |
126 | |
127 if selection >= 0 and selection < len(self.file_list): | |
128 self.fileSelected(self.path, u2s(self.file_list[selection])) | |
129 return | |
130 | |
131 if self.selectdir: | |
132 self.fileSelected(self.path) | |
133 return | |
134 | |
135 print 'FileBrowser: error, no selection.' |