Mercurial > fife-parpg
annotate engine/extensions/filebrowser.py @ 264:ea85ddce2b36
* Fixed a bug where resizing a listbox with non-ascii characters would give errors
* New pychan function: gui2str, useful for converting unicode objects to string objects which can be used by swig functions
author | cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 13 Jun 2009 14:54:42 +0000 |
parents | a0068e00f076 |
children | 0adf9423062e |
rev | line source |
---|---|
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
1 # coding: utf-8 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
2 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
3 import pychan |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
4 import pychan.widgets as widgets |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
5 import sys |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
6 |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
7 def u2s(string): |
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
8 return string.encode(sys.getfilesystemencoding()) |
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
9 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
10 class FileBrowser(object): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
11 """ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
12 FileBrowser displays directory and file listings from the vfs. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
13 The fileSelected parameter is a callback invoked when a file selection has been made; its |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
14 signature must be fileSelected(path,filename). If selectdir is set, fileSelected's |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
15 filename parameter should be optional. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
16 The savefile option provides a box for supplying a new filename that doesn't exist yet. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
17 The selectdir option allows directories to be selected as well as files. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
18 """ |
179
28532ae6f9f6
Moved all editor content out of content, as chewie says it's deprecated as hell ;-)
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
170
diff
changeset
|
19 def __init__(self, engine, fileSelected, savefile=False, selectdir=False, extensions=('xml',), guixmlpath="gui/filebrowser.xml"): |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
20 self.engine = engine |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
21 self.fileSelected = fileSelected |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
22 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
23 self._widget = None |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
24 self.savefile = savefile |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
25 self.selectdir = selectdir |
170
f55979111ba1
- minor change to filebrowser - you now can specify the directory and the filename (!) of the xml file for the filebrowser
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
26 |
f55979111ba1
- minor change to filebrowser - you now can specify the directory and the filename (!) of the xml file for the filebrowser
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
27 self.guixmlpath = guixmlpath |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 self.extensions = extensions |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
30 self.path = './..' |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
31 self.dir_list = [] |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
32 self.file_list = [] |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
33 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 def showBrowser(self): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
35 if self._widget: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
36 self._widget.show() |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
37 return |
170
f55979111ba1
- minor change to filebrowser - you now can specify the directory and the filename (!) of the xml file for the filebrowser
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
38 self._widget = pychan.loadXML(self.guixmlpath) |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
39 self._widget.mapEvents({ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
40 'dirList' : self._setDirectory, |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
41 'selectButton' : self._selectFile, |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
42 'closeButton' : self._widget.hide |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
43 }) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
44 self._setDirectory() |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
45 if self.savefile: |
255
51cc05d862f2
Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
179
diff
changeset
|
46 self._file_entry = widgets.TextField(name='saveField', text=u'') |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
47 self._widget.findChild(name="fileColumn").addChild(self._file_entry) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
48 self._widget.show() |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
49 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
50 def _setDirectory(self): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
51 selection = self._widget.collectData('dirList') |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 if not (selection < 0): |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
53 new_dir = u2s(self.dir_list[selection]) |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
54 lst = self.path.split('/') |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
55 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
56 lst.pop() |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
57 else: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
58 lst.append(new_dir) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
59 self.path = '/'.join(lst) |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
60 |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
61 def decodeList(list): |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
62 fs_encoding = sys.getfilesystemencoding() |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
63 if fs_encoding is None: fs_encoding = "ascii" |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
64 |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
65 newList = [] |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
66 for i in list: |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
67 try: newList.append(unicode(i, fs_encoding)) |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
68 except: |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
69 newList.append(unicode(i, fs_encoding, 'replace')) |
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
70 print "WARNING: Could not decode item:", i |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
71 return newList |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
72 |
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
73 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
74 |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
75 self.dir_list = [] |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
76 self.file_list = [] |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
77 |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
78 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path)) |
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
79 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path)) |
258
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
80 |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
81 self.dir_list = decodeList(dir_list) |
0b1992a3dfe7
Proper decoding of file names for filebrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
255
diff
changeset
|
82 self.file_list = decodeList(file_list) |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
83 self._widget.distributeInitialData({ |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
84 'dirList' : self.dir_list, |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
85 'fileList' : self.file_list |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
86 }) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
87 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
88 def _selectFile(self): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
89 self._widget.hide() |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
90 selection = self._widget.collectData('fileList') |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
91 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
92 if self.savefile: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
93 if self._widget.collectData('saveField'): |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
94 self.fileSelected(self.path, u2s(self._widget.collectData('saveField'))) |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
95 return |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
96 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
97 if selection >= 0: |
260
a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
258
diff
changeset
|
98 self.fileSelected(self.path, u2s(self.file_list[selection])) |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
99 return |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
100 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
101 if self.selectdir: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
102 self.fileSelected(self.path) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
103 return |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
104 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
105 print 'FileBrowser: error, no selection.' |