1 /*
2     SPDX-License-Identifier: GPL-2.0-only
3     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
4  */
5 
6 #ifndef SIMPLE_ACTION_DATA_H
7 #define SIMPLE_ACTION_DATA_H
8 
9 #include "action_data/action_data.h"
10 
11 #include "actions/actions.h"
12 #include "triggers/triggers.h"
13 
14 namespace KHotKeys
15 {
16 class Q_DECL_EXPORT SimpleActionData : public ActionData
17 {
18     typedef ActionData base;
19 
20 public:
21     SimpleActionData(ActionDataGroup *parent_P, const QString &name_P = QString(), const QString &comment_P = QString());
22 
23     /**
24      * Visitor pattern
25      * @reimp
26      */
27     void accept(ActionDataVisitor *visitor) override;
28     void accept(ActionDataConstVisitor *visitor) const override;
29 
30     //! The action associated with this hotkey
31     virtual const Action *action() const;
32     virtual Action *action();
33 
34     //! The trigger for this hotkey
35     virtual const Trigger *trigger() const;
36     virtual Trigger *trigger();
37 
38     void set_action(Action *action_P);
39     void set_trigger(Trigger *trigger_P);
40 
41 protected:
42     void doEnable() override;
43 
44     void doDisable() override;
45 
46 }; // class SimpleActionData
47 
48 /**
49  * A template adding convenience methods to SimpleActionData.
50  */
51 template<typename T, typename A> class Q_DECL_EXPORT SimpleActionDataHelper : public SimpleActionData
52 {
53     typedef SimpleActionData base;
54 
55 public:
SimpleActionDataHelper(ActionDataGroup * parent_P,const QString & name_P,const QString & comment_P)56     SimpleActionDataHelper(ActionDataGroup *parent_P, const QString &name_P, const QString &comment_P)
57         : base(parent_P, name_P, comment_P)
58     {
59     }
60 
61     //! The action associated with this hotkey
62     const A *action() const override;
63     A *action() override;
64 
65     //! The trigger for this hotkey
66     const T *trigger() const override;
67     T *trigger() override;
68 
69     void set_action(Action *action_P);
70     void set_trigger(Trigger *trigger_P);
71 };
72 
73 // ==========================================================================
74 // TEMPLATE METHOD DEFINITIONS
75 
set_action(Action * action_P)76 template<typename T, typename A> void SimpleActionDataHelper<T, A>::set_action(Action *action_P)
77 {
78     Q_ASSERT(dynamic_cast<A *>(action_P));
79     base::set_action(action_P);
80 }
81 
set_trigger(Trigger * trigger_P)82 template<typename T, typename A> void SimpleActionDataHelper<T, A>::set_trigger(Trigger *trigger_P)
83 {
84     Q_ASSERT(dynamic_cast<T *>(trigger_P));
85     base::set_trigger(trigger_P);
86 }
87 
action()88 template<typename T, typename A> const A *SimpleActionDataHelper<T, A>::action() const
89 {
90     if (actions() == nullptr || actions()->isEmpty())
91         return nullptr;
92     return dynamic_cast<const A *>(actions()->first());
93 }
94 
action()95 template<typename T, typename A> A *SimpleActionDataHelper<T, A>::action()
96 {
97     if (actions() == nullptr || actions()->isEmpty())
98         return nullptr;
99     return dynamic_cast<A *>(actions()->first());
100 }
101 
trigger()102 template<typename T, typename A> const T *SimpleActionDataHelper<T, A>::trigger() const
103 {
104     if (triggers() == nullptr || triggers()->isEmpty())
105         return nullptr;
106     return dynamic_cast<const T *>(triggers()->first());
107 }
108 
trigger()109 template<typename T, typename A> T *SimpleActionDataHelper<T, A>::trigger()
110 {
111     if (triggers() == nullptr || triggers()->isEmpty())
112         return nullptr;
113     return dynamic_cast<T *>(triggers()->first());
114 }
115 
116 } // namespace KHotKeys
117 
118 #endif
119