Warning, file /graphics/krita/libs/ui/input/config/kis_input_button.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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_button.h" 0009 0010 #include <QTimer> 0011 #include <QMouseEvent> 0012 #include <QKeyEvent> 0013 #include <KLocalizedString> 0014 #include <QPushButton> 0015 0016 #include "kis_icon_utils.h" 0017 0018 0019 class KisInputButton::Private 0020 { 0021 public: 0022 Private(KisInputButton *qq) : q(qq) { } 0023 void updateLabel(); 0024 0025 KisInputButton *q {nullptr}; 0026 0027 ButtonType type {KeyType}; 0028 0029 QList<Qt::Key> keys; 0030 Qt::MouseButtons buttons {Qt::MouseButton::NoButton}; 0031 KisShortcutConfiguration::MouseWheelMovement wheel {KisShortcutConfiguration::NoMovement}; 0032 bool newInput {false}; 0033 0034 QTimer *resetTimer {nullptr}; 0035 }; 0036 0037 KisInputButton::KisInputButton(QWidget *parent) 0038 : QPushButton(parent), d(new Private(this)) 0039 { 0040 setIcon(KisIconUtils::loadIcon("configure")); 0041 setText(i18nc("No input for this button", "None")); 0042 setCheckable(true); 0043 0044 d->resetTimer = new QTimer(this); 0045 d->resetTimer->setInterval(5000); 0046 d->resetTimer->setSingleShot(true); 0047 connect(d->resetTimer, SIGNAL(timeout()), SLOT(reset())); 0048 } 0049 0050 KisInputButton::~KisInputButton() 0051 { 0052 delete d; 0053 } 0054 0055 KisInputButton::ButtonType KisInputButton::type() const 0056 { 0057 return d->type; 0058 } 0059 0060 void KisInputButton::setType(KisInputButton::ButtonType newType) 0061 { 0062 d->type = newType; 0063 } 0064 0065 void KisInputButton::clear() 0066 { 0067 d->keys.clear(); 0068 d->buttons = QFlags<Qt::MouseButton>(); 0069 d->wheel = KisShortcutConfiguration::NoMovement; 0070 d->updateLabel(); 0071 } 0072 0073 QList< Qt::Key > KisInputButton::keys() const 0074 { 0075 return d->keys; 0076 } 0077 0078 void KisInputButton::setKeys(const QList< Qt::Key > &newKeys) 0079 { 0080 if (newKeys != d->keys) { 0081 d->keys = newKeys; 0082 d->updateLabel(); 0083 } 0084 } 0085 0086 Qt::MouseButtons KisInputButton::buttons() const 0087 { 0088 return d->buttons; 0089 } 0090 0091 void KisInputButton::setButtons(Qt::MouseButtons newButtons) 0092 { 0093 if (newButtons != d->buttons) { 0094 d->buttons = newButtons; 0095 d->updateLabel(); 0096 } 0097 } 0098 0099 KisShortcutConfiguration::MouseWheelMovement KisInputButton::wheel() const 0100 { 0101 return d->wheel; 0102 } 0103 0104 void KisInputButton::setWheel(KisShortcutConfiguration::MouseWheelMovement newWheel) 0105 { 0106 if (newWheel != d->wheel) { 0107 d->wheel = newWheel; 0108 d->updateLabel(); 0109 } 0110 } 0111 0112 void KisInputButton::mousePressEvent(QMouseEvent *event) 0113 { 0114 if (isChecked() && d->type == KisInputButton::MouseType) { 0115 d->buttons = event->buttons(); 0116 d->updateLabel(); 0117 d->resetTimer->start(); 0118 } 0119 } 0120 0121 void KisInputButton::mouseReleaseEvent(QMouseEvent *) 0122 { 0123 if (isChecked()) { 0124 reset(); 0125 } 0126 else { 0127 setChecked(true); 0128 setText(i18nc("Waiting for user input", "Input...")); 0129 d->resetTimer->start(); 0130 d->newInput = true; 0131 } 0132 } 0133 0134 void KisInputButton::wheelEvent(QWheelEvent *event) 0135 { 0136 if (isChecked() && event->delta() != 0) { 0137 switch (event->orientation()) { 0138 case Qt::Horizontal: 0139 if (event->delta() < 0) { 0140 d->wheel = KisShortcutConfiguration::WheelRight; 0141 } 0142 else { 0143 d->wheel = KisShortcutConfiguration::WheelLeft; 0144 } 0145 0146 break; 0147 0148 case Qt::Vertical: 0149 if (event->delta() > 0) { 0150 d->wheel = KisShortcutConfiguration::WheelUp; 0151 } 0152 else { 0153 d->wheel = KisShortcutConfiguration::WheelDown; 0154 } 0155 0156 break; 0157 } 0158 0159 d->updateLabel(); 0160 } 0161 } 0162 0163 void KisInputButton::keyPressEvent(QKeyEvent *event) 0164 { 0165 if (isChecked()) { 0166 if (d->newInput) { 0167 d->keys.clear(); 0168 d->newInput = false; 0169 } 0170 0171 Qt::Key key = static_cast<Qt::Key>(event->key()); 0172 0173 if (key == Qt::Key_Meta && event->modifiers().testFlag(Qt::ShiftModifier)) { 0174 key = Qt::Key_Alt; 0175 } 0176 0177 d->keys.append(key); 0178 d->updateLabel(); 0179 d->resetTimer->start(); 0180 } 0181 } 0182 0183 void KisInputButton::keyReleaseEvent(QKeyEvent *event) 0184 { 0185 if (isChecked()) { 0186 reset(); 0187 } 0188 else if (event->key() == Qt::Key_Space || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { 0189 setChecked(true); 0190 setText(i18nc("Waiting for user input", "Input...")); 0191 d->resetTimer->start(); 0192 d->newInput = true; 0193 } 0194 } 0195 0196 void KisInputButton::reset() 0197 { 0198 setChecked(false); 0199 d->updateLabel(); 0200 emit dataChanged(); 0201 } 0202 0203 void KisInputButton::Private::updateLabel() 0204 { 0205 switch (type) { 0206 case MouseType: 0207 q->setText(KisShortcutConfiguration::buttonsToText(buttons)); 0208 break; 0209 0210 case KeyType: 0211 q->setText(KisShortcutConfiguration::keysToText(keys)); 0212 break; 0213 0214 case WheelType: 0215 q->setText(KisShortcutConfiguration::wheelToText(wheel)); 0216 break; 0217 } 0218 }