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

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

"""
Package implementing various modules for source code checks.
"""

import os
import sys
import copy

from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtGui import QMenu

from TabnannyDialog import TabnannyDialog
from SyntaxCheckerDialog import SyntaxCheckerDialog
from PyLint import PyLint

from E4Gui.E4Action import E4Action

import Utilities

class Checkers(QObject):
    """ 
    Class implementing the high level interface.
    """
    def __init__(self, projectOrBrowser, parent):
        """
        Constructor
        
        @param projectOrBrowser reference to the project or project browser object
        @param parent parent object of this class (QObject)
        """
        QObject.__init__(self, parent)
        
        self.projectOrBrowser = projectOrBrowser
        self.actions = []
        
        self.tabnannyDialog = None
        self.syntaxcheckDialog = None
        
        self.pylint = PyLint(projectOrBrowser, parent)

    def initActions(self):
        """
        Public method to initialize the Checkers actions.
        """
        self.actions = []
        
        self.checkSyntaxAct = E4Action(self.trUtf8('Check Syntax'),
                self.trUtf8('&Syntax...'),0,0,
                self,'project_check_syntax')
        self.checkSyntaxAct.setStatusTip(self.trUtf8('Check syntax.'))
        self.checkSyntaxAct.setWhatsThis(self.trUtf8(
            """<b>Check Syntax...</b>"""
            """<p>This checks all Python files for syntax errors.</p>"""
        ))
        self.checkSyntaxAct.connectIt(SIGNAL('triggered(bool)'),
            self.projectOrBrowser.handleSyntaxCheck)
        self.actions.append(self.checkSyntaxAct)
        
        self.tabnannyAct = E4Action(self.trUtf8('Check Indentations'),
                self.trUtf8('&Indentations...'),0,0,
                self,'project_check_indentations')
        self.tabnannyAct.setStatusTip(self.trUtf8('Check indentations using tabnanny.'))
        self.tabnannyAct.setWhatsThis(self.trUtf8(
            """<b>Check Indentations...</b>"""
            """<p>This checks all Python files"""
            """ for bad indentations using tabnanny.</p>"""
        ))
        self.tabnannyAct.connectIt(SIGNAL('triggered(bool)'),
            self.projectOrBrowser.handleTabnanny)
        self.actions.append(self.tabnannyAct)
        
        self.pylint.initActions()

    def initMenu(self):
        """
        Public method called to build the Checkers submenu.
        
        @return the menu or None
        """
        menu = QMenu()
        menu.setTearOffEnabled(True)
        
        self.checkSyntaxAct.addTo(menu)
        self.tabnannyAct.addTo(menu)
        
        self.pylint.initMenu(menu)
        
        # connect the aboutToShow signal
        self.connect(menu, SIGNAL('aboutToShow()'), self.__showMenu)
        
        return menu
    
    def getActions(self):
        """
        Public method to get a list of all actions.
        
        @return list of all actions (list of E4Action)
        """
        return self.actions[:] + self.pylint.getActions()

    def __showMenu(self):
        """
        Private slot to handle the aboutToShow signal of the menu.
        """
        self.pylint.checkActions()

    def closeAllWindows(self):
        """
        Public method to close all windows.
        """
        self.tabnannyDialog and self.tabnannyDialog.close()
        self.syntaxcheckDialog and self.syntaxcheckDialog.close()
        self.pylint.closeAllWindows()

    def tabnanny(self, files):
        """
        Public slot used to check the project files for bad indentations.
        
        @param files File or list of files or directory to be checked
                (string or list of strings)
        """
        self.tabnannyDialog = TabnannyDialog()
        self.tabnannyDialog.show()
        self.tabnannyDialog.start(files)
    
    def syntaxCheck(self, files):
        """
        Public slot used to check the project files for bad syntax.
        
        @param files File or list of files or directory to be checked
                (string or list of strings)
        """
        self.syntaxcheckDialog = SyntaxCheckerDialog()
        self.syntaxcheckDialog.show()
        self.syntaxcheckDialog.start(files)

    ############################################################################
    ## Public interface to PyLint
    ############################################################################
    
    def pyLint(self, project, mpName):
        """
        Public method used to perform a PyLint run.
        
        @param project reference to the Project object
        @param mpName name of module or package to be checked (string or QString)
        """
        self.pylint.pyLint(project, mpName)
