
MENU_ITEM = "&Plugin Manager"


class KellerPluginManagerGUI(KellerNukePlugin):

    def configurePlugin(self):
        menubar = nuke.menu ('Nuke')
        self.m = menubar.addMenu("Edit")
        self.m.addCommand(MENU_ITEM, 'KellerPluginManagerGUI.showManager();')
        
        
      
    def unconfigurePlugin(self):
        self.m.removeItem(MENU_ITEM)



MENU_ITEM = "&Plugin Manager"



MENU_ITEM = "&Plugin Manager"



   
MENU_ITEM = "&Plugin Manager"


try:
            
    class ShapePanel(nukescripts.PythonPanel):
        def __init__(self):
            
            
            nukescripts.PythonPanel.__init__(self, 'Plugin Manager')
            
            self.setMinimumSize(800, 400)
            
            KPM = KellerPluginManager
            
            plugins =  KPM.getPlugins()
    
            for plugin in plugins:
                
                k = nuke.PyScript_Knob("Reload")
                k.setLabel("Reload")
                k.setName("Button_" + plugin.getName())
                k.setFlag(nuke.STARTLINE)
                self.addKnob(k)
                
                
                
                
                k = nuke.Boolean_Knob("")
                k.setValue(plugin.getState() == KellerNukePlugin.STATE_ACTIVE)
                k.setName("Checkbox_" + plugin.getName())
                k.setLabel("{0:<30}".format(plugin.getName()))
                k.clearFlag(nuke.STARTLINE)
                #k.setFlag(nuke.STARTLINE)
                
                self.addKnob(k)
                
                k = nuke.String_Knob("Name","")
                k.setValue(plugin.getModule().__file__)
                k.clearFlag(nuke.STARTLINE)
                self.addKnob(k)
    
                #k = nuke.PyScript_Knob("Reload","Reload","reload(\"%s\")"%plugin.getName())
                
            
            
            

            #self.addSeparator()
            k = nuke.PyScript_Knob("Reload All")
            k.setLabel("Reload All")
            k.setName("Button_ReloadAll")
            self.addKnob(k)
            k.setFlag(nuke.STARTLINE)
            
        def knobChanged(self, k):
            
            if type(k) == nuke.PyScript_Knob:
                
                if k.name() == "Button_ReloadAll":
                    reloadAll()
                    
                else:
                
                    pluginName = k.name().split("_")[1]
                    plugin = KellerPluginManager.getPluginByName(pluginName)
                    
                    plugin = KellerPluginManager.reloadPlugin(plugin)
                    
                    kcheck = self.knobs()["Checkbox_%s" % pluginName]
                    if kcheck is not None:
                        if plugin.getState() == KellerNukePlugin.STATE_ACTIVE:
                            kcheck.setValue(1)
                        else:
                            kcheck.setValue(0)
        
    
        
            if type(k) == nuke.Boolean_Knob:
                pluginName = k.name().split("_")[1]
                stateActive = (k.getValue() == 1)
                
        
                plugin = KellerPluginManager.getPluginByName(pluginName)
                
                if plugin is not None:
        
                    if (stateActive) and (plugin.getState() == KellerNukePlugin.STATE_INACTIVE):
                        plugin.activate()
                    elif (not stateActive) and (plugin.getState() == KellerNukePlugin.STATE_ACTIVE):
                        plugin.deactivate()
except:
    nuke.debug("Cannot initiate PluginManagerGUI")
    
def reloadAll():
    
    task = nuke.ProgressTask("Reloading...")

    plugins = KellerPluginManager.getPlugins()
    i = 0
    for plugin in plugins:
        i = i + 1
        if not task.isCancelled():
            task.setMessage("%s" % plugin.getName())
            KellerPluginManager.reloadPluginByName(plugin.getName())
        
        task.setProgress(int(i * 100 / len(plugins)))
           
def showManager():
    
    p = ShapePanel()

   
    p.show()
    
    
   
   
#showManager()