File indexing completed on 2024-09-08 04:30:11
0001 /* 0002 SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef POINTERLOCKER_H 0008 #define POINTERLOCKER_H 0009 0010 #include <QObject> 0011 #include <QWindow> 0012 0013 class AbstractPointerLocker : public QObject 0014 { 0015 Q_OBJECT 0016 Q_PROPERTY(bool isSupported READ isSupported NOTIFY supportedChanged) 0017 Q_PROPERTY(bool isLocked READ isLocked WRITE setLocked NOTIFY lockedChanged) 0018 Q_PROPERTY(bool isLockEffective READ isLockEffective NOTIFY lockEffectiveChanged) 0019 Q_PROPERTY(QWindow *window READ window WRITE setWindow NOTIFY windowChanged) 0020 public: 0021 AbstractPointerLocker(QObject *parent = nullptr) 0022 : QObject(parent) 0023 { 0024 } 0025 0026 virtual void setLocked(bool locked) = 0; 0027 virtual bool isLocked() const = 0; 0028 virtual bool isLockEffective() const = 0; 0029 virtual bool isSupported() const = 0; 0030 0031 virtual void setWindow(QWindow *window); 0032 QWindow *window() const 0033 { 0034 return m_window; 0035 } 0036 0037 Q_SIGNALS: 0038 void supportedChanged(bool isSupported); 0039 void lockedChanged(bool isLocked); 0040 void lockEffectiveChanged(bool isLockEffective); 0041 void windowChanged(); 0042 void pointerMoved(const QPointF &delta); 0043 0044 protected: 0045 QWindow *m_window = nullptr; 0046 }; 0047 0048 class PointerLockerQt : public AbstractPointerLocker 0049 { 0050 Q_OBJECT 0051 public: 0052 PointerLockerQt(QObject *parent = nullptr); 0053 ~PointerLockerQt() override; 0054 0055 void setLocked(bool locked) override; 0056 bool isLocked() const override; 0057 bool isSupported() const override 0058 { 0059 return true; 0060 } 0061 bool isLockEffective() const override 0062 { 0063 return isLocked(); 0064 } 0065 0066 private: 0067 bool eventFilter(QObject *watched, QEvent *event) override; 0068 0069 QPoint m_originalPosition; 0070 bool m_isLocked = false; 0071 }; 0072 0073 #endif