Add ActionCollection to group and manage (Q)Actions.
[quassel.git] / src / uisupport / actioncollection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (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 program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************
20  * This is a subset of the API of KDE's KActionCollection.                 *
21  ***************************************************************************/
22
23 #ifndef ACTIONCOLLECTION_H_
24 #define ACTIONCOLLECTION_H_
25
26 #include <QList>
27 #include <QMap>
28 #include <QObject>
29 #include <QString>
30
31 class QWidget;
32
33 class Action;
34 class QAction;
35
36 class ActionCollection : public QObject {
37   Q_OBJECT
38
39   public:
40     explicit ActionCollection(QObject *parent);
41     virtual ~ActionCollection();
42
43     /// Clears the entire action collection, deleting all actions.
44     void clear();
45
46     /// Associate all action in this collection to the given \a widget.
47     /** Not that this only adds all current actions in the collection to that widget;
48      *  subsequently added actions won't be added automatically.
49      */
50     void associateWidget(QWidget *widget) const;
51
52     /// Associate all actions in this collection to the given \a widget.
53     /** Subsequently added actions will be automagically associated with this widget as well.
54      */
55     void addAssociatedWidget(QWidget *widget);
56
57     void removeAssociatedWidget(QWidget *widget);
58     QList<QWidget *> associatedWidgets() const;
59     void clearAssociatedWidgets();
60
61     void readSettings();
62     void writeSettings() const;
63
64     inline int count() const;
65     inline bool isEmpty() const;
66
67     QAction *action(int index) const;
68     QAction *action(const QString &name) const;
69     QList<QAction *> actions() const;
70
71     QAction *addAction(const QString &name, QAction *action);
72     Action *addAction(const QString &name, Action *action);
73     Action *addAction(const QString &name, const QObject *receiver = 0, const char *member = 0);
74     void removeAction(QAction *action);
75     QAction *takeAction(QAction *action);
76
77     /// Create new action under the given name, add it to the collection and connect its triggered(bool) signal to the specified receiver.
78     template<class ActionType>
79     ActionType *add(const QString &name, const QObject *receiver = 0, const char *member = 0) {
80       ActionType *a = new ActionType(this);
81       if(receiver && member)
82         connect(a, SIGNAL(triggered(bool)), receiver, member);
83       addAction(name, a);
84       return a;
85     }
86
87   signals:
88     void inserted(QAction *action);
89     void actionHovered(QAction *action);
90     void actionTriggered(QAction *action);
91
92   protected slots:
93     virtual void connectNotify(const char *signal);
94     virtual void slotActionTriggered();
95
96   private slots:
97     void slotActionHovered();
98     void actionDestroyed(QObject *);
99     void associatedWidgetDestroyed(QObject *);
100
101   private:
102     bool unlistAction(QAction *);
103
104     QMap<QString, QAction *> _actionByName;
105     QList<QAction *> _actions;
106     QList<QWidget *> _associatedWidgets;
107
108     bool _connectHovered;
109     bool _connectTriggered;
110 };
111
112 int ActionCollection::count() const { return actions().count(); }
113 bool ActionCollection::isEmpty() const { return actions().count(); }
114
115
116 #endif