File indexing completed on 2024-06-09 04:24:32

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_ABSTRACT_INPUT_ACTION_H
0008 #define KIS_ABSTRACT_INPUT_ACTION_H
0009 
0010 #include <QHash>
0011 #include <QPoint>
0012 #include "kritaui_export.h"
0013 
0014 #include "KisInputActionGroup.h"
0015 
0016 class QPointF;
0017 class QEvent;
0018 class KisInputManager;
0019 
0020 /**
0021  * \brief Abstract base class for input actions.
0022  *
0023  * Input actions represent actions to be performed when interacting
0024  * with the canvas. They are managed by KisInputManager and activated
0025  * when KisKeyShortcut or KisStrokeShortcut detects it matches a certain
0026  * set of inputs.
0027  *
0028  * The begin() method uses an index for the type of behaviour to activate.
0029  * This index can be used to trigger behaviour when different events occur.
0030  *
0031  * The events can be of two types:
0032  * 1) Key events. The input manager calls begin() and end() sequentially
0033  *    with an \p index parameter to begin() representing the type of
0034  *    action that should be performed. The \p event parameter of both
0035  *    calls in null.
0036  * 2) Stroke events. The input manager calls begin() and end() on the
0037  *    corresponding mouse down and up events. The \p event parameter
0038  *    will be of QMouseEvent type, representing the event happened.
0039  *    All the mouse move events between begin() and end() will be
0040  *    redirected to the inputEvent() method.
0041  */
0042 class KRITAUI_EXPORT KisAbstractInputAction
0043 {
0044 public:
0045     /**
0046      * Constructor.
0047      *
0048      * \param manager The InputManager this action belongs to.
0049      */
0050     explicit KisAbstractInputAction(const QString &id);
0051     /**
0052      * Destructor.
0053      */
0054     virtual ~KisAbstractInputAction();
0055 
0056     /**
0057      * The method is called when the action is yet to be started,
0058      * that is, e.g. the user has pressed all the modifiers for the
0059      * action but hasn't started painting yet. This method is a right
0060      * place to show the user what is going to happen, e.g. change the
0061      * cursor.
0062      */
0063     virtual void activate(int shortcut);
0064 
0065     /**
0066      * The method is called when the action is not a candidate for
0067      * the starting anymore. The action should revert everything that
0068      * was done in activate() method.
0069      *
0070      * \see activate()
0071      */
0072     virtual void deactivate(int shortcut);
0073 
0074     /**
0075      * Begin the action.
0076      *
0077      * \param shortcut The index of the behaviour to trigger.
0078      * \param event The mouse event that has triggered this action.
0079      *              Is null for keyboard-activated actions.
0080      */
0081     virtual void begin(int shortcut, QEvent *event);
0082     /**
0083      * End the action.
0084      *
0085      * \param event The mouse event that has finished this action.
0086      *              Is null for keyboard-activated actions.
0087      */
0088     virtual void end(QEvent *event);
0089     /**
0090      * Process an input event.
0091      *
0092      * By default handles MouseMove events and passes the data to
0093      * a convenience cursorMoved() method
0094      *
0095      * \param event An event to process.
0096      */
0097     virtual void inputEvent(QEvent* event);
0098 
0099     /**
0100      * Returns true if the action can handle HiRes flow of move events
0101      * which is generated by the tablet. If the function returns
0102      * false, some of the events will be dropped or postponed. For
0103      * most of the actions in Krita (except of real painting) it is
0104      * perfectly acceptable, so 'false' is the default value.
0105      */
0106     virtual bool supportsHiResInputEvents(int shortcut) const;
0107 
0108     /**
0109      * \return the group of the action the specified \p shortcut belongs to
0110      */
0111     virtual KisInputActionGroup inputActionGroup(int shortcut) const;
0112 
0113     /**
0114      * The indexes of shortcut behaviours available.
0115      */
0116     virtual QHash<QString, int> shortcutIndexes() const;
0117 
0118     /**
0119      * The id of this action.
0120      */
0121     virtual QString id() const;
0122 
0123     /**
0124      * The translated name of this action.
0125      */
0126     virtual QString name() const;
0127 
0128     /**
0129      * A short description of this action.
0130      */
0131     virtual QString description() const;
0132 
0133     /**
0134      * The priority for this action.
0135      *
0136      * Priority determines how "important" the action is and is used
0137      * to resolve conflicts when multiple actions can be activated.
0138      */
0139     virtual int priority() const;
0140 
0141     /**
0142      * Returns true if an action can run with any modifiers pressed
0143      * (the shortcut's modifiers list must be empty for that). That is
0144      * used for making one type of actions default one.
0145      */
0146     virtual bool canIgnoreModifiers() const;
0147 
0148     /**
0149      * Return true when the specified shortcut is required for basic
0150      * user interaction. This is used by the configuration system to
0151      * prevent basic actions like painting from being removed.
0152      *
0153      * \param shortcut The shortcut index to check.
0154      * \return True if the shortcut is required, false if not.
0155      */
0156     virtual bool isShortcutRequired(int shortcut) const;
0157 
0158     /**
0159      * Some of the actions are available in particular situations
0160      * only.  E.g. switch frame action is available iff an animated
0161      * layer is selected. If isAvailable() returns true then the
0162      * action will *not* be triggered by the shortcut matcher.
0163      */
0164     virtual bool isAvailable() const;
0165 
0166 protected:
0167     /**
0168      * The input manager this action belongs to.
0169      */
0170     KisInputManager *inputManager() const;
0171     /**
0172      * Set the name of this action.
0173      *
0174      * \param name The new name.
0175      */
0176     void setName(const QString &name);
0177     /**
0178      * Set the description of this action.
0179      *
0180      * \param description The new description.
0181      */
0182     void setDescription(const QString &description);
0183     /**
0184      * Set the available indexes of shortcut behaviours.
0185      *
0186      * \param indexes The new indexes.
0187      */
0188     void setShortcutIndexes(const QHash<QString, int> &indexes);
0189 
0190     /**
0191      * Convenience method for handling cursor movement for tablet, mouse and touch.
0192      * The default implementation of inputEvent calls this function.
0193      */
0194     virtual void cursorMoved(const QPointF &lastPos, const QPointF &pos);
0195     virtual void cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos);
0196 
0197     /**
0198      * Convenience method to extract the position from a cursor movement event.
0199      *
0200      * \param event A mouse or tablet event.
0201      */
0202     QPoint eventPos(const QEvent *event);
0203 
0204     /**
0205      * Convenience method to extract the floating point position from a
0206      * cursor movement event.
0207      *
0208      * \param event A mouse or tablet event.
0209      */
0210     QPointF eventPosF(const QEvent *event);
0211 
0212 private:
0213     friend class KisInputManager;
0214     static void setInputManager(KisInputManager *manager);
0215 
0216     class Private;
0217     Private * const d;
0218 };
0219 
0220 #endif // KIS_ABSTRACT_INPUT_ACTION_H