source: trunk/tools/editor/scripts/gui/filemanager.py @ 3513

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