source: psi/trunk/src/actionlist.cpp

Last change on this file was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 4.4 KB
Line 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include "actionlist.h"
22
23#include <qobject.h>
24#include <qdict.h>
25
26#include "iconaction.h"
27
28//----------------------------------------------------------------------------
29// ActionList
30//----------------------------------------------------------------------------
31
32class ActionList::Private : public QObject
33{
34 Q_OBJECT
35public:
36 Private() { }
37 Private( const Private & );
38
39 QString name;
40 int id;
41 QStringList sortedActions;
42 QDict<IconAction> actions;
43
44public slots:
45 void actionDestroyed(QObject *);
46};
47
48ActionList::ActionList( QString name, int id, bool autoDelete )
49{
50 d = new Private();
51 d->actions.setAutoDelete( autoDelete );
52
53 d->name = name;
54 d->id = id;
55}
56
57ActionList::ActionList( const ActionList &from )
58{
59 d = new Private( *from.d );
60}
61
62ActionList::~ActionList()
63{
64 delete d;
65}
66
67QString ActionList::name() const
68{
69 return d->name;
70}
71
72int ActionList::id() const
73{
74 return d->id;
75}
76
77IconAction *ActionList::action( QString name ) const
78{
79 return d->actions[name];
80}
81
82QStringList ActionList::actions() const
83{
84 return d->sortedActions;
85}
86
87void ActionList::addAction( QString name, IconAction *action )
88{
89 d->sortedActions << name;
90
91 if ( action ) {
92 action->setName( name.latin1() );
93 d->actions.insert( name, action );
94 d->connect( action, SIGNAL( destroyed(QObject *) ), d, SLOT( actionDestroyed(QObject *) ) );
95 }
96}
97
98void ActionList::clear()
99{
100 d->actions.clear();
101}
102
103ActionList::Private::Private( const Private &from )
104 : QObject()
105{
106 name = from.name;
107 id = from.id;
108
109 actions = from.actions;
110 actions.setAutoDelete( from.actions.autoDelete() );
111
112 sortedActions = from.sortedActions;
113}
114
115void ActionList::Private::actionDestroyed(QObject *obj)
116{
117 bool autoDelete = actions.autoDelete();
118 actions.setAutoDelete( false );
119
120 actions.remove( obj->name() );
121
122 actions.setAutoDelete( autoDelete );
123}
124
125//----------------------------------------------------------------------------
126// MetaActionList
127//----------------------------------------------------------------------------
128
129class MetaActionList::Private
130{
131public:
132 Private() { }
133
134 QPtrList<ActionList> lists;
135};
136
137MetaActionList::MetaActionList()
138{
139 d = new Private();
140 d->lists.setAutoDelete( true );
141}
142
143MetaActionList::~MetaActionList()
144{
145 delete d;
146}
147
148ActionList *MetaActionList::actionList( QString name ) const
149{
150 QPtrListIterator<ActionList> it(d->lists);
151 for ( ; it.current() != 0; ++it ) {
152 if ( it.current()->name() == name )
153 return it.current();
154 }
155
156 return 0;
157}
158
159QPtrList<ActionList> MetaActionList::actionLists( int id ) const
160{
161 QPtrList<ActionList> list;
162
163 for ( int i = 0; i < 32; i++ ) {
164 if ( !(id & ( 1 << i )) )
165 continue;
166
167 QPtrListIterator<ActionList> it(d->lists);
168 for ( ; it.current() != 0; ++it ) {
169 ActionList *l = it.current();
170
171 if ( l->id() & ( 1 << i ) )
172 list.append( l );
173 }
174 }
175
176 return list;
177}
178
179ActionList MetaActionList::suitableActions( int id ) const
180{
181 QPtrList<ActionList> lists = actionLists( id );
182 ActionList actions("", 0, false), *list;
183
184 QPtrListIterator<ActionList> it( lists );
185 while ( (list = it.current()) ) {
186 ++it;
187 QStringList actionList = list->actions();
188 QStringList::Iterator it2 = actionList.begin();
189 for ( ; it2 != actionList.end(); ++it2 )
190 actions.addAction( *it2, list->action( *it2 ) );
191 }
192
193 return actions;
194}
195
196QStringList MetaActionList::actionLists() const
197{
198 QStringList names;
199
200 QPtrListIterator<ActionList> it(d->lists);
201 for ( ; it.current() != 0; ++it )
202 names << it.current()->name();
203
204 return names;
205}
206
207void MetaActionList::addList( ActionList *list )
208{
209 if ( list )
210 d->lists.append( list );
211}
212
213void MetaActionList::clear()
214{
215 QPtrListIterator<ActionList> it(d->lists);
216 for ( ; it.current() != 0; ++it )
217 it.current()->clear();
218}
219
220#include "actionlist.moc"
Note: See TracBrowser for help on using the repository browser.