summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets/shortcuteditor/shortcuteditordelegate.cpp
blob: a8b32bc06ab632ca0356be52d282d00d9d537857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2022 Laszlo Papp <lpapp@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "shortcuteditordelegate.h"

#include <QAbstractItemModel>
#include <QKeySequenceEdit>

//! [0]
ShortcutEditorDelegate::ShortcutEditorDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
//! [0]

//! [1]
QWidget *ShortcutEditorDelegate::createEditor(QWidget *parent,
                                              const QStyleOptionViewItem &/*option*/,
                                              const QModelIndex &/*index*/) const
{
    QKeySequenceEdit *editor = new QKeySequenceEdit(parent);
    connect(editor, &QKeySequenceEdit::editingFinished, this, &ShortcutEditorDelegate::commitAndCloseEditor);
    return editor;
}
//! [1]

//! [2]
void ShortcutEditorDelegate::commitAndCloseEditor()
{
    QKeySequenceEdit *editor = static_cast<QKeySequenceEdit *>(sender());
    Q_EMIT commitData(editor);
    Q_EMIT closeEditor(editor);
}
//! [2]

//! [3]
void ShortcutEditorDelegate::setEditorData(QWidget *editor,
                                           const QModelIndex &index) const
{
    if (!editor || !index.isValid())
        return;

    QString value = index.model()->data(index, Qt::EditRole).toString();

    QKeySequenceEdit *keySequenceEdit = static_cast<QKeySequenceEdit *>(editor);
    keySequenceEdit->setKeySequence(value);
}
//! [3]

//! [4]
void ShortcutEditorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                          const QModelIndex &index) const
{
    if (!editor || !model || !index.isValid())
        return;

    const QKeySequenceEdit *keySequenceEdit = static_cast<QKeySequenceEdit *>(editor);
    const QKeySequence keySequence = keySequenceEdit->keySequence();
    QString keySequenceString = keySequence.toString(QKeySequence::NativeText);
    model->setData(index, keySequenceString);
}
//! [4]

//! [5]
void ShortcutEditorDelegate::updateEditorGeometry(QWidget *editor,
                                                  const QStyleOptionViewItem &option,
                                                  const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect);
}
//! [5]