Package fife :: Package extensions :: Package pychan :: Module dialogs
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pychan.dialogs

  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  from fife.extensions import pychan 
 25  from fife.extensions.pychan import loadXML 
 26  import fife.extensions.pychan.tools as tools 
 27  import widgets 
 28  from internal import get_manager, screen_width, screen_height 
 29  from StringIO import StringIO 
 30   
 31  OK,YES,NO,CANCEL = True,True,False,None 
 32   
 35   
36 -class XMLDialog(object):
37 - def __init__(self, xml, ok_field = None, cancel_field = None,initial_data={},data={}):
38 self.gui = loadXML(xml) 39 self.ok_field = ok_field 40 self.cancel_field = cancel_field 41 self.initial_data= initial_data 42 self.data= data 43 self.max_size=None 44 self.min_size=None
45 # self.gui.capture(print_event,"mouseEntered") 46
47 - def execute(self):
48 self.gui.distributeInitialData(self.initial_data) 49 self.gui.distributeData(self.data) 50 51 screen_w, screen_h = screen_width(), screen_height() 52 if self.max_size is None: 53 self.max_size = screen_w/2, screen_h/3 54 if self.min_size is None: 55 self.min_size = screen_w/2, screen_h/4 56 self.gui.max_size = self.max_size 57 self.gui.min_size = self.min_size 58 59 resultMap = {} 60 if self.gui.findChild(name="okButton"): 61 resultMap["okButton"] = OK 62 63 if self.gui.findChild(name="cancelButton"): 64 resultMap["cancelButton"] = CANCEL 65 66 if self.gui.findChild(name="yesButton"): 67 resultMap["noButton"] = NO 68 69 if self.gui.findChild(name="yesButton"): 70 resultMap["yesButton"] = YES 71 72 ok = self.gui.execute(resultMap) 73 if ok: 74 return self.getOkResult() 75 return self.getCancelResult()
76
77 - def getOkResult(self):
78 if self.ok_field: 79 return self.gui.collectData(self.ok_field) 80 return True
81
82 - def getCancelResult(self):
83 if self.cancel_field: 84 return self.gui.collectData(self.cancel_field) 85 return False
86 87 MESSAGE_BOX_XML = """\ 88 <Window name="window" title="Message"> 89 <ScrollArea> 90 <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> 91 </ScrollArea> 92 <HBox> 93 <Spacer/><Button min_size="50,0" name="okButton" text="OK"/> 94 </HBox> 95 </Window> 96 """ 97 98 YESNO_BOX_XML = """\ 99 <Window name="window" title="Question"> 100 <ScrollArea> 101 <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> 102 </ScrollArea> 103 <HBox> 104 <Spacer/> 105 <Button min_size="50,0" name="yesButton" text="Yes"/> 106 <Button min_size="50,0" name="noButton" text="No"/> 107 </HBox> 108 </Window> 109 """ 110 111 YESNOCANCEL_BOX_XML = """\ 112 <Window name="window" title="Question"> 113 <ScrollArea> 114 <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> 115 </ScrollArea> 116 <HBox> 117 <Spacer/> 118 <Button min_width="50" name="yesButton" text="Yes"/> 119 <Button min_width="50" name="noButton" text="No"/> 120 <Button min_width="50" name="cancelButton" text="Cancel"/> 121 </HBox> 122 </Window> 123 """ 124 125 SELECT_BOX_XML = """\ 126 <Window name="window" title="Select"> 127 <Label wrap_text="1" text="$MESSAGE" name="message"/> 128 <ScrollArea> 129 <ListBox name="selection"> 130 </ListBox> 131 </ScrollArea> 132 <HBox> 133 <Spacer/> 134 <Button min_width="50" name="okButton" text="Select"/> 135 <Button min_width="50" name="cancelButton" text="Cancel"/> 136 </HBox> 137 </Window> 138 """ 139 140 EXCEPTION_CATCHER_XML="""\ 141 <Window name="window" title="An exception occurred - what now?"> 142 <VBox hexpand="1"> 143 <Label wrap_text="1" max_size="400,90000" text="$MESSAGE" name="message"/> 144 <ScrollArea> 145 <Label text="$MESSAGE" name="traceback"/> 146 </ScrollArea> 147 </VBox> 148 <HBox> 149 <Spacer/> 150 <Button name="yesButton" text="Retry"/> 151 <Button name="noButton" text="Ignore"/> 152 <Button name="cancelButton" text="Reraise"/> 153 </HBox> 154 </Window> 155 """ 156
157 -def _make_text(message):
158 if callable(message): 159 message = message() 160 if hasattr(message,"read"): 161 message = message.read() 162 return message
163
164 -def message(message="",caption="Message"):
165 text = _make_text(message) 166 dialog = XMLDialog(StringIO(MESSAGE_BOX_XML)) 167 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 168 dialog.gui.findChild(name="message").text = text 169 dialog.gui.findChild(name="window").title = caption 170 171 dialog.execute()
172
173 -def yesNo(message="",caption="Message"):
174 text = _make_text(message) 175 dialog = XMLDialog(StringIO(YESNO_BOX_XML)) 176 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 177 dialog.gui.findChild(name="message").text = text 178 dialog.gui.findChild(name="window").title = caption 179 180 return dialog.execute()
181
182 -def yesNoCancel(message="",caption="Message"):
183 text = _make_text(message) 184 dialog = XMLDialog(StringIO(YESNOCANCEL_BOX_XML)) 185 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 186 dialog.gui.findChild(name="message").text = text 187 dialog.gui.findChild(name="window").title = caption 188 189 return dialog.execute()
190
191 -def select(message="",options=[],caption="Message"):
192 text = _make_text(message) 193 dialog = XMLDialog(StringIO(SELECT_BOX_XML)) 194 dialog.size = screen_width()/3, 2*screen_height()/3 195 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 196 dialog.gui.findChild(name="message").text = text 197 dialog.gui.findChild(name="window").title = caption 198 199 listbox = dialog.gui.findChild(name="selection") 200 listbox.items = options 201 if dialog.execute(): 202 return listbox.selected_item 203 return None
204
205 -def trace(f):
206 import sys, traceback 207 def new_f(*args,**kwargs): 208 try: 209 return pychan.tools.applyOnlySuitable(f,*args,**kwargs) 210 211 except Exception, e: 212 dialog = XMLDialog(StringIO(EXCEPTION_CATCHER_XML)) 213 214 dialog.gui.findChild(name="message").text = str(e) 215 216 tb = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback) 217 dialog.gui.findChild(name="traceback").text = "".join(tb) 218 dialog.min_size = screen_width()/2,3*screen_height()/4 219 dialog.max_size = screen_width()/2,3*screen_height()/4 220 result = dialog.execute() 221 if result == YES: 222 return new_f(*args,**kwargs) 223 elif result == NO: 224 return 225 raise
226 return new_f 227