KIdentityManagement

identitytreedelegate.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "identitytreedelegate.h"
7#include "identitytreemodel.h"
8#include <KLineEditEventHandler>
9#include <QLineEdit>
10using namespace KIdentityManagementWidgets;
11IdentityTreeDelegate::IdentityTreeDelegate(QObject *parent)
12 : QStyledItemDelegate{parent}
13{
14}
15
16IdentityTreeDelegate::~IdentityTreeDelegate() = default;
17
18QWidget *IdentityTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
19{
20 Q_UNUSED(index);
21 Q_UNUSED(option);
22 auto *lineEdit = new QLineEdit(parent);
24 auto *that = const_cast<IdentityTreeDelegate *>(this);
25 connect(lineEdit, &QLineEdit::editingFinished, this, [this, that]() {
26 Q_EMIT that->commitData(mLineEdit);
27 Q_EMIT that->closeEditor(mLineEdit);
28 });
29 const_cast<IdentityTreeDelegate *>(this)->mLineEdit = lineEdit;
30 return lineEdit;
31}
32
33void IdentityTreeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
34{
35 auto identityNameIndex = index.model()->index(index.row(), KIdentityManagementCore::IdentityTreeModel::IdentityNameRole);
36 qobject_cast<QLineEdit *>(editor)->setText(identityNameIndex.data().toString());
37}
38
39void IdentityTreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
40{
41 auto identityNameIndex = index.model()->index(index.row(), KIdentityManagementCore::IdentityTreeModel::IdentityNameRole);
42 model->setData(identityNameIndex, qobject_cast<QLineEdit *>(editor)->text());
43}
44
45void IdentityTreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const
46{
47 editor->setGeometry(option.rect);
48}
49
50#include "moc_identitytreedelegate.cpp"
void catchReturnKey(QObject *lineEdit)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
void editingFinished()
const QAbstractItemModel * model() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.