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 ***************************************************************************/
26 #include "actioncollection.h"
30 ActionCollection::ActionCollection(QObject *parent) : QObject(parent) {
31 _connectTriggered = _connectHovered = false;
34 ActionCollection::~ActionCollection() {
38 void ActionCollection::clear() {
39 _actionByName.clear();
44 QAction *ActionCollection::action(const QString &name) const {
45 return _actionByName.value(name, 0);
48 QList<QAction *> ActionCollection::actions() const {
52 Action *ActionCollection::addAction(const QString &name, Action *action) {
53 QAction *act = addAction(name, static_cast<QAction *>(action));
54 Q_ASSERT(act == action);
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);
65 QAction *ActionCollection::addAction(const QString &name, QAction *action) {
69 const QString origName = action->objectName();
70 QString indexName = name;
72 if(indexName.isEmpty())
73 indexName = action->objectName();
75 action->setObjectName(indexName);
76 if(indexName.isEmpty())
77 indexName = QString("unnamed-%1").arg((int)action, 16);
79 // do we already have this action?
80 if(_actionByName.value(indexName, 0) == action)
82 // or maybe another action under this name?
83 if(QAction *oldAction = _actionByName.value(indexName))
84 takeAction(oldAction);
86 // do we already have this action under a different name?
87 int oldIndex = _actions.indexOf(action);
89 _actionByName.remove(origName);
90 _actions.removeAt(oldIndex);
94 _actionByName.insert(indexName, action);
95 _actions.append(action);
97 foreach(QWidget *widget, _associatedWidgets) {
98 widget->addAction(action);
101 connect(action, SIGNAL(destroyed(QObject *)), SLOT(actionDestroyed(QObject *)));
103 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
104 if(_connectTriggered)
105 connect(action, SIGNAL(triggered(bool)), SLOT(slotActionTriggered()));
107 emit inserted(action);
111 void ActionCollection::removeAction(QAction *action) {
112 delete takeAction(action);
115 QAction *ActionCollection::takeAction(QAction *action) {
116 if(!unlistAction(action))
119 foreach(QWidget *widget, _associatedWidgets) {
120 widget->removeAction(action);
123 action->disconnect(this);
127 void ActionCollection::readSettings() {
131 void ActionCollection::writeSettings() const {
136 void ActionCollection::slotActionTriggered() {
137 QAction *action = qobject_cast<QAction *>(sender());
139 emit actionTriggered(action);
142 void ActionCollection::slotActionHovered() {
143 QAction *action = qobject_cast<QAction *>(sender());
145 emit actionHovered(action);
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);
152 unlistAction(action);
155 void ActionCollection::connectNotify(const char *signal) {
156 if(_connectHovered && _connectTriggered)
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()));
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()));
173 QObject::connectNotify(signal);
176 void ActionCollection::associateWidget(QWidget *widget) const {
177 foreach(QAction *action, actions()) {
178 if(!widget->actions().contains(action))
179 widget->addAction(action);
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 *)));
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 *)));
198 QList<QWidget *> ActionCollection::associatedWidgets() const {
199 return _associatedWidgets;
202 void ActionCollection::clearAssociatedWidgets() {
203 foreach(QWidget *widget, _associatedWidgets)
204 foreach(QAction *action, actions())
205 widget->removeAction(action);
207 _associatedWidgets.clear();
210 void ActionCollection::associatedWidgetDestroyed(QObject *obj) {
211 _associatedWidgets.removeAll(static_cast<QWidget *>(obj));
214 bool ActionCollection::unlistAction(QAction *action) {
215 // This might be called with a partly destroyed QAction!
217 int index = _actions.indexOf(action);
218 if(index == -1) return false;
220 QString name = action->objectName();
221 _actionByName.remove(name);
222 _actions.removeAt(index);
224 // TODO: remove from ActionCategory if we ever get that