1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
28 #include "actioncollection.h"
32 ActionCollection::ActionCollection(QObject *parent) : QObject(parent) {
33 _connectTriggered = _connectHovered = false;
36 ActionCollection::~ActionCollection() {
40 void ActionCollection::clear() {
41 _actionByName.clear();
46 QAction *ActionCollection::action(const QString &name) const {
47 return _actionByName.value(name, 0);
50 QList<QAction *> ActionCollection::actions() const {
54 Action *ActionCollection::addAction(const QString &name, Action *action) {
55 QAction *act = addAction(name, static_cast<QAction *>(action));
57 Q_ASSERT(act == action);
61 Action *ActionCollection::addAction(const QString &name, const QObject *receiver, const char *member) {
62 Action *a = new Action(this);
63 if(receiver && member)
64 connect(a, SIGNAL(triggered(bool)), receiver, member);
65 return addAction(name, a);
68 QAction *ActionCollection::addAction(const QString &name, QAction *action) {
72 const QString origName = action->objectName();
73 QString indexName = name;
75 if(indexName.isEmpty())
76 indexName = action->objectName();
78 action->setObjectName(indexName);
79 if(indexName.isEmpty())
80 indexName = indexName.sprintf("unnamed-%p", (void *)action);
82 // do we already have this action?
83 if(_actionByName.value(indexName, 0) == action)
85 // or maybe another action under this name?
86 if(QAction *oldAction = _actionByName.value(indexName))
87 takeAction(oldAction);
89 // do we already have this action under a different name?
90 int oldIndex = _actions.indexOf(action);
92 _actionByName.remove(origName);
93 _actions.removeAt(oldIndex);
97 _actionByName.insert(indexName, action);
98 _actions.append(action);
100 foreach(QWidget *widget, _associatedWidgets) {
101 widget->addAction(action);
104 connect(action, SIGNAL(destroyed(QObject *)), SLOT(actionDestroyed(QObject *)));
106 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
107 if(_connectTriggered)
108 connect(action, SIGNAL(triggered(bool)), SLOT(slotActionTriggered()));
110 emit inserted(action);
114 void ActionCollection::removeAction(QAction *action) {
115 delete takeAction(action);
118 QAction *ActionCollection::takeAction(QAction *action) {
119 if(!unlistAction(action))
122 foreach(QWidget *widget, _associatedWidgets) {
123 widget->removeAction(action);
126 action->disconnect(this);
130 void ActionCollection::readSettings() {
134 void ActionCollection::writeSettings() const {
139 void ActionCollection::slotActionTriggered() {
140 QAction *action = qobject_cast<QAction *>(sender());
142 emit actionTriggered(action);
145 void ActionCollection::slotActionHovered() {
146 QAction *action = qobject_cast<QAction *>(sender());
148 emit actionHovered(action);
151 void ActionCollection::actionDestroyed(QObject *obj) {
152 // remember that this is not an QAction anymore at this point
153 QAction *action = static_cast<QAction *>(obj);
155 unlistAction(action);
158 void ActionCollection::connectNotify(const char *signal) {
159 if(_connectHovered && _connectTriggered)
162 if(QMetaObject::normalizedSignature(SIGNAL(actionHovered(QAction*))) == signal) {
163 if(!_connectHovered) {
164 _connectHovered = true;
165 foreach (QAction* action, actions())
166 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
168 } else if(QMetaObject::normalizedSignature(SIGNAL(actionTriggered(QAction*))) == signal) {
169 if(!_connectTriggered) {
170 _connectTriggered = true;
171 foreach (QAction* action, actions())
172 connect(action, SIGNAL(triggered(bool)), SLOT(slotActionTriggered()));
176 QObject::connectNotify(signal);
179 void ActionCollection::associateWidget(QWidget *widget) const {
180 foreach(QAction *action, actions()) {
181 if(!widget->actions().contains(action))
182 widget->addAction(action);
186 void ActionCollection::addAssociatedWidget(QWidget *widget) {
187 if(!_associatedWidgets.contains(widget)) {
188 widget->addActions(actions());
189 _associatedWidgets.append(widget);
190 connect(widget, SIGNAL(destroyed(QObject *)), SLOT(associatedWidgetDestroyed(QObject *)));
194 void ActionCollection::removeAssociatedWidget(QWidget *widget) {
195 foreach(QAction *action, actions())
196 widget->removeAction(action);
197 _associatedWidgets.removeAll(widget);
198 disconnect(widget, SIGNAL(destroyed(QObject *)), this, SLOT(associatedWidgetDestroyed(QObject *)));
201 QList<QWidget *> ActionCollection::associatedWidgets() const {
202 return _associatedWidgets;
205 void ActionCollection::clearAssociatedWidgets() {
206 foreach(QWidget *widget, _associatedWidgets)
207 foreach(QAction *action, actions())
208 widget->removeAction(action);
210 _associatedWidgets.clear();
213 void ActionCollection::associatedWidgetDestroyed(QObject *obj) {
214 _associatedWidgets.removeAll(static_cast<QWidget *>(obj));
217 bool ActionCollection::unlistAction(QAction *action) {
218 // This might be called with a partly destroyed QAction!
220 int index = _actions.indexOf(action);
221 if(index == -1) return false;
223 QString name = action->objectName();
224 _actionByName.remove(name);
225 _actions.removeAt(index);
227 // TODO: remove from ActionCategory if we ever get that
232 #endif /* HAVE_KDE */