File indexing completed on 2024-12-01 09:50:02

0001 /*
0002     SPDX-FileCopyrightText: 2009 Michael Leupold <lemma@confuego.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KMODIFIERKEYINFOPROVIDER_P_H
0008 #define KMODIFIERKEYINFOPROVIDER_P_H
0009 
0010 #include "kguiaddons_export.h"
0011 
0012 #include <QHash>
0013 #include <QObject>
0014 #include <QSharedData>
0015 
0016 /**
0017  * Background class that implements the behaviour of KModifierKeyInfo for
0018  * the different supported platforms.
0019  * @internal
0020  */
0021 class KGUIADDONS_EXPORT KModifierKeyInfoProvider : public QObject, public QSharedData
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     enum ModifierState {
0027         Nothing = 0x0,
0028         Pressed = 0x1,
0029         Latched = 0x2,
0030         Locked = 0x4,
0031     };
0032     Q_ENUM(ModifierState)
0033     Q_DECLARE_FLAGS(ModifierStates, ModifierState)
0034 
0035     KModifierKeyInfoProvider();
0036     ~KModifierKeyInfoProvider() override;
0037 
0038     /**
0039      * Detect if a key is pressed.
0040      * @param key Modifier key to query
0041      * @return true if the key is pressed, false if it isn't.
0042      */
0043     bool isKeyPressed(Qt::Key key) const;
0044 
0045     /**
0046      * Detect if a key is latched.
0047      * @param key Modifier key to query
0048      * @return true if the key is latched, false if it isn't.
0049      */
0050     bool isKeyLatched(Qt::Key key) const;
0051 
0052     /**
0053      * Set the latched state of a key.
0054      * @param key Modifier to set the latched state for
0055      * @param latched true to latch the key, false to unlatch it
0056      * @return true if the key is known, false else
0057      */
0058     virtual bool setKeyLatched(Qt::Key key, bool latched);
0059 
0060     /**
0061      * Detect if a key is locked.
0062      * @param key Modifier key to query
0063      * @return true if the key is locked, false if it isn't.
0064      */
0065     bool isKeyLocked(Qt::Key key) const;
0066 
0067     /**
0068      * Set the locked state of a key.
0069      * @param key Modifier to set the locked state for
0070      * @param latched true to lock the key, false to unlock it
0071      * @return true if the key is known, false else
0072      */
0073     virtual bool setKeyLocked(Qt::Key key, bool locked);
0074 
0075     /**
0076      * Check if a mouse button is pressed.
0077      * @param button Mouse button to check
0078      * @return true if pressed, false else
0079      */
0080     bool isButtonPressed(Qt::MouseButton button) const;
0081 
0082     /**
0083      * Check if a key is known/can be queried
0084      * @param key Modifier key to check
0085      * @return true if the key is known, false if it isn't.
0086      */
0087     bool knowsKey(Qt::Key key) const;
0088 
0089     /**
0090      * Get a list of known keys
0091      * @return List of known keys.
0092      */
0093     const QList<Qt::Key> knownKeys() const;
0094 
0095 Q_SIGNALS:
0096     void keyLatched(Qt::Key key, bool state);
0097     void keyLocked(Qt::Key key, bool state);
0098     void keyPressed(Qt::Key key, bool state);
0099     void buttonPressed(Qt::MouseButton button, bool state);
0100     void keyAdded(Qt::Key key);
0101     void keyRemoved(Qt::Key key);
0102 
0103 protected:
0104     void stateUpdated(Qt::Key key, KModifierKeyInfoProvider::ModifierStates state);
0105 
0106     // the state of each known modifier
0107     QHash<Qt::Key, ModifierStates> m_modifierStates;
0108 
0109     // the state of each known mouse button
0110     QHash<Qt::MouseButton, bool> m_buttonStates;
0111 };
0112 
0113 Q_DECLARE_INTERFACE(KModifierKeyInfoProvider, "org.kde.kguiaddons.KModifierKeyInfoProvider")
0114 Q_DECLARE_OPERATORS_FOR_FLAGS(KModifierKeyInfoProvider::ModifierStates)
0115 
0116 #endif