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()
54 m_parentGUIClient = 0L;
56 configIsGlobal =
false;
58 connectHovered = connectTriggered =
false;
60 configGroup =
"Shortcuts";
63 void setComponentForAction(
KAction *kaction)
64 { kaction->d->maybeSetComponentData(m_componentData); }
66 static QList<KActionCollection*> s_allCollections;
68 void _k_associatedWidgetDestroyed(
QObject *obj);
69 void _k_actionDestroyed(
QObject *obj);
71 bool writeKXMLGUIConfigFile();
80 QList<QAction*> actions;
85 bool configIsGlobal : 1;
87 bool connectTriggered : 1;
88 bool connectHovered : 1;
92 QList<QWidget*> associatedWidgets;
95 QList<KActionCollection*> KActionCollectionPrivate::s_allCollections;
99 , d(new KActionCollectionPrivate)
102 KActionCollectionPrivate::s_allCollections.append(
this);
109 , d(new KActionCollectionPrivate)
112 KActionCollectionPrivate::s_allCollections.append(
this);
114 d->m_parentGUIClient=parent;
120 KActionCollectionPrivate::s_allCollections.removeAll(
this);
127 d->actionByName.clear();
128 qDeleteAll(d->actions);
136 if ( !name.isEmpty() )
137 action = d->actionByName.value (name);
150 return d->actions.count();
167 kWarning(129) <<
"this does not work on a KActionCollection containing actions!";
171 d->m_componentData = cData;
179 return d->m_componentData;
184 return d->m_parentGUIClient;
196 if (!action->actionGroup())
203 QSet<QActionGroup*>
set;
205 if (action->actionGroup())
206 set.
insert(action->actionGroup());
213 Q_ASSERT(ret == action);
223 const QString objectName = action->objectName();
226 if (indexName.isEmpty()) {
228 indexName = objectName;
233 if ((!objectName.isEmpty()) && (objectName != indexName)) {
238 kDebug(125) <<
"Registering action " << objectName <<
" under new name " << indexName;
244 kError() <<
"Changing action name from " << objectName <<
" to " << indexName <<
"\nignored because of active global shortcut.";
245 indexName = objectName;
250 action->setObjectName(indexName);
255 if( indexName.isEmpty() ) {
256 indexName = indexName.sprintf(
"unnamed-%p", (
void*)action);
257 action->setObjectName(indexName);
262 Q_ASSERT(!action->objectName().isEmpty());
265 if (d->actionByName.value(indexName, 0) ==
action ) {
267 Q_ASSERT( d->actionByName.count(indexName)==1);
273 action->setEnabled(
false);
274 action->setVisible(
false);
275 action->blockSignals(
true);
279 if (
QAction *oldAction = d->actionByName.value(indexName)) {
286 const int oldIndex = d->actions.indexOf(action);
287 if (oldIndex != -1) {
288 d->actionByName.remove(d->actionByName.key(action));
289 d->actions.removeAt(oldIndex);
293 d->actionByName.insert(indexName, action);
294 d->actions.append(action);
296 foreach (
QWidget* widget, d->associatedWidgets) {
297 widget->addAction(action);
300 connect(action, SIGNAL(destroyed(
QObject*)), SLOT(_k_actionDestroyed(
QObject*)));
303 if (
KAction *kaction = dynamic_cast<KAction *>(action)) {
304 d->setComponentForAction(kaction);
307 if (d->connectHovered)
308 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
310 if (d->connectTriggered)
324 if (!d->unlistAction(action))
328 foreach (
QWidget* widget, d->associatedWidgets) {
329 widget->removeAction(action);
332 action->disconnect(
this);
345 const QObject *receiver,
const char *member)
352 action->setParent(
this);
354 action->setObjectName(name);
362 if (receiver && member)
363 connect(a, SIGNAL(triggered(
bool)), receiver, member);
369 return d->configGroup;
374 d->configGroup = group;
379 return d->configIsGlobal;
384 d->configIsGlobal = global;
390 if( !config || !config->
exists()) {
395 it != d->actionByName.constEnd(); ++it) {
404 if( !entry.isEmpty() ) {
425 it != d->actionByName.constEnd(); ++it) {
434 if( !entry.isEmpty() ) {
452 QList<QAction*> writeActions =
actions();
455 it != d->actionByName.constEnd(); ++it) {
464 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
465 kError() <<
"Skipped exporting Shortcut for action without name " << kaction->text() <<
"!";
470 bool bConfigHasAction = !config->
readEntry( actionName,
QString() ).isEmpty();
477 if( writeAll || !bSameAsDefault ) {
481 kDebug(125) <<
"\twriting " << actionName <<
" = " << s;
486 else if( bConfigHasAction ) {
487 kDebug(125) <<
"\tremoving " << actionName <<
" because == default";
497 bool KActionCollectionPrivate::writeKXMLGUIConfigFile()
501 if (!kxmlguiClient || kxmlguiClient->
xmlFile().isEmpty()) {
507 QString attrShortcut = QLatin1String(
"shortcut");
512 doc.setContent( sXml );
521 it != actionByName.constEnd(); ++it) {
531 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
532 kError() <<
"Skipped writing shortcut for action " << actionName <<
"(" << kaction->text() <<
")!";
537 kDebug(129) <<
"name = " << actionName
545 if ( act_elem.isNull() )
548 if( bSameAsDefault ) {
549 act_elem.removeAttribute( attrShortcut );
551 if( act_elem.attributes().count() == 1 )
552 elem.removeChild( act_elem );
568 if (config==0 && d->writeKXMLGUIConfigFile() ) {
577 QList<QAction*> writeActions;
579 writeActions.append(oneAction);
586 it != d->actionByName.constEnd(); ++it) {
598 if (actionName.startsWith(QLatin1String(
"unnamed-"))) {
599 kError() <<
"Skipped saving Shortcut for action without name " << kaction->text() <<
"!";
605 bool bConfigHasAction = !config->
readEntry( actionName,
QString() ).isEmpty();
616 if( writeAll || !bSameAsDefault ) {
622 kDebug(125) <<
"\twriting " << actionName <<
" = " << s;
625 }
else if( bConfigHasAction ) {
628 kDebug(125) <<
"\tremoving " << actionName <<
" because == default";
649 void KActionCollection::slotActionHovered( )
659 void KActionCollectionPrivate::_k_actionDestroyed(
QObject *obj )
665 if (!unlistAction(action))
669 emit q->removed(action);
674 if (d->connectHovered && d->connectTriggered)
679 if (!d->connectHovered) {
680 d->connectHovered =
true;
682 connect(action, SIGNAL(hovered()), SLOT(slotActionHovered()));
686 if (!d->connectTriggered) {
687 d->connectTriggered =
true;
698 return KActionCollectionPrivate::s_allCollections;
704 if (!widget->actions().contains(action))
705 widget->addAction(action);
711 if (!d->associatedWidgets.contains(widget)) {
714 d->associatedWidgets.append(widget);
715 connect(widget, SIGNAL(destroyed(
QObject*)),
this, SLOT(_k_associatedWidgetDestroyed(
QObject*)));
722 widget->removeAction(action);
724 d->associatedWidgets.removeAll(widget);
725 disconnect(widget, SIGNAL(destroyed(
QObject*)),
this, SLOT(_k_associatedWidgetDestroyed(
QObject*)));
737 int index = actions.indexOf(action);
740 if (index==-1)
return NULL;
743 Q_ASSERT(actions.indexOf(action,index+1)==-1);
749 actionByName.remove(name);
750 actions.removeAt(index);
753 QList<KActionCategory*> categories = q->findChildren<
KActionCategory*>();
755 category->unlistAction(action);
764 return d->associatedWidgets;
769 foreach (
QWidget* widget, d->associatedWidgets)
771 widget->removeAction(action);
773 d->associatedWidgets.clear();
776 void KActionCollectionPrivate::_k_associatedWidgetDestroyed(
QObject *obj)
778 associatedWidgets.removeAll(static_cast<QWidget*>(obj));
784 #include "kactioncollection.moc"