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

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

"""
Module implementing the CVS Options Dialog for a new project from the repository.
"""

import os

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

from KdeQt import KQFileDialog

from E4Gui.E4Completers import E4DirCompleter

from Ui_CvsNewProjectOptionsDialog import Ui_CvsNewProjectOptionsDialog

import Utilities

class CvsNewProjectOptionsDialog(QDialog, Ui_CvsNewProjectOptionsDialog):
    """
    Class implementing the Options Dialog for a new project from the repository.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent parent widget (QWidget)
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        
        self.vcsDirectoryCompleter = E4DirCompleter(self.vcsDirectoryEdit)
        self.vcsProjectDirCompleter = E4DirCompleter(self.vcsProjectDirEdit)
        
        hd = Utilities.toNativeSeparators(QDir.homePath())
        hd = os.path.join(unicode(hd), 'cvsroot')
        self.vcsDirectoryEdit.setText(hd)
        
        self.cvsTagValidator = QRegExpValidator(QRegExp(r"[a-zA-Z][a-zA-Z0-9_-]*"), self)
        self.vcsTagEdit.setValidator(self.cvsTagValidator)
        
    @pyqtSignature("")
    def on_vcsDirButton_clicked(self):
        """
        Private slot to display a directory selection dialog.
        """
        directory = KQFileDialog.getExistingDirectory(\
            self,
            self.trUtf8("Select Repository Directory"),
            self.vcsDirectoryEdit.text(),
            QFileDialog.Options(QFileDialog.ShowDirsOnly))
        
        if not directory.isEmpty():
            self.vcsDirectoryEdit.setText(Utilities.toNativeSeparators(directory))
        
    @pyqtSignature("")
    def on_projectDirButton_clicked(self):
        """
        Private slot to display a directory selection dialog.
        """
        directory = KQFileDialog.getExistingDirectory(\
            self,
            self.trUtf8("Select Project Directory"),
            self.vcsProjectDirEdit.text(),
            QFileDialog.Options(QFileDialog.ShowDirsOnly))
        
        if not directory.isEmpty():
            self.vcsProjectDirEdit.setText(Utilities.toNativeSeparators(directory))
        
    def getData(self):
        """
        Public slot to retrieve the data entered into the dialog.
        
        @return a tuple of a string (project directory) and a dictionary
            containing the data entered.
        """
        vcsdatadict = {
            "repository" : unicode(self.vcsDirectoryEdit.text()),
            "archive" : unicode(self.vcsArchiveEdit.text()),
            "tag" : unicode(self.vcsTagEdit.text()),
        }
        return (unicode(self.vcsProjectDirEdit.text()), vcsdatadict)
