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

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

"""
Module implementing the Editor General configuration page.
"""

from PyQt4.QtCore import pyqtSignature, QStringList
from PyQt4.QtGui import QListWidgetItem, QInputDialog, QLineEdit
from PyQt4.Qsci import QsciScintilla

from KdeQt import KQMessageBox

import QScintilla.Lexers

from ConfigurationPageBase import ConfigurationPageBase
from Ui_EditorFilePage import Ui_EditorFilePage

from Utilities import supportedCodecs
import Preferences

class EditorFilePage(ConfigurationPageBase, Ui_EditorFilePage):
    """
    Class implementing the Editor File configuration page.
    """
    def __init__(self):
        """
        Constructor
        """
        ConfigurationPageBase.__init__(self)
        self.setupUi(self)
        self.setObjectName("EditorFilePage")
        
        self.__showsOpenFilters = True
        self.openFileFilters = \
            QStringList(Preferences.getEditor("AdditionalOpenFilters"))
        self.saveFileFilters = \
            QStringList(Preferences.getEditor("AdditionalSaveFilters"))
        self.fileFiltersList.addItems(self.openFileFilters)
        
        self.__setDefaultFiltersLists()
        
        self.defaultEncodingComboBox.addItems(sorted(supportedCodecs))
        
        # set initial values
        self.autosaveSlider.setValue(\
            Preferences.getEditor("AutosaveInterval"))
        self.createBackupFileCheckBox.setChecked(\
            Preferences.getEditor("CreateBackupFile"))
        self.automaticSyntaxCheckCheckBox.setChecked(\
            Preferences.getEditor("AutoCheckSyntax"))
        self.defaultEncodingComboBox.setCurrentIndex(\
            self.defaultEncodingComboBox.findText(\
                Preferences.getEditor("DefaultEncoding")))
        self.advEncodingCheckBox.setChecked(\
            Preferences.getEditor("AdvancedEncodingDetection"))
        self.warnFilesizeSpinBox.setValue(\
            Preferences.getEditor("WarnFilesize"))
        self.clearBreakpointsCheckBox.setChecked(\
            Preferences.getEditor("ClearBreaksOnClose"))
        self.automaticReopenCheckBox.setChecked(\
            Preferences.getEditor("AutoReopen"))
        self.stripWhitespaceCheckBox.setChecked(\
            Preferences.getEditor("StripTrailingWhitespace"))
        self.openFilesFilterComboBox.setCurrentIndex(\
            self.openFilesFilterComboBox.findText(\
                Preferences.getEditor("DefaultOpenFilter")))
        self.saveFilesFilterComboBox.setCurrentIndex(\
            self.saveFilesFilterComboBox.findText(\
                Preferences.getEditor("DefaultSaveFilter")))
        self.automaticEolConversionCheckBox.setChecked(\
            Preferences.getEditor("AutomaticEOLConversion"))
        
        eolMode = Preferences.getEditor("EOLMode")
        if eolMode == QsciScintilla.EolWindows:
            self.crlfRadioButton.setChecked(True)
        elif eolMode == QsciScintilla.EolMac:
            self.crRadioButton.setChecked(True)
        elif eolMode == QsciScintilla.EolUnix:
            self.lfRadioButton.setChecked(True)
        
    def save(self):
        """
        Public slot to save the Editor General configuration.
        """
        Preferences.setEditor("AutosaveInterval", 
            self.autosaveSlider.value())
        Preferences.setEditor("CreateBackupFile",
            int(self.createBackupFileCheckBox.isChecked()))
        Preferences.setEditor("AutoCheckSyntax",
            int(self.automaticSyntaxCheckCheckBox.isChecked()))
        enc = unicode(self.defaultEncodingComboBox.currentText())
        if not enc:
            enc = "utf-8"
        Preferences.setEditor("DefaultEncoding", enc)
        Preferences.setEditor("AdvancedEncodingDetection", 
            int(self.advEncodingCheckBox.isChecked()))
        Preferences.setEditor("WarnFilesize",
            self.warnFilesizeSpinBox.value())
        Preferences.setEditor("ClearBreaksOnClose",
            int(self.clearBreakpointsCheckBox.isChecked()))
        Preferences.setEditor("AutoReopen",
            int(self.automaticReopenCheckBox.isChecked()))
        Preferences.setEditor("StripTrailingWhitespace", 
            int(self.stripWhitespaceCheckBox.isChecked()))
        Preferences.setEditor("DefaultOpenFilter",
            self.openFilesFilterComboBox.currentText())
        Preferences.setEditor("DefaultSaveFilter",
            self.saveFilesFilterComboBox.currentText())
        Preferences.setEditor("AutomaticEOLConversion",
            int(self.automaticEolConversionCheckBox.isChecked()))
        
        if self.crlfRadioButton.isChecked():
            Preferences.setEditor("EOLMode", QsciScintilla.EolWindows)
        elif self.crRadioButton.isChecked():
            Preferences.setEditor("EOLMode", QsciScintilla.EolMac)
        elif self.lfRadioButton.isChecked():
            Preferences.setEditor("EOLMode", QsciScintilla.EolUnix)
        
        self.__extractFileFilters()
        Preferences.setEditor("AdditionalOpenFilters", self.openFileFilters)
        Preferences.setEditor("AdditionalSaveFilters", self.saveFileFilters)
    
    def __setDefaultFiltersLists(self, keepSelection = False):
        """
        Private slot to set the default file filter combo boxes.
        
        @param keepSelection flag indicating to keep the current selection
            if possible (boolean)
        """
        if keepSelection:
            selectedOpenFilter = self.openFilesFilterComboBox.currentText()
            selectedSaveFilter = self.saveFilesFilterComboBox.currentText()
        
        openFileFiltersList = \
            QScintilla.Lexers.getOpenFileFiltersList(False, withAdditional = False) + \
            self.openFileFilters
        openFileFiltersList.sort()
        self.openFilesFilterComboBox.clear()
        self.openFilesFilterComboBox.addItems(openFileFiltersList)
        saveFileFiltersList = \
            QScintilla.Lexers.getSaveFileFiltersList(False, withAdditional = False) + \
            self.saveFileFilters
        saveFileFiltersList.sort()
        self.saveFilesFilterComboBox.clear()
        self.saveFilesFilterComboBox.addItems(saveFileFiltersList)
        
        if keepSelection:
            self.openFilesFilterComboBox.setCurrentIndex(
                self.openFilesFilterComboBox.findText(selectedOpenFilter))
            self.saveFilesFilterComboBox.setCurrentIndex(
                self.saveFilesFilterComboBox.findText(selectedSaveFilter))
    
    def __extractFileFilters(self):
        """
        Private method to extract the file filters.
        """
        filters = QStringList()
        for row in range(self.fileFiltersList.count()):
            filters.append(self.fileFiltersList.item(row).text())
        if self.__showsOpenFilters:
            self.openFileFilters = filters
        else:
            self.saveFileFilters = filters
    
    def __checkFileFilter(self, filter):
        """
        Private method to check a file filter for validity.
        
        @param filter file filter pattern to check (QString)
        @return flag indicating validity (boolean)
        """
        if not self.__showsOpenFilters and \
           filter.count("*") != 1:
            KQMessageBox.critical(self,
                self.trUtf8("Add File Filter"),
                self.trUtf8("""A Save File Filter must contain exactly one"""
                            """ wildcard pattern. Yours contains %1.""")\
                            .arg(filter.count("*")))
            return False
        
        if filter.count("*") == 0:
            KQMessageBox.critical(self,
                self.trUtf8("Add File Filter"),
                self.trUtf8("""A File Filter must contain at least one"""
                            """ wildcard pattern."""))
            return False
        
        return True
    
    @pyqtSignature("bool")
    def on_openFiltersButton_toggled(self, checked):
        """
        Private slot to switch the list of file filters.
        
        @param checked flag indicating the check state of the button (boolean)
        """
        self.__extractFileFilters()
        self.__showsOpenFilters = checked
        self.fileFiltersList.clear()
        if checked:
            self.fileFiltersList.addItems(self.openFileFilters)
        else:
            self.fileFiltersList.addItems(self.saveFileFilters)
    
    @pyqtSignature("QListWidgetItem*, QListWidgetItem*")
    def on_fileFiltersList_currentItemChanged(self, current, previous):
        """
        Private slot to set the state of the edit and delete buttons.
        
        @param current new current item (QListWidgetItem)
        @param previous previous current item (QListWidgetItem)
        """
        self.editFileFilterButton.setEnabled(current is not None)
        self.deleteFileFilterButton.setEnabled(current is not None)
    
    @pyqtSignature("")
    def on_addFileFilterButton_clicked(self):
        """
        Private slot to add a file filter to the list.
        """
        filter, ok = QInputDialog.getText(
            self,
            self.trUtf8("Add File Filter"),
            self.trUtf8("Enter the file filter entry:"),
            QLineEdit.Normal)
        if ok and filter:
            if self.__checkFileFilter(filter):
                self.fileFiltersList.addItem(filter)
                self.__extractFileFilters()
                self.__setDefaultFiltersLists(keepSelection = True)
    
    @pyqtSignature("")
    def on_editFileFilterButton_clicked(self):
        """
        Private slot called to edit a file filter entry.
        """
        filter = self.fileFiltersList.currentItem().text()
        filter, ok = QInputDialog.getText(
            self,
            self.trUtf8("Add File Filter"),
            self.trUtf8("Enter the file filter entry:"),
            QLineEdit.Normal, 
            filter)
        if ok and filter:
            if self.__checkFileFilter(filter):
                self.fileFiltersList.currentItem().setText(filter)
                self.__extractFileFilters()
                self.__setDefaultFiltersLists(keepSelection = True)
    
    @pyqtSignature("")
    def on_deleteFileFilterButton_clicked(self):
        """
        Private slot called to delete a file filter entry.
        """
        self.fileFiltersList.takeItem(self.fileFiltersList.currentRow())
        self.__extractFileFilters()
        self.__setDefaultFiltersLists(keepSelection = True)
    
def create(dlg):
    """
    Module function to create the configuration page.
    
    @param dlg reference to the configuration dialog
    """
    page = EditorFilePage()
    return page
