def __init__(self, parent): """ Constructor. """ wx.Toolbook.__init__(self, parent, wx.NewIdRef(), style=wx.BK_DEFAULT) ### don't try to translate this labels with _() because there are used to find png L = [('General', "(self)"), ('Simulation', "(self)"), ('Editor', "(self)"), ('Plugins', "(self)")] # make an image list using the LBXX images il = wx.ImageList(25, 25) for img in [ wx.Bitmap(os.path.join(ICON_PATH, "%s_pref.png" % a[0])) for a in L ]: il.Add(img) self.AssignImageList(il) imageIdGenerator = iter(range(il.GetImageCount())) for p, label in [("%sPanel%s" % (s, str(args)), _(s)) for s, args in L]: page = eval(p) self.AddPage(page, label, imageId=next(imageIdGenerator)) ### Plug-in page setting (populate is done when page is changed) self.pluginPanel = self.GetPage(self.GetPageCount() - 1) self.CheckList = GeneralPluginsList(self.pluginPanel.GetRightPanel()) self.pluginPanel.SetPluginsList(self.CheckList) lpanel = self.pluginPanel.GetLeftPanel() ### Buttons for insert or delete plug-ins self.addBtn = wx.Button(lpanel, wx.ID_ADD, size=(140, -1)) self.delBtn = wx.Button(lpanel, wx.ID_DELETE, size=(140, -1)) self.refBtn = wx.Button(lpanel, wx.ID_REFRESH, size=(140, -1)) if wx.VERSION_STRING >= '4.0': self.addBtn.SetToolTipString = self.addBtn.SetToolTip self.delBtn.SetToolTipString = self.delBtn.SetToolTip self.refBtn.SetToolTipString = self.refBtn.SetToolTip self.addBtn.SetToolTipString(_("Add new plug-ins")) self.delBtn.SetToolTipString(_("Delete all existing plug-ins")) self.refBtn.SetToolTipString(_("Refresh plug-ins list")) ### add widget to plug-in panel self.pluginPanel.AddWidget(3, self.addBtn) self.pluginPanel.AddWidget(4, self.delBtn) self.pluginPanel.AddWidget(5, self.refBtn) ### Binding self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging) self.Bind(wx.EVT_BUTTON, self.OnAdd, id=self.addBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=self.delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=self.refBtn.GetId())
class Preferences(wx.Toolbook): """ Based Toolbook Preference class """ def __init__(self, parent): """ Constructor. """ wx.Toolbook.__init__(self, parent, wx.ID_ANY, style=wx.BK_DEFAULT) ### don't try to translate this labels with _() because there are used to find png L = [('General', "(self)"), ('Simulation', "(self)"), ('Editor', "(self)"), ('Plugins', "(self)")] # make an image list using the LBXX images il = wx.ImageList(32, 32) for img in map( lambda a: wx.Image( os.path.join(ICON_PATH, "%s_pref.png" % a[0]), wx. BITMAP_TYPE_PNG).ConvertToBitmap(), L): il.Add(img) self.AssignImageList(il) imageIdGenerator = iter(range(il.GetImageCount())) for page, label in [(eval("%sPanel%s" % (s, str(args))), _(s)) for s, args in L]: self.AddPage(page, label, imageId=imageIdGenerator.next()) ### Plug-in page setting (populate is done when page is changed) self.pluginPanel = self.GetPage(self.GetPageCount() - 1) self.CheckList = GeneralPluginsList(self.pluginPanel.GetRightPanel()) self.pluginPanel.SetPluginsList(self.CheckList) lpanel = self.pluginPanel.GetLeftPanel() ### Buttons for insert or delete plug-ins self.addBtn = wx.Button(lpanel, wx.ID_ADD, size=(140, -1)) self.delBtn = wx.Button(lpanel, wx.ID_DELETE, size=(140, -1)) self.refBtn = wx.Button(lpanel, wx.ID_REFRESH, size=(140, -1)) self.addBtn.SetToolTipString(_("Add new plug-ins")) self.delBtn.SetToolTipString(_("Delete all existing plug-ins")) self.refBtn.SetToolTipString(_("Refresh plug-ins list")) ### add widget to plug-in panel self.pluginPanel.AddWidget(3, self.addBtn) self.pluginPanel.AddWidget(4, self.delBtn) self.pluginPanel.AddWidget(5, self.refBtn) ### Binding self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging) self.Bind(wx.EVT_BUTTON, self.OnAdd, id=self.addBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=self.delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=self.refBtn.GetId()) #---------------------------------------------------------------------- def OnPageChanged(self, event): """ """ # old = event.GetOldSelection() new = event.GetSelection() # sel = self.GetSelection() # print 'OnPageChanged, old:%d, new:%d, sel:%d\n' % (old, new, sel) parent = self.GetTopLevelParent() ### plug-ins page if new == 3: parent.SetSize((700, 500)) else: parent.SetSize((700, 450)) event.Skip() def OnPageChanging(self, event): """ """ new = event.GetSelection() ### plug-in page if new == 3: ### list of plug-ins file in plug-in directory l = list(os.walk(PLUGINS_PATH)) ### populate checklist with file in plug-ins directory wx.CallAfter(self.CheckList.Populate, (l)) event.Skip() def OnAdd(self, event): """ Add plugin """ wcd = 'All files (*)|*|Editor files (*.py)|*.py' open_dlg = wx.FileDialog(self, message=_('Choose a file'), defaultDir=HOME_PATH, defaultFile='', wildcard=wcd, style=wx.OPEN | wx.CHANGE_DIR) if open_dlg.ShowModal() == wx.ID_OK: filename = open_dlg.GetPath() ### sure is python file if filename.endswith('.py'): ### Insert item in list basename, ext = os.path.splitext(os.path.basename(filename)) root = os.path.dirname(filename) self.CheckList.InsertItem(root, basename) ### trying to copy file in plug-in directory try: shutil.copy2(filename, PLUGINS_PATH) except Exception, info: sys.stderr.write( _('ERROR: %s copy failed!\n%s') % (os.path.basename(filemane), str(info))) else: sys.stderr.write( _('ERROR: %s is not a python file.\nOnly python file can be added as plugin.' ) % (os.path.basename(filemane))) open_dlg.Destroy()
class Preferences(wx.Toolbook): """ Based Toolbook Preference class """ def __init__(self, parent): """ Constructor. """ wx.Toolbook.__init__(self, parent, wx.NewIdRef(), style=wx.BK_DEFAULT) ### don't try to translate this labels with _() because there are used to find png L = [('General', "(self)"), ('Simulation', "(self)"), ('Editor', "(self)"), ('Plugins', "(self)")] # make an image list using the LBXX images il = wx.ImageList(25, 25) for img in [ wx.Bitmap(os.path.join(ICON_PATH, "%s_pref.png" % a[0])) for a in L ]: il.Add(img) self.AssignImageList(il) imageIdGenerator = iter(range(il.GetImageCount())) for p, label in [("%sPanel%s" % (s, str(args)), _(s)) for s, args in L]: page = eval(p) self.AddPage(page, label, imageId=next(imageIdGenerator)) ### Plug-in page setting (populate is done when page is changed) self.pluginPanel = self.GetPage(self.GetPageCount() - 1) self.CheckList = GeneralPluginsList(self.pluginPanel.GetRightPanel()) self.pluginPanel.SetPluginsList(self.CheckList) lpanel = self.pluginPanel.GetLeftPanel() ### Buttons for insert or delete plug-ins self.addBtn = wx.Button(lpanel, wx.ID_ADD, size=(140, -1)) self.delBtn = wx.Button(lpanel, wx.ID_DELETE, size=(140, -1)) self.refBtn = wx.Button(lpanel, wx.ID_REFRESH, size=(140, -1)) if wx.VERSION_STRING >= '4.0': self.addBtn.SetToolTipString = self.addBtn.SetToolTip self.delBtn.SetToolTipString = self.delBtn.SetToolTip self.refBtn.SetToolTipString = self.refBtn.SetToolTip self.addBtn.SetToolTipString(_("Add new plug-ins")) self.delBtn.SetToolTipString(_("Delete all existing plug-ins")) self.refBtn.SetToolTipString(_("Refresh plug-ins list")) ### add widget to plug-in panel self.pluginPanel.AddWidget(3, self.addBtn) self.pluginPanel.AddWidget(4, self.delBtn) self.pluginPanel.AddWidget(5, self.refBtn) ### Binding self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging) self.Bind(wx.EVT_BUTTON, self.OnAdd, id=self.addBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnDelete, id=self.delBtn.GetId()) self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=self.refBtn.GetId()) def OnPageChanged(self, event): """ """ # old = event.GetOldSelection() new = event.GetSelection() # sel = self.GetSelection() parent = self.GetTopLevelParent() ### plug-ins page if new == 3: parent.SetSize((700, 500)) else: parent.SetSize((700, 450)) event.Skip() def OnPageChanging(self, event): """ """ new = event.GetSelection() ### plug-in page if new == 3: ### list of plug-ins file in plug-in directory l = list(os.walk(PLUGINS_PATH)) ### populate checklist with file in plug-ins directory wx.CallAfter(self.CheckList.Populate, (l)) event.Skip() def OnAdd(self, event): """ Add plug-in """ wcd = 'All files (*)|*|Editor files (*.py)|*.py' open_dlg = wx.FileDialog(self, message=_('Choose a file'), defaultDir=HOME_PATH, defaultFile='', wildcard=wcd, style=wx.OPEN | wx.CHANGE_DIR) if open_dlg.ShowModal() == wx.ID_OK: filename = open_dlg.GetPath() ### sure is python file if filename.endswith(('.py', 'pyc')): ### Insert item in list basename, ext = os.path.splitext(os.path.basename(filename)) root = os.path.dirname(filename) self.CheckList.Importing(root, basename) ### trying to copy file in plug-in directory in order to find it again when the plugins list is populate (depending on the __init__.py file) try: shutil.copy2(filename, PLUGINS_PATH) except Exception as info: sys.stderr.write( _('ERROR: %s copy failed!\n%s') % (os.path.basename(filename), str(info))) else: ### rewrite the new __init__.py file that contain the new imported plugin (basename) in order to populate the future generale plugins list AddToInitFile(PLUGINS_PATH, [basename]) else: sys.stderr.write( _('ERROR: %s is not a python file.\nOnly python file can be added as plugin.' ) % (os.path.basename(filename))) open_dlg.Destroy() def OnDelete(self, event): """ Delete plugins item and python source file. """ for i in range(self.CheckList.GetItemCount()): if self.CheckList.IsSelected(i): ### Delete query dial = wx.MessageDialog( self, _('Do you want to delete the selected %s plugins?' % self.CheckList.GetItemText(i)), _('Plugin MAnager'), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) if dial.ShowModal() == wx.ID_YES: ### for selected plug-ins module = self.CheckList.GetPyData(i)[0] basename, ext = os.path.splitext( os.path.basename(module.__file__)) ### delete item self.CheckList.DeleteItem(i) ### TODO: remove also into __init__.py ### delete the selected plugin from__init__.py DelToInitFile(PLUGINS_PATH, [basename]) try: #name, ext = os.path.splitext(module.__file__) dlg = wx.MessageDialog( self, _('Do you want to remove the corresponding file %s?' ) % basename, _('Preference Manager'), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: os.remove(module.__file__) except Exception: sys.stderr.write(_('ERROR: plugin file not deleted!')) else: dlg.Destroy() else: sys.stderr.write(_('Select plugins to delete')) dial.Destroy() def OnRefresh(self, event): """ Refresh list of plugins. """ self.CheckList.Clear() l = list(os.walk(PLUGINS_PATH)) ### populate checklist with file in plug-ins directory wx.CallAfter(self.CheckList.Populate, (l)) def OnApply(self, evt): """ Apply button has been pressed and we must take into account all changes for each panel """ for page in [self.GetPage(i) for i in range(self.GetPageCount())]: page.OnApply(evt)