Пример #1
0
class EditableLabel(QLabel):
    def __init__(self, save, failCallback, *args, **kwargs):
        QLabel.__init__(self, *args, **kwargs)
        self.save = save
        self.editable = True
        self.failCallback = failCallback
        self.editor = QLineEdit(self)
        self.editor.setWindowFlags(Qt.Popup)
        self.editor.setFocusProxy(self)
        self.editor.editingFinished.connect(self.handleEditingFinished)
        self.editor.installEventFilter(self)
        self.callback = lambda text: 0

    def eventFilter(self, widget, event):
        if ((event.type() == QEvent.MouseButtonPress
             and not self.editor.geometry().contains(event.globalPos()))
                or (event.type() == QEvent.KeyPress
                    and event.key() == Qt.Key_Escape)):
            self.editor.hide()
            return True

        return QLabel.eventFilter(self, widget, event)

    def disableEdit(self):
        self.editable = False
        self.editor.hide()

    def enableEdit(self):
        self.editable = True

    def mouseDoubleClickEvent(self, event=None):
        if not self.editable:
            if self.failCallback:
                self.failCallback()

            return

        rect = self.rect()
        self.editor.setFixedSize(rect.size())
        self.editor.move(self.mapToGlobal(rect.topLeft()))
        self.editor.setText(self.text())
        self.editor.setFocus(Qt.MouseFocusReason)
        self.editor.selectAll()

        if not self.editor.isVisible():
            self.editor.show()

    def handleEditingFinished(self):
        text = self.editor.text()
        self.editor.hide()

        if self.save:
            self.setText(text)

        self.callback(text)
Пример #2
0
class Item(QWidget):

    item_finished_signal = Signal(int)
    item_commit_signal = Signal(int)
    item_tab_pressed_signal = Signal(int)

    def __init__(self, index, level=0, text=None):
        super().__init__()
        self.index = index
        self.level = level

        self.layout = QHBoxLayout()
        self.checkbox = QCheckBox()
        self.lineedit = QLineEdit()

        self.checkbox.setFocusPolicy(Qt.NoFocus)
        self.lineedit.setFocusPolicy(Qt.ClickFocus)
        self.lineedit.setText(text)
        self.lineedit.installEventFilter(self)

        self.layout.addWidget(self.checkbox)
        self.layout.addWidget(self.lineedit)
        self.setLayout(self.layout)

        self.checkbox.stateChanged.connect(self.checked_slot)
        self.lineedit.returnPressed.connect(self.return_slot)

    def checked_slot(self):
        self.item_finished_signal.emit(self.index)
        self.deleteLater()

    def return_slot(self):
        if self.lineedit.text():
            self.item_commit_signal.emit(self.index)

    def eventFilter(self, obj, event):
        if obj == self.lineedit:
            if event.type() == QEvent.KeyPress:
                if event.key() == Qt.Key_Tab:
                    self.item_tab_pressed_signal.emit(self.index)
                    return True
        return QWidget.eventFilter(self, obj, event)