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