Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_input_button.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "kis_input_button.h"
9
10#include <QTimer>
11#include <QMouseEvent>
12#include <QKeyEvent>
13#include <KLocalizedString>
14#include <QPushButton>
15
16#include "kis_icon_utils.h"
17
18
20{
21public:
22 Private(KisInputButton *qq) : q(qq) { }
23 void updateLabel();
24
25 KisInputButton *q {nullptr};
26
28
30 Qt::MouseButtons buttons {Qt::MouseButton::NoButton};
32 bool newInput {false};
33
34 QTimer *resetTimer {nullptr};
35};
36
38 : QPushButton(parent), d(new Private(this))
39{
40 setIcon(KisIconUtils::loadIcon("configure"));
41 setText(i18nc("No input for this button", "None"));
42 setCheckable(true);
43
44 d->resetTimer = new QTimer(this);
45 d->resetTimer->setInterval(5000);
46 d->resetTimer->setSingleShot(true);
47 connect(d->resetTimer, SIGNAL(timeout()), SLOT(reset()));
48}
49
54
59
61{
62 d->type = newType;
63}
64
66{
67 d->keys.clear();
68 d->buttons = QFlags<Qt::MouseButton>();
70 d->updateLabel();
71}
72
74{
75 return d->keys;
76}
77
79{
80 if (newKeys != d->keys) {
81 d->keys = newKeys;
82 d->updateLabel();
83 }
84}
85
86Qt::MouseButtons KisInputButton::buttons() const
87{
88 return d->buttons;
89}
90
91void KisInputButton::setButtons(Qt::MouseButtons newButtons)
92{
93 if (newButtons != d->buttons) {
94 d->buttons = newButtons;
95 d->updateLabel();
96 }
97}
98
103
105{
106 if (newWheel != d->wheel) {
107 d->wheel = newWheel;
108 d->updateLabel();
109 }
110}
111
112void KisInputButton::mousePressEvent(QMouseEvent *event)
113{
114 if (isChecked() && d->type == KisInputButton::MouseType) {
115 d->buttons = event->buttons();
116 d->updateLabel();
117 d->resetTimer->start();
118 }
119}
120
122{
123 if (isChecked()) {
124 reset();
125 }
126 else {
127 setChecked(true);
128 setText(i18nc("Waiting for user input", "Input..."));
129 d->resetTimer->start();
130 d->newInput = true;
131 }
132}
133
134void KisInputButton::wheelEvent(QWheelEvent *event)
135{
136 if (isChecked() && !event->angleDelta().isNull()) {
137 if (event->angleDelta().x() < 0) {
139 } else if (event->angleDelta().x() > 0) {
141 }
142
143 if (event->angleDelta().y() < 0) {
145 } else if (event->angleDelta().y() > 0) {
147 }
148
149 d->updateLabel();
150 }
151}
152
153void KisInputButton::keyPressEvent(QKeyEvent *event)
154{
155 if (isChecked()) {
156 if (d->newInput) {
157 d->keys.clear();
158 d->newInput = false;
159 }
160
161 Qt::Key key = static_cast<Qt::Key>(event->key());
162
163 if (key == Qt::Key_Meta && event->modifiers().testFlag(Qt::ShiftModifier)) {
164 key = Qt::Key_Alt;
165 }
166
167 d->keys.append(key);
168 d->updateLabel();
169 d->resetTimer->start();
170 }
171}
172
174{
175 if (isChecked()) {
176 reset();
177 }
178 else if (event->key() == Qt::Key_Space || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
179 setChecked(true);
180 setText(i18nc("Waiting for user input", "Input..."));
181 d->resetTimer->start();
182 d->newInput = true;
183 }
184}
185
187{
188 setChecked(false);
189 d->updateLabel();
190 Q_EMIT dataChanged();
191}
192
194{
195 switch (type) {
196 case MouseType:
198 break;
199
200 case KeyType:
202 break;
203
204 case WheelType:
206 break;
207 }
208}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
Private(KisInputButton *qq)
KisShortcutConfiguration::MouseWheelMovement wheel
A button that can detect input and will store its value.
Qt::MouseButtons buttons() const
void setButtons(Qt::MouseButtons newButtons)
~KisInputButton() override
void setType(ButtonType newType)
void keyPressEvent(QKeyEvent *event) override
Private *const d
void mousePressEvent(QMouseEvent *event) override
KisInputButton(QWidget *parent=nullptr)
void setKeys(const QList< Qt::Key > &newKeys)
@ WheelType
Detect and store mouse wheel movement.
@ MouseType
Detect and store any combination of pressed mouse buttons.
@ KeyType
Detect and store any combination of key presses.
void mouseReleaseEvent(QMouseEvent *) override
KisShortcutConfiguration::MouseWheelMovement wheel() const
ButtonType type() const
void wheelEvent(QWheelEvent *event) override
void dataChanged()
void setWheel(KisShortcutConfiguration::MouseWheelMovement wheel)
void keyReleaseEvent(QKeyEvent *event) override
QList< Qt::Key > keys() const
static QString buttonsToText(Qt::MouseButtons buttons)
static QString keysToText(const QList< Qt::Key > &keys)
static QString wheelToText(MouseWheelMovement wheel)
@ WheelDown
Downwards movement, toward the user.
@ WheelUp
Upwards movement, away from the user.
QIcon loadIcon(const QString &name)