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