Mercurial > fife-parpg
comparison tools/editor/scripts/gui/filemanager.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 | 60621d858548 |
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 os, math, traceback, sys | |
25 from fife import fife | |
26 from fife.extensions import pychan | |
27 from fife.extensions import filebrowser | |
28 from fife.extensions import loaders, savers | |
29 import action | |
30 import scripts.editor | |
31 import fife.extensions.pychan.widgets as widgets | |
32 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | |
33 from scripts.gui.error import ErrorDialog | |
34 from action import Action, ActionGroup | |
35 from input import InputDialog | |
36 from selection import SelectionDialog, ClickSelectionDialog | |
37 from scripts.events import events | |
38 from scripts.gui.cameradialog import CameraDialog | |
39 from scripts.gui.layerdialog import LayerDialog | |
40 | |
41 class FileManager(object): | |
42 def __init__(self): | |
43 from fife import fife | |
44 self.editor = scripts.editor.getEditor() | |
45 self.engine = self.editor.getEngine() | |
46 self._map = None | |
47 self._layer = None | |
48 self._mapdlg = None | |
49 self._layerdlg = None | |
50 self._cameradlg = None | |
51 | |
52 self._filebrowser = None | |
53 self._importbrowser = None | |
54 self._savebrowser = None | |
55 | |
56 newAction = Action(u"New map", "gui/icons/new_map.png") | |
57 loadAction = Action(u"Open", "gui/icons/load_map.png") | |
58 closeAction = Action(u"Close", "gui/icons/close_map.png") | |
59 saveAction = Action(u"Save", "gui/icons/save_map.png") | |
60 saveAsAction = Action(u"Save as", "gui/icons/save_mapas.png") | |
61 saveAllAction = Action(u"Save all", "gui/icons/save_allmaps.png") | |
62 importFileAction = Action(u"Import file", "gui/icons/import_file.png") | |
63 importDirAction = Action(u"Import directory", "gui/icons/import_dir.png") | |
64 | |
65 newAction.helptext = u"Create new map" | |
66 loadAction.helptext = u"Open existing map" | |
67 closeAction.helptext = u"Close map" | |
68 saveAction.helptext = u"Save map" | |
69 saveAsAction.helptext = u"Save map as" | |
70 saveAllAction.helptext = u"Save all opened maps" | |
71 importFileAction.helptext = u"Imports an object file" | |
72 importDirAction.helptext = u"Recursively imports all objects from a directory" | |
73 | |
74 action.activated.connect(self.showMapWizard, sender=newAction) | |
75 action.activated.connect(self.showLoadDialog, sender=loadAction) | |
76 action.activated.connect(self.closeCurrentMap, sender=closeAction) | |
77 action.activated.connect(self.save, sender=saveAction) | |
78 action.activated.connect(self.saveAs, sender=saveAsAction) | |
79 action.activated.connect(self.editor.saveAll, sender=saveAllAction) | |
80 | |
81 self._importFileCallback = cbwa(self.showImportDialog, self.importFile, False) | |
82 self._importDirCallback = cbwa(self.showImportDialog, self.importDir, True) | |
83 action.activated.connect(self._importFileCallback, sender=importFileAction) | |
84 action.activated.connect(self._importDirCallback, sender=importDirAction) | |
85 | |
86 eventlistener = self.editor.getEventListener() | |
87 | |
88 eventlistener.getKeySequenceSignal(fife.Key.N, ["ctrl"]).connect(self.showMapWizard) | |
89 eventlistener.getKeySequenceSignal(fife.Key.O, ["ctrl"]).connect(self.showLoadDialog) | |
90 eventlistener.getKeySequenceSignal(fife.Key.W, ["ctrl"]).connect(self.closeCurrentMap) | |
91 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl"]).connect(self.save) | |
92 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl", "shift"]).connect(self.editor.saveAll) | |
93 | |
94 fileGroup = ActionGroup() | |
95 fileGroup.addAction(newAction) | |
96 fileGroup.addAction(loadAction) | |
97 fileGroup.addSeparator() | |
98 fileGroup.addAction(closeAction) | |
99 fileGroup.addSeparator() | |
100 fileGroup.addAction(saveAction) | |
101 fileGroup.addAction(saveAsAction) | |
102 fileGroup.addAction(saveAllAction) | |
103 fileGroup.addSeparator() | |
104 fileGroup.addAction(importFileAction) | |
105 fileGroup.addAction(importDirAction) | |
106 | |
107 self.editor.getToolBar().insertAction(fileGroup, 0) | |
108 self.editor.getToolBar().insertSeparator(None, 1) | |
109 self.editor._file_menu.insertAction(fileGroup, 0) | |
110 self.editor._file_menu.insertSeparator(None, 1) | |
111 | |
112 def closeCurrentMap(self): | |
113 mapview = self.editor.getActiveMapView() | |
114 if mapview is None: | |
115 print "No map to close" | |
116 return | |
117 | |
118 mapview.close() | |
119 | |
120 def showLoadDialog(self): | |
121 if self._filebrowser is None: | |
122 self._filebrowser = filebrowser.FileBrowser(self.engine, self.loadFile, extensions = loaders.fileExtensions) | |
123 self._filebrowser.showBrowser() | |
124 | |
125 def showSaveDialog(self): | |
126 if self._savebrowser is None: | |
127 self._savebrowser = filebrowser.FileBrowser(self.engine, self.saveFile, savefile=True, extensions = loaders.fileExtensions) | |
128 self._savebrowser.showBrowser() | |
129 | |
130 def showImportDialog(self, callback, selectdir): | |
131 if self._importbrowser is None: | |
132 self._importbrowser = filebrowser.FileBrowser(self.engine, callback, extensions = loaders.fileExtensions) | |
133 self._importbrowser.fileSelected = callback | |
134 self._importbrowser.selectdir = selectdir | |
135 self._importbrowser.showBrowser() | |
136 | |
137 def saveFile(self, path, filename): | |
138 mapview = self.editor.getActiveMapView() | |
139 if mapview is None: | |
140 print "No map is open" | |
141 return | |
142 | |
143 fname = '/'.join([path, filename]) | |
144 mapview.saveAs(fname) | |
145 | |
146 def saveAs(self): | |
147 mapview = self.editor.getActiveMapView() | |
148 if mapview is None: | |
149 print "No map is open" | |
150 return | |
151 self.showSaveDialog() | |
152 | |
153 def loadFile(self, path, filename): | |
154 self.editor.openFile('/'.join([path, filename])) | |
155 | |
156 def showMapWizard(self): | |
157 if self._cameradlg: | |
158 self._cameradlg._widget.show() | |
159 elif self._layerdlg: | |
160 self._layerdlg._widget.show() | |
161 elif self._mapdlg: | |
162 self._mapdlg._widget.show() | |
163 else: | |
164 self._newMap() | |
165 | |
166 def _newMap(self): | |
167 self._mapdlg = InputDialog(u'Enter a map name:', self._newLayer, self._clean) | |
168 | |
169 def _newLayer(self, mapId): | |
170 if mapId == '': | |
171 print "Please enter a map ID" | |
172 return self._newMap() | |
173 | |
174 self._map = self.engine.getModel().createMap(str(mapId)) | |
175 self._layerdlg = LayerDialog(self.engine, self._map, self._newCamera, self._clean) | |
176 | |
177 def _newCamera(self, layer): | |
178 self._layer = layer | |
179 | |
180 self._cameradlg = CameraDialog(self.engine, self._addMap, self._clean, self._map, self._layer) | |
181 | |
182 def _addMap(self): | |
183 self.editor.newMapView(self._map) | |
184 self._clean() | |
185 | |
186 def _clean(self): | |
187 self._mapdlg = None | |
188 self._layerdlg = None | |
189 self._cameradlg = None | |
190 self._map = None | |
191 self._layer = None | |
192 | |
193 def save(self): | |
194 curname = None | |
195 mapview = self.editor.getActiveMapView() | |
196 if mapview is None: | |
197 print "No map is open" | |
198 return | |
199 | |
200 try: | |
201 curname = mapview.getMap().getResourceLocation().getFilename() | |
202 except RuntimeError: | |
203 self.showSaveDialog() | |
204 return | |
205 | |
206 mapview.save() | |
207 | |
208 def importFile(self, path, filename): | |
209 file = os.path.normpath(os.path.join(path, filename)) | |
210 # FIXME: This is necassary for the files to be loaded properly. | |
211 # The loader should be fixed to support native (windows) | |
212 # path separators. | |
213 file = file.replace('\\', '/') | |
214 | |
215 try: | |
216 if os.path.isfile(file): | |
217 loaders.loadImportFile(file, self.engine) | |
218 else: | |
219 raise file+ " is not a file!" | |
220 except: | |
221 traceback.print_exc(sys.exc_info()[1]) | |
222 errormsg = u"Importing file failed:\n" | |
223 errormsg += u"File: "+unicode(file)+u"\n" | |
224 errormsg += u"Error: "+unicode(sys.exc_info()[1]) | |
225 ErrorDialog(errormsg) | |
226 return None | |
227 | |
228 events.onObjectsImported.send(sender=self) | |
229 | |
230 def importDir(self, path, filename=""): | |
231 if os.path.isdir(os.path.join(path, filename)): | |
232 path = os.path.join(path, filename) | |
233 path = os.path.normpath(path) | |
234 | |
235 # FIXME: This is necassary for the files to be loaded properly. | |
236 # The loader should be fixed to support native (windows) | |
237 # path separators. | |
238 path = path.replace('\\', '/') | |
239 | |
240 try: | |
241 if os.path.isdir(path): | |
242 loaders.loadImportDirRec(path, self.engine) | |
243 else: | |
244 raise file+ " is not a directory!" | |
245 except: | |
246 traceback.print_exc(sys.exc_info()[1]) | |
247 errormsg = u"Importing directory failed:\n" | |
248 errormsg += u"File: "+unicode(file)+u"\n" | |
249 errormsg += u"Error: "+unicode(sys.exc_info()[1]) | |
250 ErrorDialog(errormsg) | |
251 return None | |
252 | |
253 events.onObjectsImported.send(sender=self) | |
254 |