PyXR

c:\projects\bitpim\src \ imp_cal_wizard.py



0001 #!/usr/bin/env python
0002 
0003 ### BITPIM
0004 ###
0005 ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org>
0006 ###
0007 ### This program is free software; you can redistribute it and/or modify
0008 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0009 ###
0010 ### $Id: imp_cal_wizard.py 4377 2007-08-27 04:58:33Z djpham $
0011 
0012 """ Handle Import Calendar wizard
0013 """
0014 
0015 # System
0016 from __future__ import with_statement
0017 # wx
0018 import wx
0019 import wx.wizard as wiz
0020 import  wx.lib.scrolledpanel as scrolled
0021 from wx.lib.expando import ExpandoTextCtrl
0022 
0023 # BitPim
0024 import common_calendar
0025 import guihelper
0026 import importexport
0027 import setphone_wizard
0028 
0029 # modules constants
0030 IMP_OPTION_REPLACEALL=0
0031 IMP_OPTION_ADD=1
0032 IMP_OPTION_MERGE=2
0033 
0034 #-------------------------------------------------------------------------------
0035 class ImportTypePage(setphone_wizard.MyPage):
0036     def __init__(self, parent):
0037         self._data=importexport.GetCalendarImports()
0038         super(ImportTypePage, self).__init__(parent,
0039                                              'Select Calendar Import Type')
0040         self._populate()
0041 
0042     def GetMyControls(self):
0043         vbs=wx.BoxSizer(wx.VERTICAL)
0044         vbs.Add(wx.StaticText(self, -1, 'Import Calendar Type:'), 0,
0045                 wx.EXPAND|wx.ALL, 5)
0046         self._type_lb=wx.ListBox(self, -1,
0047                                  style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0048         vbs.Add(self._type_lb, 0, wx.EXPAND|wx.ALL, 5)
0049         return vbs
0050 
0051     def _populate(self):
0052         # populate the listbox with data
0053         self._type_lb.Clear()
0054         for _l in self._data:
0055             self._type_lb.Append(_l['type'], _l)
0056 
0057     def ok(self):
0058         # ready to move to the next page?  By default, yes
0059         return self._type_lb.GetSelection()!=wx.NOT_FOUND
0060 
0061     def get(self, data):
0062         # return data to the main wizard, data is a dict
0063         _idx=self._type_lb.GetSelection()
0064         _type=self._type_lb.GetString(_idx)
0065         _new_type=data.get('type', None)!=_type
0066         if _new_type or not data.has_key('data'):
0067             data['type']=_type
0068             _info=self._type_lb.GetClientData(_idx)
0069             data['data']=_info['data']
0070             data['data_obj']=_info['data']()
0071             data['source_class']=_info['source']
0072             data['source_obj']=_info['source']()
0073         if _new_type:
0074             # new type selected
0075             data['type']=_type
0076             if data.has_key('source_id'):
0077                 del data['source_id']
0078 
0079     def set(self, data):
0080         # pass current data to this page
0081         if data.get('type', None):
0082             self._type_lb.SetStringSelection(data['type'])
0083 
0084 #-------------------------------------------------------------------------------
0085 class ImportSourcePage(setphone_wizard.MyPage):
0086     def __init__(self, parent):
0087         self._source=None
0088         super(ImportSourcePage, self).__init__(parent,
0089                                                'Select Import Source')
0090     def GetMyControls(self):
0091         vbs=wx.BoxSizer(wx.VERTICAL)
0092         vbs.Add(wx.StaticText(self, -1, 'Source of data:'), 0,
0093                 wx.ALL|wx.EXPAND, 5)
0094         self._source_lbl=ExpandoTextCtrl(self, -1, '', style=wx.TE_READONLY)
0095         self._source_lbl.SetBackgroundColour(self.GetBackgroundColour())
0096         vbs.Add(self._source_lbl, 0, wx.ALL|wx.EXPAND, 5)
0097 
0098         _btn=wx.Button(self, -1, 'Browse')
0099         wx.EVT_BUTTON(self, _btn.GetId(), self._OnBrowse)
0100         vbs.Add(_btn, 0, wx.ALL, 5)
0101         return vbs
0102 
0103     def setlabel(self):
0104         self._source_lbl.SetValue(self._source.name())
0105 
0106     @guihelper.BusyWrapper
0107     def _OnBrowse(self, _=None):
0108         if not self._source:
0109             return
0110         self._source.browse(self)
0111         self.setlabel()
0112             
0113     def ok(self):
0114         return self._source and self._source.get()
0115     def get(self, data):
0116         data['source_obj']=self._source
0117         data['source_id']=self._source.id
0118         data['imported']=False
0119     def set(self, data):
0120         self._source=data['source_obj']
0121         if self._source:
0122             if data.has_key('source_id'):
0123                 self._source.id=data['source_id']
0124             self.setlabel()
0125     def GetActiveDatabase(self):
0126         return self.GetParent().GetActiveDatabase()
0127 
0128 #-------------------------------------------------------------------------------
0129 class ImportDataAll(setphone_wizard.MyPage):
0130     def __init__(self, parent):
0131         self._type=None
0132         self._source=None
0133         super(ImportDataAll, self).__init__(parent, 'Import Data Preview')
0134 
0135     def GetMyControls(self):
0136         vbs=wx.BoxSizer(wx.VERTICAL)
0137         self._data_lb=wx.ListBox(self, -1,
0138                                  style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0139         vbs.Add(self._data_lb, 1, wx.EXPAND|wx.ALL, 5)
0140         _btn=wx.Button(self, -1, 'Data Filtering Parameters')
0141         vbs.Add(_btn, 0, wx.ALL, 5)
0142         wx.EVT_BUTTON(self, _btn.GetId(), self._OnFilter)
0143         return vbs
0144 
0145     def _populate_lb(self):
0146         self._data_lb.Clear()
0147         for _key, _entry in self._type.get_display_data().items():
0148             self._data_lb.Append('%s - %s'%(common_calendar.bp_date_str(_entry, _entry['start']),
0149                                             _entry['description']))
0150     @guihelper.BusyWrapper
0151     def _populate(self):
0152         self._data_lb.Clear()
0153         if not self._type or not self._source:
0154             # nothing to import
0155             return
0156         with guihelper.WXDialogWrapper(wx.ProgressDialog('Calendar Data Import',
0157                                                          'Importing data, please wait ...',
0158                                                          parent=self)) as dlg:
0159             self._type.read(self._source.get(), dlg)
0160             self._populate_lb()
0161 
0162     def _OnFilter(self, _):
0163         cat_list=self._type.get_category_list()
0164         with guihelper.WXDialogWrapper(common_calendar.FilterDialog(self, -1, 'Filtering Parameters',
0165                                                                     cat_list),
0166                                        True) as (dlg, retcode):
0167             if retcode==wx.ID_OK:
0168                 self._type.set_filter(dlg.get())
0169                 self._populate_lb()
0170 
0171     def set(self, data):
0172         self._type=data.get('data_obj', None)
0173         self._source=data.get('source_obj', None)
0174         if not data.get('imported', False):
0175             self._populate()
0176     def get(self, data):
0177         data['imported']=True
0178 
0179 #-------------------------------------------------------------------------------
0180 class ImportOptionPage(setphone_wizard.MyPage):
0181     _choices=('Replace All', 'Add', 'Merge')
0182     def __init__(self, parent):
0183         super(ImportOptionPage, self).__init__(parent,
0184                                                'Import Options')
0185     def GetMyControls(self):
0186         vbs=wx.BoxSizer(wx.VERTICAL)
0187         self._option_rb=wx.RadioBox(self, -1, 'Import Options',
0188                                     choices=self._choices,
0189                                     style=wx.RA_SPECIFY_ROWS)
0190         vbs.Add(self._option_rb, 0, wx.EXPAND|wx.ALL, 5)
0191         return vbs
0192 
0193     def set(self, data):
0194         self._option_rb.SetSelection(data.get('option', 0))
0195     def get(self, data):
0196         data['option']=self._option_rb.GetSelection()
0197 
0198 #-------------------------------------------------------------------------------
0199 class ImportCalendarWizard(wiz.Wizard):
0200     ID_ADD=wx.NewId()
0201     ID_MERGE=wx.NewId()
0202     def __init__(self, parent, id=-1, title='Calendar Import Wizard'):
0203         super(ImportCalendarWizard, self).__init__(parent, id, title)
0204         self._data={}
0205         _import_type_page=ImportTypePage(self)
0206         _import_source_page=ImportSourcePage(self)
0207         _import_data_all=ImportDataAll(self)
0208         _import_option=ImportOptionPage(self)
0209 
0210         wiz.WizardPageSimple_Chain(_import_type_page, _import_source_page)
0211         wiz.WizardPageSimple_Chain(_import_source_page, _import_data_all)
0212         wiz.WizardPageSimple_Chain(_import_data_all, _import_option)
0213         self.first_page=_import_type_page
0214         self.GetPageAreaSizer().Add(self.first_page, 1, wx.EXPAND|wx.ALL, 5)
0215         wiz.EVT_WIZARD_PAGE_CHANGING(self, self.GetId(), self.OnPageChanging)
0216         wiz.EVT_WIZARD_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
0217 
0218     def RunWizard(self, firstPage=None):
0219         return super(ImportCalendarWizard, self).RunWizard(firstPage or self.first_page)
0220 
0221     def OnPageChanging(self, evt):
0222         pg=evt.GetPage()
0223         if not evt.GetDirection() or pg.ok():
0224             pg.get(self._data)
0225         else:
0226             evt.Veto()
0227 
0228     def OnPageChanged(self, evt):
0229         evt.GetPage().set(self._data)
0230 
0231     def get(self):
0232         if self._data.get('data_obj', None):
0233             return self._data['data_obj'].get()
0234         return {}
0235 
0236     def GetActiveDatabase(self):
0237         return self.GetParent().GetActiveDatabase()
0238     def get_categories(self):
0239         if self._data.get('data_obj', None):
0240             return self._data['data_obj'].get_category_list()
0241         return []
0242 
0243     def ShowModal(self):
0244         global IMP_OPTION_REPLACEALL
0245         # run the wizard and return a code
0246         if self.RunWizard():
0247             return [wx.ID_OK, self.ID_ADD, self.ID_MERGE][self._data.get('option',
0248                                                                          IMP_OPTION_REPLACEALL)]
0249         return wx.ID_CANCEL
0250 
0251 #-------------------------------------------------------------------------------
0252 # Testing
0253 if __name__=="__main__":
0254     app=wx.PySimpleApp()
0255     f=wx.Frame(None, title='imp_cal_wizard')
0256     w=ImportCalendarWizard(f)
0257     print w.RunWizard()
0258     print w.get()
0259     w.Destroy()
0260 

Generated by PyXR 0.9.4