Package application :: Package dialogs :: Module Util
[hide private]
[frames] | no frames]

Source Code for Module application.dialogs.Util

  1  #   Copyright (c) 2004-2007 Open Source Applications Foundation 
  2  # 
  3  #   Licensed under the Apache License, Version 2.0 (the "License"); 
  4  #   you may not use this file except in compliance with the License. 
  5  #   You may obtain a copy of the License at 
  6  # 
  7  #       http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  #   Unless required by applicable law or agreed to in writing, software 
 10  #   distributed under the License is distributed on an "AS IS" BASIS, 
 11  #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 12  #   See the License for the specific language governing permissions and 
 13  #   limitations under the License. 
 14   
 15   
 16  import os, codecs 
 17  import wx 
 18  from i18n import ChandlerMessageFactory as _ 
 19   
 20  # A helper method and class for allowing the user to modify an item's attributes 
 21  """ 
 22  Note: need to migrate translation logic to a base wx dialog class that can 
 23        handle all the work for sub classes 
 24  """ 
 25   
26 -def promptForItemValues(title, item, attrList):
27 """ 28 Given an item and a list of attributes, display a modal dialog with 29 a text field per attribute, with each field populated directly from 30 the item's attribute values. If the user OK's the dialog, the new 31 values are applied to the item's attributes. 32 33 @param frame: A wx parent frame 34 @type frame: wx frame 35 @param title: The title string for the dialog 36 @type title: String 37 @param item: A chandler item 38 @type item: Item 39 @param attrList: A list of dictionaries, each one having the following 40 keys:: 41 42 "attr": an attribute name 43 "label": a label to display for the field 44 "password": an optional key, set to True if you want this field to 45 be displayed like a password (with asterisks) 46 47 """ 48 49 win = ItemValuesDialog(title, item, attrList) 50 win.CenterOnScreen() 51 val = win.ShowModal() 52 53 if val == wx.ID_OK: 54 # Assign the new values 55 win.AssignNewValues() 56 57 win.Destroy() 58 return val == wx.ID_OK
59
60 -class ItemValuesDialog(wx.Dialog):
61 - def __init__(self, title, item, attrList, size=wx.DefaultSize, 62 pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE):
63 64 # Instead of calling wx.Dialog.__init__ we precreate the dialog 65 # so we can set an extra style that must be set before 66 # creation, and then we create the GUI dialog using the Create 67 # method. 68 pre = wx.PreDialog() 69 pre.Create(None, -1, title, pos, size, style) 70 71 # This next step is the most important, it turns this Python 72 # object into the real wrapper of the dialog (instead of pre) 73 # as far as the wxPython extension is concerned. 74 self.this = pre.this 75 76 # Now continue with the normal construction of the dialog 77 # contents 78 sizer = wx.BoxSizer(wx.VERTICAL) 79 80 textControls = [] 81 for valueDict in attrList: 82 box = wx.BoxSizer(wx.HORIZONTAL) 83 84 label = wx.StaticText(self, -1, valueDict["label"]) 85 box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 86 87 if valueDict.get("password", False): 88 text = wx.TextCtrl(self, -1, 89 getattr(item, valueDict["attr"]), 90 wx.DefaultPosition, [400,-1], wx.TE_PASSWORD) 91 else: 92 text = wx.TextCtrl(self, -1, 93 getattr(item, valueDict["attr"]), 94 wx.DefaultPosition, [400,-1]) 95 box.Add(text, 1, wx.ALIGN_CENTRE|wx.ALL, 5) 96 97 sizer.AddSizer(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 98 textControls.append(text) 99 100 line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL) 101 sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5) 102 103 box = wx.BoxSizer(wx.HORIZONTAL) 104 105 btn = wx.Button(self, wx.ID_OK) 106 btn.SetDefault() 107 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 108 109 btn = wx.Button(self, wx.ID_CANCEL) 110 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 111 112 sizer.Add(box, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 113 114 self.SetSizer(sizer) 115 self.SetAutoLayout(True) 116 sizer.Fit(self) 117 118 # Store these, using attribute names that hopefully wont collide with 119 # any wx attributes 120 self.chandlerTextControls = textControls 121 self.chandlerItem = item 122 self.chandlerAttrs = attrList
123
124 - def AssignNewValues(self):
125 i = 0 126 for (valueDict) in self.chandlerAttrs: 127 setattr(self.chandlerItem, valueDict["attr"], 128 self.chandlerTextControls[i].GetValue()) 129 i += 1
130 131 132 # A simple "prompt-the-user-for-a-string" dialog 133
134 -def promptUser(title, message, defaultValue=""):
135 """ 136 Prompt the user to enter in a string. Return None if cancel is hit. 137 138 @param title: The title string for the dialog 139 @type title: String 140 @param message: A message prompting the user for input 141 @type message: String 142 @param defaultValue: A value to populate the text field with 143 @type defaultValue: String 144 """ 145 win = promptUserDialog(title, message, defaultValue) 146 win.CenterOnScreen() 147 val = win.ShowModal() 148 149 if val == wx.ID_OK: 150 # Assign the new values 151 value = win.GetValue() 152 153 else: 154 value = None 155 156 win.Destroy() 157 158 return value
159
160 -def promptUserAction(title, message, 161 okayTitle=wx.EmptyString, cancelTitle=wx.EmptyString):
162 """ 163 Prompt the user to choose between two actions, as defined by 164 the two buttons in the dialog. No text entry is provided for. 165 166 @param title: The title string for the dialog 167 @type title: String 168 @param message: A message prompting the user for input 169 @type message: String 170 @param defaultValue: A value to populate the text field with 171 @type defaultValue: String 172 @param okayTitle: The title for the first button, selecting which 173 will return the value wx.ID_OK 174 @type okayTitle: String 175 @param cancelTitle: The title for the second button, selecting which 176 will return the value wx.ID_CANCEL 177 @type cancelTitle: String 178 """ 179 win = promptUserDialog(title, message, None, size=(500,-1), 180 button1Title=okayTitle, button2Title=cancelTitle) 181 win.CenterOnScreen() 182 val = win.ShowModal() 183 184 win.Destroy() 185 186 return val
187
188 -def mailAccountError(view, message, account):
189 # importing AccountPreferences imports osaf.sharing, but Util is loaded 190 # by a sharing dependency, so to avoid import loops, only import 191 # AccountPreferences when we need it 192 import AccountPreferences 193 win = MailAccountErrorDialog(message) 194 win.CenterOnScreen() 195 val = win.ShowModal() 196 197 win.Destroy() 198 199 if val == wx.ID_OK: 200 AccountPreferences.ShowAccountPreferencesDialog(account, view)
201 202
203 -def mailAddressError():
204 message = _(u"You have addressed this message to invalid email addresses.") 205 win = MailAddressErrorDialog(message) 206 win.CenterOnScreen() 207 val = win.ShowModal() 208 209 win.Destroy() 210 211 if val == wx.ID_OK: 212 return False 213 214 return True
215
216 -class MailErrorBaseDialog(wx.Dialog):
217 - def __init__(self, message):
218 size = wx.DefaultSize 219 pos = wx.DefaultPosition 220 style = wx.DEFAULT_DIALOG_STYLE 221 222 # Instead of calling wx.Dialog.__init__ we precreate the dialog 223 # so we can set an extra style that must be set before 224 # creation, and then we create the GUI dialog using the Create 225 # method. 226 pre = wx.PreDialog() 227 pre.Create(None, -1, _(u"Mail Error"), pos, size, style) 228 229 # This next step is the most important, it turns this Python 230 # object into the real wrapper of the dialog (instead of pre) 231 # as far as the wxPython extension is concerned. 232 self.this = pre.this 233 234 # Now continue with the normal construction of the dialog 235 # contents 236 sizer = wx.BoxSizer(wx.VERTICAL) 237 label = wx.StaticText(self, -1, message) 238 sizer.Add(label, 0, wx.ALIGN_CENTER|wx.ALL, 55) 239 240 box = wx.BoxSizer(wx.HORIZONTAL) 241 242 self.addButtons(box) 243 244 sizer.Add(box, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 245 246 self.SetSizer(sizer) 247 self.SetAutoLayout(True) 248 sizer.Fit(self)
249
250 - def addButtons(self, sizer):
251 raise NotImplementedError()
252
253 -class MailAccountErrorDialog(MailErrorBaseDialog):
254 - def addButtons(self, sizer):
255 btn = wx.Button(self, wx.ID_CANCEL) 256 btn.SetDefault() 257 sizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 258 259 btn = wx.Button(self, wx.ID_OK, _(u" Edit Account Settings ")) 260 sizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
261 262
263 -class MailAddressErrorDialog(MailErrorBaseDialog):
264 - def addButtons(self, sizer):
265 btn = wx.Button(self, wx.ID_CANCEL, _(u"Fix email addresses")) 266 btn.SetDefault() 267 sizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 268 269 btn = wx.Button(self, wx.ID_OK, _(u"Send anyway")) 270 sizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
271 272
273 -class promptUserDialog(wx.Dialog):
274 - def __init__(self, title, message, value, isPassword=False, 275 size=wx.DefaultSize, pos=wx.DefaultPosition, 276 style=wx.DEFAULT_DIALOG_STYLE, 277 button1Title=wx.EmptyString, button2Title=wx.EmptyString):
278 279 # Instead of calling wx.Dialog.__init__ we precreate the dialog 280 # so we can set an extra style that must be set before 281 # creation, and then we create the GUI dialog using the Create 282 # method. 283 pre = wx.PreDialog() 284 pre.Create(None, -1, title, pos, size, style) 285 286 # This next step is the most important, it turns this Python 287 # object into the real wrapper of the dialog (instead of pre) 288 # as far as the wxPython extension is concerned. 289 self.this = pre.this 290 291 # Now continue with the normal construction of the dialog 292 # contents 293 sizer = wx.BoxSizer(wx.VERTICAL) 294 label = wx.StaticText(self, -1, message, size=[500,-1]) 295 sizer.Add(label, 0, wx.ALIGN_CENTER|wx.ALL, 5) 296 297 box = wx.BoxSizer(wx.HORIZONTAL) 298 299 if value is not None: 300 if isPassword: 301 text = wx.TextCtrl(self, -1, value, wx.DefaultPosition, [500,-1], 302 wx.TE_PASSWORD) 303 else: 304 text = wx.TextCtrl(self, -1, value, wx.DefaultPosition, [500,-1]) 305 box.Add(text, 1, wx.ALIGN_CENTRE|wx.ALL, 5) 306 else: 307 text = None 308 309 sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 310 311 # line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL) 312 # sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5) 313 314 box = wx.BoxSizer(wx.HORIZONTAL) 315 316 btn = wx.Button(self, wx.ID_OK, button1Title) 317 btn.SetDefault() 318 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 319 320 btn = wx.Button(self, wx.ID_CANCEL, button2Title) 321 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 322 323 sizer.Add(box, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 324 325 self.SetSizer(sizer) 326 self.SetAutoLayout(True) 327 sizer.Fit(self) 328 329 # Store these, using attribute names that hopefully wont collide with 330 # any wx attributes 331 self.textControl = text 332 if text is not None: 333 text.SetFocus()
334
335 - def GetValue(self):
336 val = None 337 if self.textControl is not None: 338 val = self.textControl.GetValue() 339 return val
340 341
342 -class checkboxUserDialog(wx.Dialog):
343 - def __init__(self, parent, title, message, value, 344 size=wx.DefaultSize, pos=wx.DefaultPosition, 345 style=wx.DEFAULT_DIALOG_STYLE):
346 347 # Instead of calling wx.Dialog.__init__ we precreate the dialog 348 # so we can set an extra style that must be set before 349 # creation, and then we create the GUI dialog using the Create 350 # method. 351 pre = wx.PreDialog() 352 pre.Create(parent, -1, title, pos, size, style) 353 354 # This next step is the most important, it turns this Python 355 # object into the real wrapper of the dialog (instead of pre) 356 # as far as the wxPython extension is concerned. 357 self.this = pre.this 358 359 # Now continue with the normal construction of the dialog 360 # contents 361 sizer = wx.BoxSizer(wx.VERTICAL) 362 label = wx.StaticText(self, -1, message) 363 label.Wrap(400) 364 sizer.Add(label, 0, wx.ALIGN_CENTER|wx.ALL, 5) 365 366 # [ [ checkboxctrl ] [ buttonctrl buttonctr ] ] 367 368 row = wx.BoxSizer(wx.HORIZONTAL) 369 370 box = wx.BoxSizer(wx.HORIZONTAL) 371 372 checkbox = wx.CheckBox(self, -1, value, wx.DefaultPosition) 373 374 box.Add(checkbox, 1, wx.ALIGN_LEFT|wx.ALL, 5) 375 376 row.Add(box, 1, wx.ALIGN_LEFT|wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 377 378 # line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL) 379 # sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5) 380 381 box = wx.BoxSizer(wx.HORIZONTAL) 382 383 btn = wx.Button(self, wx.ID_YES) 384 btn.SetDefault() 385 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 386 387 btn = wx.Button(self, wx.ID_NO) 388 box.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 389 390 row.Add(box, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 391 sizer.Add(row, 0, wx.GROW|wx.ALL, 5) 392 393 self.SetSizer(sizer) 394 self.SetAutoLayout(True) 395 sizer.Fit(self) 396 397 # Store these, using attribute names that hopefully wont collide with 398 # any wx attributes 399 self.checkbox = checkbox 400 #if wx.Platform != '__WXMAC__': 401 # checkbox.SetFocus() 402 403 self.Bind(wx.EVT_BUTTON, self.End, id=wx.ID_YES) 404 self.Bind(wx.EVT_BUTTON, self.End, id=wx.ID_NO)
405
406 - def End(self, event):
407 self.EndModal(event.GetId())
408
409 - def GetValue(self):
410 return self.checkbox.GetValue()
411 412
413 -def displayLogWindow(logList):
414 415 win = LogWindow(logList) 416 win.CenterOnScreen() 417 win.ShowModal() 418 win.Destroy()
419
420 -class LogWindow(wx.Dialog):
421 - def __init__(self, logList, size=wx.DefaultSize, 422 pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE):
423 424 # Instead of calling wx.Dialog.__init__ we precreate the dialog 425 # so we can set an extra style that must be set before 426 # creation, and t