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

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

"""
Module implementing the project browser part of the eric4 UI.
"""

import os
import sys

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

from UI.Browser import Browser
from E4Gui.E4TabWidget import E4TabWidget

from ProjectSourcesBrowser import ProjectSourcesBrowser
from ProjectFormsBrowser import ProjectFormsBrowser
from ProjectTranslationsBrowser import ProjectTranslationsBrowser
from ProjectResourcesBrowser import ProjectResourcesBrowser
from ProjectInterfacesBrowser import ProjectInterfacesBrowser
from ProjectOthersBrowser import ProjectOthersBrowser

import UI.PixmapCache
import Preferences

class ProjectBrowser(E4TabWidget):
    """
    Class implementing the project browser part of the eric4 UI.
    
    It generates a widget with four tabs. The individual tabs contain
    the project sources browser, the project forms browser,
    the project translations browser and a browser for stuff
    that doesn't fit these categories. Optionally it contains a fifth
    tab with the file system browser.
    """
    def __init__(self, project, qtdir, qt4dir, parent = None, embeddedBrowser = True):
        """
        Constructor
        
        @param project reference to the project object
        @param qtdir Qt installation directory (string)
        @param parent parent widget (QWidget)
        @param embeddedBrowser flag indicating whether the file browser should
                be included. This flag is set to False by those layouts, that
                have the file browser in a separate window or embedded
                in the debeug browser instead
        """
        E4TabWidget.__init__(self, parent)
        self.project = project
        
        self.setWindowIcon(UI.PixmapCache.getIcon("eric.png"))
        
        # add the sources browser
        self.psBrowser = ProjectSourcesBrowser(self.project)
        index = self.addTab(self.psBrowser,
            UI.PixmapCache.getIcon("projectSources.png"), '')
        self.setTabToolTip(index, self.psBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.psBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.psBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.psBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.psBrowser._initMenusAndVcs)
        
        # add the forms browser
        self.pfBrowser = ProjectFormsBrowser(self.project, qtdir, qt4dir)
        index = self.addTab(self.pfBrowser, 
            UI.PixmapCache.getIcon("projectForms.png"), '')
        self.setTabToolTip(index, self.pfBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.pfBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.pfBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.pfBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.pfBrowser._initMenusAndVcs)
        
        # add the resources browser
        self.prBrowser = ProjectResourcesBrowser(self.project, qt4dir)
        index = self.addTab(self.prBrowser, 
            UI.PixmapCache.getIcon("projectResources.png"), '')
        self.setTabToolTip(index, self.prBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.prBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.prBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.prBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.prBrowser._initMenusAndVcs)
        
        # add the translations browser
        self.ptBrowser = ProjectTranslationsBrowser(self.project, qtdir, qt4dir)
        index = self.addTab(self.ptBrowser, 
            UI.PixmapCache.getIcon("projectTranslations.png"), '')
        self.setTabToolTip(index, self.ptBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.ptBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.ptBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.ptBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.ptBrowser._initMenusAndVcs)
        
        # add the interfaces (IDL)  browser
        self.piBrowser = ProjectInterfacesBrowser(self.project)
        index = self.addTab(self.piBrowser, 
            UI.PixmapCache.getIcon("projectInterfaces.png"), '')
        self.setTabToolTip(index, self.piBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.piBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.piBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.piBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.piBrowser._initMenusAndVcs)
        
        # add the others browser
        self.poBrowser = ProjectOthersBrowser(self.project)
        index = self.addTab(self.poBrowser, 
            UI.PixmapCache.getIcon("projectOthers.png"), '')
        self.setTabToolTip(index, self.poBrowser.windowTitle())
        self.connect(self.project, SIGNAL('projectClosed'),
                self.poBrowser._projectClosed)
        self.connect(self.project, SIGNAL('projectOpened'),
                self.poBrowser._projectOpened)
        self.connect(self.project, SIGNAL('newProject'),
                self.poBrowser._newProject)
        self.connect(self.project, SIGNAL('reinitVCS'),
                self.poBrowser._initMenusAndVcs)
        
        # add the file browser, if it should be embedded here
        self.embeddedBrowser = embeddedBrowser
        if embeddedBrowser:
            self.fileBrowser = Browser()
            index = self.addTab(self.fileBrowser, 
                UI.PixmapCache.getIcon("browser.png"), '')
            self.setTabToolTip(index, self.fileBrowser.windowTitle())
        
        # add signal connection to ourself
        self.connect(self.project, SIGNAL('projectOpened'),
                self.__projectOpened)
        self.connect(self.project, SIGNAL('projectClosed'),
                self.__projectClosed)
        self.connect(self.project, SIGNAL('newProject'),
                self.__newProject)
        self.connect(self.project, SIGNAL('projectPropertiesChanged'),
                self.__projectPropertiesChanged)
        self.connect(self, SIGNAL("currentChanged(int)"), self.__currentChanged)
        
        if self.embeddedBrowser:
            self.setCurrentWidget(self.fileBrowser)
        else:
            self.setCurrentWidget(self.psBrowser)
        self.__projectPropertiesChanged()
        
    def showEvent(self, evt):
        """
        Protected method handleing the show event.
        
        @param evt show event to handle (QShowEvent)
        """
        E4TabWidget.showEvent(self, evt)
        if self.embeddedBrowser:
            self.fileBrowser.layoutDisplay()
        
    def __currentChanged(self, index):
        """
        Private slot to handle the currentChanged(int) signal.
        """
        browser = self.widget(index)
        browser.layoutDisplay()
        
    def __projectOpened(self):
        """
        Private slot to handle the projectOpened signal.
        """
        self.setCurrentWidget(self.psBrowser)
        self.__projectPropertiesChanged()
        
    def __projectClosed(self):
        """
        Private slot to handle the projectClosed signal.
        """
        if self.embeddedBrowser:
            self.setCurrentWidget(self.fileBrowser)
        else:
            self.setCurrentWidget(self.psBrowser)
        self.__projectPropertiesChanged()
        self.__setSourcesIcon()
        
    def __newProject(self):
        """
        Private slot to handle the newProject signal.
        """
        self.setCurrentWidget(self.psBrowser)
        self.__projectPropertiesChanged()
        
    def __projectPropertiesChanged(self):
        """
        Private slot to handle the projectPropertiesChanged signal.
        """
        self.__setBrowserTabEnabled(self.psBrowser,
            self.project.isOpen())
        self.__setBrowserTabEnabled(self.pfBrowser,
            self.project.isOpen() and \
            self.project.pdata["UITYPE"][0] in ["Qt", "Qt4", "Kde"])
        self.__setBrowserTabEnabled(self.prBrowser,
            self.project.isOpen() and \
            self.project.pdata["UITYPE"][0] in ["Qt4", "Qt4C"])
        self.__setBrowserTabEnabled(self.ptBrowser,
            self.project.isOpen() and \
            self.project.pdata["UITYPE"][0] in ["Qt", "Qt4", "Qt4C"])
        self.__setBrowserTabEnabled(self.piBrowser,
            self.project.isOpen())
        self.__setBrowserTabEnabled(self.poBrowser,
            self.project.isOpen())
        
        self.__setSourcesIcon()
        
    def __setBrowserTabEnabled(self, browser, enabled):
        """
        Private method to set the enabled status of a browser tab.
        
        @param browser reference to the browser object (ProjectBaseBrowser)
        @param enabled flag indicating the enabled status (boolean)
        """
        self.setTabEnabled(self.indexOf(browser), enabled)
        
    def __setSourcesIcon(self):
        """
        Private method to set the right icon for the sources browser tab.
        """
        if not self.project.isOpen():
            icon = UI.PixmapCache.getIcon("projectSources.png")
        else:
            if self.project.pdata["PROGLANGUAGE"][0] == "Python":
                if self.project.pdata["MIXEDLANGUAGE"][0]:
                    icon = UI.PixmapCache.getIcon("projectSourcesPyMixed.png")
                else:
                    icon = UI.PixmapCache.getIcon("projectSourcesPy.png")
            elif self.project.pdata["PROGLANGUAGE"][0] == "Ruby":
                if self.project.pdata["MIXEDLANGUAGE"][0]:
                    icon = UI.PixmapCache.getIcon("projectSourcesRbMixed.png")
                else:
                    icon = UI.PixmapCache.getIcon("projectSourcesRb.png")
            else:
                icon = UI.PixmapCache.getIcon("projectSources.png")
        self.setTabIcon(self.indexOf(self.psBrowser), icon)
        
    def handleEditorChanged(self, fn):
        """
        Public slot to handle the editorChanged signal.
        
        @param fn The filename of the saved files. (string or QString)
        """
        if self.project.isProjectSource(fn):
            self.psBrowser.selectFile(fn)
        elif self.project.isProjectForm(fn):
            self.pfBrowser.selectFile(fn)
        elif self.project.isProjectInterface(fn):
            self.piBrowser.selectFile(fn)
    
    def getProjectBrowsers(self):
        """
        Public method to get references to the individual project browsers.
        
        @return list of references to project browsers
        """
        return [self.psBrowser, self.pfBrowser, self.ptBrowser,
                self.piBrowser, self.poBrowser]
