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

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

"""
Module implementing the input dialog wizard.
"""

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import SIGNAL

from KdeQt import KQMessageBox

from Wizards.WizardIFace import WizardIFace
from InputDialogWizardDialog import InputDialogWizardDialog

from E4Gui.E4Action import E4Action

class InputDialogWizard(WizardIFace):
    """
    Class implementing the input dialog wizard.
    """
    def __init__(self, parent = None):
        """
        Constructor
        
        @param parent parent widget (QWidget)
        """
        WizardIFace.__init__(self, parent)
        
    def __callForm(self, editor):
        """
        Private method to display a dialog and get the code.
        
        @param editor reference to the current editor
        @return the generated code (string)
        """
        dlg = InputDialogWizardDialog(None)
        if dlg.exec_() == QDialog.Accepted:
            line, index = editor.getCursorPosition()
            indLevel = editor.indentation(line)/editor.indentationWidth()
            if editor.indentationsUseTabs():
                indString = '\t'
            else:
                indString = editor.indentationWidth() * ' '
            return (dlg.getCode(indLevel, indString), True)
        else:
            return (None, False)
        
    def handle(self):
        """
        Worker method associated with the wizard action 
        """
        editor = self.parent().getCurrentEditor()
        
        if editor == None:
                KQMessageBox.critical(None, 
                self.trUtf8('No current editor'),
                self.trUtf8('Please open or create a file first.'))
        else:
            code, ok = self.__callForm(editor)
            if ok:
                line, index = editor.getCursorPosition()
                # It should be done on this way to allow undo
                editor.beginUndoAction()
                editor.insertAt(code, line, index)
                editor.endUndoAction()

    def initAction(self):
        """
        Public method to create the action associated with this class wizard. 
        
        @return the associated action
        """
        self.action = E4Action(self.trUtf8('QInputDialog Wizard'),
             self.trUtf8('Q&InputDialog Wizard...'), 0, 0, self,
             'wizards_qinputdialog')
        self.action.setStatusTip(self.trUtf8('QInputDialog Wizard'))
        self.action.setWhatsThis(self.trUtf8(
            """<b>QInputDialog Wizard</b>"""
            """<p>This wizard opens a dialog for entering all the parameters"""
            """ needed to create a QInputDialog. The generated code is inserted"""
            """ at the current cursor position.</p>"""
        ))
        self.action.connectIt(SIGNAL('triggered()'), self.handle)
        return self.action
    
    def getAction(self):
        """
        Public method to get the action associated with this class wizard. 
        
        @return the associated action
        """
        return self.action
