1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************
20 * Parts of this implementation are based on KDE's KActionCollection. *
21 ***************************************************************************/
23 #include "actioncollection.h"
27 #include <QMetaMethod>
30 #include "uisettings.h"
32 void ActionCollection::addActions(const std::vector<std::pair<QString, Action*>>& actions)
34 for (auto&& p : actions) {
35 addAction(p.first, p.second);
41 int ActionCollection::count() const
43 return actions().count();
46 bool ActionCollection::isEmpty() const
48 return actions().count();
51 void ActionCollection::clear()
53 _actionByName.clear();
58 QAction* ActionCollection::action(const QString& name) const
60 return _actionByName.value(name, 0);
63 QList<QAction*> ActionCollection::actions() const
68 QAction* ActionCollection::addAction(const QString& name, QAction* action)
73 const QString origName = action->objectName();
74 QString indexName = name;
76 if (indexName.isEmpty())
77 indexName = action->objectName();
79 action->setObjectName(indexName);
80 if (indexName.isEmpty())
81 indexName = indexName.sprintf("unnamed-%p", (void*)action);
83 // do we already have this action?
84 if (_actionByName.value(indexName, 0) == action)
86 // or maybe another action under this name?
87 if (QAction* oldAction = _actionByName.value(indexName))
88 takeAction(oldAction);
90 // do we already have this action under a different name?
91 int oldIndex = _actions.indexOf(action);
93 _actionByName.remove(origName);
94 _actions.removeAt(oldIndex);
98 _actionByName.insert(indexName, action);
99 _actions.append(action);
101 foreach (QWidget* widget, _associatedWidgets) {
102 widget->addAction(action);
105 connect(action, &QObject::destroyed, this, &ActionCollection::actionDestroyed);
107 connect(action, &QAction::hovered, this, &ActionCollection::slotActionHovered);
108 if (_connectTriggered)
109 connect(action, &QAction::triggered, this, &ActionCollection::slotActionTriggered);
111 emit inserted(action);
115 void ActionCollection::removeAction(QAction* action)
117 delete takeAction(action);
120 QAction* ActionCollection::takeAction(QAction* action)
122 if (!unlistAction(action))
125 foreach (QWidget* widget, _associatedWidgets) {
126 widget->removeAction(action);
129 action->disconnect(this);
133 void ActionCollection::readSettings()
136 QStringList savedShortcuts = s.savedShortcuts();
138 foreach (const QString& name, _actionByName.keys()) {
139 if (!savedShortcuts.contains(name))
141 auto* action = qobject_cast<Action*>(_actionByName.value(name));
143 action->setShortcut(s.loadShortcut(name), Action::ActiveShortcut);
147 void ActionCollection::writeSettings() const
150 foreach (const QString& name, _actionByName.keys()) {
151 auto* action = qobject_cast<Action*>(_actionByName.value(name));
154 if (!action->isShortcutConfigurable())
156 if (action->shortcut(Action::ActiveShortcut) == action->shortcut(Action::DefaultShortcut))
158 s.saveShortcut(name, action->shortcut(Action::ActiveShortcut));
162 void ActionCollection::slotActionTriggered()
164 auto* action = qobject_cast<QAction*>(sender());
166 emit actionTriggered(action);
169 void ActionCollection::slotActionHovered()
171 auto* action = qobject_cast<QAction*>(sender());
173 emit actionHovered(action);
176 void ActionCollection::actionDestroyed(QObject* obj)
178 // remember that this is not an QAction anymore at this point
179 auto* action = static_cast<QAction*>(obj);
181 unlistAction(action);
184 void ActionCollection::connectNotify(const QMetaMethod& signal)
186 if (_connectHovered && _connectTriggered)
189 if (QMetaMethod::fromSignal(&ActionCollection::actionHovered) == signal) {
190 if (!_connectHovered) {
191 _connectHovered = true;
192 foreach (QAction* action, actions())
193 connect(action, &QAction::hovered, this, &ActionCollection::slotActionHovered);
196 else if (QMetaMethod::fromSignal(&ActionCollection::actionTriggered) == signal) {
197 if (!_connectTriggered) {
198 _connectTriggered = true;
199 foreach (QAction* action, actions())
200 connect(action, &QAction::triggered, this, &ActionCollection::slotActionTriggered);
204 QObject::connectNotify(signal);
207 void ActionCollection::associateWidget(QWidget* widget) const
209 foreach (QAction* action, actions()) {
210 if (!widget->actions().contains(action))
211 widget->addAction(action);
215 void ActionCollection::addAssociatedWidget(QWidget* widget)
217 if (!_associatedWidgets.contains(widget)) {
218 widget->addActions(actions());
219 _associatedWidgets.append(widget);
220 connect(widget, &QObject::destroyed, this, &ActionCollection::associatedWidgetDestroyed);
224 void ActionCollection::removeAssociatedWidget(QWidget* widget)
226 foreach (QAction* action, actions())
227 widget->removeAction(action);
228 _associatedWidgets.removeAll(widget);
229 disconnect(widget, &QObject::destroyed, this, &ActionCollection::associatedWidgetDestroyed);
232 QList<QWidget*> ActionCollection::associatedWidgets() const
234 return _associatedWidgets;
237 void ActionCollection::clearAssociatedWidgets()
239 foreach (QWidget* widget, _associatedWidgets)
240 foreach (QAction* action, actions())
241 widget->removeAction(action);
243 _associatedWidgets.clear();
246 void ActionCollection::associatedWidgetDestroyed(QObject* obj)
248 _associatedWidgets.removeAll(static_cast<QWidget*>(obj));
251 bool ActionCollection::unlistAction(QAction* action)
253 // This might be called with a partly destroyed QAction!
255 int index = _actions.indexOf(action);
259 QString name = action->objectName();
260 _actionByName.remove(name);
261 _actions.removeAt(index);
263 // TODO: remove from ActionCategory if we ever get that
268 #endif /* HAVE_KDE */