File indexing completed on 2024-11-10 10:53:31

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only
0003     SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0004  */
0005 
0006 #include "action_data_group.h"
0007 
0008 #include "action_data/action_data_visitor.h"
0009 
0010 #include <QDebug>
0011 #include <kconfiggroup.h>
0012 
0013 namespace KHotKeys
0014 {
0015 ActionDataGroup::ActionDataGroup(ActionDataGroup *parent_P,
0016                                  const QString &name_P,
0017                                  const QString &comment_P,
0018                                  Condition_list *conditions_P,
0019                                  system_group_t system_group_P)
0020     : ActionDataBase(parent_P, name_P, comment_P, conditions_P)
0021     , _list()
0022     , _system_group(system_group_P)
0023 {
0024 }
0025 
0026 ActionDataGroup::~ActionDataGroup()
0027 {
0028     while (!_list.isEmpty()) {
0029         delete _list.takeFirst();
0030     }
0031 }
0032 
0033 void ActionDataGroup::accept(ActionDataVisitor *visitor)
0034 {
0035     visitor->visitActionDataGroup(this);
0036 }
0037 
0038 void ActionDataGroup::accept(ActionDataConstVisitor *visitor) const
0039 {
0040     visitor->visitActionDataGroup(this);
0041 }
0042 
0043 Action::ActionTypes ActionDataGroup::allowedActionTypes() const
0044 {
0045     switch (_system_group) {
0046     case SYSTEM_MENUENTRIES:
0047         return Action::MenuEntryActionType;
0048 
0049     default:
0050         return Action::AllTypes;
0051     }
0052 }
0053 
0054 Trigger::TriggerTypes ActionDataGroup::allowedTriggerTypes() const
0055 {
0056     switch (_system_group) {
0057     case SYSTEM_MENUENTRIES:
0058         return Trigger::ShortcutTriggerType;
0059 
0060     default:
0061         return Trigger::AllTypes;
0062     }
0063 }
0064 
0065 bool ActionDataGroup::is_system_group() const
0066 {
0067     return _system_group != SYSTEM_NONE && _system_group != SYSTEM_ROOT;
0068 }
0069 
0070 void ActionDataGroup::set_system_group(system_group_t group)
0071 {
0072     _system_group = group;
0073 }
0074 
0075 ActionDataGroup::system_group_t ActionDataGroup::system_group() const
0076 {
0077     return _system_group;
0078 }
0079 
0080 void ActionDataGroup::add_child(ActionDataBase *child_P)
0081 {
0082     // Just make sure we don't get the same child twice
0083     Q_ASSERT(!_list.contains(child_P));
0084     if (_list.contains(child_P))
0085         return;
0086 
0087     if (child_P->parent()) {
0088         child_P->parent()->remove_child(child_P);
0089     }
0090 
0091     child_P->_parent = this;
0092 
0093     _list.append(child_P);
0094 }
0095 
0096 void ActionDataGroup::add_child(ActionDataBase *child_P, int position)
0097 {
0098     // Just make sure we don't get the same child twice
0099     Q_ASSERT(!_list.contains(child_P));
0100     if (_list.contains(child_P))
0101         return;
0102 
0103     if (child_P->parent()) {
0104         child_P->parent()->remove_child(child_P);
0105     }
0106 
0107     child_P->_parent = this;
0108 
0109     _list.insert(position, child_P);
0110 }
0111 
0112 void ActionDataGroup::aboutToBeErased()
0113 {
0114     Q_FOREACH (ActionDataBase *child, _list) {
0115         child->aboutToBeErased();
0116     }
0117 }
0118 
0119 const QList<ActionDataBase *> ActionDataGroup::children() const
0120 {
0121     return _list;
0122 }
0123 
0124 void ActionDataGroup::doDisable()
0125 {
0126     Q_FOREACH (ActionDataBase *child, _list) {
0127         child->update_triggers();
0128     }
0129 }
0130 
0131 void ActionDataGroup::doEnable()
0132 {
0133     Q_FOREACH (ActionDataBase *child, _list) {
0134         child->update_triggers();
0135     }
0136 }
0137 
0138 void ActionDataGroup::remove_child(ActionDataBase *child_P)
0139 {
0140     child_P->_parent = nullptr;
0141     _list.removeAll(child_P); // is not auto-delete
0142 }
0143 
0144 int ActionDataGroup::size() const
0145 {
0146     return _list.size();
0147 }
0148 
0149 void ActionDataGroup::update_triggers()
0150 {
0151     Q_FOREACH (ActionDataBase *child, children()) {
0152         child->update_triggers();
0153     }
0154 }
0155 
0156 } // namespace KHotKeys
0157 
0158 #include "moc_action_data_group.cpp"