cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / uisupport / actioncollection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 #include <utility>
28 #include <vector>
29
30 #ifdef HAVE_KDE
31 #    include <KXmlGui/KActionCollection>
32 #endif
33
34 #include <QDebug>
35 #include <QList>
36 #include <QMap>
37 #include <QObject>
38 #include <QString>
39
40 class QWidget;
41
42 class Action;
43 class QAction;
44
45 #ifdef HAVE_KDE
46 using ActionCollectionParent = KActionCollection;
47 #else
48 using ActionCollectionParent = QObject;
49 #endif
50
51 class UISUPPORT_EXPORT ActionCollection : public ActionCollectionParent
52 {
53     Q_OBJECT
54
55 public:
56     using ActionCollectionParent::ActionCollectionParent;
57
58     // Convenience function that takes a list of Actions
59     void addActions(const std::vector<std::pair<QString, Action*>>& actions);
60
61 // Everything else is defined in KActionCollection
62 #ifndef HAVE_KDE
63     /// Clears the entire action collection, deleting all actions.
64     void clear();
65
66     /// Associate all action in this collection to the given \a widget.
67     /** Not that this only adds all current actions in the collection to that widget;
68      *  subsequently added actions won't be added automatically.
69      */
70     void associateWidget(QWidget* widget) const;
71
72     /// Associate all actions in this collection to the given \a widget.
73     /** Subsequently added actions will be automagically associated with this widget as well.
74      */
75     void addAssociatedWidget(QWidget* widget);
76
77     void removeAssociatedWidget(QWidget* widget);
78     QList<QWidget*> associatedWidgets() const;
79     void clearAssociatedWidgets();
80
81     void readSettings();
82     void writeSettings() const;
83
84     int count() const;
85     bool isEmpty() const;
86
87     QAction* action(int index) const;
88     QAction* action(const QString& name) const;
89     QList<QAction*> actions() const;
90
91     QAction* addAction(const QString& name, QAction* action);
92     void removeAction(QAction* action);
93     QAction* takeAction(QAction* action);
94
95 signals:
96     void inserted(QAction* action);
97     void actionHovered(QAction* action);
98     void actionTriggered(QAction* action);
99
100 protected slots:
101     void connectNotify(const QMetaMethod& signal) override;
102     virtual void slotActionTriggered();
103
104 private slots:
105     void slotActionHovered();
106     void actionDestroyed(QObject*);
107     void associatedWidgetDestroyed(QObject*);
108
109 private:
110     bool unlistAction(QAction*);
111
112     QMap<QString, QAction*> _actionByName;
113     QList<QAction*> _actions;
114     QList<QWidget*> _associatedWidgets;
115
116     bool _connectHovered{false};
117     bool _connectTriggered{false};
118
119 #endif /* HAVE_KDE */
120 };