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=[], parent=None):
67 """Set up the dialog. 68 69 @keyword missing: The list of missing data types. 70 @type missing: list of str 71 @keyword parent: The parent wx element. 72 @type parent: wx object 73 """ 74 75 # Initialise the base class. 76 wx.Dialog.__init__(self, parent, title='Missing data', style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.STAY_ON_TOP) 77 78 # Set up the window icon. 79 self.SetIcons(relax_icons) 80 81 # Set the initial size. 82 self.SetSize((600, 400)) 83 84 # Centre the window. 85 self.Centre() 86 87 # A sizer for the dialog. 88 main_sizer = wx.BoxSizer(wx.HORIZONTAL) 89 self.SetSizer(main_sizer) 90 91 # Build the central sizer, with borders. 92 sizer = gui.misc.add_border(main_sizer, border=10, packing=wx.HORIZONTAL) 93 94 # Add the graphic. 95 bitmap = wx.StaticBitmap(self, -1, wx.Bitmap(icon_48x48.user_busy, wx.BITMAP_TYPE_ANY)) 96 sizer.Add(bitmap) 97 98 # Spacing. 99 sizer.AddSpacer(20) 100 101 # A scrolled panel for the text. 102 panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1) 103 panel.SetAutoLayout(1) 104 panel.SetupScrolling() 105 sizer.Add(panel, 1, wx.ALL|wx.EXPAND, 0) 106 107 # A sizer for the panel. 108 panel_sizer = wx.BoxSizer(wx.HORIZONTAL) 109 panel.SetSizer(panel_sizer) 110 111 # The message. 112 msg = "The set up is incomplete.\n\n" 113 if not len(missing): 114 msg = msg + "Please check for missing data.\n" 115 else: 116 msg = msg + "Please check for the following missing information:\n" 117 for data in missing: 118 msg = msg + " %s\n" % data 119 120 # Convert to a text element. 121 text = wx.StaticText(panel, -1, msg, style=wx.TE_MULTILINE) 122 panel_sizer.Add(text) 123 124 # Show the GUI element. 125 if status.show_gui: 126 self.ShowModal() 127 128 # Otherwise throw the error out to stderr. 129 else: 130 sys.stderr.write("Missing data: %s\n" % msg)
131 132 133
134 -class Question(wx.Dialog):
135 """Question box GUI element for obtaining a yes/no response from the user.""" 136 137 # Some class variables. 138 border = 10 139 spacer_button = 10 140 spacer_main = 20 141 height_button = 30 142 width_button = 100 143
144 - def __init__(self, msg, parent=None, title='', size=(350, 125), default=False):
145 """A generic question box. 146 147 @param msg: The text message to display. 148 @type msg: str 149 @keyword parent: The parent wx object. 150 @type parent: wx.object instance 151 @keyword title: The window title. 152 @type title: str 153 @keyword default: If True, the default button will be 'yes', otherwise it will be 'no'. 154 @type default: bool 155 @return: The answer. 156 @rtype: bool 157 """ 158 159 # Initialise the base class. 160 wx.Dialog.__init__(self, parent, title=title, size=size, style=wx.DEFAULT_DIALOG_STYLE|wx.STAY_ON_TOP) 161 162 # Flag to indicate that a button was pressed. 163 self.pressed = False 164 165 # The default. 166 if default: 167 self.answer = wx.ID_YES 168 else: 169 self.answer = wx.ID_NO 170 171 # Set up the window icon. 172 self.SetIcons(relax_icons) 173 174 # Centre the window. 175 self.Centre() 176 177 # A sizer for the dialog. 178 main_sizer = wx.BoxSizer(wx.HORIZONTAL) 179 self.SetSizer(main_sizer) 180 181 # Build the central sizer, with borders. 182 sizer = gui.misc.add_border(main_sizer, border=self.border, packing=wx.HORIZONTAL) 183 184 # Add the graphic. 185 bitmap = wx.StaticBitmap(self, -1, wx.Bitmap(icon_48x48.dialog_warning_relax, wx.BITMAP_TYPE_ANY)) 186 sizer.Add(bitmap) 187 188 # Spacing. 189 sizer.AddSpacer(self.spacer_main) 190 191 # A vertical sizer for the right hand contents. 192 sub_sizer = wx.BoxSizer(wx.VERTICAL) 193 sizer.Add(sub_sizer, 1, wx.ALL|wx.EXPAND, 0) 194 195 # Convert to a text element. 196 text = wx.StaticText(self, -1, msg, style=wx.TE_MULTILINE) 197 text.SetFont(font.normal) 198 sub_sizer.Add(text, 1, wx.ALL|wx.EXPAND, 0) 199 200 # A sizer for the buttons. 201 button_sizer = wx.BoxSizer(wx.HORIZONTAL) 202 sub_sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT, 0) 203 204 # The yes button. 205 button_yes = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Yes") 206 button_yes.SetBitmapLabel(wx.Bitmap(icon_22x22.dialog_ok, wx.BITMAP_TYPE_ANY)) 207 button_yes.SetFont(font.normal) 208 button_yes.SetMinSize((self.width_button, self.height_button)) 209 button_sizer.Add(button_yes, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 210 self.Bind(wx.EVT_BUTTON, self.yes, button_yes) 211 212 # Button spacing. 213 button_sizer.AddSpacer(self.spacer_button) 214 215 # The no button. 216 button_no = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " No") 217 button_no.SetBitmapLabel(wx.Bitmap(icon_22x22.dialog_cancel, wx.BITMAP_TYPE_ANY)) 218 button_no.SetFont(font.normal) 219 button_no.SetMinSize((self.width_button, self.height_button)) 220 button_sizer.Add(button_no, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 221 self.Bind(wx.EVT_BUTTON, self.no, button_no) 222 223 # Set the focus to the default button. 224 if self.answer == wx.ID_YES: 225 button_yes.SetFocus() 226 else: 227 button_no.SetFocus() 228 229 # Bind some events. 230 self.Bind(wx.EVT_CLOSE, self.handler_close)
231 232
233 - def ShowModal(self):
234 """Replacement ShowModal method. 235 236 @return: The answer to the question, either wx.ID_YES or wx.ID_NO. 237 @rtype: int 238 """ 239 240 # Call the dialog's ShowModal method. 241 if status.show_gui: 242 super(Question, self).ShowModal() 243 244 # Return the answer. 245 return self.answer
246 247
248 - def handler_close(self, event):
249 """Event handler for the close window action. 250 251 @param event: The wx event. 252 @type event: wx event 253 """ 254 255 # Set the answer to no. 256 if not self.pressed: 257 self.answer = wx.ID_NO 258 259 # Continue the event. 260 event.Skip()
261 262
263 - def no(self, event):
264 """No selection. 265 266 @param event: The wx event. 267 @type event: wx event 268 """ 269 270 # Button flag. 271 self.pressed = True 272 273 # Set the answer. 274 self.answer = wx.ID_NO 275 276 # Close the dialog. 277 self.Close()
278 279
280 - def yes(self, event):
281 """Yes selection. 282 283 @param event: The wx event. 284 @type event: wx event 285 """ 286 287 # Button flag. 288 self.pressed = True 289 290 # Set the answer. 291 self.answer = wx.ID_YES 292 293 # Close the dialog. 294 self.Close()
295