26 #include "kactioncollection.h"
27 #include "kactionshortcutlist.h"
29 #include "kxmlguifactory.h"
30 #include "kxmlguiclient.h"
33 #include <kaccelbase.h>
34 #include <kapplication.h>
37 #include <tqpopupmenu.h>
38 #include <tqptrdict.h>
39 #include <tqvariant.h>
41 class KActionCollection::KActionCollectionPrivate
44 KActionCollectionPrivate()
49 m_bAutoConnectShortcuts =
true;
51 m_kaccel = m_builderKAccel = 0;
52 m_dctHighlightContainers.setAutoDelete(
true );
54 m_currentHighlightAction = 0;
55 m_statusCleared =
true;
56 m_parentGUIClient = 0L;
61 bool m_bAutoConnectShortcuts;
66 TQValueList<KActionCollection*> m_docList;
71 TQAsciiDict<KAction> m_actionDict;
72 TQPtrDict< TQPtrList<KAction> > m_dctHighlightContainers;
74 KAction *m_currentHighlightAction;
79 KActionCollection::KActionCollection( TQWidget *parent,
const char *name,
81 : TQObject( parent,
name )
83 kdDebug(129) <<
"KActionCollection::KActionCollection( " << parent <<
", " <<
name <<
" ): this = " <<
this <<
endl;
84 d =
new KActionCollectionPrivate;
88 setInstance( instance );
92 KActionCollection::KActionCollection( TQWidget *watch, TQObject* parent,
const char *name,
94 : TQObject( parent, name )
96 kdDebug(129) <<
"KActionCollection::KActionCollection( " << watch <<
", " << parent <<
", " << name <<
" ): this = " <<
this <<
endl;
97 d =
new KActionCollectionPrivate;
104 #ifndef KDE_NO_COMPAT
106 KActionCollection::KActionCollection( TQObject *parent,
const char *name,
108 : TQObject( parent, name )
110 kdWarning(129) <<
"KActionCollection::KActionCollection( TQObject *parent, const char *name, KInstance *instance )" <<
endl;
112 d =
new KActionCollectionPrivate;
113 TQWidget* w = tqt_dynamic_cast<TQWidget*>( parent );
123 kdWarning(129) <<
"KActionCollection::KActionCollection( const KActionCollection & ): function is severely deprecated." <<
endl;
124 d =
new KActionCollectionPrivate;
129 KActionCollection::KActionCollection(
const char *name,
const KXMLGUIClient *parent )
130 : TQObject( 0L,
name )
132 d =
new KActionCollectionPrivate;
133 d->m_parentGUIClient=parent;
138 KActionCollection::~KActionCollection()
140 kdDebug(129) <<
"KActionCollection::~KActionCollection(): this = " <<
this <<
endl;
141 for ( TQAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
142 KAction* pAction = it.current();
143 if ( pAction->m_parentCollection ==
this )
144 pAction->m_parentCollection = 0L;
148 delete d->m_builderKAccel;
159 if ( !d->m_widget ) {
161 d->m_kaccel =
new KAccel( w,
this,
"KActionCollection-KAccel" );
163 else if ( d->m_widget != w )
164 kdWarning(129) <<
"KActionCollection::setWidget(): tried to change widget from " << d->m_widget <<
" to " << w <<
endl;
169 d->m_bAutoConnectShortcuts = b;
174 return d->m_bAutoConnectShortcuts;
179 d->m_docList.append( pDoc );
183 void KActionCollection::beginXMLPlug( TQWidget *widget )
185 kdDebug(129) <<
"KActionCollection::beginXMLPlug( buildWidget = " << widget <<
" ): this = " <<
this <<
" d->m_builderKAccel = " << d->m_builderKAccel <<
endl;
187 if( widget && !d->m_builderKAccel ) {
188 d->m_builderKAccel =
new KAccel( widget,
this,
"KActionCollection-BuilderKAccel" );
192 void KActionCollection::endXMLPlug()
194 kdDebug(129) <<
"KActionCollection::endXMLPlug(): this = " <<
this <<
endl;
198 void KActionCollection::prepareXMLUnplug()
200 kdDebug(129) <<
"KActionCollection::prepareXMLUnplug(): this = " <<
this <<
endl;
201 unplugShortcuts( d->m_kaccel );
203 if( d->m_builderKAccel ) {
204 unplugShortcuts( d->m_builderKAccel );
205 delete d->m_builderKAccel;
206 d->m_builderKAccel = 0;
210 void KActionCollection::unplugShortcuts(
KAccel* kaccel )
212 for ( TQAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
213 KAction* pAction = it.current();
214 pAction->removeKAccel(
kaccel );
217 for( uint i = 0; i < d->m_docList.count(); i++ )
218 d->m_docList[i]->unplugShortcuts(
kaccel );
302 static const char* actionDictKey(
KAction* action,
char* buffer )
304 const char* name = action->name();
305 if( !qstrcmp( name,
"unnamed" ) )
307 sprintf(buffer,
"unnamed-%p", (
void *)action);
313 void KActionCollection::_insert(
KAction* action )
315 char unnamed_name[100];
316 const char *
name = actionDictKey(
action, unnamed_name );
321 d->m_actionDict.insert( name,
action );
326 void KActionCollection::_remove(
KAction* action )
328 char unnamed_name[100];
329 const char *
name = actionDictKey(
action, unnamed_name );
331 KAction *a = d->m_actionDict.take( name );
343 char unnamed_name[100];
344 const char *
name = actionDictKey(
action, unnamed_name );
346 KAction *a = d->m_actionDict.take( name );
350 if ( a->m_parentCollection ==
this )
351 a->m_parentCollection = 0;
358 void KActionCollection::_clear()
360 TQAsciiDictIterator<KAction> it( d->m_actionDict );
361 while ( it.current() )
362 _remove( it.current() );
377 if ( !classname && name )
378 pAction = d->m_actionDict[ name ];
381 TQAsciiDictIterator<KAction> it( d->m_actionDict );
382 for( ; it.current(); ++it )
384 if ( ( !name || !strcmp( it.current()->name(), name ) ) &&
385 ( !classname || !strcmp( it.current()->className(), classname ) ) ) {
386 pAction = it.current();
393 for( uint i = 0; i < d->m_docList.count() && !pAction; i++ )
394 pAction = d->m_docList[i]->action( name, classname );
402 TQAsciiDictIterator<KAction> it( d->m_actionDict );
410 return KActionShortcutList(
this).readSettings( sConfigGroup, pConfig );
415 return KActionShortcutList((
KActionCollection*)
this).writeSettings( sConfigGroup, pConfig );
420 return d->m_actionDict.count();
427 TQAsciiDictIterator<KAction> it( d->m_actionDict );
428 for( ; it.current(); ++it )
429 if ( !it.current()->group().isEmpty() && !lst.contains( it.current()->group() ) )
430 lst.append( it.current()->group() );
439 TQAsciiDictIterator<KAction> it( d->m_actionDict );
440 for( ; it.current(); ++it )
441 if ( it.current()->group() == group )
442 lst.append( it.current() );
443 else if ( it.current()->group().isEmpty() && group.isEmpty() )
444 lst.append( it.current() );
453 TQAsciiDictIterator<KAction> it( d->m_actionDict );
454 for( ; it.current(); ++it )
455 lst.append( it.current() );
460 void KActionCollection::setInstance(
KInstance *instance )
470 return d->m_instance;
475 d->m_sXMLFile = sXMLFile;
480 return d->m_sXMLFile;
485 d->m_highlight = enable;
490 return d->m_highlight;
495 if ( !d->m_highlight )
498 TQPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
502 actionList =
new TQPtrList<KAction>;
504 if ( ::tqqt_cast<TQPopupMenu *>( container ) )
506 connect( container, TQT_SIGNAL( highlighted(
int ) ),
507 this, TQT_SLOT( slotMenuItemHighlighted(
int ) ) );
508 connect( container, TQT_SIGNAL( aboutToHide() ),
509 this, TQT_SLOT( slotMenuAboutToHide() ) );
511 else if ( ::tqqt_cast<KToolBar *>( container ) )
513 connect( container, TQT_SIGNAL( highlighted(
int,
bool ) ),
514 this, TQT_SLOT( slotToolBarButtonHighlighted(
int,
bool ) ) );
517 connect( container, TQT_SIGNAL( destroyed() ),
518 this, TQT_SLOT( slotDestroyed() ) );
520 d->m_dctHighlightContainers.insert( container, actionList );
523 actionList->append(
action );
528 if ( !d->m_highlight )
531 TQPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
536 actionList->removeRef(
action );
538 if ( actionList->isEmpty() )
539 d->m_dctHighlightContainers.remove( container );
542 void KActionCollection::slotMenuItemHighlighted(
int id )
544 if ( !d->m_highlight )
547 if ( d->m_currentHighlightAction )
550 TQWidget *container =
const_cast<TQWidget*
>(TQT_TQWIDGET_CONST( sender() ));
552 d->m_currentHighlightAction = findAction( container,
id );
554 if ( !d->m_currentHighlightAction )
556 if ( !d->m_statusCleared )
558 d->m_statusCleared =
true;
562 d->m_statusCleared =
false;
568 void KActionCollection::slotMenuAboutToHide()
570 if ( d->m_currentHighlightAction )
572 d->m_currentHighlightAction = 0;
574 if ( !d->m_statusCleared )
576 d->m_statusCleared =
true;
579 void KActionCollection::slotToolBarButtonHighlighted(
int id,
bool highlight )
581 if ( !d->m_highlight )
584 TQWidget *container =
const_cast<TQWidget*
>(TQT_TQWIDGET_CONST( sender() ));
590 d->m_currentHighlightAction = 0;
599 d->m_currentHighlightAction =
action;
602 d->m_currentHighlightAction = 0;
607 void KActionCollection::slotDestroyed()
609 d->m_dctHighlightContainers.remove(
reinterpret_cast<void *
>(
const_cast<TQObject*
>(TQT_TQOBJECT_CONST(sender())) ) );
612 KAction *KActionCollection::findAction( TQWidget *container,
int id )
614 TQPtrList<KAction> *actionList = d->m_dctHighlightContainers[
reinterpret_cast<void *
>( container ) ];
619 TQPtrListIterator<KAction> it( *actionList );
620 for (; it.current(); ++it )
621 if ( it.current()->isPlugged( container,
id ) )
629 return d->m_parentGUIClient;
632 #ifndef KDE_NO_COMPAT
636 kdWarning(129) <<
"KActionCollection::operator+(): function is severely deprecated." <<
endl;
640 TQValueList<KAction *>::ConstIterator it =
actions.begin();
641 TQValueList<KAction *>::ConstIterator end =
actions.end();
642 for (; it != end; ++it )
650 kdWarning(129) <<
"KActionCollection::operator=(): function is severely deprecated." <<
endl;
655 d->m_widget =
copy.d->m_widget;
656 d->m_kaccel =
copy.d->m_kaccel;
657 d->m_actionDict =
copy.d->m_actionDict;
658 setInstance(
copy.instance() );
664 kdWarning(129) <<
"KActionCollection::operator+=(): function is severely deprecated." <<
endl;
665 TQAsciiDictIterator<KAction> it(c.d->m_actionDict);
666 for ( ; it.current(); ++it )
678 : m_actions( *pColl )
680 KActionShortcutList::~KActionShortcutList()
682 uint KActionShortcutList::count()
const
683 {
return m_actions.count(); }
684 TQString KActionShortcutList::name( uint i )
const
685 {
return m_actions.action(i)->name(); }
686 TQString KActionShortcutList::label( uint i )
const
687 {
return m_actions.action(i)->text(); }
688 TQString KActionShortcutList::whatsThis( uint i )
const
689 {
return m_actions.action(i)->whatsThis(); }
690 const KShortcut& KActionShortcutList::shortcut( uint i )
const
691 {
return m_actions.action(i)->shortcut(); }
692 const KShortcut& KActionShortcutList::shortcutDefault( uint i )
const
693 {
return m_actions.action(i)->shortcutDefault(); }
694 bool KActionShortcutList::isConfigurable( uint i )
const
695 {
return m_actions.action(i)->isShortcutConfigurable(); }
696 bool KActionShortcutList::setShortcut( uint i,
const KShortcut& cut )
697 {
return m_actions.action(i)->setShortcut( cut ); }
698 const KInstance* KActionShortcutList::instance()
const
699 {
return m_actions.instance(); }
700 TQVariant KActionShortcutList::getOther( Other, uint )
const
701 {
return TQVariant(); }
702 bool KActionShortcutList::setOther( Other, uint, TQVariant )
704 const KAction *KActionShortcutList::action( uint i)
const
705 {
return m_actions.action(i); }
707 bool KActionShortcutList::save()
const
710 const TQString xmlFile=guiClient ? guiClient->
xmlFile() : m_actions.xmlFile();
711 kdDebug(129) <<
"KActionShortcutList::save(): xmlFile = " << xmlFile <<
endl;
713 if( m_actions.xmlFile().isEmpty() )
714 return writeSettings();
716 TQString attrShortcut = TQString::fromLatin1(
"shortcut");
717 TQString attrAccel = TQString::fromLatin1(
"accel");
720 TQString sXml( KXMLGUIFactory::readConfigFile( xmlFile,
false,
instance() ) );
722 doc.setContent( sXml );
730 uint nSize = count();
731 for( uint i = 0; i < nSize; i++ ) {
732 const TQString& sName =
name(i);
740 if ( act_elem.isNull() )
743 act_elem.removeAttribute( attrAccel );
744 if( bSameAsDefault ) {
745 act_elem.removeAttribute( attrShortcut );
747 if( act_elem.attributes().count() == 1 )
748 elem.removeChild( act_elem );
750 act_elem.setAttribute( attrShortcut,
shortcut(i).toStringInternal() );
755 return KXMLGUIFactory::saveConfigFile( doc, guiClient ? guiClient->localXMLFile() : m_actions.xmlFile(),
instance() );
762 KActionPtrShortcutList::KActionPtrShortcutList( KActionPtrList& list )
765 KActionPtrShortcutList::~KActionPtrShortcutList()
767 uint KActionPtrShortcutList::count()
const
768 {
return m_actions.count(); }
769 TQString KActionPtrShortcutList::name( uint i )
const
770 {
return m_actions[i]->name(); }
771 TQString KActionPtrShortcutList::label( uint i )
const
772 {
return m_actions[i]->text(); }
773 TQString KActionPtrShortcutList::whatsThis( uint i )
const
774 {
return m_actions[i]->whatsThis(); }
775 const KShortcut& KActionPtrShortcutList::shortcut( uint i )
const
776 {
return m_actions[i]->shortcut(); }
777 const KShortcut& KActionPtrShortcutList::shortcutDefault( uint i )
const
778 {
return m_actions[i]->shortcutDefault(); }
779 bool KActionPtrShortcutList::isConfigurable( uint i )
const
780 {
return m_actions[i]->isShortcutConfigurable(); }
781 bool KActionPtrShortcutList::setShortcut( uint i,
const KShortcut& cut )
782 {
return m_actions[i]->setShortcut( cut ); }
783 TQVariant KActionPtrShortcutList::getOther( Other, uint )
const
784 {
return TQVariant(); }
785 bool KActionPtrShortcutList::setOther( Other, uint, TQVariant )
787 bool KActionPtrShortcutList::save()
const
790 void KActionShortcutList::virtual_hook(
int id,
void* data )
793 void KActionPtrShortcutList::virtual_hook(
int id,
void* data )
796 void KActionCollection::virtual_hook(
int,
void* )
802 #include "kactioncollection.moc"
A managed set of KAction objects.
void insert(KAction *action)
Add an action to the collection.
KAccel * builderKAccel() const
void connectHighlight(TQWidget *container, KAction *action)
Call this function if you want to receive a signal whenever a KAction is highlighted in a menu or a t...
const TQString & xmlFile() const
KAccel * kaccel()
Returns the KAccel object of the most recently set widget.
virtual void setWidget(TQWidget *widget)
This sets the widget to which the keyboard shortcuts should be attached.
virtual KActionPtrList actions() const
Returns the list of actions managed by this action collection.
void actionHighlighted(KAction *action)
Emitted when action is highlighted.
bool readShortcutSettings(const TQString &sConfigGroup=TQString::null, KConfigBase *pConfig=0)
Used for reading shortcut configuration from a non-XML rc file.
void remove(KAction *action)
Removes an action from the collection and deletes it.
virtual KAction * action(int index) const
Return the KAction* at position "index" in the action collection.
void clear()
Clears the entire actionCollection, deleting all actions.
void clearStatusText()
Emitted when an action loses highlighting.
virtual KAccel * accel() KDE_DEPRECATED
Returns the number of widgets which this collection is associated with.
const KXMLGUIClient * parentGUIClient() const
The parent KXMLGUIClient, return 0L if not available.
void setAutoConnectShortcuts(bool)
This indicates whether new actions which are created in this collection should have their keyboard sh...
void setHighlightingEnabled(bool enable)
Enable highlighting notification for specific KActions.
bool writeShortcutSettings(const TQString &sConfigGroup=TQString::null, KConfigBase *pConfig=0) const
Used for writing shortcut configuration to a non-XML rc file.
KInstance * instance() const
The instance with which this class is associated.
void actionStatusText(const TQString &text)
Emitted when an action is highlighted, with text being the tooltip for the action.
void setXMLFile(const TQString &)
void disconnectHighlight(TQWidget *container, KAction *action)
Disconnect highlight notifications for a particular pair of contianer and action.
virtual KActionPtrList actions(const TQString &group) const
Returns the list of actions in a particular group managed by this action collection.
KAction * take(KAction *action)
Removes an action from the collection.
virtual uint count() const
Returns the KAccel object associated with widget #.
virtual TQStringList groups() const
Returns a list of all the groups of all the KActions in this action collection.
bool highlightingEnabled() const
Return whether highlighting notifications are enabled.
bool isAutoConnectShortcuts()
This indicates whether new actions which are created in this collection have their keyboard shortcuts...
bool addDocCollection(KActionCollection *pDoc)
This sets the default shortcut scope for new actions created in this collection.
Class to encapsulate user-driven action or event.
static KInstance * instance()
virtual void virtual_hook(int id, void *data)
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
virtual TQString xmlFile() const
This will return the name of the XML file as set by setXMLFile().
virtual KInstance * instance() const
static TQDomElement findActionByName(TQDomElement &elem, const TQString &sName, bool create)
static TQDomElement actionPropertiesElement(TQDomDocument &doc)
kndbgstream & endl(kndbgstream &s)
TQString kdBacktrace(int levels=-1)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
TQString name(StdAccel id)
KShortcut shortcutDefault(StdAccel id)
const KShortcut & shortcut(StdAccel id)