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 <QLineEdit>
9using namespace KIdentityManagementWidgets;
10IdentityTreeDelegate::IdentityTreeDelegate(QObject *parent)
11 : QStyledItemDelegate{parent}
12{
13}
14
15IdentityTreeDelegate::~IdentityTreeDelegate() = default;
16
17QWidget *IdentityTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
18{
19 Q_UNUSED(index);
20 Q_UNUSED(option);
21 auto *lineEdit = new QLineEdit(parent);
22 auto *that = const_cast<IdentityTreeDelegate *>(this);
23 connect(lineEdit, &QLineEdit::editingFinished, this, [this, that]() {
24 Q_EMIT that->commitData(mLineEdit);
25 Q_EMIT that->closeEditor(mLineEdit);
26 });
27 const_cast<IdentityTreeDelegate *>(this)->mLineEdit = lineEdit;
28 return lineEdit;
29}
30
31void IdentityTreeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
32{
33 auto identityNameIndex = index.model()->index(index.row(), KIdentityManagementCore::IdentityTreeModel::DisplayIdentityNameRole);
34 qobject_cast<QLineEdit *>(editor)->setText(identityNameIndex.data().toString());
35}
36
37void IdentityTreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
38{
39 auto identityNameIndex = index.model()->index(index.row(), KIdentityManagementCore::IdentityTreeModel::DisplayIdentityNameRole);
40 model->setData(identityNameIndex, qobject_cast<QLineEdit *>(editor)->text());
41}
42
43void IdentityTreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/) const
44{
45 editor->setGeometry(option.rect);
46}
47
48#include "moc_identitytreedelegate.cpp"
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
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:55:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.