"""
     qtgui/plugins/AboutPlugins.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2008 University of York, CCLRC
     Copyright (C) 2009-2010 University of York

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
"""
"""
Simple Gui to print info on plugins
"""
from PyQt4 import QtCore, QtGui, QtOpenGL
import MGSimpleDialog
import MGApplication
import sys,os

def handleAboutPlugins(checked=False):
  mainwin = MGApplication.GetMainWindow()
  if not sys.argv.count('-quit'):
    mainwin.plugin_dialog = AboutPlugins(mainwin)
  label_text = mainwin.plugin_dialog.getLabelText(list(set(mainwin.plugins)))
  mainwin.plugin_dialog.label.setText(label_text)
  mainwin.plugin_dialog.show()
  mainwin.plugin_dialog.raise_()

def initializePlugin():
  mainwin = MGApplication.GetMainWindow()

  if not sys.argv.count('-quit'):
    menu_defn = {}
    menu_defn['icon'] = 'plugininfo'
    menu_defn['icon_path'] = os.path.normpath(os.path.dirname(__file__))
    menu_defn['text'] = "Plugins Information"
    menu_defn["tip"] = "Plugins Information"
    window_item = mainwin.addMenuDefinition(menu_defn['text'],menu_defn,'Tools',draw_menu=False)
    window_item.triggered.connect(handleAboutPlugins)

class AboutPlugins(MGSimpleDialog.MGSimpleDialog):

    def UnClose(self):
        mainwin = MGApplication.GetMainWindow()
        #print "The plugins ...."
        #print mainwin.plugins
        label_text = self.getLabelText(mainwin.plugins)
        self.label.setText(label_text)
        MGSimpleDialog.MGSimpleDialog.UnClose(self)

    def getPluginInfo(self,plugins_in=[]):
        #print "getPluginInfo",plugins_in
        plugins = sorted(plugins_in,key=lambda x: x.__name__.lower())

        plugin_text = ""
        for plug in plugins:
          if plug.__name__ != __name__:
	    if plug.__name__ or plug.__doc__:
               plugin_text += "<li>"
	       if plug.__name__: plugin_text += "<b>"+plug.__name__.strip()+":</b>"
	       if plug.__doc__: plugin_text += " "+plug.__doc__.strip()+"</li>\n"
               plugin_text += "</li>\n"

        return plugin_text

    def getLabelText (self,plugins_in=[]):
        #print "getLabelText",plugins_in
        label_text = "<center><b>The following plugins are available</b></center>"
        label_text += "<ul>"
        plugin_text = self.getPluginInfo(plugins_in)
        label_text += plugin_text
        label_text += "</ul>"
        return label_text

    def __init__(self,parent=None):
        MGSimpleDialog.MGSimpleDialog.__init__(self,parent)
        self.setWindowTitle(self.tr("Plugins Information"))
        layout = QtGui.QVBoxLayout(self)
        self.label = QtGui.QLabel(self)

        label_text = self.getLabelText()

        self.label.setText(label_text)
        self.label.setAlignment(QtCore.Qt.AlignLeft)
        layout.addWidget(self.label)
        self.setLayout(layout)
        self.setSizeGripEnabled(0)
        self.menu_defn['icon'] = 'plugininfo'
        self.menu_defn['icon_path'] = os.path.normpath(os.path.dirname(__file__))
        #self.CreateMenuEntry()
