Package gui :: Module message
[hide private]
[frames] | no frames]

Source Code for Module gui.message

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Michael Bieri                                            # 
  4  # Copyright (C) 2010-2011 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax 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               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports 
 25  from os import sep 
 26  import sys 
 27  import wx 
 28  import wx.lib.buttons 
 29  import wx.lib.scrolledpanel 
 30   
 31  # relax module imports. 
 32  from status import Status; status = Status() 
 33   
 34  # relax GUI module imports. 
 35  from gui.fonts import font 
 36  from gui.icons import relax_icons 
 37  from gui.paths import IMAGE_PATH, icon_22x22, icon_48x48 
 38  import gui 
 39   
 40   
41 -def error_message(msg, caption=''):
42 """Message box for general errors. 43 44 @param msg: The message to display. 45 @type msg: str 46 """ 47 48 # Show the message box. 49 if status.show_gui: 50 wx.MessageBox(msg, caption=caption, style=wx.OK|wx.ICON_ERROR) 51 52 # Otherwise throw the error out to stderr. 53 else: 54 # Combine the caption and message. 55 if caption: 56 msg = "%s: %s" % (caption, msg) 57 58 # Write out. 59 sys.stderr.write(msg + "\n")
60 61 62
63 -class Missing_data(wx.Dialog):
64 """Message box GUI element for when a setup is incomplete or there is missing data.""" 65
66 - def __init__(self, missing=[]):
67 """Set up the dialog. 68 69 @keyword missing: The list of missing data types. 70 @type missing: list of str 71 """ 72 73 # Initialise the base class. 74 wx.Dialog.__init__(self, None, title='Missing data', style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.STAY_ON_TOP) 75 76 # Set up the window icon. 77 self.SetIcons(relax_icons) 78 79 # Set the initial size. 80 self.SetSize((600, 400)) 81 82 # Centre the window. 83 self.Centre() 84 85 # A sizer for the dialog. 86 main_sizer = wx.BoxSizer(wx.HORIZONTAL) 87 self.SetSizer(main_sizer) 88 89 # Build the central sizer, with borders. 90 sizer = gui.misc.add_border(main_sizer, border=10, packing=wx.HORIZONTAL) 91 92 # Add the graphic. 93 bitmap = wx.StaticBitmap(self, -1, wx.Bitmap(icon_48x48.user_busy, wx.BITMAP_TYPE_ANY)) 94 sizer.Add(bitmap) 95 96 # Spacing. 97 sizer.AddSpacer(20) 98 99 # A scrolled panel for the text. 100 panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1) 101 panel.SetAutoLayout(1) 102 panel.SetupScrolling() 103 sizer.Add(panel, 1, wx.ALL|wx.EXPAND, 0) 104 105 # A sizer for the panel. 106 panel_sizer = wx.BoxSizer(wx.HORIZONTAL) 107 panel.SetSizer(panel_sizer) 108 109 # The message. 110 msg = "The set up is incomplete.\n\n" 111 if not len(missing): 112 msg = msg + "Please check for missing data.\n" 113 else: 114 msg = msg + "Please check for the following missing information:\n" 115 for data in missing: 116 msg = msg + " %s\n" % data 117 118 # Convert to a text element. 119 text = wx.StaticText(panel, -1, msg, style=wx.TE_MULTILINE) 120 panel_sizer.Add(text) 121 122 # Show the GUI element. 123 if status.show_gui: 124 self.ShowModal() 125 126 # Otherwise throw the error out to stderr. 127 else: 128 sys.stderr.write("Missing data: %s\n" % msg)
129 130 131
132 -class Question(wx.Dialog):
133 """Question box GUI element for obtaining a yes/no response from the user.""" 134 135 # Some class variables. 136 border = 10 137 spacer_button = 10 138 spacer_main = 20 139 height_button = 30 140 width_button = 100 141
142 - def __init__(self, msg, parent=None, title='', size=(350, 125), default=False):
143 """A generic question box. 144 145 @param msg: The text message to display. 146 @type msg: str 147 @keyword parent: The parent wx object. 148 @type parent: wx.object instance 149 @keyword title: The window title. 150 @type title: str 151 @keyword default: If True, the default button will be 'yes', otherwise it will be 'no'. 152 @type default: bool 153 @return: The answer. 154 @rtype: bool 155 """ 156 157 # Initialise the base class. 158 wx.Dialog.__init__(self, parent, title=title, size=size, style=wx.DEFAULT_DIALOG_STYLE|wx.STAY_ON_TOP) 159 160 # Flag to indicate that a button was pressed. 161 self.pressed = False 162 163 # The default. 164 if default: 165 self.answer = wx.ID_YES 166 else: 167 self.answer = wx.ID_NO 168 169 # Set up the window icon. 170 self.SetIcons(relax_icons) 171 172 # Centre the window. 173 self.Centre() 174 175 # A sizer for the dialog. 176 main_sizer = wx.BoxSizer(wx.HORIZONTAL) 177 self.SetSizer(main_sizer) 178 179 # Build the central sizer, with borders. 180 sizer = gui.misc.add_border(main_sizer, border=self.border, packing=wx.HORIZONTAL) 181 182 # Add the graphic. 183 bitmap = wx.StaticBitmap(self, -1, wx.Bitmap(icon_48x48.dialog_warning_relax, wx.BITMAP_TYPE_ANY)) 184 sizer.Add(bitmap) 185 186 # Spacing. 187 sizer.AddSpacer(self.spacer_main) 188 189 # A vertical sizer for the right hand contents. 190 sub_sizer = wx.BoxSizer(wx.VERTICAL) 191 sizer.Add(sub_sizer, 1, wx.ALL|wx.EXPAND, 0) 192 193 # Convert to a text element. 194 text = wx.StaticText(self, -1, msg, style=wx.TE_MULTILINE) 195 text.SetFont(font.normal) 196 sub_sizer.Add(text, 1, wx.ALL|wx.EXPAND, 0) 197 198 # A sizer for the buttons. 199 button_sizer = wx.BoxSizer(wx.HORIZONTAL) 200 sub_sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT, 0) 201 202 # The yes button. 203 button_yes = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Yes") 204 button_yes.SetBitmapLabel(wx.Bitmap(icon_22x22.dialog_ok, wx.BITMAP_TYPE_ANY)) 205 button_yes.SetFont(font.normal) 206 button_yes.SetMinSize((self.width_button, self.height_button)) 207 button_sizer.Add(button_yes, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 208 self.Bind(wx.EVT_BUTTON, self.yes, button_yes) 209 210 # Button spacing. 211 button_sizer.AddSpacer(self.spacer_button) 212 213 # The no button. 214 button_no = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " No") 215 button_no.SetBitmapLabel(wx.Bitmap(icon_22x22.dialog_cancel, wx.BITMAP_TYPE_ANY)) 216 button_no.SetFont(font.normal) 217 button_no.SetMinSize((self.width_button, self.height_button)) 218 button_sizer.Add(button_no, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 219 self.Bind(wx.EVT_BUTTON, self.no, button_no) 220 221 # Set the focus to the default button. 222 if self.answer == wx.ID_YES: 223 button_yes.SetFocus() 224 else: 225 button_no.SetFocus() 226 227 # Bind some events. 228 self.Bind(wx.EVT_CLOSE, self.handler_close)
229 230
231 - def ShowModal(self):
232 """Replacement ShowModal method. 233 234 @return: The answer to the question, either wx.ID_YES or wx.ID_NO. 235 @rtype: int 236 """ 237 238 # Call the dialog's ShowModal method. 239 if status.show_gui: 240 super(Question, self).ShowModal() 241 242 # Return the answer. 243 return self.answer
244 245
246 - def handler_close(self, event):
247 """Event handler for the close window action. 248 249 @param event: The wx event. 250 @type event: wx event 251 """ 252 253 # Set the answer to no. 254 if not self.pressed: 255 self.answer = wx.ID_NO 256 257 # Continue the event. 258 event.Skip()
259 260
261 - def no(self, event):
262 """No selection. 263 264 @param event: The wx event. 265 @type event: wx event 266 """ 267 268 # Button flag. 269 self.pressed = True 270 271 # Set the answer. 272 self.answer = wx.ID_NO 273 274 # Close the dialog. 275 self.Close()
276 277
278 - def yes(self, event):
279 """Yes selection. 280 281 @param event: The wx event. 282 @type event: wx event 283 """ 284 285 # Button flag. 286 self.pressed = True 287 288 # Set the answer. 289 self.answer = wx.ID_YES 290 291 # Close the dialog. 292 self.Close()
293