from ..Qt import QtWidgets, QtGui, QtCore


class QListWidgetSelect(QtWidgets.QListWidget):
    """
    Extension of QListWidget to change the check state of all selected QListWidgetItems with space key
    """

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

    def keyPressEvent(self, evt: QtGui.QKeyEvent):
        if evt.key() == QtCore.Qt.Key.Key_Space:
            for idx in self.selectedIndexes():
                item = self.itemFromIndex(idx)
                cs = item.checkState()
                item.setCheckState(QtCore.Qt.CheckState.Unchecked if cs == QtCore.Qt.CheckState.Checked
                                   else QtCore.Qt.CheckState.Checked)
        else:
            super().keyPressEvent(evt)
