36 #include "kaction_p.h"
38 #include <QtXml/QDomDocument>
39 #include <QtCore/QSet>
40 #include <QtCore/QMap>
41 #include <QtCore/QList>
42 #include <QtGui/QAction>
48 class KActionCollectionPrivate
51 KActionCollectionPrivate()
52 : m_parentGUIClient(0L),
53 configGroup(
"Shortcuts"),
54 configIsGlobal(false),
55 connectTriggered(false),
56 connectHovered(false),
62 void setComponentForAction(
KAction *kaction)
63 { kaction->d->maybeSetComponentData(m_componentData); }
65 static QList<KActionCollection*> s_allCollections;
67 void _k_associatedWidgetDestroyed(
QObject *obj);
68 void _k_actionDestroyed(
QObject *obj);
70 bool writeKXMLGUIConfigFile();
79 QList<QAction*> actions;
84 bool configIsGlobal : 1;
86 bool connectTriggered : 1;
87 bool connectHovered : 1;
91 QList<QWidget*> associatedWidgets;
94 QList<KActionCollection*> KActionCollectionPrivate::s_allCollections;
98 , d(new KActionCollectionPrivate)
101 KActionCollectionPrivate::s_allCollections.append(
this);
108 , d(new KActionCollectionPrivate)
111 KActionCollectionPrivate::s_allCollections.append(
this);
113 d->m_parentGUIClient=parent;
119 KActionCollectionPrivate::s_allCollections.removeAll(
this);
126 d->actionByName.clear();
127 qDeleteAll(d->actions);
135 if ( !name.isEmpty() )
136 action = d->actionByName.value (name);
149 return d->actions.count();
166 kWarning(129) <<
"this does not work on a KActionCollection containing actions!";
170 d->m_componentData = cData;
178 return d->m_componentData;
183 return d->m_parentGUIClient;
195 if (!action->actionGroup())
202 QSet<QActionGroup*>
set;
204 if (action->actionGroup())
205 set.
insert(action->actionGroup());
212 Q_ASSERT(ret == action);
222 const QString objectName = action->objectName();
225 if (indexName.isEmpty()) {
227 indexName = objectName;
232 if ((!objectName.isEmpty()) && (objectName != indexName)) {
237 kDebug(125) <<
"Registering action " << objectName <<
" under new name " << indexName;
243 kError() <<
"Changing action name from " << objectName <<
" to " << indexName <<
"\nignored because of active global shortcut.";
244 indexName = objectName;
249 action->setObjectName(indexName);
254 if( indexName.isEmpty() ) {
255 indexName = indexName.sprintf(
"unnamed-%p", (
void*)action);
256 action->setObjectName(indexName);
261 Q_ASSERT(!action->objectName().isEmpty());
264 if (d->actionByName.value(indexName, 0) ==
action ) {
266 Q_ASSERT( d->actionByName.count(indexName)==1);
272 action->setEnabled(
false);
273 action->setVisible(
false);
274 action->blockSignals(
true);
278 if (
QAction *oldAction = d->actionByName.value(indexName)) {
285 const int oldIndex = d->actions.indexOf(action);
286 if (oldIndex != -1) {
287 d->actionByName.remove(d->actionByName.key(action));
288 d->actions.removeAt(oldIndex);
292 d->actionByName.insert(indexName, action);
293 d->actions.append(action);
295 foreach (
QWidget* widget, d->associatedWidgets) {
296 widget->addAction(action);
299 connect(action, SIGNAL(destroyed(
QObject*)), SLOT(_k_actionDestroyed(
QObject*)));
302 if (
KAction *kaction = dynamic_cast<KAction *>(action)) {
303 d->setComponentForAction(kaction);
306 if (d->connectHovered)
307 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
309 if (d->connectTriggered)
323 if (!d->unlistAction(action))
327 foreach (
QWidget* widget, d->associatedWidgets) {
328 widget->removeAction(action);
331 action->disconnect(
this);
344 const QObject *receiver,
const char *member)
351 action->setParent(
this);
353 action->setObjectName(name);
361 if (receiver && member)
362 connect(a, SIGNAL(triggered(
bool)), receiver, member);
368 return d->configGroup;
373 d->configGroup = group;
378 return d->configIsGlobal;
383 d->configIsGlobal = global;
389 if( !config || !config->
exists()) {
394 it != d->actionByName.constEnd(); ++it) {
403 if( !entry.isEmpty() ) {
424 it != d->actionByName.constEnd(); ++it) {
433 if( !entry.isEmpty() ) {
451 QList<QAction*> writeActions =
actions();
454 it != d->actionByName.constEnd(); ++it) {
463 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
464 kError() <<
"Skipped exporting Shortcut for action without name " << kaction->text() <<
"!";
469 bool bConfigHasAction = !config->
readEntry( actionName,
QString() ).isEmpty();
476 if( writeAll || !bSameAsDefault ) {
480 kDebug(125) <<
"\twriting " << actionName <<
" = " << s;
485 else if( bConfigHasAction ) {
486 kDebug(125) <<
"\tremoving " << actionName <<
" because == default";
496 bool KActionCollectionPrivate::writeKXMLGUIConfigFile()
500 if (!kxmlguiClient || kxmlguiClient->
xmlFile().isEmpty()) {
506 QString attrShortcut = QLatin1String(
"shortcut");
511 doc.setContent( sXml );
520 it != actionByName.constEnd(); ++it) {
530 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
531 kError() <<
"Skipped writing shortcut for action " << actionName <<
"(" << kaction->text() <<
")!";
536 kDebug(129) <<
"name = " << actionName
544 if ( act_elem.isNull() )
547 if( bSameAsDefault ) {
548 act_elem.removeAttribute( attrShortcut );
550 if( act_elem.attributes().count() == 1 )
551 elem.removeChild( act_elem );
567 if (config==0 && d->writeKXMLGUIConfigFile() ) {
576 QList<QAction*> writeActions;
578 writeActions.append(oneAction);
585 it != d->actionByName.constEnd(); ++it) {
597 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
598 kError() <<
"Skipped saving Shortcut for action without name " << kaction->text() <<
"!";
604 bool bConfigHasAction = !config->
readEntry( actionName,
QString() ).isEmpty();
615 if( writeAll || !bSameAsDefault ) {
621 kDebug(125) <<
"\twriting " << actionName <<
" = " << s;
624 }
else if( bConfigHasAction ) {
627 kDebug(125) <<
"\tremoving " << actionName <<
" because == default";
648 void KActionCollection::slotActionHovered( )
658 void KActionCollectionPrivate::_k_actionDestroyed(
QObject *obj )
664 if (!unlistAction(action))
668 emit q->removed(action);
673 if (d->connectHovered && d->connectTriggered)
678 if (!d->connectHovered) {
679 d->connectHovered =
true;
681 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
685 if (!d->connectTriggered) {
686 d->connectTriggered =
true;
697 return KActionCollectionPrivate::s_allCollections;
703 if (!widget->actions().contains(action))
704 widget->addAction(action);
710 if (!d->associatedWidgets.contains(widget)) {
713 d->associatedWidgets.append(widget);
714 connect(widget, SIGNAL(destroyed(
QObject*)),
this, SLOT(_k_associatedWidgetDestroyed(
QObject*)));
721 widget->removeAction(action);
723 d->associatedWidgets.removeAll(widget);
724 disconnect(widget, SIGNAL(destroyed(
QObject*)),
this, SLOT(_k_associatedWidgetDestroyed(
QObject*)));
736 int index = actions.indexOf(action);
739 if (index==-1)
return NULL;
742 Q_ASSERT(actions.indexOf(action,index+1)==-1);
748 actionByName.remove(name);
749 actions.removeAt(index);
752 QList<KActionCategory*> categories = q->findChildren<
KActionCategory*>();
754 category->unlistAction(action);
763 return d->associatedWidgets;
768 foreach (
QWidget* widget, d->associatedWidgets)
770 widget->removeAction(action);
772 d->associatedWidgets.clear();
775 void KActionCollectionPrivate::_k_associatedWidgetDestroyed(
QObject *obj)
777 associatedWidgets.removeAll(static_cast<QWidget*>(obj));
783 #include "kactioncollection.moc"