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

import sys

from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import *

from rdigc.utils import (dicoHasKey, getThemeIcon, windowIcon)
                          
try:
    _encoding = QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QApplication.translate(context, text, disambig)

class virtualKeyboard(QMdiSubWindow):
    def __init__(self, parent):

        super(virtualKeyboard, self).__init__()
        self.setObjectName("virtualKeyboard")
        self.setWindowTitle("Calvier tactile")
        self.parent = parent
        self.setWindowOpacity(0.75)
        self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
        self.setFixedSize(1024, 310)
        self.resize(1024, 310)

        self.setStyleSheet("QMdiSubWindow {color: black; background-color:white; border:1px; border-width: 0px;}")
        windowIcon(self, "keyboard_off.png")
        
    
    def setupUi(self):
        
        self.gridFrame = QFrame(self)
        self.gridFrame.setGeometry(10, 10, 1001, 290)
        self.gridFrame.setStyleSheet("QFrame { border: 1px solid;    border-radius: 5px;    opacity: 0.5;    background-color: white;}")
        self.gridFrame.setObjectName(("gridFrame"))
        self.gridLayout = QGridLayout(self.gridFrame)
        self.gridLayout.setSizeConstraint(QLayout.SetDefaultConstraint)
        self.gridLayout.setObjectName(("gridLayout"))
        
        self.upper = False

        self.boutonsGl1 = {"delete" : ["Effacer", (0, 13, 1, 1), None], "b13" : ["+", (0, 12, 1, 1), None], "b12" : ["_",(0, 11, 1, 1), None],
                        "b21" : ["6", (1, 6, 1, 1), None], "backspace" : ["Retour Arr.", (1, 13, 1, 1), None], "rshift" : ["Shift",(4, 12, 1, 2), None],
                        "b32" : ["E", (2, 3, 1, 1), None], "b39" : ["P", (2, 10, 1, 1), None], "b18" : ["3",(1, 3, 1, 1), None],
                        "b27" : ["=", (1, 12, 1, 1), None], "b23" : ["8", (1, 8, 1, 1), None], "b20" : ["5",(1, 5, 1, 1), None],
                        "b33" : ["R", (2, 4, 1, 1), None], "b36" : ["U", (2, 7, 1, 1), None], "b15" : ["`",(1, 0, 1, 1), None],
                        "b37" : ["I", (2, 8, 1, 1), None], "b34" : ["T", (2, 5, 1, 1), None], "b41" : ["]",(2, 12, 1, 1), None],
                        "b19" : ["4", (1, 4, 1, 1), None], "tab" : ["Tab", (2, 0, 1, 1), None], "b30" : ["Q",(3, 1, 1, 1), None],
                        "b17" : ["2", (1, 2, 1, 1), None], "b16" : ["1", (1, 1, 1, 1), None], 
                        "b38" : ["O", (2, 9, 1, 1), None], "b22" : ["7", (1, 7, 1, 1), None], "b26" : ["-",(1, 11, 1, 1), None],
                        "b31" : ["W", (4, 2, 1, 1), None], "b64" : [",", (4, 9, 1, 1), None], "b61" : ["B",(4, 6, 1, 1), None],                        
                        "b58" : ["X", (4, 3, 1, 1), None], "b54" : ["\'", (3, 11, 1, 1), None], "b50" : ["J",(3, 7, 1, 1), None],
                        "b40" : ["[", (2, 11, 1, 1), None], "b51" : ["K", (3, 8, 1, 1), None], 
                        "b62" : ["N", (4, 7, 1, 1), None],
                        "b24" : ["9", (1, 9, 1, 1), None], "b45" : ["S",(3, 2, 1, 1), None],
                        "b46" : ["D", (3, 3, 1, 1), None], "b47" : ["F", (3, 4, 1, 1), None], "b59" : ["C",(4, 4, 1, 1), None],
                        "b57" : ["Z", (2, 2, 1, 1), None], "b25" : ["0", (1, 10, 1, 1), None], "b48" : ["G",(3, 5, 1, 1), None],
                        "b42" : ["\\", (2, 13, 1, 1), None], "b66" : ["/", (4, 11, 1, 1), None], "b65" : [".",(4, 10, 1, 1), None],
                        "b44" : ["A", (2, 1, 1, 1), None], "b49" : ["H", (3, 6, 1, 1), None], "b53" : [";",(3, 10, 1, 1), None],
                        "b52" : ["L", (3, 9, 1, 1), None], "b63" : ["M",(4, 8, 1, 1), None],
                        "b35" : ["Y", (2, 6, 1, 1), None], "b1" : ["~", (0, 0, 1, 1), None], "b9" : ["*",(0, 8, 1, 1), None],
                        "b11" : [")", (0, 10, 1, 1), None], "b4" : ["#", (0, 3, 1, 1), None], "b5" : ["$",(0, 4, 1, 1), None],
                        "b8" : ["&&", (0, 7, 1, 1), None], "b2" : ["!", (0, 1, 1, 1), None], 
                        "b6" : ["%", (0, 5, 1, 1), None], "lshift" : ["Shift", (4, 0, 1, 2), None], "b60" : ["V",(4, 5, 1, 1), None],
                        "space" : ["Espace", (5, 4, 1, 6), None], "b7" : ["^", (0, 6, 1, 1), None],
                        "b3" : ["@",(0, 2, 1, 1), None], "b10" : ["(", (0, 9, 1, 1), None]                         
                        }    

        for key in self.boutonsGl1.keys():
            
            button = QPushButton(self.gridFrame)
            button.setObjectName(key)
            button.setStyleSheet("QPushButton {border: 1px solid #000000;  font-size: 12px; border-radius: 6px;  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,  stop: 0 #f6f7fa, stop: 1 #dadbde); height: 30px; min-width: 60px;  opacity: 1.05;}"
                                 "QPushButton:hover {color : white; font-size: 14px; text-decoration: dashed underline overline; font-weight: bold; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #186dc1, stop: 1 #39eafd);  opacity: 1.0;}")
 
            button.setToolTip(self.boutonsGl1[key][0])
               
            setattr(self, key, button)
            position = self.boutonsGl1[key][1]                                
            self.gridLayout.addWidget(button, position[0],  position[1],  position[2],  position[3])

            button.clicked.connect(self.keyPress)
        
        self.line = QFrame(self)
        self.line.setGeometry(QRect(27, 10, 971, 20))
        self.line.setFrameShape(QFrame.HLine)
        self.line.setFrameShadow(QFrame.Sunken)
        self.line.setObjectName("line")

        self.retranslateUi()
        QMetaObject.connectSlotsByName(self)
        

    def retranslateUi(self):
        self.setWindowTitle(_translate("self", "Clavier tactile", None))

        for child in self.gridFrame.children() :
            if type(child) == QPushButton :
               child.setText(_translate("self", child.toolTip(), None))

        self.lshift.setText("%s [%s]" % (self.lshift.toolTip(), "Min"))
        self.rshift.setText("%s [%s]" % (self.rshift.toolTip(), "Min"))



    def keyPress(self):
        objectName = self.sender().objectName()
        
        if not self.parent.widgetToolBarIdentifyData :
           if not self.parent.widgetToolBarIdentify : self.parent.getToolBarIdentify()
           self.parent.getToolBarIdentifyData();

        if self.parent.widgetToolBarIdentifyData :
           if not self.parent.widgetToolBarIdentifyData.isVisible() : self.parent.widgetToolBarIdentifyData.show();           

        toolTip = self.sender().toolTip()
        
        if objectName in ("lshift", "rshift") :
            self.upper = not self.upper
            button = self.findChild(QPushButton, "lshift")              
            button.setText("%s [%s]" % (toolTip, "Maj" if self.upper else "Min"))
            button = self.findChild(QPushButton, "rshift")              
            button.setText("%s [%s]" % (toolTip, "Maj" if self.upper else "Min"))                            
            return

        
        if toolTip == "&&" : toolTip = "&"
          
        if self.sender().objectName() == "backspace" :  self.parent.textForDataSearch.setText(self.parent.textForDataSearch.text()[:-1])
        elif self.sender().objectName() == "delete" :  self.parent.textForDataSearch.setText("")
        elif self.sender().objectName() == "space" : self.parent.textForDataSearch.setText("%s " % self.parent.textForDataSearch.text())
        elif self.sender().objectName() == "tab" : self.parent.textForDataSearch.setText("%s\t" % self.parent.textForDataSearch.text())
        else :
            toolTip = toolTip.lower() if not self.upper else toolTip.upper()
            self.parent.textForDataSearch.setText("%s%s" % (self.parent.textForDataSearch.text(), toolTip))
        

    def closeEvent(self, event):
        self.parent.buttonVirtualKeyboard.setIcon(QIcon(getThemeIcon("keyboard_off.png")))
         

