| 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 |
|
|---|
| 32 | class ActionList::Private : public QObject
|
|---|
| 33 | {
|
|---|
| 34 | Q_OBJECT
|
|---|
| 35 | public:
|
|---|
| 36 | Private() { }
|
|---|
| 37 | Private( const Private & );
|
|---|
| 38 |
|
|---|
| 39 | QString name;
|
|---|
| 40 | int id;
|
|---|
| 41 | QStringList sortedActions;
|
|---|
| 42 | QDict<IconAction> actions;
|
|---|
| 43 |
|
|---|
| 44 | public slots:
|
|---|
| 45 | void actionDestroyed(QObject *);
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | ActionList::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 |
|
|---|
| 57 | ActionList::ActionList( const ActionList &from )
|
|---|
| 58 | {
|
|---|
| 59 | d = new Private( *from.d );
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | ActionList::~ActionList()
|
|---|
| 63 | {
|
|---|
| 64 | delete d;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | QString ActionList::name() const
|
|---|
| 68 | {
|
|---|
| 69 | return d->name;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int ActionList::id() const
|
|---|
| 73 | {
|
|---|
| 74 | return d->id;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | IconAction *ActionList::action( QString name ) const
|
|---|
| 78 | {
|
|---|
| 79 | return d->actions[name];
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | QStringList ActionList::actions() const
|
|---|
| 83 | {
|
|---|
| 84 | return d->sortedActions;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void 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 |
|
|---|
| 98 | void ActionList::clear()
|
|---|
| 99 | {
|
|---|
| 100 | d->actions.clear();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | ActionList::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 |
|
|---|
| 115 | void 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 |
|
|---|
| 129 | class MetaActionList::Private
|
|---|
| 130 | {
|
|---|
| 131 | public:
|
|---|
| 132 | Private() { }
|
|---|
| 133 |
|
|---|
| 134 | QPtrList<ActionList> lists;
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | MetaActionList::MetaActionList()
|
|---|
| 138 | {
|
|---|
| 139 | d = new Private();
|
|---|
| 140 | d->lists.setAutoDelete( true );
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | MetaActionList::~MetaActionList()
|
|---|
| 144 | {
|
|---|
| 145 | delete d;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | ActionList *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 |
|
|---|
| 159 | QPtrList<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 |
|
|---|
| 179 | ActionList 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 |
|
|---|
| 196 | QStringList 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 |
|
|---|
| 207 | void MetaActionList::addList( ActionList *list )
|
|---|
| 208 | {
|
|---|
| 209 | if ( list )
|
|---|
| 210 | d->lists.append( list );
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void 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"
|
|---|