Krita Source Code Documentation
Loading...
Searching...
No Matches
KisInputButton Class Reference

A button that can detect input and will store its value. More...

#include <kis_input_button.h>

+ Inheritance diagram for KisInputButton:

Classes

class  Private
 

Public Types

enum  ButtonType { MouseType , KeyType , WheelType }
 

Public Slots

void clear ()
 

Signals

void dataChanged ()
 

Public Member Functions

Qt::MouseButtons buttons () const
 
QList< Qt::Key > keys () const
 
 KisInputButton (QWidget *parent=nullptr)
 
void setButtons (Qt::MouseButtons newButtons)
 
void setKeys (const QList< Qt::Key > &newKeys)
 
void setType (ButtonType newType)
 
void setWheel (KisShortcutConfiguration::MouseWheelMovement wheel)
 
ButtonType type () const
 
KisShortcutConfiguration::MouseWheelMovement wheel () const
 
 ~KisInputButton () override
 

Protected Member Functions

void keyPressEvent (QKeyEvent *event) override
 
void keyReleaseEvent (QKeyEvent *event) override
 
void mousePressEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *) override
 
void wheelEvent (QWheelEvent *event) override
 

Private Slots

void reset ()
 

Private Attributes

Private *const d {nullptr}
 

Detailed Description

A button that can detect input and will store its value.

This button, when pressed, will detect input based on what type has been set. It is mainly intended for shortcut configuration, that is, picking some input that is later reused for shortcuts or similar.

Definition at line 23 of file kis_input_button.h.

Member Enumeration Documentation

◆ ButtonType

The type of button.

Enumerator
MouseType 

Detect and store any combination of pressed mouse buttons.

KeyType 

Detect and store any combination of key presses.

WheelType 

Detect and store mouse wheel movement.

Definition at line 30 of file kis_input_button.h.

30 {
31 MouseType,
32 KeyType,
33 WheelType,
34 };
@ 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.

Constructor & Destructor Documentation

◆ KisInputButton()

KisInputButton::KisInputButton ( QWidget * parent = nullptr)
explicit

Constructor.

Definition at line 37 of file kis_input_button.cpp.

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}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
Private *const d
QIcon loadIcon(const QString &name)
void setText(QSpinBox *spinBox, const QStringView textTemplate)

References connect(), d, KisIconUtils::loadIcon(), reset(), and KisInputButton::Private::resetTimer.

◆ ~KisInputButton()

KisInputButton::~KisInputButton ( )
override

Destructor.

Definition at line 50 of file kis_input_button.cpp.

51{
52 delete d;
53}

References d.

Member Function Documentation

◆ buttons()

Qt::MouseButtons KisInputButton::buttons ( ) const
Returns
The mouse buttons that were detected. Only applicable when type is MouseType.

Definition at line 86 of file kis_input_button.cpp.

87{
88 return d->buttons;
89}

References KisInputButton::Private::buttons, and d.

◆ clear

void KisInputButton::clear ( )
slot

Clear all detected input and reset the button to an empty state.

Definition at line 65 of file kis_input_button.cpp.

66{
67 d->keys.clear();
68 d->buttons = QFlags<Qt::MouseButton>();
70 d->updateLabel();
71}
KisShortcutConfiguration::MouseWheelMovement wheel

References KisInputButton::Private::buttons, d, KisInputButton::Private::keys, KisShortcutConfiguration::NoMovement, KisInputButton::Private::updateLabel(), and KisInputButton::Private::wheel.

◆ dataChanged

void KisInputButton::dataChanged ( )
signal

Emitted whenever one of the values (keys, buttons, wheel) changes.

◆ keyPressEvent()

void KisInputButton::keyPressEvent ( QKeyEvent * event)
overrideprotected

Definition at line 153 of file kis_input_button.cpp.

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}

References d, KisInputButton::Private::keys, KisInputButton::Private::newInput, KisInputButton::Private::resetTimer, and KisInputButton::Private::updateLabel().

◆ keyReleaseEvent()

void KisInputButton::keyReleaseEvent ( QKeyEvent * event)
overrideprotected

Definition at line 173 of file kis_input_button.cpp.

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}

References d, KisInputButton::Private::newInput, reset(), and KisInputButton::Private::resetTimer.

◆ keys()

QList< Qt::Key > KisInputButton::keys ( ) const
Returns
The list of keys that was detected. Only applicable when type is KeyType.

Definition at line 73 of file kis_input_button.cpp.

74{
75 return d->keys;
76}

References d, and KisInputButton::Private::keys.

