from PyQt5 import QtCore, QtWidgets, QtGui
import sys
import model


# ------------------------------------------------------------------------------
class TableView(QtWidgets.QTableView):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.blocked = False

    def keyPressEvent(self, event):
        key = event.key()
        mod = int(event.modifiers())

        if key == QtCore.Qt.Key_Q and mod == QtCore.Qt.CTRL:
            self.close()
            exit()

        super().keyPressEvent(event)


# ------------------------------------------------------------------------------
class Delegate(QtWidgets.QStyledItemDelegate):

    def createEditor(self, parent, option, index):
        self.editor = EditWidget(parent)
        self.filter = EventFilter()
        self.editor.installEventFilter(self.filter)    # <--- install Event Filter
        return self.editor

    def setEditorData(self, label, index):
        model = index.model()
        v = model.data(index, QtCore.Qt.EditRole)
        model.setData(index, None, QtCore.Qt.EditRole)

    def setModelData(self, label, model, index):
        value = label.text()
        row = index.row()
        col = index.column()
        model.setData(index, value, QtCore.Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

    def eventFilter(self, target, event):

        if event.type() == QtCore.QEvent.KeyPress:
            key = event.key()
            mod = int(event.modifiers())

            # ASCII input
            if (
                key >= QtCore.Qt.Key_Space and key <= QtCore.Qt.Key_AsciiTilde and 
                (mod == QtCore.Qt.NoModifier or mod == QtCore.Qt.SHIFT)
            ):
                text = self.editor.text()
                self.editor.setText(text + event.text())
                return True

        return False


# ------------------------------------------------------------------------------
class EditWidget(QtWidgets.QLineEdit):

    def __init__(self, parent=None):
        super().__init__(parent)


class EventFilter(QtCore.QObject):    # <--- Event Filter object

    def eventFilter(self, target, event):

        if event.type() == QtCore.QEvent.FocusAboutToChange:
            print('>> <FocusAboutToChange>')
            return True
        if event.type() == QtCore.QEvent.FocusOut:
            print('>> <FocusOut>')
            return True

        return False


# ------------------------------------------------------------------------------
if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('fusion')

    rowCount = 3
    columnCount = 4
    data = [
        ['foo', 'goo', 'zoo', 'moo'],
        ['bar', 'zar', 'jar', 'gar'],
        ['qux', 'lux', 'mux', 'sux']
        ]

    view = TableView()
    view.resize(550, 160)
    model = model.TableModel(data)
    view.setModel(model)
    view.show()

    delegate = Delegate()
    view.setItemDelegate(delegate)

    sys.exit(app.exec_())
