# -*- coding: utf-8 -*-

# Copyright (c) 2003 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Package implementing interfaces to source code documentation tools.

It implements the high level interface and all the supporting dialogs
for the supported source code documentors.
"""

import sys
import os.path
import shutil
import copy

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import Utilities
from EricdocConfigDialog import EricdocConfigDialog
from EricapiConfigDialog import EricapiConfigDialog
from DocGeneratorExecDialog import DocGeneratorExecDialog

from E4Gui.E4Action import E4Action

class DocumentationTools(QObject):
    """ 
    Class implementing the high level interface.
    """
    def __init__(self, project, parent):
        """
        Constructor
        
        @param project project object of the current project
        @param parent parent object of this class (QObject)
        """
        QObject.__init__(self, parent)
        
        self.project = project
        
    def initActions(self):
        """
        Public method to initialize the API documentation actions.
        """
        self.actions = []
        
        self.ericapiAct = E4Action(self.trUtf8('Generate API file (eric4-api)'),
            self.trUtf8('Generate &API file (eric4-api)'),
            0, 0, self, 'doc_eric4_api')
        self.ericapiAct.setStatusTip(\
            self.trUtf8('Generate an API file using eric4-api'))
        self.ericapiAct.setWhatsThis(self.trUtf8(
            """<b>Generate API file</b>"""
            """<p>Generate an API file using eric4-api.</p>"""
        ))
        self.ericapiAct.connectIt(SIGNAL('triggered()'), self.__doEricapi)
        self.actions.append(self.ericapiAct)
        
        self.ericdocAct = E4Action(self.trUtf8('Generate documentation (eric4-doc)'),
            self.trUtf8('Generate &documentation (eric4-doc)'),
            0, 0, self, 'doc_eric4_doc')
        self.ericdocAct.setStatusTip(\
            self.trUtf8('Generate API documentation using eric4-doc'))
        self.ericdocAct.setWhatsThis(self.trUtf8(
            """<b>Generate documentation</b>"""
            """<p>Generate API documentation using eric4-doc.</p>"""
        ))
        self.ericdocAct.connectIt(SIGNAL('triggered()'),self.__doEricdoc)
        self.actions.append(self.ericdocAct)
        
    def initMenu(self):
        """
        Public method called to build the project API documentation submenu.
        
        @return the menu or None
        """
        menu = QMenu(self.parent())
        menu.setTearOffEnabled(True)
        
        self.ericdocAct.addTo(menu)
        self.ericapiAct.addTo(menu)
        
        return menu
        
    def __doEricdoc(self):
        """
        Private slot to perform the eric4-doc api documentation generation.
        """
        if len(self.project.pdata['ERIC4DOCPARMS']):
            parms = copy.deepcopy(self.project.pdata['ERIC4DOCPARMS'][0])
        else:
            parms = None
        dlg = EricdocConfigDialog(self.project.ppath, parms)
        if dlg.exec_() == QDialog.Accepted:
            args, parms = dlg.generateParameters()
            
            # test for changes of selected parameters and save the in the project
            # 1. there were none, now there are
            if len(self.project.pdata['ERIC4DOCPARMS']) == 0 and len(parms) > 0:
                self.project.pdata['ERIC4DOCPARMS'] = [copy.deepcopy(parms)]
                self.project.setDirty(1)
            # 2. there were some, now there aren't
            elif len(self.project.pdata['ERIC4DOCPARMS']) > 0 and len(parms) == 0:
                self.project.pdata['ERIC4DOCPARMS'] = []
                self.project.setDirty(1)
            # 3. there were some and still are
            else:
                if parms != self.project.pdata['ERIC4DOCPARMS'][0]:
                    self.project.pdata['ERIC4DOCPARMS'] = [copy.deepcopy(parms)]
                    self.project.setDirty(1)
                    
            # now do the call
            dia = DocGeneratorExecDialog("Ericdoc")
            res = dia.start(args, self.project.ppath)
            if res:
                dia.exec_()
                
            outdir = parms['outputDirectory']
            if outdir == '':
                outdir = 'doc'      # that is eric4-docs default output dir
                
            # add it to the project data, if it isn't in already
            outdir = outdir.replace(self.project.ppath+os.sep, '')
            if outdir not in self.project.pdata['OTHERS']:
                self.project.pdata['OTHERS'].append(outdir)
                self.project.setDirty(1)
                self.project.othersAdded(outdir)
        
    def __doEricapi(self):
        """
        Private slot to perform the eric4-api api generation.
        """
        if len(self.project.pdata['ERIC4APIPARMS']):
            parms = copy.deepcopy(self.project.pdata['ERIC4APIPARMS'][0])
        else:
            parms = None
        dlg = EricapiConfigDialog(self.project.ppath, parms)
        if dlg.exec_() == QDialog.Accepted:
            args, parms = dlg.generateParameters()
            
            # test for changes of selected parameters and save the in the project
            # 1. there were none, now there are
            if len(self.project.pdata['ERIC4APIPARMS']) == 0 and len(parms) > 0:
                self.project.pdata['ERIC4APIPARMS'] = [copy.deepcopy(parms)]
                self.project.setDirty(1)
            # 2. there were some, now there aren't
            elif len(self.project.pdata['ERIC4APIPARMS']) > 0 and len(parms) == 0:
                self.project.pdata['ERIC4APIPARMS'] = []
                self.project.setDirty(1)
            # 3. there were some and still are
            else:
                if parms != self.project.pdata['ERIC4APIPARMS'][0]:
                    self.project.pdata['ERIC4APIPARMS'] = [copy.deepcopy(parms)]
                    self.project.setDirty(1)
                    
            # now do the call
            dia = DocGeneratorExecDialog("Ericapi")
            res = dia.start(args, self.project.ppath)
            if res:
                dia.exec_()
                
            outfile = parms['outputFile']
                
            # add it to the project data, if it isn't in already
            outfile = outfile.replace(self.project.ppath+os.sep, '')
            if outfile not in self.project.pdata['OTHERS']:
                self.project.pdata['OTHERS'].append(outfile)
                self.project.setDirty(1)
                self.project.othersAdded(outfile)
        
    def getActions(self):
        """
        Public method to get a list of all actions.
        
        @return list of all actions (list of E4Action)
        """
        return self.actions[:]
