Exemplo n.º 1
0
    def __init__(self, preferences, plugin_dir, *args, **kwargs):

        wx.Panel.__init__(self, *args, **kwargs)
        self.preferences = preferences
        self.plugin_dir = plugin_dir
        self.cfgparser = PluginConfigParser()
        self.pluginList = []
        self.InitUI()
        self.InitPlugins()
Exemplo n.º 2
0
class PluginPanel(wx.Panel):

    def __init__(self, preferences, plugin_dir, *args, **kwargs):

        wx.Panel.__init__(self, *args, **kwargs)
        self.preferences = preferences
        self.plugin_dir = plugin_dir
        self.cfgparser = PluginConfigParser()
        self.pluginList = []
        self.InitUI()
        self.InitPlugins()


    def InitUI(self):
        self._title = wx.StaticText(self, -1, 'Plugins:')
        if self.preferences:
            self._title.SetForegroundColour(self.preferences.GetValue('M_FG_COLOUR'))
            font = self.preferences.GetValue('M_FONT')
            font.SetWeight( wx.BOLD )
            self._title.SetFont(font)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self._title, proportion=0, flag=wx.ALL, border=4)
        self.SetSizer(self.sizer)
        if self.preferences:
            self.SetBackgroundColour(self.preferences.GetValue('M_BG_COLOUR') )


    def InitPluginDirectory(self):
        if os.path.exists(self.plugin_dir):
            return True
        try:
            os.makedirs(self.plugin_dir)
        except OSError as e:
            return False
        else:
            return True


    def InitPlugins(self):
        self.InitPluginDirectory()
        cfgfiles = [os.path.join(self.plugin_dir, d, "plugin.cfg") for d in os.listdir(self.plugin_dir)]
        for cfgfile in cfgfiles:
            try:
                self.AppendPlugin(cfgfile)
            except Exception as e:
                wx.MessageBox("%s" % e,
                              "Plugin Error",
                              wx.ICON_ERROR)


    def AppendPlugin(self, cfgfile):
        plugin = self.CreatePlugin(cfgfile)
        if plugin:
            self.pluginList.append(os.path.dirname(cfgfile))
            self.sizer.Add(plugin, flag=wx.ALL|wx.ALIGN_CENTER, border=4)
            self.Layout()


    def CopyPlugin(self, src, dst):
        shutil.copytree(src, dst)


    def CreatePlugin(self, cfgfile):
        config_data = self.cfgparser.read(cfgfile)
        if not config_data:
            return
        config = config_data[0]
        return PluginBitmapButton(parent=self,
                                  **config)


    def RemovePlugin(self, plugin_dir):
        shutil.rmtree(plugin_dir)
        index = self.pluginList.index(plugin_dir)
        self.pluginList.remove(plugin_dir)
        self.sizer.Remove(index+1)
        self.Layout()


    def Import(self):
        """ Open the plugin import frame.
            Return:      none
            Exceptions:  none
        """
        dlg = ImportPluginDialog(self)
        if not dlg.ShowModal() == wx.ID_OK:
            dlg.Destroy()
            return
        src = dlg.GetPluginDir()
        dst = os.path.join(self.plugin_dir, os.path.basename(src))
        if os.path.exists(dst):
            self.RemovePlugin(dst)
        self.CopyPlugin(src, dst)
        cfgfile = os.path.join(dst, 'plugin.cfg')
        try:
            self.AppendPlugin(cfgfile)
        except Exception as e:
            wx.MessageBox("%s" % e,
                          "Plugin Error",
                          wx.ICON_ERROR)


    def SetPrefs(self, prefs):
        """
        Fix new preferences.
        """
        self._prefsIO = prefs
        self.SetBackgroundColour( self._prefsIO.GetValue('M_BG_COLOUR') )
        self.SetForegroundColour( self._prefsIO.GetValue('M_FG_COLOUR') )
        self.SetFont( self._prefsIO.GetValue('M_FONT') )

        font = self._prefsIO.GetValue('M_FONT')
        font.SetWeight( wx.BOLD )
        self._title.SetFont( font )
        self._title.SetForegroundColour( self._prefsIO.GetValue('M_FG_COLOUR') )