1 /*
2 * actionlist.cpp - the customizeable action list
3 * Copyright (C) 2004 Michail Pishchagin
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include <QList>
22
23 #include "actionlist.h"
24
25 #include <QHash>
26
27 #include "iconaction.h"
28
29 //----------------------------------------------------------------------------
30 // ActionList
31 //----------------------------------------------------------------------------
32
33 class ActionList::Private : public QObject
34 {
35 Q_OBJECT
36 public:
Private()37 Private() { }
38 Private( const Private & );
39
40 QString name;
41 int id;
42 QStringList sortedActions;
43 QHash<QString, IconAction*> actions;
44 bool autoDeleteActions;
45
46 public slots:
47 void actionDestroyed(QObject *);
48 };
49
ActionList(const QString & name,int id,bool autoDelete)50 ActionList::ActionList( const QString &name, int id, bool autoDelete )
51 : QObject()
52 {
53 d = new Private();
54 d->autoDeleteActions = autoDelete;
55
56 d->name = name;
57 d->id = id;
58 }
59
ActionList(const ActionList & from)60 ActionList::ActionList( const ActionList &from )
61 : QObject()
62 {
63 d = new Private( *from.d );
64 }
65
~ActionList()66 ActionList::~ActionList()
67 {
68 if (d->autoDeleteActions)
69 qDeleteAll(d->actions);
70 delete d;
71 }
72
name() const73 QString ActionList::name() const
74 {
75 return d->name;
76 }
77
id() const78 int ActionList::id() const
79 {
80 return d->id;
81 }
82
action(const QString & name) const83 IconAction *ActionList::action( const QString &name ) const
84 {
85 return d->actions[name];
86 }
87
actions() const88 QStringList ActionList::actions() const
89 {
90 return d->sortedActions;
91 }
92
addAction(const QString & name,IconAction * action)93 void ActionList::addAction( const QString &name, IconAction *action )
94 {
95 d->sortedActions << name;
96
97 if ( action ) {
98 action->setObjectName( name );
99 d->actions.insert( name, action );
100 d->connect( action, SIGNAL( destroyed(QObject *) ), d, SLOT( actionDestroyed(QObject *) ) );
101 }
102 }
103
clear()104 void ActionList::clear()
105 {
106 if (d->autoDeleteActions)
107 qDeleteAll(d->actions);
108 d->actions.clear();
109 }
110
Private(const Private & from)111 ActionList::Private::Private( const Private &from )
112 : QObject()
113 {
114 name = from.name;
115 id = from.id;
116
117 actions = from.actions;
118 autoDeleteActions = from.autoDeleteActions;
119
120 sortedActions = from.sortedActions;
121 }
122
actionDestroyed(QObject * obj)123 void ActionList::Private::actionDestroyed(QObject *obj)
124 {
125 actions.remove( obj->objectName() );
126 }
127
128 //----------------------------------------------------------------------------
129 // MetaActionList
130 //----------------------------------------------------------------------------
131
132 class MetaActionList::Private
133 {
134 public:
Private()135 Private() { }
136
137 QList<ActionList*> lists;
138 };
139
MetaActionList()140 MetaActionList::MetaActionList()
141 : QObject()
142 {
143 d = new Private();
144 }
145
~MetaActionList()146 MetaActionList::~MetaActionList()
147 {
148 while (!d->lists.isEmpty())
149 delete d->lists.takeFirst();
150 delete d;
151 }
152
actionList(const QString & name) const153 ActionList *MetaActionList::actionList( const QString &name ) const
154 {
155 foreach(ActionList* a, d->lists) {
156 if (a->name() == name)
157 return a;
158 }
159
160 return 0;
161 }
162
actionLists(int id) const163 QList<ActionList*> MetaActionList::actionLists( int id ) const
164 {
165 QList<ActionList*> list;
166
167 for ( int i = 0; i < 32; i++ ) {
168 if ( !(id & ( 1 << i )) )
169 continue;
170
171 foreach(ActionList* a, d->lists) {
172 if ( a->id() & ( 1 << i ) )
173 list.append(a);
174 }
175 }
176
177 return list;
178 }
179
suitableActions(int id) const180 ActionList MetaActionList::suitableActions( int id ) const
181 {
182 QList<ActionList*> lists = actionLists( id );
183 ActionList actions("", 0, false);
184
185 foreach(ActionList* list, lists) {
186 QStringList actionList = list->actions();
187 QStringList::Iterator it2 = actionList.begin();
188 for ( ; it2 != actionList.end(); ++it2 )
189 actions.addAction( *it2, list->action( *it2 ) );
190 }
191
192 return actions;
193 }
194
actionLists() const195 QStringList MetaActionList::actionLists() const
196 {
197 QStringList names;
198
199 foreach(ActionList* l, d->lists)
200 names << l->name();
201
202 return names;
203 }
204
addList(ActionList * list)205 void MetaActionList::addList( ActionList *list )
206 {
207 if ( list )
208 d->lists.append( list );
209 }
210
clear()211 void MetaActionList::clear()
212 {
213 foreach(ActionList* l, d->lists) {
214 l->clear();
215 }
216 }
217
218 #include "actionlist.moc"
219