src: Yearly copyright bump
[quassel.git] / src / uisupport / actioncollection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************
20  * This is a subset of the API of KDE's KActionCollection.                 *
21  ***************************************************************************/
22
23 #pragma once
24
25 #ifndef HAVE_KDE
26
27 #include <QDebug>
28 #include <QList>
29 #include <QMap>
30 #include <QObject>
31 #include <QString>
32
33 class QWidget;
34
35 class Action;
36 class QAction;
37
38 class ActionCollection : public QObject
39 {
40     Q_OBJECT
41
42 public:
43     explicit ActionCollection(QObject *parent);
44     virtual ~ActionCollection();
45
46     /// Clears the entire action collection, deleting all actions.
47     void clear();
48
49     /// Associate all action in this collection to the given \a widget.
50     /** Not that this only adds all current actions in the collection to that widget;
51      *  subsequently added actions won't be added automatically.
52      */
53     void associateWidget(QWidget *widget) const;
54
55     /// Associate all actions in this collection to the given \a widget.
56     /** Subsequently added actions will be automagically associated with this widget as well.
57      */
58     void addAssociatedWidget(QWidget *widget);
59
60     void removeAssociatedWidget(QWidget *widget);
61     QList<QWidget *> associatedWidgets() const;
62     void clearAssociatedWidgets();
63
64     void readSettings();
65     void writeSettings() const;
66
67     inline int count() const;
68     inline bool isEmpty() const;
69
70     QAction *action(int index) const;
71     QAction *action(const QString &name) const;
72     QList<QAction *> actions() const;
73
74     QAction *addAction(const QString &name, QAction *action);
75     Action *addAction(const QString &name, Action *action);
76     Action *addAction(const QString &name, const QObject *receiver = 0, const char *member = 0);
77     void removeAction(QAction *action);
78     QAction *takeAction(QAction *action);
79
80     /// Create new action under the given name, add it to the collection and connect its triggered(bool) signal to the specified receiver.
81     template<class ActionType>
82     ActionType *add(const QString &name, const QObject *receiver = 0, const char *member = 0)
83     {
84         ActionType *a = new ActionType(this);
85         if (receiver && member)
86             connect(a, SIGNAL(triggered(bool)), receiver, member);
87         addAction(name, a);
88         return a;
89     }
90
91
92 signals:
93     void inserted(QAction *action);
94     void actionHovered(QAction *action);
95     void actionTriggered(QAction *action);
96
97 protected slots:
98 #if QT_VERSION >= 0x050000
99     virtual void connectNotify(const QMetaMethod &signal);
100 #else
101     virtual void connectNotify(const char *signal);
102 #endif
103     virtual void slotActionTriggered();
104
105 private slots:
106     void slotActionHovered();
107     void actionDestroyed(QObject *);
108     void associatedWidgetDestroyed(QObject *);
109
110 private:
111     bool unlistAction(QAction *);
112
113     QMap<QString, QAction *> _actionByName;
114     QList<QAction *> _actions;
115     QList<QWidget *> _associatedWidgets;
116
117     bool _connectHovered;
118     bool _connectTriggered;
119 };
120
121
122 int ActionCollection::count() const { return actions().count(); }
123 bool ActionCollection::isEmpty() const { return actions().count(); }
124
125 #else /* HAVE_KDE */
126 #  ifdef HAVE_KDE4
127 #    include <KActionCollection>
128 #  else
129 #    include <KXmlGui/KActionCollection>
130 #  endif
131
132 class ActionCollection : public KActionCollection
133 {
134     Q_OBJECT
135
136 public:
137     explicit ActionCollection(QObject *parent) : KActionCollection(parent) {};
138 };
139
140 #endif /* HAVE_KDE */