File indexing completed on 2024-12-22 04:12:36

0001 /*
0002  * This file is part of the KDE project
0003  * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "kis_input_editor_delegate.h"
0009 
0010 #include <QApplication>
0011 #include <QPushButton>
0012 
0013 #include <KLocalizedString>
0014 
0015 #include "input/kis_input_profile.h"
0016 #include "input/kis_shortcut_configuration.h"
0017 #include "kis_input_button.h"
0018 #include "kis_mouse_input_editor.h"
0019 #include "kis_wheel_input_editor.h"
0020 #include "kis_key_input_editor.h"
0021 #include "KisGestureSelector.h"
0022 
0023 class KisInputEditorDelegate::Private
0024 {
0025 public:
0026     Private() { }
0027 };
0028 
0029 KisInputEditorDelegate::KisInputEditorDelegate(QObject *parent)
0030     : QStyledItemDelegate(parent), d(new Private())
0031 {
0032 }
0033 
0034 KisInputEditorDelegate::~KisInputEditorDelegate()
0035 {
0036     delete d;
0037 
0038 }
0039 
0040 QWidget *KisInputEditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
0041 {
0042     QWidget *editor = 0;
0043     KisShortcutConfiguration *s = index.data(Qt::EditRole).value<KisShortcutConfiguration *>();
0044 
0045     switch (s->type()) {
0046     case KisShortcutConfiguration::KeyCombinationType:
0047         editor = new KisKeyInputEditor(parent);
0048         break;
0049 
0050     case KisShortcutConfiguration::MouseButtonType:
0051         editor = new KisMouseInputEditor(parent);
0052         break;
0053 
0054     case KisShortcutConfiguration::MouseWheelType:
0055         editor = new KisWheelInputEditor(parent);
0056         break;
0057 
0058     case KisShortcutConfiguration::GestureType:
0059         editor = new KisGestureSelector(parent);
0060         break;
0061 
0062     default:
0063         break;
0064     }
0065 
0066     return editor;
0067 }
0068 
0069 void KisInputEditorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0070 {
0071     KisShortcutConfiguration *s = index.model()->data(index, Qt::EditRole).value<KisShortcutConfiguration *>();
0072 
0073     switch (s->type()) {
0074     case KisShortcutConfiguration::KeyCombinationType: {
0075         KisKeyInputEditor *e = qobject_cast<KisKeyInputEditor *>(editor);
0076         e->setKeys(s->keys());
0077         break;
0078     }
0079 
0080     case KisShortcutConfiguration::MouseButtonType: {
0081         KisMouseInputEditor *e = qobject_cast<KisMouseInputEditor *>(editor);
0082         e->setKeys(s->keys());
0083         e->setButtons(s->buttons());
0084         break;
0085     }
0086 
0087     case KisShortcutConfiguration::MouseWheelType: {
0088         KisWheelInputEditor *e = qobject_cast<KisWheelInputEditor *>(editor);
0089         e->setKeys(s->keys());
0090         e->setWheel(s->wheel());
0091         break;
0092     }
0093     case KisShortcutConfiguration::GestureType: {
0094         KisGestureSelector *e = qobject_cast<KisGestureSelector *>(editor);
0095         e->setGesture(s->gesture());
0096         break;
0097     }
0098 
0099     default:
0100         break;
0101     }
0102 }
0103 
0104 void KisInputEditorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0105 {
0106     KisShortcutConfiguration *s = model->data(index, Qt::EditRole).value<KisShortcutConfiguration *>();
0107 
0108     switch (s->type()) {
0109     case KisShortcutConfiguration::KeyCombinationType: {
0110         KisKeyInputEditor *e = qobject_cast<KisKeyInputEditor *>(editor);
0111         s->setKeys(e->keys());
0112         break;
0113     }
0114 
0115     case KisShortcutConfiguration::MouseButtonType: {
0116         KisMouseInputEditor *e = qobject_cast<KisMouseInputEditor *>(editor);
0117         s->setKeys(e->keys());
0118         s->setButtons(e->buttons());
0119         break;
0120     }
0121 
0122     case KisShortcutConfiguration::MouseWheelType: {
0123         KisWheelInputEditor *e = qobject_cast<KisWheelInputEditor *>(editor);
0124         s->setKeys(e->keys());
0125         s->setWheel(e->wheel());
0126         break;
0127     }
0128     case KisShortcutConfiguration::GestureType: {
0129         KisGestureSelector *e = qobject_cast<KisGestureSelector *>(editor);
0130         s->setGesture(e->gesture());
0131         break;
0132     }
0133     break;
0134 
0135     default:
0136         break;
0137     }
0138 
0139     model->setData(index, QVariant::fromValue(s), Qt::EditRole);
0140 }
0141 
0142 void KisInputEditorDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
0143 {
0144     editor->setGeometry(option.rect);
0145 }
0146 
0147 QSize KisInputEditorDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0148 {
0149     return QStyledItemDelegate::sizeHint(option, index) + QSize(6, 6);
0150 }