00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "kactioncollection.h"
00027 #include "kactionshortcutlist.h"
00028
00029 #include <qptrdict.h>
00030 #include <qvariant.h>
00031
00032 #include <kaccel.h>
00033 #include <kaccelbase.h>
00034 #include <kapplication.h>
00035 #include <kdebug.h>
00036 #include <kxmlguifactory.h>
00037 #include <kxmlguiclient.h>
00038
00039 class KActionCollection::KActionCollectionPrivate
00040 {
00041 public:
00042 KActionCollectionPrivate()
00043 {
00044 m_instance = 0;
00045
00046
00047 m_bAutoConnectShortcuts = true;
00048 m_widget = 0;
00049 m_kaccel = m_builderKAccel = 0;
00050 m_dctHighlightContainers.setAutoDelete( true );
00051 m_highlight = false;
00052 m_currentHighlightAction = 0;
00053 m_statusCleared = true;
00054 m_parentGUIClient = 0L;
00055 }
00056
00057 KInstance *m_instance;
00058 QString m_sXMLFile;
00059 bool m_bAutoConnectShortcuts;
00060
00061
00062
00063
00064 QValueList<KActionCollection*> m_docList;
00065 QWidget *m_widget;
00066 KAccel *m_kaccel;
00067 KAccel *m_builderKAccel;
00068
00069 QAsciiDict<KAction> m_actionDict;
00070 QPtrDict< QPtrList<KAction> > m_dctHighlightContainers;
00071 bool m_highlight;
00072 KAction *m_currentHighlightAction;
00073 bool m_statusCleared;
00074 const KXMLGUIClient *m_parentGUIClient;
00075 };
00076
00077 KActionCollection::KActionCollection( QWidget *parent, const char *name,
00078 KInstance *instance )
00079 : QObject( parent, name )
00080 {
00081 kdDebug(129) << "KActionCollection::KActionCollection( " << parent << ", " << name << " ): this = " << this << endl;
00082 d = new KActionCollectionPrivate;
00083 if( parent )
00084 setWidget( parent );
00085
00086 setInstance( instance );
00087 }
00088
00089
00090 KActionCollection::KActionCollection( QWidget *watch, QObject* parent, const char *name,
00091 KInstance *instance )
00092 : QObject( parent, name )
00093 {
00094 kdDebug(129) << "KActionCollection::KActionCollection( " << watch << ", " << parent << ", " << name << " ): this = " << this << endl;
00095 d = new KActionCollectionPrivate;
00096 if( watch )
00097 setWidget( watch );
00098
00099 setInstance( instance );
00100 }
00101
00102
00103 KActionCollection::KActionCollection( QObject *parent, const char *name,
00104 KInstance *instance )
00105 : QObject( parent, name )
00106 {
00107 kdWarning(129) << "KActionCollection::KActionCollection( QObject *parent, const char *name, KInstance *instance )" << endl;
00108 kdDebug(129) << kdBacktrace() << endl;
00109 d = new KActionCollectionPrivate;
00110 QWidget* w = dynamic_cast<QWidget*>( parent );
00111 if( w )
00112 setWidget( w );
00113
00114 setInstance( instance );
00115 }
00116
00117 KActionCollection::KActionCollection( const KActionCollection © )
00118 : QObject()
00119 {
00120 kdWarning(129) << "KActionCollection::KActionCollection( const KActionCollection & ): function is severely deprecated." << endl;
00121 d = new KActionCollectionPrivate;
00122 *this = copy;
00123 }
00124
00125
00126
00127 KActionCollection::KActionCollection( const char *name, const KXMLGUIClient *parent )
00128 : QObject( 0L, name )
00129 {
00130 d = new KActionCollectionPrivate;
00131 d->m_parentGUIClient=parent;
00132 d->m_instance=parent->instance();
00133 }
00134
00135
00136 KActionCollection::~KActionCollection()
00137 {
00138 kdDebug(129) << "KActionCollection::~KActionCollection(): this = " << this << endl;
00139 for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
00140 KAction* pAction = it.current();
00141 if ( pAction->m_parentCollection == this )
00142 pAction->m_parentCollection = 0L;
00143 }
00144
00145 delete d->m_kaccel;
00146 delete d->m_builderKAccel;
00147 delete d; d = 0;
00148 }
00149
00150 void KActionCollection::setWidget( QWidget* w )
00151 {
00152
00153
00154
00155
00156
00157 if ( !d->m_widget ) {
00158 d->m_widget = w;
00159 d->m_kaccel = new KAccel( w, this, "KActionCollection-KAccel" );
00160 }
00161 else if ( d->m_widget != w )
00162 kdWarning(129) << "KActionCollection::setWidget(): tried to change widget from " << d->m_widget << " to " << w << endl;
00163 }
00164
00165 void KActionCollection::setAutoConnectShortcuts( bool b )
00166 {
00167 d->m_bAutoConnectShortcuts = b;
00168 }
00169
00170 bool KActionCollection::isAutoConnectShortcuts()
00171 {
00172 return d->m_bAutoConnectShortcuts;
00173 }
00174
00175 bool KActionCollection::addDocCollection( KActionCollection* pDoc )
00176 {
00177 d->m_docList.append( pDoc );
00178 return true;
00179 }
00180
00181 void KActionCollection::beginXMLPlug( QWidget *widget )
00182 {
00183 kdDebug(129) << "KActionCollection::beginXMLPlug( buildWidget = " << widget << " ): this = " << this << " d->m_builderKAccel = " << d->m_builderKAccel << endl;
00184
00185 if( widget && !d->m_builderKAccel ) {
00186 d->m_builderKAccel = new KAccel( widget, this, "KActionCollection-BuilderKAccel" );
00187 }
00188 }
00189
00190 void KActionCollection::endXMLPlug()
00191 {
00192 kdDebug(129) << "KActionCollection::endXMLPlug(): this = " << this << endl;
00193
00194 }
00195
00196 void KActionCollection::prepareXMLUnplug()
00197 {
00198 kdDebug(129) << "KActionCollection::prepareXMLUnplug(): this = " << this << endl;
00199 unplugShortcuts( d->m_kaccel );
00200
00201 if( d->m_builderKAccel ) {
00202 unplugShortcuts( d->m_builderKAccel );
00203 delete d->m_builderKAccel;
00204 d->m_builderKAccel = 0;
00205 }
00206 }
00207
00208 void KActionCollection::unplugShortcuts( KAccel* kaccel )
00209 {
00210 for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
00211 KAction* pAction = it.current();
00212 pAction->removeKAccel( kaccel );
00213 }
00214
00215 for( uint i = 0; i < d->m_docList.count(); i++ )
00216 d->m_docList[i]->unplugShortcuts( kaccel );
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 KAccel* KActionCollection::kaccel()
00281 {
00282
00283
00284
00285
00286 return d->m_kaccel;
00287 }
00288
00289 const KAccel* KActionCollection::kaccel() const
00290 {
00291
00292
00293
00294
00295 return d->m_kaccel;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 void KActionCollection::_insert( KAction* action )
00313 {
00314 char unnamed_name[100];
00315 const char *name = action->name();
00316 if( qstrcmp( name, "unnamed" ) == 0 )
00317 {
00318 sprintf(unnamed_name, "unnamed-%p", (void *)action);
00319 name = unnamed_name;
00320 }
00321 KAction *a = d->m_actionDict[ name ];
00322 if ( a == action )
00323 return;
00324
00325 d->m_actionDict.insert( name, action );
00326
00327 emit inserted( action );
00328 }
00329
00330 void KActionCollection::_remove( KAction* action )
00331 {
00332 delete _take( action );
00333 }
00334
00335 KAction* KActionCollection::_take( KAction* action )
00336 {
00337 char unnamed_name[100];
00338 const char *name = action->name();
00339 if( qstrcmp( name, "unnamed" ) == 0 )
00340 {
00341 sprintf(unnamed_name, "unnamed-%p", (void *) action);
00342 name = unnamed_name;
00343 }
00344
00345 KAction *a = d->m_actionDict.take( name );
00346 if ( !a || a != action )
00347 return 0;
00348
00349 if ( a->m_parentCollection == this )
00350 a->m_parentCollection = 0;
00351
00352 emit removed( action );
00353 return a;
00354 }
00355
00356 void KActionCollection::_clear()
00357 {
00358 QAsciiDictIterator<KAction> it( d->m_actionDict );
00359 while ( it.current() )
00360 _remove( it.current() );
00361 }
00362
00363 void KActionCollection::insert( KAction* action ) { _insert( action ); }
00364 void KActionCollection::remove( KAction* action ) { _remove( action ); }
00365 KAction* KActionCollection::take( KAction* action ) { return _take( action ); }
00366 void KActionCollection::clear() { _clear(); }
00367 KAccel* KActionCollection::accel() { return kaccel(); }
00368 const KAccel* KActionCollection::accel() const { return kaccel(); }
00369 KAccel* KActionCollection::builderKAccel() const { return d->m_builderKAccel; }
00370
00371 KAction* KActionCollection::action( const char* name, const char* classname ) const
00372 {
00373 KAction* pAction = 0;
00374
00375 if ( !classname && name )
00376 pAction = d->m_actionDict[ name ];
00377
00378 else {
00379 QAsciiDictIterator<KAction> it( d->m_actionDict );
00380 for( ; it.current(); ++it )
00381 {
00382 if ( ( !name || strcmp( it.current()->name(), name ) == 0 ) &&
00383 ( !classname || strcmp( it.current()->className(), classname ) == 0 ) ) {
00384 pAction = it.current();
00385 break;
00386 }
00387 }
00388 }
00389
00390 if( !pAction ) {
00391 for( uint i = 0; i < d->m_docList.count() && !pAction; i++ )
00392 pAction = d->m_docList[i]->action( name, classname );
00393 }
00394
00395 return pAction;
00396 }
00397
00398 KAction* KActionCollection::action( int index ) const
00399 {
00400 QAsciiDictIterator<KAction> it( d->m_actionDict );
00401 it += index;
00402 return it.current();
00403
00404 }
00405
00406 bool KActionCollection::readShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig )
00407 {
00408 return KActionShortcutList(this).readSettings( sConfigGroup, pConfig );
00409 }
00410
00411 bool KActionCollection::writeShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig ) const
00412 {
00413 return KActionShortcutList((KActionCollection*)this).writeSettings( sConfigGroup, pConfig );
00414 }
00415
00416 uint KActionCollection::count() const
00417 {
00418 return d->m_actionDict.count();
00419 }
00420
00421 QStringList KActionCollection::groups() const
00422 {
00423 QStringList lst;
00424
00425 QAsciiDictIterator<KAction> it( d->m_actionDict );
00426 for( ; it.current(); ++it )
00427 if ( !it.current()->group().isEmpty() && !lst.contains( it.current()->group() ) )
00428 lst.append( it.current()->group() );
00429
00430 return lst;
00431 }
00432
00433 KActionPtrList KActionCollection::actions( const QString& group ) const
00434 {
00435 KActionPtrList lst;
00436
00437 QAsciiDictIterator<KAction> it( d->m_actionDict );
00438 for( ; it.current(); ++it )
00439 if ( it.current()->group() == group )
00440 lst.append( it.current() );
00441 else if ( it.current()->group().isEmpty() && group.isEmpty() )
00442 lst.append( it.current() );
00443
00444 return lst;
00445 }
00446
00447 KActionPtrList KActionCollection::actions() const
00448 {
00449 KActionPtrList lst;
00450
00451 QAsciiDictIterator<KAction> it( d->m_actionDict );
00452 for( ; it.current(); ++it )
00453 lst.append( it.current() );
00454
00455 return lst;
00456 }
00457
00458 void KActionCollection::setInstance( KInstance *instance )
00459 {
00460 if ( instance )
00461 d->m_instance = instance;
00462 else
00463 d->m_instance = KGlobal::instance();
00464 }
00465
00466 KInstance *KActionCollection::instance() const
00467 {
00468 return d->m_instance;
00469 }
00470
00471 void KActionCollection::setXMLFile( const QString& sXMLFile )
00472 {
00473 d->m_sXMLFile = sXMLFile;
00474 }
00475
00476 const QString& KActionCollection::xmlFile() const
00477 {
00478 return d->m_sXMLFile;
00479 }
00480
00481 void KActionCollection::setHighlightingEnabled( bool enable )
00482 {
00483 d->m_highlight = enable;
00484 }
00485
00486 bool KActionCollection::highlightingEnabled() const
00487 {
00488 return d->m_highlight;
00489 }
00490
00491 void KActionCollection::connectHighlight( QWidget *container, KAction *action )
00492 {
00493 if ( !d->m_highlight )
00494 return;
00495
00496 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
00497
00498 if ( !actionList )
00499 {
00500 actionList = new QPtrList<KAction>;
00501
00502 if ( container->inherits( "QPopupMenu" ) )
00503 {
00504 connect( container, SIGNAL( highlighted( int ) ),
00505 this, SLOT( slotMenuItemHighlighted( int ) ) );
00506 connect( container, SIGNAL( aboutToHide() ),
00507 this, SLOT( slotMenuAboutToHide() ) );
00508 }
00509 else if ( container->inherits( "KToolBar" ) )
00510 {
00511 connect( container, SIGNAL( highlighted( int, bool ) ),
00512 this, SLOT( slotToolBarButtonHighlighted( int, bool ) ) );
00513 }
00514
00515 connect( container, SIGNAL( destroyed() ),
00516 this, SLOT( slotDestroyed() ) );
00517
00518 d->m_dctHighlightContainers.insert( container, actionList );
00519 }
00520
00521 actionList->append( action );
00522 }
00523
00524 void KActionCollection::disconnectHighlight( QWidget *container, KAction *action )
00525 {
00526 if ( !d->m_highlight )
00527 return;
00528
00529 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
00530
00531 if ( !actionList )
00532 return;
00533
00534 actionList->removeRef( action );
00535
00536 if ( actionList->count() == 0 )
00537 d->m_dctHighlightContainers.remove( container );
00538 }
00539
00540 void KActionCollection::slotMenuItemHighlighted( int id )
00541 {
00542 if ( !d->m_highlight )
00543 return;
00544
00545 if ( d->m_currentHighlightAction )
00546 emit actionHighlighted( d->m_currentHighlightAction, false );
00547
00548 QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
00549
00550 d->m_currentHighlightAction = findAction( container, id );
00551
00552 if ( !d->m_currentHighlightAction )
00553 {
00554 if ( !d->m_statusCleared )
00555 emit clearStatusText();
00556 d->m_statusCleared = true;
00557 return;
00558 }
00559
00560 d->m_statusCleared = false;
00561 emit actionHighlighted( d->m_currentHighlightAction );
00562 emit actionHighlighted( d->m_currentHighlightAction, true );
00563 emit actionStatusText( d->m_currentHighlightAction->toolTip() );
00564 }
00565
00566 void KActionCollection::slotMenuAboutToHide()
00567 {
00568 if ( d->m_currentHighlightAction )
00569 emit actionHighlighted( d->m_currentHighlightAction, false );
00570 d->m_currentHighlightAction = 0;
00571
00572 if ( !d->m_statusCleared )
00573 emit clearStatusText();
00574 d->m_statusCleared = true;
00575 }
00576
00577 void KActionCollection::slotToolBarButtonHighlighted( int id, bool highlight )
00578 {
00579 if ( !d->m_highlight )
00580 return;
00581
00582 QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
00583
00584 KAction *action = findAction( container, id );
00585
00586 if ( !action )
00587 {
00588 d->m_currentHighlightAction = 0;
00589
00590
00591 return;
00592 }
00593
00594 emit actionHighlighted( action, highlight );
00595
00596 if ( highlight )
00597 d->m_currentHighlightAction = action;
00598 else
00599 {
00600 d->m_currentHighlightAction = 0;
00601
00602 }
00603 }
00604
00605 void KActionCollection::slotDestroyed()
00606 {
00607 d->m_dctHighlightContainers.remove( reinterpret_cast<void *>( const_cast<QObject *>(sender()) ) );
00608 }
00609
00610 KAction *KActionCollection::findAction( QWidget *container, int id )
00611 {
00612 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ reinterpret_cast<void *>( container ) ];
00613
00614 if ( !actionList )
00615 return 0;
00616
00617 QPtrListIterator<KAction> it( *actionList );
00618 for (; it.current(); ++it )
00619 if ( it.current()->isPlugged( container, id ) )
00620 return it.current();
00621
00622 return 0;
00623 }
00624
00625 const KXMLGUIClient *KActionCollection::parentGUIClient() const
00626 {
00627 return d->m_parentGUIClient;
00628 }
00629
00630
00631 KActionCollection KActionCollection::operator+(const KActionCollection &c ) const
00632 {
00633 kdWarning(129) << "KActionCollection::operator+(): function is severely deprecated." << endl;
00634 KActionCollection ret( *this );
00635
00636 QValueList<KAction *> actions = c.actions();
00637 QValueList<KAction *>::ConstIterator it = actions.begin();
00638 QValueList<KAction *>::ConstIterator end = actions.end();
00639 for (; it != end; ++it )
00640 ret.insert( *it );
00641
00642 return ret;
00643 }
00644
00645 KActionCollection &KActionCollection::operator=( const KActionCollection © )
00646 {
00647 kdWarning(129) << "KActionCollection::operator=(): function is severely deprecated." << endl;
00648
00649
00650
00651
00652 d->m_widget = copy.d->m_widget;
00653 d->m_kaccel = copy.d->m_kaccel;
00654 d->m_actionDict = copy.d->m_actionDict;
00655 setInstance( copy.instance() );
00656 return *this;
00657 }
00658
00659 KActionCollection &KActionCollection::operator+=( const KActionCollection &c )
00660 {
00661 kdWarning(129) << "KActionCollection::operator+=(): function is severely deprecated." << endl;
00662 QAsciiDictIterator<KAction> it(c.d->m_actionDict);
00663 for ( ; it.current(); ++it )
00664 insert( it.current() );
00665
00666 return *this;
00667 }
00668
00669
00670
00671
00672
00673
00674 KActionShortcutList::KActionShortcutList( KActionCollection* pColl )
00675 : m_actions( *pColl )
00676 { }
00677 KActionShortcutList::~KActionShortcutList()
00678 { }
00679 uint KActionShortcutList::count() const
00680 { return m_actions.count(); }
00681 QString KActionShortcutList::name( uint i ) const
00682 { return m_actions.action(i)->name(); }
00683 QString KActionShortcutList::label( uint i ) const
00684 { return m_actions.action(i)->text(); }
00685 QString KActionShortcutList::whatsThis( uint i ) const
00686 { return m_actions.action(i)->whatsThis(); }
00687 const KShortcut& KActionShortcutList::shortcut( uint i ) const
00688 { return m_actions.action(i)->shortcut(); }
00689 const KShortcut& KActionShortcutList::shortcutDefault( uint i ) const
00690 { return m_actions.action(i)->shortcutDefault(); }
00691 bool KActionShortcutList::isConfigurable( uint i ) const
00692 { return m_actions.action(i)->isShortcutConfigurable(); }
00693 bool KActionShortcutList::setShortcut( uint i, const KShortcut& cut )
00694 { return m_actions.action(i)->setShortcut( cut ); }
00695 const KInstance* KActionShortcutList::instance() const
00696 { return m_actions.instance(); }
00697 QVariant KActionShortcutList::getOther( Other, uint ) const
00698 { return QVariant(); }
00699 bool KActionShortcutList::setOther( Other, uint, QVariant )
00700 { return false; }
00701 const KAction *KActionShortcutList::action( uint i) const
00702 { return m_actions.action(i); }
00703
00704 bool KActionShortcutList::save() const
00705 {
00706 const KXMLGUIClient* guiClient=m_actions.parentGUIClient();
00707 const QString xmlFile=guiClient ? guiClient->xmlFile() : m_actions.xmlFile();
00708 kdDebug(129) << "KActionShortcutList::save(): xmlFile = " << xmlFile << endl;
00709
00710 if( m_actions.xmlFile().isEmpty() )
00711 return writeSettings();
00712
00713 QString attrShortcut = QString::fromLatin1("shortcut");
00714 QString attrAccel = QString::fromLatin1("accel");
00715
00716
00717 QString sXml( KXMLGUIFactory::readConfigFile( xmlFile, false, instance() ) );
00718 QDomDocument doc;
00719 doc.setContent( sXml );
00720
00721
00722
00723
00724 QDomElement elem = KXMLGUIFactory::actionPropertiesElement( doc );
00725
00726
00727 uint nSize = count();
00728 for( uint i = 0; i < nSize; i++ ) {
00729 const QString& sName = name(i);
00730
00731 bool bSameAsDefault = (shortcut(i) == shortcutDefault(i));
00732
00733
00734
00735
00736 QDomElement act_elem = KXMLGUIFactory::findActionByName( elem, sName, !bSameAsDefault );
00737 if ( act_elem.isNull() )
00738 continue;
00739
00740 act_elem.removeAttribute( attrAccel );
00741 if( bSameAsDefault ) {
00742 act_elem.removeAttribute( attrShortcut );
00743
00744 if( act_elem.attributes().count() == 1 )
00745 elem.removeChild( act_elem );
00746 } else {
00747 act_elem.setAttribute( attrShortcut, shortcut(i).toStringInternal() );
00748 }
00749 }
00750
00751
00752 return KXMLGUIFactory::saveConfigFile( doc, guiClient ? guiClient->localXMLFile() : m_actions.xmlFile(), instance() );
00753 }
00754
00755
00756
00757
00758
00759 KActionPtrShortcutList::KActionPtrShortcutList( KActionPtrList& list )
00760 : m_actions( list )
00761 { }
00762 KActionPtrShortcutList::~KActionPtrShortcutList()
00763 { }
00764 uint KActionPtrShortcutList::count() const
00765 { return m_actions.count(); }
00766 QString KActionPtrShortcutList::name( uint i ) const
00767 { return m_actions[i]->name(); }
00768 QString KActionPtrShortcutList::label( uint i ) const
00769 { return m_actions[i]->text(); }
00770 QString KActionPtrShortcutList::whatsThis( uint i ) const
00771 { return m_actions[i]->whatsThis(); }
00772 const KShortcut& KActionPtrShortcutList::shortcut( uint i ) const
00773 { return m_actions[i]->shortcut(); }
00774 const KShortcut& KActionPtrShortcutList::shortcutDefault( uint i ) const
00775 { return m_actions[i]->shortcutDefault(); }
00776 bool KActionPtrShortcutList::isConfigurable( uint i ) const
00777 { return m_actions[i]->isShortcutConfigurable(); }
00778 bool KActionPtrShortcutList::setShortcut( uint i, const KShortcut& cut )
00779 { return m_actions[i]->setShortcut( cut ); }
00780 QVariant KActionPtrShortcutList::getOther( Other, uint ) const
00781 { return QVariant(); }
00782 bool KActionPtrShortcutList::setOther( Other, uint, QVariant )
00783 { return false; }
00784 bool KActionPtrShortcutList::save() const
00785 { return false; }
00786
00787 void KActionShortcutList::virtual_hook( int id, void* data )
00788 { KShortcutList::virtual_hook( id, data ); }
00789
00790 void KActionPtrShortcutList::virtual_hook( int id, void* data )
00791 { KShortcutList::virtual_hook( id, data ); }
00792
00793 void KActionCollection::virtual_hook( int, void* )
00794 { }
00795
00796
00797
00798
00799 #include "kactioncollection.moc"