Test our newly acquired shortcut capabilities by finally allowing Ctrl+F to trigger...
[quassel.git] / src / uisupport / actioncollection.cpp
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  * Parts of this implementation are based on KDE's KActionCollection.      *
21  ***************************************************************************/
22
23 #include <QAction>
24 #include <QDebug>
25
26 #include "actioncollection.h"
27
28 #include "action.h"
29
30 ActionCollection::ActionCollection(QObject *parent) : QObject(parent) {
31   _connectTriggered = _connectHovered = false;
32 }
33
34 ActionCollection::~ActionCollection() {
35
36 }
37
38 void ActionCollection::clear() {
39   _actionByName.clear();
40   qDeleteAll(_actions);
41   _actions.clear();
42 }
43
44 QAction *ActionCollection::action(const QString &name) const {
45   return _actionByName.value(name, 0);
46 }
47
48 QList<QAction *> ActionCollection::actions() const {
49   return _actions;
50 }
51
52 Action *ActionCollection::addAction(const QString &name, Action *action) {
53   QAction *act = addAction(name, static_cast<QAction *>(action));
54   Q_ASSERT(act == action);
55   return action;
56 }
57
58 Action *ActionCollection::addAction(const QString &name, const QObject *receiver, const char *member) {
59   Action *a = new Action(this);
60   if(receiver && member)
61     connect(a, SIGNAL(triggered(bool)), receiver, member);
62   return addAction(name, a);
63 }
64
65 QAction *ActionCollection::addAction(const QString &name, QAction *action) {
66   if(!action)
67     return action;
68
69   const QString origName = action->objectName();
70   QString indexName = name;
71
72   if(indexName.isEmpty())
73     indexName = action->objectName();
74   else
75     action->setObjectName(indexName);
76   if(indexName.isEmpty())
77     indexName = QString("unnamed-%1").arg((int)action, 16);
78
79   // do we already have this action?
80   if(_actionByName.value(indexName, 0) == action)
81     return action;
82   // or maybe another action under this name?
83   if(QAction *oldAction = _actionByName.value(indexName))
84     takeAction(oldAction);
85
86   // do we already have this action under a different name?
87   int oldIndex = _actions.indexOf(action);
88   if(oldIndex != -1) {
89     _actionByName.remove(origName);
90     _actions.removeAt(oldIndex);
91   }
92
93   // add action
94   _actionByName.insert(indexName, action);
95   _actions.append(action);
96
97   foreach(QWidget *widget, _associatedWidgets) {
98     widget->addAction(action);
99   }
100
101   connect(action, SIGNAL(destroyed(QObject *)), SLOT(actionDestroyed(QObject *)));
102   if(_connectHovered)
103     connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
104   if(_connectTriggered)
105     connect(action, SIGNAL(triggered(bool)), SLOT(slotActionTriggered()));
106
107   emit inserted(action);
108   return action;
109 }
110
111 void ActionCollection::removeAction(QAction *action) {
112   delete takeAction(action);
113 }
114
115 QAction *ActionCollection::takeAction(QAction *action) {
116   if(!unlistAction(action))
117     return 0;
118
119   foreach(QWidget *widget, _associatedWidgets) {
120     widget->removeAction(action);
121   }
122
123   action->disconnect(this);
124   return action;
125 }
126
127 void ActionCollection::readSettings() {
128
129 }
130
131 void ActionCollection::writeSettings() const {
132
133
134 }
135
136 void ActionCollection::slotActionTriggered() {
137   QAction *action = qobject_cast<QAction *>(sender());
138   if(action)
139     emit actionTriggered(action);
140 }
141
142 void ActionCollection::slotActionHovered() {
143   QAction *action = qobject_cast<QAction *>(sender());
144   if(action)
145     emit actionHovered(action);
146 }
147
148 void ActionCollection::actionDestroyed(QObject *obj) {
149   // remember that this is not an QAction anymore at this point
150   QAction *action = static_cast<QAction *>(obj);
151
152   unlistAction(action);
153 }
154
155 void ActionCollection::connectNotify(const char *signal) {
156   if(_connectHovered && _connectTriggered)
157     return;
158
159   if(QMetaObject::normalizedSignature(SIGNAL(actionHovered(QAction*))) == signal) {
160     if(!_connectHovered) {
161       _connectHovered = true;
162       foreach (QAction* action, actions())
163         connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
164     }
165   } else if(QMetaObject::normalizedSignature(SIGNAL(actionTriggered(QAction*))) == signal) {
166     if(!_connectTriggered) {
167       _connectTriggered = true;
168       foreach (QAction* action, actions())
169         connect(action, SIGNAL(triggered(bool)), SLOT(slotActionTriggered()));
170     }
171   }
172
173   QObject::connectNotify(signal);
174 }
175
176 void ActionCollection::associateWidget(QWidget *widget) const {
177   foreach(QAction *action, actions()) {
178     if(!widget->actions().contains(action))
179       widget->addAction(action);
180   }
181 }
182
183 void ActionCollection::addAssociatedWidget(QWidget *widget) {
184   if(!_associatedWidgets.contains(widget)) {
185     widget->addActions(actions());
186     _associatedWidgets.append(widget);
187     connect(widget, SIGNAL(destroyed(QObject *)), SLOT(associatedWidgetDestroyed(QObject *)));
188   }
189 }
190
191 void ActionCollection::removeAssociatedWidget(QWidget *widget) {
192   foreach(QAction *action, actions())
193     widget->removeAction(action);
194   _associatedWidgets.removeAll(widget);
195   disconnect(widget, SIGNAL(destroyed(QObject *)), this, SLOT(associatedWidgetDestroyed(QObject *)));
196 }
197
198 QList<QWidget *> ActionCollection::associatedWidgets() const {
199   return _associatedWidgets;
200 }
201
202 void ActionCollection::clearAssociatedWidgets() {
203   foreach(QWidget *widget, _associatedWidgets)
204     foreach(QAction *action, actions())
205       widget->removeAction(action);
206
207   _associatedWidgets.clear();
208 }
209
210 void ActionCollection::associatedWidgetDestroyed(QObject *obj) {
211   _associatedWidgets.removeAll(static_cast<QWidget *>(obj));
212 }
213
214 bool ActionCollection::unlistAction(QAction *action) {
215   // This might be called with a partly destroyed QAction!
216
217   int index = _actions.indexOf(action);
218   if(index == -1) return false;
219
220   QString name = action->objectName();
221   _actionByName.remove(name);
222   _actions.removeAt(index);
223
224   // TODO: remove from ActionCategory if we ever get that
225
226   return true;
227 }