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 (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program 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 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports 
 24  import sys 
 25  import wx 
 26  import wx.lib.buttons 
 27  import wx.lib.scrolledpanel 
 28   
 29  # relax module imports. 
 30  from graphics import fetch_icon 
 31  import gui 
 32  from gui.fonts import font 
 33  from gui.icons import Relax_icons 
 34  from gui.misc import bitmap_setup 
 35  from status import Status; status = Status() 
 36   
 37   
38 -def error_message(msg, caption=''):
39 """Message box for general errors. 40 41 @param msg: The message to display. 42 @type msg: str 43 """ 44 45 # Show the message box. 46 if status.show_gui: 47 wx.MessageBox(msg, caption=caption, style=wx.OK|wx.ICON_ERROR) 48 49 # Otherwise throw the error out to stderr. 50 else: 51 # Combine the caption and message. 52 if caption: 53 msg = "%s: %s" % (caption, msg) 54 55 # Write out. 56 sys.stderr.write(msg + "\n") 57 sys.stderr.flush()
58 59 60
61 -class Missing_data(wx.Dialog):
62 """Message box GUI element for when a setup is incomplete or there is missing data.""" 63
64 - def __init__(self, missing=[], parent=None):
65 """Set up the dialog. 66 67 @keyword missing: The list of missing data types. 68 @type missing: list of str 69 @keyword parent: The parent wx element. 70 @type parent: wx object 71 """ 72 73 # Initialise the base class. 74 wx.Dialog.__init__(self, parent, 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, bitmap_setup(fetch_icon('oxygen.status.user-busy', "48x48"))) 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 sys.stderr.flush()
130 131 132
133 -class Question(wx.Dialog):
134 """Question box GUI element for obtaining a yes/no response from the user.""" 135 136 # Some class variables. 137 border = 10 138 spacer_button = 10 139 spacer_main = 20 140 height_button = 30 141 width_button = 100 142
143 - def __init__(self, msg, parent=None, title='', size=(350, 125), default=False):
144 """A generic question box. 145 146 @param msg: The text message to display. 147 @type msg: str 148 @keyword parent: The parent wx object. 149 @type parent: wx.object instance 150 @keyword title: The window title. 151 @type title: str 152 @keyword default: If True, the default button will be 'yes', otherwise it will be 'no'. 153 @type default: bool 154 @return: The answer. 155 @rtype: bool 156 """ 157 158 # Initialise the base class. 159 wx.Dialog.__init__(self, parent, title=title, size=size, style=wx.DEFAULT_DIALOG_STYLE|wx.STAY_ON_TOP) 160 161 # Flag to indicate that a button was pressed. 162 self.pressed = False 163 164 # The default. 165 if default: 166 self.answer = wx.ID_YES 167 else: 168 self.answer = wx.ID_NO 169 170 # Set up the window icon. 171 self.SetIcons(Relax_icons()) 172 173 # Centre the window. 174 self.Centre() 175 176 # A sizer for the dialog. 177 main_sizer = wx.BoxSizer(wx.HORIZONTAL) 178 self.SetSizer(main_sizer) 179 180 # Build the central sizer, with borders. 181 sizer = gui.misc.add_border(main_sizer, border=self.border, packing=wx.HORIZONTAL) 182 183 # Add the graphic. 184 bitmap = wx.StaticBitmap(self, -1, bitmap_setup(fetch_icon('oxygen.status.dialog-warning-relax-blue', "48x48"))) 185 sizer.Add(bitmap) 186 187 # Spacing. 188 sizer.AddSpacer(self.spacer_main) 189 190 # A vertical sizer for the right hand contents. 191 sub_sizer = wx.BoxSizer(wx.VERTICAL) 192 sizer.Add(sub_sizer, 1, wx.ALL|wx.EXPAND, 0) 193 194 # Convert to a text element. 195 text = wx.StaticText(self, -1, msg, style=wx.TE_MULTILINE) 196 text.SetFont(font.normal) 197 sub_sizer.Add(text, 1, wx.ALL|wx.EXPAND, 0) 198 199 # A sizer for the buttons. 200 button_sizer = wx.BoxSizer(wx.HORIZONTAL) 201 sub_sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT, 0) 202 203 # The yes button. 204 button_yes = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " Yes") 205 button_yes.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.dialog-ok', "22x22"), wx.BITMAP_TYPE_ANY)) 206 button_yes.SetFont(font.normal) 207 button_yes.SetMinSize((self.width_button, self.height_button)) 208 button_sizer.Add(button_yes, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 209 self.Bind(wx.EVT_BUTTON, self.yes, button_yes) 210 211 # Button spacing. 212 button_sizer.AddSpacer(self.spacer_button) 213 214 # The no button. 215 button_no = wx.lib.buttons.ThemedGenBitmapTextButton(self, -1, None, " No") 216 button_no.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.dialog-cancel', "22x22"), wx.BITMAP_TYPE_ANY)) 217 button_no.SetFont(font.normal) 218 button_no.SetMinSize((self.width_button, self.height_button)) 219 button_sizer.Add(button_no, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 220 self.Bind(wx.EVT_BUTTON, self.no, button_no) 221 222 # Set the focus to the default button. 223 if self.answer == wx.ID_YES: 224 button_yes.SetFocus() 225 else: 226 button_no.SetFocus() 227 228 # Bind some events. 229 self.Bind(wx.EVT_CLOSE, self.handler_close)
230 231
232 - def ShowModal(self):
233 """Replacement ShowModal method. 234 235 @return: The answer to the question, either wx.ID_YES or wx.ID_NO. 236 @rtype: int 237 """ 238 239 # Call the dialog's ShowModal method. 240 if status.show_gui: 241 super(Question, self).ShowModal() 242 243 # Return the answer. 244 return self.answer
245 246
247 - def handler_close(self, event):
248 """Event handler for the close window action. 249 250 @param event: The wx event. 251 @type event: wx event 252 """ 253 254 # Set the answer to no. 255 if not self.pressed: 256 self.answer = wx.ID_NO 257 258 # Continue the event. 259 event.Skip()
260 261
262 - def no(self, event):
263 """No selection. 264 265 @param event: The wx event. 266 @type event: wx event 267 """ 268 269 # Button flag. 270 self.pressed = True 271 272 # Set the answer. 273 self.answer = wx.ID_NO 274 275 # Close the dialog. 276 self.Close()
277 278
279 - def yes(self, event):
280 """Yes selection. 281 282 @param event: The wx event. 283 @type event: wx event 284 """ 285 286 # Button flag. 287 self.pressed = True 288 289 # Set the answer. 290 self.answer = wx.ID_YES 291 292 # Close the dialog. 293 self.Close()
294