comparison clients/editor/scripts/gui/filemanager.py @ 282:0adf9423062e

* Buttons and functions for importing objects in the editor * Fixed an index overflow bug in FileBrowser which could occur when selectDirs was enabled * Removed unused import functions from MapView
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 17 Jun 2009 01:34:59 +0000
parents 51cc05d862f2
children 54de5aebf732
comparison
equal deleted inserted replaced
281:0b9b493138a8 282:0adf9423062e
1 import math, fife, pychan, filebrowser 1 import os, math, fife, pychan, filebrowser, traceback, sys
2 import loaders, savers 2 import loaders, savers
3 import action 3 import action
4 import scripts.editor 4 import scripts.editor
5 import pychan.widgets as widgets 5 import pychan.widgets as widgets
6 6 from pychan.tools import callbackWithArguments as cbwa
7 from scripts.gui.error import ErrorDialog
7 from action import Action, ActionGroup 8 from action import Action, ActionGroup
8 from input import InputDialog 9 from input import InputDialog
9 from selection import SelectionDialog, ClickSelectionDialog 10 from selection import SelectionDialog, ClickSelectionDialog
10 11
11 class FileManager(object): 12 class FileManager(object):
17 self._mapdlg = None 18 self._mapdlg = None
18 self._layerdlg = None 19 self._layerdlg = None
19 self._cameradlg = None 20 self._cameradlg = None
20 21
21 self._filebrowser = None 22 self._filebrowser = None
23 self._importbrowser = None
22 self._savebrowser = None 24 self._savebrowser = None
23 25
24 newAction = Action(u"New map", "gui/icons/new_map.png") 26 newAction = Action(u"New map", "gui/icons/new_map.png")
25 loadAction = Action(u"Open", "gui/icons/load_map.png") 27 loadAction = Action(u"Open", "gui/icons/load_map.png")
26 saveAction = Action(u"Save", "gui/icons/save_map.png") 28 saveAction = Action(u"Save", "gui/icons/save_map.png")
27 saveAsAction = Action(u"Save as", "gui/icons/save_mapas.png") 29 saveAsAction = Action(u"Save as", "gui/icons/save_mapas.png")
28 saveAllAction = Action(u"Save all", "gui/icons/save_allmaps.png") 30 saveAllAction = Action(u"Save all", "gui/icons/save_allmaps.png")
31 importFileAction = Action(u"Import file", "gui/icons/import_file.png")
32 importDirAction = Action(u"Import directory", "gui/icons/import_dir.png")
29 33
30 newAction.helptext = u"Create new map" 34 newAction.helptext = u"Create new map"
31 loadAction.helptext = u"Open existing map" 35 loadAction.helptext = u"Open existing map"
32 saveAction.helptext = u"Save map" 36 saveAction.helptext = u"Save map"
33 saveAsAction.helptext = u"Save map as" 37 saveAsAction.helptext = u"Save map as"
34 saveAllAction.helptext = u"Save all opened maps" 38 saveAllAction.helptext = u"Save all opened maps"
39 importFileAction.helptext = u"Imports an object file"
40 importDirAction.helptext = u"Recursively imports all objects from a directory"
35 41
36 action.activated.connect(self.showMapWizard, sender=newAction) 42 action.activated.connect(self.showMapWizard, sender=newAction)
37 action.activated.connect(self.showLoadDialog, sender=loadAction) 43 action.activated.connect(self.showLoadDialog, sender=loadAction)
38 action.activated.connect(self.save, sender=saveAction) 44 action.activated.connect(self.save, sender=saveAction)
39 action.activated.connect(self.saveAs, sender=saveAsAction) 45 action.activated.connect(self.saveAs, sender=saveAsAction)
40 action.activated.connect(self.editor.saveAll, sender=saveAllAction) 46 action.activated.connect(self.editor.saveAll, sender=saveAllAction)
47
48 self._importFileCallback = cbwa(self.showImportDialog, self.importFile, False)
49 self._importDirCallback = cbwa(self.showImportDialog, self.importDir, True)
50 action.activated.connect(self._importFileCallback, sender=importFileAction)
51 action.activated.connect(self._importDirCallback, sender=importDirAction)
41 52
42 eventlistener = self.editor.getEventListener() 53 eventlistener = self.editor.getEventListener()
43 eventlistener.getKeySequenceSignal(fife.Key.N, ["ctrl"]).connect(self.showMapWizard) 54 eventlistener.getKeySequenceSignal(fife.Key.N, ["ctrl"]).connect(self.showMapWizard)
44 eventlistener.getKeySequenceSignal(fife.Key.O, ["ctrl"]).connect(self.showLoadDialog) 55 eventlistener.getKeySequenceSignal(fife.Key.O, ["ctrl"]).connect(self.showLoadDialog)
45 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl"]).connect(self.save) 56 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl"]).connect(self.save)
49 fileGroup.addAction(newAction) 60 fileGroup.addAction(newAction)
50 fileGroup.addAction(loadAction) 61 fileGroup.addAction(loadAction)
51 fileGroup.addAction(saveAction) 62 fileGroup.addAction(saveAction)
52 fileGroup.addAction(saveAsAction) 63 fileGroup.addAction(saveAsAction)
53 fileGroup.addAction(saveAllAction) 64 fileGroup.addAction(saveAllAction)
65 fileGroup.addSeparator()
66 fileGroup.addAction(importFileAction)
67 fileGroup.addAction(importDirAction)
54 68
55 self.editor.getToolBar().insertAction(fileGroup, 0) 69 self.editor.getToolBar().insertAction(fileGroup, 0)
56 self.editor.getToolBar().insertSeparator(None, 1) 70 self.editor.getToolBar().insertSeparator(None, 1)
57 self.editor._fileMenu.insertAction(fileGroup, 0) 71 self.editor._fileMenu.insertAction(fileGroup, 0)
58 self.editor._fileMenu.insertSeparator(None, 1) 72 self.editor._fileMenu.insertSeparator(None, 1)
64 78
65 def showSaveDialog(self): 79 def showSaveDialog(self):
66 if self._savebrowser is None: 80 if self._savebrowser is None:
67 self._savebrowser = filebrowser.FileBrowser(self.engine, self.saveFile, savefile=True, extensions = loaders.fileExtensions) 81 self._savebrowser = filebrowser.FileBrowser(self.engine, self.saveFile, savefile=True, extensions = loaders.fileExtensions)
68 self._savebrowser.showBrowser() 82 self._savebrowser.showBrowser()
83
84 def showImportDialog(self, callback, selectdir):
85 if self._importbrowser is None:
86 self._importbrowser = filebrowser.FileBrowser(self.engine, callback, extensions = loaders.fileExtensions)
87 self._importbrowser.fileSelected = callback
88 self._importbrowser.selectdir = selectdir
89 self._importbrowser.showBrowser()
69 90
70 def saveFile(self, path, filename): 91 def saveFile(self, path, filename):
71 mapview = self.editor.getActiveMapView() 92 mapview = self.editor.getActiveMapView()
72 if mapview is None: 93 if mapview is None:
73 print "No map is open" 94 print "No map is open"
103 self._map = self.engine.getModel().createMap(str(mapId)) 124 self._map = self.engine.getModel().createMap(str(mapId))
104 self._layerdlg = InputDialog(u'Enter a layer identifier for a default layer:', self._newCamera, self._clean) 125 self._layerdlg = InputDialog(u'Enter a layer identifier for a default layer:', self._newCamera, self._clean)
105 126
106 def _newCamera(self, layerId): 127 def _newCamera(self, layerId):
107 grid = fife.SquareGrid() 128 grid = fife.SquareGrid()
108 layer = self._map.createLayer(str(layerId), grid) 129 self._layer = self._map.createLayer(str(layerId), grid)
109 grid.thisown = 0 130 grid.thisown = 0
110 131
111 self._cameradlg = CameraEditor(self.engine, self._addMap, self._clean, self._map, self._layer) 132 self._cameradlg = CameraEditor(self.engine, self._addMap, self._clean, self._map, self._layer)
112 133
113 def _addMap(self): 134 def _addMap(self):
114 self.editor.newMapView(self._map) 135 self.editor.newMapView(self._map)
115 self._clean() 136 self._clean()
133 except RuntimeError: 154 except RuntimeError:
134 self.showSaveDialog() 155 self.showSaveDialog()
135 return 156 return
136 157
137 mapview.save() 158 mapview.save()
159
160 def importFile(self, path, filename):
161 file = os.path.normpath(os.path.join(path, filename))
162 # FIXME: This is necassary for the files to be loaded properly.
163 # The loader should be fixed to support native (windows)
164 # path separators.
165 file = file.replace('\\', '/')
166
167 try:
168 if os.path.isfile(file):
169 loaders.loadImportFile(file, self.engine)
170 else:
171 raise file+ " is not a file!"
172 except:
173 traceback.print_exc(sys.exc_info()[1])
174 errormsg = u"Importing file failed:\n"
175 errormsg += u"File: "+unicode(file)+u"\n"
176 errormsg += u"Error: "+unicode(sys.exc_info()[1])
177 ErrorDialog(errormsg)
178 return None
179
180 def importDir(self, path, filename=""):
181 if os.path.isdir(os.path.join(path, filename)):
182 path = os.path.join(path, filename)
183 path = os.path.normpath(path)
184
185 # FIXME: This is necassary for the files to be loaded properly.
186 # The loader should be fixed to support native (windows)
187 # path separators.
188 path = path.replace('\\', '/')
189
190 try:
191 if os.path.isdir(path):
192 loaders.loadImportDirRec(path, self.engine)
193 else:
194 raise file+ " is not a directory!"
195 except:
196 traceback.print_exc(sys.exc_info()[1])
197 errormsg = u"Importing directory failed:\n"
198 errormsg += u"File: "+unicode(file)+u"\n"
199 errormsg += u"Error: "+unicode(sys.exc_info()[1])
200 ErrorDialog(errormsg)
201 return None
138 202
139 class CameraEditor(object): 203 class CameraEditor(object):
140 """ 204 """
141 CameraEditor provides a gui dialog for camera creation. The callback is called when camera creation is complete. A 205 CameraEditor provides a gui dialog for camera creation. The callback is called when camera creation is complete. A
142 partial specification of the camera parameters may optionally be given. 206 partial specification of the camera parameters may optionally be given.