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

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

"""
Module implementing the highlevel wizard functions.
"""

import sys
import os
import glob

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

from KdeQt.KQApplication import e4App

# import the standard wizards
import MessageBoxWizard.MessageBoxWizard
import FileDialogWizard.FileDialogWizard
import InputDialogWizard.InputDialogWizard
import ColorDialogWizard.ColorDialogWizard
import FontDialogWizard.FontDialogWizard
import QRegExpWizard.QRegExpWizard
import PyRegExpWizard.PyRegExpWizard

from eric4config import getConfig

class Wizards(QObject):
    """
    Class implementing the highlevel wizard functions.
    """
    def __init__(self, loc, parent = None):
        """
        Constructor
        
        @param loc the localizer string as determined by the main script (string)
        """
        QObject.__init__(self, parent)
        self.registeredWizards = []
        self.registeredTranslators = []
        self.locale = loc
        self.__initialize()
       
    def __initialize(self):
        """
        Private method to register all the wizards.
        """
        self.actions = []
        
        self.wizDir = os.path.abspath(getConfig('ericWizardsDir'))
        
        wizard = MessageBoxWizard.MessageBoxWizard.MessageBoxWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        wizard = FileDialogWizard.FileDialogWizard.FileDialogWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        wizard = InputDialogWizard.InputDialogWizard.InputDialogWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        self.__addSeparator()
        
        wizard = ColorDialogWizard.ColorDialogWizard.ColorDialogWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        wizard = FontDialogWizard.FontDialogWizard.FontDialogWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        self.__addSeparator()
        
        wizard = QRegExpWizard.QRegExpWizard.QRegExpWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
        wizard = PyRegExpWizard.PyRegExpWizard.PyRegExpWizard(self)
        self.registeredWizards.append(wizard)
        self.actions.append(wizard.initAction())
        self.__loadWizardTranslator(wizard)
        
    def __loadWizardTranslator(self, wizard):
        """
        Private method to load a wizard's translator.
        
        @param wizard reference to the wizard object.
        """
        trans = wizard.getTranslationFileBaseName()
        if trans is not None:
            trans = "%s%s" % (trans, self.locale)
            qtrans = QTranslator()
            dir, trans = os.path.split('%s.%s' % (self.wizDir, trans))
            dir = dir.replace('.', os.sep)
            if qtrans.load(trans, dir):
                self.registeredTranslators.append(qtrans)
                QApplication.installTranslator(qtrans)
        
    def __addSeparator(self):
        """
        Private method to prepare a menu separator.
        """
        self.registeredWizards.append(None)
        
    def initMenu(self):
        """
        Public slot to initialize the wizards menu.
        
        @return the generated menu
        """
        menu = QMenu(self.trUtf8('Wi&zards'), self.parent())
        menu.setTearOffEnabled(True)
        
        for wizard in self.registeredWizards:
            if wizard == None:
                menu.addSeparator()
            else:
                action = wizard.getAction()
                action.addTo(menu)
            
        return menu
        
    def getCurrentEditor(self):
        """
        Public method to get a reference to the current editor.
        
        @return the current editor
        """
        return  e4App().getObject("ViewManager").activeWindow() 
        
    def getActions(self):
        """
        Public method to get a list of all actions.
        
        @return list of all actions (list of E4Action)
        """
        return self.actions[:]
