File indexing completed on 2025-01-26 05:09:31

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef KEYSEQUENCEINPUTBUTTON_H
0021 #define KEYSEQUENCEINPUTBUTTON_H
0022 
0023 #include <QPushButton>
0024 #include <QScopedPointer>
0025 #include <QtGlobal>
0026 
0027 // forward declarations
0028 class QKeySequence;
0029 
0030 namespace Wacom
0031 {
0032 
0033 // forward declarations
0034 class KeySequenceInputButtonPrivate;
0035 
0036 /**
0037  * @class KeySequenceInputButton
0038  *
0039  * @brief A copy of KKeySequenceWidget which returns all modifiers.
0040  *
0041  * This code is mostly copied from KKeySequenceWidget but it allows us to store
0042  * all key codes of the sequence. While the original implementation would convert
0043  * "Shift+1" to "!", this implementation will actually return "Shift+!". The wacom
0044  * driver can not convert shifted keys back to its original key sequence as it
0045  * treats every key as a keycode. Therefore we have to make sure we return every
0046  * key which was pressed.
0047  */
0048 class KeySequenceInputButton : public QPushButton
0049 {
0050     Q_OBJECT
0051 
0052 public:
0053     //! Default Constructor
0054     KeySequenceInputButton(QWidget *parent = nullptr);
0055 
0056     //! Destructor
0057     ~KeySequenceInputButton() override;
0058 
0059     //! Clears the current sequence.
0060     void clearSequence();
0061 
0062     //! Gets the current sequence.
0063     const QKeySequence &getSequence() const;
0064 
0065     //! Sets the current sequence.
0066     void setSequence(const QKeySequence &sequence);
0067 
0068 Q_SIGNALS:
0069 
0070     //! Emitted when the key sequences changes.
0071     void keySequenceChanged(const QKeySequence &sequence);
0072 
0073 protected:
0074     //! Cancels shortcut recording.
0075     void cancelRecording();
0076 
0077     // overloaded from QPushButton
0078     bool event(QEvent *event) override;
0079 
0080     // overloaded from QPushButton
0081     void keyPressEvent(QKeyEvent *event) override;
0082 
0083     // overloaded from QPushButton
0084     void keyReleaseEvent(QKeyEvent *event) override;
0085 
0086     //! Starts shortcut recording.
0087     void startRecording();
0088 
0089     //! Finish shortcut recording.
0090     void stopRecording();
0091 
0092 private Q_SLOTS:
0093 
0094     void onButtonClicked();
0095 
0096 private:
0097     //! Hidden Copy Constructor
0098     KeySequenceInputButton(const KeySequenceInputButton &other) = delete;
0099 
0100     //! Hidden Copy Operator
0101     KeySequenceInputButton &operator=(const KeySequenceInputButton &other) = delete;
0102 
0103     void recordKey(uint modifierKeys, int keyQt);
0104 
0105     void setupUi();
0106 
0107     void updateShortcutDisplay();
0108 
0109     Q_DECLARE_PRIVATE(KeySequenceInputButton)
0110     const QScopedPointer<KeySequenceInputButtonPrivate> d_ptr; //!< D-Pointer to private members.
0111 
0112 }; // CLASS KeySequenceInputButton
0113 } // NAMESPACE Wacom
0114 #endif // KEYSEQUENCEINPUTBUTTON_H