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


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

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

    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 __init__(self, parent=None):
        super().__init__(parent)
        self.editor = None

    def createEditor(self, parent, option, index):
        self.view = parent.parent()
        self.editor = CellEditor(parent)
        return self.editor

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

    def setModelData(self, editor, model, index):
        value = editor.text()
        model.setData(index, value, QtCore.Qt.EditRole)

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

    def eventFilter(self, target, event):
        # For some reason, this suppresses opening up next cell for editing
        # when Tab is pressed.  Don't know why ...
        return False


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

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

        self.style = '''
            font-family: "Candara";
            font-size: 11pt;
            font-style: italic;
            font-weight: bold;
        '''
        self.setStyleSheet(self.style + 'color: rgb(224, 22, 49);')

        self.textChanged.connect(self.text_changed)

        self.label = QtWidgets.QLabel(parent=self)
        self.label.setWindowFlags(QtCore.Qt.Widget | QtCore.Qt.FramelessWindowHint)
        self.label.setFocusPolicy(QtCore.Qt.NoFocus)
        self.label.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)


    def text_changed(self, text):
        w = self.label
        s = 'abcdefghijkl'

        # Write user input portion to editor widget
        user = s[:len(text)]
        self.setText(user)

        # Write autocompletion portion to display widget
        auto = s[len(text):]
        w.setMinimumSize(
            w.fontMetrics().boundingRect(auto).width(),
            self.geometry().height()
        )
        w.adjustSize()
        w.setStyleSheet('color: rgb(30, 98, 206);')
        w.setText(auto)

        # Position display widget
        w.move(self.fontMetrics().horizontalAdvance(user), 0)
        w.show()



# ------------------------------------------------------------------------------
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_())
