changeset 373:20fa29f376c7

Editor: * If a map is already open, the user will be presented with a dialog asking if he wants to reload the map * CancelCallback argument in InputDialog is no longer mandatory * New dialog: YesNoDialog
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 20 Nov 2009 02:04:39 +0000
parents e94cd7d1dab6
children 1115f7cae9a3
files clients/editor/gui/yesnodialog.xml clients/editor/scripts/editor.py clients/editor/scripts/gui/input.py clients/editor/scripts/gui/yesnodialog.py
diffstat 4 files changed, 127 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clients/editor/gui/yesnodialog.xml	Fri Nov 20 02:04:39 2009 +0000
@@ -0,0 +1,9 @@
+<Window name="Input" title="Input box:">
+	<VBox>
+		<Label name="prompt" text="Enter your data:"/>
+		<HBox>
+			<Button name="okButton" text="OK"/>
+			<Button name="cancelButton" text="Cancel"/>
+		</HBox>
+	</VBox>
+</Window>
--- a/clients/editor/scripts/editor.py	Fri Nov 20 00:41:56 2009 +0000
+++ b/clients/editor/scripts/editor.py	Fri Nov 20 02:04:39 2009 +0000
@@ -49,6 +49,7 @@
 from gui.mapeditor import MapEditor
 from gui.menubar import Menu, MenuBar
 from gui.error import ErrorDialog
+from gui.yesnodialog import YesNoDialog
 from mapview import MapView
 from settings import Settings
 
@@ -66,6 +67,7 @@
 		Editor.editor = self
 	
 		self._filemanager = None
+		self._open_files = []
 	
 		self._params = params
 		self._eventlistener = None
@@ -344,35 +346,71 @@
 		
 		self.showMapView(mapview)
 		
-		events.preMapClosed.connect(self._mapRemoved)
+		events.preMapClosed.connect(self._preMapRemoved)
+		events.postMapClosed.connect(self._postMapRemoved)
 		events.mapAdded.send(sender=self, map=map)
 		
 		return mapview
 		
-	def _mapRemoved(self, mapview):
-		events.preMapClosed.disconnect(self._mapRemoved)
-		
-		index = self._mapviewlist.index(mapview)-1
-		self._mapviewlist.remove(mapview)
+	def _preMapRemoved(self, sender, mapview):
+		events.preMapClosed.disconnect(self._preMapRemoved)
+	
+		# Remove from open files list
+		try:
+			path = mapview.getMap().getResourceLocation().getFilename()
+			try:
+				self._open_files.remove(path)
+			except ValueError:
+				pass
+		except RuntimeError:
+			# Mapview is not saved
+			return
 		
 		# Remove tab
 		for map_action in self._mapgroup.getActions():
 			if map_action.text == unicode(mapview.getMap().getId()):
 				self._mapgroup.removeAction(map_action)
 				break
-				
+			
+	def _postMapRemoved(self, mapview):
+		events.postMapClosed.disconnect(self._postMapRemoved)
+		
+		# Remove from mapviewlist
+		index = self._mapviewlist.index(mapview)-1
+		self._mapviewlist.remove(mapview)
+		
 		# Change mapview
 		if index >= 0:
 			self.showMapView(self._mapviewlist[index])
 		else:
 			self._mapview = None
 			self.getEngine().getView().clearCameras()
+			
+	def getMapviewByPath(self, path):
+		mappath = ""
+		for mv in self._mapviewlist:
+			try:
+				mappath = mv.getMap().getResourceLocation().getFilename()
+			except RuntimeError:
+				# Mapview is not saved yet
+				continue
+				
+			if mappath == path:
+				return mv
 
 	def openFile(self, path):
+		if path in self._open_files:
+			# Map is already open, ask user if he wants to reload the map
+			mapview = self.getMapviewByPath(path)
+			YesNoDialog("Map is already open. Do you want to reload it?", cbwa(self.reloadMapview, mapview=mapview))
+			return
+	
 		""" Opens a file """
 		try:
 			map = loaders.loadMapFile(path, self.engine)
-			return self.newMapView(map)
+			mapview = self.newMapView(map)
+			self._open_files.append(path)
+			return mapview
 		except:
 			traceback.print_exc(sys.exc_info()[1])
 			errormsg = u"Opening map failed:\n"
@@ -380,6 +418,19 @@
 			errormsg += u"Error: "+unicode(sys.exc_info()[1])
 			ErrorDialog(errormsg)
 			return None
+			
+	def reloadMapview(self, mapview=None):
+		if mapview is None:
+			mapview = self._mapview
+			
+		if mapview is None:
+			print "Can't reload map: No maps are open."
+			return
+			
+		path = mapview.getMap().getResourceLocation().getFilename()
+		mapview.close()
+		self.openFile(path)
+			
 	
 	def saveAll(self):
 		""" Saves all open maps """
--- a/clients/editor/scripts/gui/input.py	Fri Nov 20 00:41:56 2009 +0000
+++ b/clients/editor/scripts/gui/input.py	Fri Nov 20 02:04:39 2009 +0000
@@ -29,7 +29,7 @@
 	Input supplies a text box for entering data. The result is passed to onEntry.
 	onEntry - the function to call when a input is complete. Accepts one argument: a string of text.
 	"""
-	def __init__(self, prompt, onEntry, onCancel):
+	def __init__(self, prompt, onEntry, onCancel=None):
 		self._callback = onEntry
 		self._cancelCallback = onCancel
 
@@ -50,6 +50,7 @@
 		self._widget.hide()
 		
 	def _cancel(self):
-		self._cancelCallback()
+		if self._cancelCallback:
+			self._cancelCallback()
 		self._widget.hide()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clients/editor/scripts/gui/yesnodialog.py	Fri Nov 20 02:04:39 2009 +0000
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*
+
+# ####################################################################
+#  Copyright (C) 2005-2009 by the FIFE team
+#  http://www.fifengine.de
+#  This file is part of FIFE.
+#
+#  FIFE is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License, or (at your option) any later version.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, write to the
+#  Free Software Foundation, Inc.,
+#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+# ####################################################################
+
+import pychan
+import pychan.widgets as widgets
+
+class YesNoDialog:
+	"""
+	Presents a dialog with some text and OK and cancel buttons
+	"""
+	def __init__(self, prompt, onEntry, onCancel=None):
+		self._callback = onEntry
+		self._cancelCallback = onCancel
+
+		self._widget = pychan.loadXML('gui/yesnodialog.xml')
+
+		self._widget.mapEvents({
+			'okButton'     : self._complete,
+			'cancelButton' : self._cancel
+		})
+
+		self._widget.distributeInitialData({
+			'prompt' : prompt
+		})
+		self._widget.show()
+
+	def _complete(self):
+		self._callback()
+		self._widget.hide()
+		
+	def _cancel(self):
+		if self._cancelCallback:
+			self._cancelCallback()
+		self._widget.hide()
+
+