File indexing completed on 2024-06-16 04:16:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_CUSTOM_MODIFIERS_CATCHER_H
0008 #define __KIS_CUSTOM_MODIFIERS_CATCHER_H
0009 
0010 #include <QScopedPointer>
0011 #include <QObject>
0012 
0013 /**
0014  * @brief The KisCustomModifiersCatcher class is a special utility class that
0015  * tracks custom modifiers pressed. Its main purpose is to avoid manual tracking
0016  * of KeyPress/KeyRelease/FocusIn events in the class and reuse the common code in
0017  * multiple widgets.
0018  *
0019  * ~~~~~~~~~~~~~~~~~~~~{.cpp}
0020  * // in the c-tor of your widget create the catcher, it will automatically
0021  * // connect to the passed parent
0022  * KisCustomModifiersCatcher *catcher = new KisCustomModifiersCatcher(parent);
0023  *
0024  * // Register a tracked modifier
0025  * catcher->addModifier("pan-zoom", Qt::Key_Space);
0026  *
0027  * // in the pointer tracking event handlers just check
0028  * // if the modifier is pressed or not
0029  * bool isPressed = catcher->modifierPressed("pan-zoom");
0030  * ~~~~~~~~~~~~~~~~~~~~
0031  */
0032 
0033 class KisCustomModifiersCatcher : public QObject
0034 {
0035 public:
0036     /**
0037      * Create the catcher and connect to the passed widget/object to
0038      * track its key events
0039      */
0040     KisCustomModifiersCatcher(QObject *parent);
0041     ~KisCustomModifiersCatcher() override;
0042 
0043     bool eventFilter(QObject* object, QEvent* event) override;
0044 
0045     /**
0046      * @brief addModifier registers a custom modifier
0047      * @param id a unique id string associated with the modifier. Later, you will use this string to fetch the modifier state.
0048      * @param modifier the key to track
0049      */
0050     void addModifier(const QString &id, Qt::Key modifier);
0051 
0052     /**
0053      * @brief modifierPressed returns the state of the tracked modifier
0054      */
0055     bool modifierPressed(const QString &id);
0056 
0057 private:
0058     struct Private;
0059     const QScopedPointer<Private> m_d;
0060 };
0061 
0062 #endif /* __KIS_CUSTOM_MODIFIERS_CATCHER_H */