◆ mousePressEvent()

void KisInputButton::mousePressEvent ( QMouseEvent * event)
overrideprotected

Definition at line 112 of file kis_input_button.cpp.

113{
114 if (isChecked() && d->type == KisInputButton::MouseType) {
115 d->buttons = event->buttons();
116 d->updateLabel();
117 d->resetTimer->start();
118 }
119}

References KisInputButton::Private::buttons, d, MouseType, KisInputButton::Private::resetTimer, KisInputButton::Private::type, and KisInputButton::Private::updateLabel().

◆ mouseReleaseEvent()

void KisInputButton::mouseReleaseEvent ( QMouseEvent * )
overrideprotected

Definition at line 121 of file kis_input_button.cpp.

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}

References d, KisInputButton::Private::newInput, reset(), and KisInputButton::Private::resetTimer.

◆ reset

void KisInputButton::reset ( )
privateslot

Definition at line 186 of file kis_input_button.cpp.

187{
188 setChecked(false);
189 d->updateLabel();
190 Q_EMIT dataChanged();
191}
void dataChanged()

References d, dataChanged(), and KisInputButton::Private::updateLabel().

◆ setButtons()

void KisInputButton::setButtons ( Qt::MouseButtons newButtons)

Set the mouse buttons to display.

This is mostly intended to make sure the button displays the right buttons when viewed in a dialog or similar UI.

Only applicable when type is MouseType.

Parameters
newButtonsThe mouse buttons to display.

Definition at line 91 of file kis_input_button.cpp.

92{
93 if (newButtons != d->buttons) {
94 d->buttons = newButtons;
95 d->updateLabel();
96 }
97}

References KisInputButton::Private::buttons, d, and KisInputButton::Private::updateLabel().

◆ setKeys()

void KisInputButton::setKeys ( const QList< Qt::Key > & newKeys)

Set the list of keys to display.

This is mostly intended to make sure the button displays the right keys when viewed in a dialog or similar UI.

Only applicable when type is KeyType.

Parameters
newKeysThe list of keys to display.

Definition at line 78 of file kis_input_button.cpp.

79{
80 if (newKeys != d->keys) {
81 d->keys = newKeys;
82 d->updateLabel();
83 }
84}

References d, KisInputButton::Private::keys, and KisInputButton::Private::updateLabel().

◆ setType()

void KisInputButton::setType ( KisInputButton::ButtonType newType)

Set the type of input this button should detect.

Parameters
newTypeThe type of input to detect.

Definition at line 60 of file kis_input_button.cpp.

61{
62 d->type = newType;
63}

References d, and KisInputButton::Private::type.

◆ setWheel()

void KisInputButton::setWheel ( KisShortcutConfiguration::MouseWheelMovement wheel)

Set the mouse wheel movement to display.

This is mostly intended to make sure the button displays the right wheel movement when viewed in a dialog or similar UI.

Only applicable when type is WheelType.

Parameters
wheelThe wheel movement to display.

Definition at line 104 of file kis_input_button.cpp.

105{
106 if (newWheel != d->wheel) {
107 d->wheel = newWheel;
108 d->updateLabel();
109 }
110}

References d, KisInputButton::Private::updateLabel(), and KisInputButton::Private::wheel.

◆ type()

KisInputButton::ButtonType KisInputButton::type ( ) const
Returns
The type of input this button detects.

Definition at line 55 of file kis_input_button.cpp.

56{
57 return d->type;
58}

References d, and KisInputButton::Private::type.

◆ wheel()

KisShortcutConfiguration::MouseWheelMovement KisInputButton::wheel ( ) const
Returns
The mouse wheel movement that was detected. Only applicable when type is WheelType.

Definition at line 99 of file kis_input_button.cpp.

100{
101 return d->wheel;
102}

References d, and KisInputButton::Private::wheel.

◆ wheelEvent()

void KisInputButton::wheelEvent ( QWheelEvent * event)
overrideprotected

Definition at line 134 of file kis_input_button.cpp.

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}
@ WheelDown
Downwards movement, toward the user.
@ WheelUp
Upwards movement, away from the user.

References d, KisInputButton::Private::updateLabel(), KisInputButton::Private::wheel, KisShortcutConfiguration::WheelDown, KisShortcutConfiguration::WheelLeft, KisShortcutConfiguration::WheelRight, and KisShortcutConfiguration::WheelUp.

Member Data Documentation

◆ d

Private* const KisInputButton::d {nullptr}
private

Definition at line 128 of file kis_input_button.h.

128{nullptr};

The documentation for this class was generated from the following files: