File indexing completed on 2024-04-28 16:44:51

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only
0003     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0004  */
0005 
0006 #ifndef SIMPLE_ACTION_DATA_H
0007 #define SIMPLE_ACTION_DATA_H
0008 
0009 #include "action_data/action_data.h"
0010 
0011 #include "actions/actions.h"
0012 #include "triggers/triggers.h"
0013 
0014 namespace KHotKeys
0015 {
0016 class Q_DECL_EXPORT SimpleActionData : public ActionData
0017 {
0018     typedef ActionData base;
0019 
0020 public:
0021     SimpleActionData(ActionDataGroup *parent_P, const QString &name_P = QString(), const QString &comment_P = QString());
0022 
0023     /**
0024      * Visitor pattern
0025      * @reimp
0026      */
0027     void accept(ActionDataVisitor *visitor) override;
0028     void accept(ActionDataConstVisitor *visitor) const override;
0029 
0030     //! The action associated with this hotkey
0031     virtual const Action *action() const;
0032     virtual Action *action();
0033 
0034     //! The trigger for this hotkey
0035     virtual const Trigger *trigger() const;
0036     virtual Trigger *trigger();
0037 
0038     void set_action(Action *action_P);
0039     void set_trigger(Trigger *trigger_P);
0040 
0041 protected:
0042     void doEnable() override;
0043 
0044     void doDisable() override;
0045 
0046 }; // class SimpleActionData
0047 
0048 /**
0049  * A template adding convenience methods to SimpleActionData.
0050  */
0051 template<typename T, typename A> class Q_DECL_EXPORT SimpleActionDataHelper : public SimpleActionData
0052 {
0053     typedef SimpleActionData base;
0054 
0055 public:
0056     SimpleActionDataHelper(ActionDataGroup *parent_P, const QString &name_P, const QString &comment_P)
0057         : base(parent_P, name_P, comment_P)
0058     {
0059     }
0060 
0061     //! The action associated with this hotkey
0062     const A *action() const override;
0063     A *action() override;
0064 
0065     //! The trigger for this hotkey
0066     const T *trigger() const override;
0067     T *trigger() override;
0068 
0069     void set_action(Action *action_P);
0070     void set_trigger(Trigger *trigger_P);
0071 };
0072 
0073 // ==========================================================================
0074 // TEMPLATE METHOD DEFINITIONS
0075 
0076 template<typename T, typename A> void SimpleActionDataHelper<T, A>::set_action(Action *action_P)
0077 {
0078     Q_ASSERT(dynamic_cast<A *>(action_P));
0079     base::set_action(action_P);
0080 }
0081 
0082 template<typename T, typename A> void SimpleActionDataHelper<T, A>::set_trigger(Trigger *trigger_P)
0083 {
0084     Q_ASSERT(dynamic_cast<T *>(trigger_P));
0085     base::set_trigger(trigger_P);
0086 }
0087 
0088 template<typename T, typename A> const A *SimpleActionDataHelper<T, A>::action() const
0089 {
0090     if (actions() == nullptr || actions()->isEmpty())
0091         return nullptr;
0092     return dynamic_cast<const A *>(actions()->first());
0093 }
0094 
0095 template<typename T, typename A> A *SimpleActionDataHelper<T, A>::action()
0096 {
0097     if (actions() == nullptr || actions()->isEmpty())
0098         return nullptr;
0099     return dynamic_cast<A *>(actions()->first());
0100 }
0101 
0102 template<typename T, typename A> const T *SimpleActionDataHelper<T, A>::trigger() const
0103 {
0104     if (triggers() == nullptr || triggers()->isEmpty())
0105         return nullptr;
0106     return dynamic_cast<const T *>(triggers()->first());
0107 }
0108 
0109 template<typename T, typename A> T *SimpleActionDataHelper<T, A>::trigger()
0110 {
0111     if (triggers() == nullptr || triggers()->isEmpty())
0112         return nullptr;
0113     return dynamic_cast<T *>(triggers()->first());
0114 }
0115 
0116 } // namespace KHotKeys
0117 
0118 #endif