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