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

"""DialogCharTables adjusted for Eric4"""


from PyQt4 import QtGui, QtCore 

from KdeQt.KQApplication import e4App
import Preferences

import chartables
from chartables_model_options import Options 
#***********************************************************************
# 
#***********************************************************************
class E4CharTablesDialog(chartables.DialogCharTables):

    def __init__(self, localeName=None):
        """
        constructor

        @param localeName localeName to select the language of the dialog or None
            to skip translation
        """
                
        flagsEx = Options.CTFEX_NOLANGUAGESELECT
        if not localeName:
            flagsEx |= Options.CTFEX_NOTRANSLATE
        chartables.DialogCharTables.__init__(
                self,
                flagsEx=flagsEx,
                localeName=localeName
                )

        
        self.__hasCurrentSelection = False
        
        # setup InsertAt button
        btInsert = self.controlById(chartables.ID_BTINSERT)
        btInsert.setVisible(True)
        btInsert.connect(
                btInsert, 
                QtCore.SIGNAL("clicked()"), 
                self._onBtInsertClicked
                )
        

    
    def getSettingsObject(self):
        """retuns the QSettings object to store settings of the dialog to"""
        return Preferences.Prefs.settings
    
    
    
    def __setEditorAvailable(self, flag):
        """slot triggered by the ViewManager when an editor becomes available

        @param flag (bool) True if an editor is available, False otherwise
        """
        btInsert = self.controlById(chartables.ID_BTINSERT)
        btInsert.setEnabled(self.__hasCurrentSelection and flag)
    
    def editorOpened(self, fn):
        """
        slot triggered by the ViewManager when an editor becomes available
        
        @param fn filename of the new editor (string or QString) (ignored)
        """
        self.__setEditorAvailable(True)
    
    def lastEditorClosed(self):
        """
        slot triggered by the ViewManager when the last editor was closed
        """
        self.__setEditorAvailable(False)
        
    def _enableSelectionButtons(self, flag):
        """protected slot to enable/disable buttons acting on the current selection"""
        chartables.DialogCharTables._enableSelectionButtons(self, flag)

        self.__hasCurrentSelection = flag
        if flag:
            if not e4App().getObject("ViewManager").activeWindow():
                flag = False
        btInsert = self.controlById(chartables.ID_BTINSERT)
        btInsert.setEnabled(flag)
        
            
    
    
    def _onActivated(self, index):
        """protected slot triggered when the user double clicks a cell in a table"""
        self._onBtInsertClicked()
        
    
    def _onBtInsertClicked(self):
        """called when the Insert button is hit"""
        
        table = self.controlById(chartables.ID_TABLE)
        selection = table.selectionModel()
        index = selection.currentIndex()
        if index.isValid():
            v = index.data()
            if v.isValid():
                text =  v.toString()
                if text:
                    editor = e4App().getObject("ViewManager").activeWindow()
                    if editor:
                        line, index = editor.getCursorPosition()
                        # It should be done on this way to allow undo
                        editor.beginUndoAction()
                        editor.insertAt(text, line, index)
                        editor.endUndoAction()
                        editor.setCursorPosition(line, index + text.length())


#***********************************************************************
#
#***********************************************************************
if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    dlg = E4CharTablesDialog()
    dlg.show()
    res = app.exec_()
    sys.exit(res)


