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

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

"""
Module implementing the Start Program dialog.
"""

import os

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

from KdeQt import KQFileDialog

from E4Gui.E4Completers import E4DirCompleter

import Utilities

class StartDialog(QDialog):
    """
    Class implementing the Start Program dialog.
    
    It implements a dialog that is used to start an
    application for debugging. It asks the user to enter
    the commandline parameters, the working directory and
    whether exception reporting should be disabled.
    """
    def __init__(self, caption, argvList, wdList, envList, exceptions, parent = None,
        type = 0, modfuncList = None, tracePython = False, covexcList = None,
        autoClearShell = True, autoContinue = True):
        """
        Constructor
        
        @param caption the caption to be displayed (QString)
        @param argvList history list of commandline arguments (QStringList)
        @param wdList history list of working directories (QStringList)
        @param envList history list of environment settings (QStringList)
        @param exceptions exception reporting flag (boolean)
        @param parent parent widget of this dialog (QWidget)
        @param type type of the start dialog
                <ul>
                <li>0 = start debug dialog</li>
                <li>1 = start run dialog</li>
                <li>2 = start coverage dialog</li>
                <li>3 = start profile dialog</li>
                <li>4 = start cyclops dialog</li>
                </ul>
        @keyparam modfuncList history list of module functions (QStringList)
        @keyparam tracePython flag indicating if the Python library should 
            be traced as well (boolean)
        @keyparam covexcList history list of coverage exclusion patterns (QStringList)
        @keyparam autoClearShell flag indicating, that the interpreter window should
            be cleared automatically (boolean)
        @keyparam autoContinue flag indicating, that the debugger should not stop at
            the first executable line (boolean)
        """
        QDialog.__init__(self, parent)
        self.setModal(True)
        
        self.type = type
        if type == 0:
            from Ui_StartDebugDialog import Ui_StartDebugDialog
            self.ui = Ui_StartDebugDialog()
        elif type == 1:
            from Ui_StartRunDialog import Ui_StartRunDialog
            self.ui = Ui_StartRunDialog()
        elif type == 2:
            from Ui_StartCoverageDialog import Ui_StartCoverageDialog
            self.ui = Ui_StartCoverageDialog()
        elif type == 3:
            from Ui_StartProfileDialog import Ui_StartProfileDialog
            self.ui = Ui_StartProfileDialog()
        elif type == 4:
            from Ui_StartCyclopsDialog import Ui_StartCyclopsDialog
            self.ui = Ui_StartCyclopsDialog()
        self.ui.setupUi(self)
        
        self.workdirCompleter = E4DirCompleter(self.ui.workdirCombo)
        
        self.setWindowTitle(caption)
        self.ui.cmdlineCombo.clear()
        self.ui.cmdlineCombo.addItems(argvList)
        if argvList.count() > 0:
            self.ui.cmdlineCombo.setCurrentIndex(0)
        self.ui.workdirCombo.clear()
        self.ui.workdirCombo.addItems(wdList)
        if wdList.count() > 0:
            self.ui.workdirCombo.setCurrentIndex(0)
        self.ui.environmentCombo.clear()
        self.ui.environmentCombo.addItems(envList)
        self.ui.exceptionCheckBox.setChecked(exceptions)
        self.ui.clearShellCheckBox.setChecked(autoClearShell)
        
        if type == 0:        # start debug dialog
            self.ui.tracePythonCheckBox.setChecked(tracePython)
            self.ui.tracePythonCheckBox.show()
            self.ui.autoContinueCheckBox.setChecked(autoContinue)
        
        if type == 2:       # start coverage dialog
            if covexcList:
                self.ui.covexcCombo.clear()
                self.ui.covexcCombo.addItems(covexcList)
                if covexcList.count() > 0:
                    self.ui.covexcCombo.setCurrentIndex(0)
        
        if type == 3:   # start coverage or profile dialog
            self.ui.eraseCheckBox.setChecked(True)
        
        if type == 4:       # start cyclops dialog
            if modfuncList:
                self.ui.modFuncCombo.clear()
                self.ui.modFuncCombo.addItems(modfuncList)
                if modfuncList.count() > 0:
                    self.ui.modFuncCombo.setCurrentIndex(0)
            self.ui.buttonBox.button(QDialogButtonBox.Ok).setDisabled(\
                self.ui.modFuncCombo.currentText().isEmpty())
        
    @pyqtSignature("")
    def on_dirButton_clicked(self):
        """
        Private method used to open a directory selection dialog.
        """
        cwd = self.ui.workdirCombo.currentText()
        d = KQFileDialog.getExistingDirectory(\
            self,
            self.trUtf8("Working directory"),
            cwd,
            QFileDialog.Options(QFileDialog.ShowDirsOnly))
            
        if not d.isNull() and not d.isEmpty():
            self.ui.workdirCombo.setEditText(Utilities.toNativeSeparators(d))
        
    def on_modFuncCombo_editTextChanged(self):
        """
        Private slot to enable/disable the OK button.
        """
        self.ui.buttonBox.button(QDialogButtonBox.Ok).setDisabled(\
            self.ui.modFuncCombo.currentText().isEmpty())
        
    def getData(self):
        """
        Public method to retrieve the data entered into this dialog.
        
        @return a tuple of argv (QString), workdir (QString), environment (QString),
            exceptions flag (boolean) and clear interpreter flag (boolean)
        """
        cmdLine = self.ui.cmdlineCombo.currentText()
        workdir = self.ui.workdirCombo.currentText()
        environment = self.ui.environmentCombo.currentText()
        
        return (cmdLine, 
                workdir,
                environment,
                self.ui.exceptionCheckBox.isChecked(),
                self.ui.clearShellCheckBox.isChecked())
        
    def getDebugData(self):
        """
        Public method to retrieve the debug related data entered into this dialog.
        
        @return a tuple of a flag indicating, if the Python library should be traced
            as well, and a flag indicating, that the debugger should not stop at the
            first executable line (boolean)
        """
        if self.type == 0:
            return (self.ui.tracePythonCheckBox.isChecked(),
                    self.ui.autoContinueCheckBox.isChecked())
        
    def getCoverageData(self):
        """
        Public method to retrieve the coverage related data entered into this dialog.
        
        @return tuple of flag indicating erasure of coverage info (boolean) and
            coverage exclusion pattern (QString)
        """
        if self.type == 2:
            covexc = self.ui.covexcCombo.currentText()
            return (self.ui.eraseCheckBox.isChecked(), covexc)
        
    def getProfilingData(self):
        """
        Public method to retrieve the profiling related data entered into this dialog.
        
        @return flag indicating erasure of profiling info (boolean)
        """
        if self.type == 3:
            return self.ui.eraseCheckBox.isChecked()
        
    def getCyclopsData(self):
        """
        Public method to retrieve the coverage related data entered into this dialog.
        
        @return tuple of module function that is the entry point (QString) and
            bitmask of reports to be generated (integer)
        """
        if self.type == 4:
            from DebugClients.Python.Cyclops.RunCyclops import CyclopsStats, \
                CyclopsCycles, CyclopsCycleObjs, CyclopsSCCS, CyclopsArcs, CyclopsIterate
            
            mask = 0
            if self.ui.cyclopsStatsCheckBox.isChecked():
                mask += CyclopsStats
            if self.ui.cyclopsCyclesCheckBox.isChecked():
                mask += CyclopsCycles
            if self.ui.cyclopsCycleObjsCheckBox.isChecked():
                mask += CyclopsCycleObjs
            if self.ui.cyclopsSCCSCheckBox.isChecked():
                mask += CyclopsSCCS
            if self.ui.cyclopsArcsCheckBox.isChecked():
                mask += CyclopsArcs
            if self.ui.cyclopsIterateCheckBox.isChecked():
                mask += CyclopsIterate
            modFunc = self.ui.modFuncCombo.currentText()
            return (modFunc, mask)
