kdeui Library API Documentation

kaction.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
00003               (C) 1999 Simon Hausmann <hausmann@kde.org>
00004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
00005               (C) 2000 Kurt Granroth <granroth@kde.org>
00006               (C) 2000 Michael Koch <koch@kde.org>
00007           (C) 2001 Holger Freyther <freyther@kde.org>
00008               (C) 2002 Ellis Whitehead <ellis@kde.org>
00009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
00010 
00011     This library is free software; you can redistribute it and/or
00012     modify it under the terms of the GNU Library General Public
00013     License version 2 as published by the Free Software Foundation.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Library General Public License for more details.
00019 
00020     You should have received a copy of the GNU Library General Public License
00021     along with this library; see the file COPYING.LIB.  If not, write to
00022     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023     Boston, MA 02111-1307, USA.
00024 */
00025 
00026 #ifdef KDE_USE_FINAL
00027 #undef Always
00028 #include <qdockwindow.h>
00029 #endif
00030 
00031 #include "kaction.h"
00032 #include "kactionshortcutlist.h"
00033 
00034 #include <assert.h>
00035 
00036 #include <qfontdatabase.h>
00037 #include <qobjectlist.h>
00038 #include <qptrdict.h>
00039 #include <qregexp.h>
00040 #include <qtl.h>
00041 #include <qtooltip.h>
00042 #include <qvariant.h>
00043 #include <qwhatsthis.h>
00044 #include <qtimer.h>
00045 
00046 #include <kaccel.h>
00047 #include <kaccelbase.h>
00048 #include <kapplication.h>
00049 #include <kcombobox.h>
00050 #include <kconfig.h>
00051 #include <kdebug.h>
00052 #include <kfontcombo.h>
00053 #include <kglobalsettings.h>
00054 #include <kguiitem.h>
00055 #include <kiconloader.h>
00056 #include <kmainwindow.h>
00057 #include <kmenubar.h>
00058 #include <kpopupmenu.h>
00059 #include <kstdaccel.h>
00060 #include <ktoolbar.h>
00061 #include <ktoolbarbutton.h>
00062 #include <kurl.h>
00063 #include <klocale.h>
00064 
00086 static QFontDatabase *fontDataBase = 0;
00087 
00088 static void cleanupFontDatabase()
00089 {
00090     delete fontDataBase;
00091     fontDataBase = 0;
00092 }
00093 
00094 static void get_fonts( QStringList &lst )
00095 {
00096     if ( !fontDataBase ) {
00097         fontDataBase = new QFontDatabase();
00098         qAddPostRoutine( cleanupFontDatabase );
00099     }
00100     lst.clear();
00101     QStringList families = fontDataBase->families();
00102     for ( QStringList::Iterator it = families.begin(); it != families.end(); ++it )
00103     {
00104         QString family = *it;
00105         if ( family. contains('-') ) // remove foundry
00106             family = family.right( family.length() - family.find('-' ) - 1);
00107         if ( !lst.contains( family ) )
00108             lst.append( family );
00109     }
00110     lst.sort();
00111 }
00112 
00113 static QValueList<int> get_standard_font_sizes()
00114 {
00115     if ( !fontDataBase ) {
00116         fontDataBase = new QFontDatabase();
00117         qAddPostRoutine( cleanupFontDatabase );
00118     }
00119     return fontDataBase->standardSizes();
00120 }
00121 
00122 int KAction::getToolButtonID()
00123 {
00124     static int toolbutton_no = -2;
00125     return toolbutton_no--;
00126 }
00127 
00128 //---------------------------------------------------------------------
00129 // KAction::KActionPrivate
00130 //---------------------------------------------------------------------
00131 
00132 class KAction::KActionPrivate : public KGuiItem
00133 {
00134 public:
00135   KActionPrivate() : KGuiItem()
00136   {
00137     m_kaccel = 0;
00138     m_configurable = true;
00139   }
00140 
00141   KAccel *m_kaccel;
00142   QValueList<KAccel*> m_kaccelList;
00143 
00144   QString m_groupText;
00145   QString m_group;
00146 
00147   KShortcut m_cut;
00148   KShortcut m_cutDefault;
00149 
00150   bool m_configurable;
00151 
00152   struct Container
00153   {
00154     Container() { m_container = 0; m_representative = 0; m_id = 0; }
00155     Container( const Container& s ) { m_container = s.m_container;
00156                                       m_id = s.m_id; m_representative = s.m_representative; }
00157     QWidget* m_container;
00158     int m_id;
00159     QWidget* m_representative;
00160   };
00161 
00162   QValueList<Container> m_containers;
00163 };
00164 
00165 //---------------------------------------------------------------------
00166 // KAction
00167 //---------------------------------------------------------------------
00168 
00169 KAction::KAction( const QString& text, const KShortcut& cut,
00170              const QObject* receiver, const char* slot,
00171              KActionCollection* parent, const char* name )
00172 : QObject( parent, name )
00173 {
00174     initPrivate( text, cut, receiver, slot );
00175 }
00176 
00177 KAction::KAction( const QString& text, const QString& sIconName, const KShortcut& cut,
00178     const QObject* receiver, const char* slot,
00179     KActionCollection* parent, const char* name )
00180 : QObject( parent, name )
00181 {
00182     initPrivate( text, cut, receiver, slot );
00183     d->setIconName( sIconName );
00184 }
00185 
00186 KAction::KAction( const QString& text, const QIconSet& pix, const KShortcut& cut,
00187     const QObject* receiver, const char* slot,
00188     KActionCollection* parent, const char* name )
00189 : QObject( parent, name )
00190 {
00191     initPrivate( text, cut, receiver, slot );
00192     d->setIconSet( pix );
00193 }
00194 
00195 KAction::KAction( const KGuiItem& item, const KShortcut& cut,
00196     const QObject* receiver, const char* slot,
00197     KActionCollection* parent, const char* name )
00198 : QObject( parent, name )
00199 {
00200     initPrivate( item.text(), cut, receiver, slot );
00201     if( item.hasIconSet() )
00202         setIcon( item.iconName() );
00203     setToolTip( item.toolTip() );
00204     setWhatsThis( item.whatsThis() );
00205 }
00206 
00207 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
00208 KAction::KAction( const QString& text, const KShortcut& cut,
00209                   QObject* parent, const char* name )
00210  : QObject( parent, name )
00211 {
00212     initPrivate( text, cut, 0, 0 );
00213 }
00214 
00215 KAction::KAction( const QString& text, const KShortcut& cut,
00216                   const QObject* receiver,
00217                   const char* slot, QObject* parent, const char* name )
00218  : QObject( parent, name )
00219 {
00220     initPrivate( text, cut, receiver, slot );
00221 }
00222 
00223 KAction::KAction( const QString& text, const QIconSet& pix,
00224                   const KShortcut& cut,
00225                   QObject* parent, const char* name )
00226  : QObject( parent, name )
00227 {
00228     initPrivate( text, cut, 0, 0 );
00229     setIconSet( pix );
00230 }
00231 
00232 KAction::KAction( const QString& text, const QString& pix,
00233                   const KShortcut& cut,
00234                   QObject* parent, const char* name )
00235 : QObject( parent, name )
00236 {
00237     initPrivate( text, cut, 0, 0 );
00238     d->setIconName( pix );
00239 }
00240 
00241 KAction::KAction( const QString& text, const QIconSet& pix,
00242                   const KShortcut& cut,
00243                   const QObject* receiver, const char* slot, QObject* parent,
00244                   const char* name )
00245  : QObject( parent, name )
00246 {
00247     initPrivate( text, cut, receiver, slot );
00248     setIconSet( pix );
00249 }
00250 
00251 KAction::KAction( const QString& text, const QString& pix,
00252                   const KShortcut& cut,
00253                   const QObject* receiver, const char* slot, QObject* parent,
00254                   const char* name )
00255   : QObject( parent, name )
00256 {
00257     initPrivate( text, cut, receiver, slot );
00258     d->setIconName(pix);
00259 }
00260 
00261 KAction::KAction( QObject* parent, const char* name )
00262  : QObject( parent, name )
00263 {
00264     initPrivate( QString::null, KShortcut(), 0, 0 );
00265 }
00266 #endif // KDE_VERSION < 4.0.0
00267 
00268 KAction::~KAction()
00269 {
00270     kdDebug(129) << "KAction::~KAction( this = \"" << name() << "\" )" << endl; // -- ellis
00271 #ifndef KDE_NO_COMPAT
00272      if (d->m_kaccel)
00273        unplugAccel();
00274 #endif
00275 
00276     // If actionCollection hasn't already been destructed,
00277     if ( m_parentCollection ) {
00278         m_parentCollection->take( this );
00279         for( uint i = 0; i < d->m_kaccelList.count(); i++ )
00280             d->m_kaccelList[i]->remove( name() );
00281     }
00282 
00283     // Do not call unplugAll from here, as tempting as it sounds.
00284     // KAction is designed around the idea that you need to plug
00285     // _and_ to unplug it "manually". Unplugging leads to an important
00286     // slowdown when e.g. closing the window, in which case we simply
00287     // want to destroy everything asap, not to remove actions one by one
00288     // from the GUI.
00289 
00290     delete d; d = 0;
00291 }
00292 
00293 void KAction::initPrivate( const QString& text, const KShortcut& cut,
00294                   const QObject* receiver, const char* slot )
00295 {
00296     d = new KActionPrivate;
00297 
00298     d->m_cutDefault = cut;
00299 
00300     m_parentCollection = dynamic_cast<KActionCollection *>( parent() );
00301     kdDebug(129) << "KAction::initPrivate(): this = " << this << " name = \"" << name() << "\" cut = " << cut.toStringInternal() << " m_parentCollection = " << m_parentCollection << endl;
00302     if ( m_parentCollection )
00303         m_parentCollection->insert( this );
00304 
00305     if ( receiver && slot )
00306         connect( this, SIGNAL( activated() ), receiver, slot );
00307 
00308     if( !cut.isNull() && qstrcmp( name(), "unnamed" ) == 0 )
00309         kdWarning(129) << "KAction::initPrivate(): trying to assign a shortcut (" << cut.toStringInternal() << ") to an unnamed action." << endl;
00310     d->setText( text );
00311     initShortcut( cut );
00312 }
00313 
00314 bool KAction::isPlugged() const
00315 {
00316   return (containerCount() > 0) || d->m_kaccel;
00317 }
00318 
00319 bool KAction::isPlugged( const QWidget *container ) const
00320 {
00321   return findContainer( container ) > -1;
00322 }
00323 
00324 bool KAction::isPlugged( const QWidget *container, int id ) const
00325 {
00326   int i = findContainer( container );
00327   return ( i > -1 && itemId( i ) == id );
00328 }
00329 
00330 bool KAction::isPlugged( const QWidget *container, const QWidget *_representative ) const
00331 {
00332   int i = findContainer( container );
00333   return ( i > -1 && representative( i ) == _representative );
00334 }
00335 
00336 
00337 /*
00338 Three actionCollection conditions:
00339     1) Scope is known on creation and KAccel object is created (e.g. KMainWindow)
00340     2) Scope is unknown and no KAccel object is available (e.g. KXMLGUIClient)
00341         a) addClient() will be called on object
00342         b) we just want to add the actions to another KXMLGUIClient object
00343 
00344 The question is how to do we incorporate #2b into the XMLGUI framework?
00345 
00346 
00347 We have a KCommandHistory object with undo and redo actions in a passed actionCollection
00348 We have a KoDoc object which holds a KCommandHistory object and the actionCollection
00349 We have two KoView objects which both point to the same KoDoc object
00350 Undo and Redo should be available in both KoView objects, and
00351     calling the undo->setEnabled() should affect both KoViews
00352 
00353 When addClient is called, it needs to be able to find the undo and redo actions
00354 When it calls plug() on them, they need to be inserted into the KAccel object of the appropriate KoView
00355 
00356 In this case, the actionCollection belongs to KoDoc and we need to let it know that its shortcuts
00357 have the same scope as the KoView actionCollection
00358 
00359 KXMLGUIClient::addSubActionCollection
00360 
00361 Document:
00362     create document actions
00363 
00364 View
00365     create view actions
00366     add document actionCollection as sub-collection
00367 
00368 A parentCollection is created
00369 Scenario 1: parentCollection has a focus widget set (e.g. via KMainWindow)
00370     A KAccel object is created in the parentCollection
00371     A KAction is created with parent=parentCollection
00372     The shortcut is inserted into this actionCollection
00373     Scenario 1a: xml isn't used
00374         done
00375     Scenario 1b: KXMLGUIBuilder::addClient() called
00376         setWidget is called -- ignore
00377         shortcuts are set
00378 Scenario 2: parentCollection has no focus widget (e.g., KParts)
00379     A KAction is created with parent=parentCollection
00380     Scenario 2a: xml isn't used
00381         no shortcuts
00382     Scenario 2b: KXMLGUIBuilder::addClient() called
00383         setWidget is called
00384         shortcuts are inserted into current KAccel
00385         shortcuts are set in all other KAccels, if the action is present in the other KAccels
00386 */
00387 
00388 /*
00389 shortcut may be set:
00390     - on construction
00391     - on plug
00392     - on reading XML
00393     - on plugAccel (deprecated)
00394 
00395 On Construction: [via initShortcut()]
00396     insert into KAccel of m_parentCollection,
00397         if kaccel() && isAutoConnectShortcuts() exists
00398 
00399 On Plug: [via plug() -> plugShortcut()]
00400     insert into KAccel of m_parentCollection, if exists and not already inserted into
00401 
00402 On Read XML: [via setShortcut()]
00403     set in all current KAccels
00404     insert into KAccel of m_parentCollection, if exists and not already inserted into
00405 */
00406 
00407 KAccel* KAction::kaccelCurrent()
00408 {
00409   if( m_parentCollection && m_parentCollection->builderKAccel() )
00410     return m_parentCollection->builderKAccel();
00411   else if( m_parentCollection && m_parentCollection->kaccel() )
00412     return m_parentCollection->kaccel();
00413   else
00414     return 0L;
00415 }
00416 
00417 // Only to be called from initPrivate()
00418 bool KAction::initShortcut( const KShortcut& cut )
00419 {
00420     d->m_cut = cut;
00421 
00422     // Only insert action into KAccel if it has a valid name,
00423     if( qstrcmp( name(), "unnamed" ) != 0 &&
00424         m_parentCollection &&
00425         m_parentCollection->isAutoConnectShortcuts() &&
00426         m_parentCollection->kaccel() )
00427     {
00428         insertKAccel( m_parentCollection->kaccel() );
00429         return true;
00430     } else
00431         return false;
00432  }
00433 
00434 // Only to be called from plug()
00435 void KAction::plugShortcut()
00436 {
00437   KAccel* kaccel = kaccelCurrent();
00438 
00439   //kdDebug(129) << "KAction::plugShortcut(): this = " << this << " kaccel() = " << (m_parentCollection ? m_parentCollection->kaccel() : 0) << endl;
00440   if( kaccel && qstrcmp( name(), "unnamed" ) != 0 ) {
00441     // Check if already plugged into current KAccel object
00442     for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
00443       if( d->m_kaccelList[i] == kaccel )
00444         return;
00445     }
00446 
00447     insertKAccel( kaccel );
00448   }
00449 }
00450 
00451 bool KAction::setShortcut( const KShortcut& cut )
00452 {
00453   bool bChanged = (d->m_cut != cut);
00454   d->m_cut = cut;
00455 
00456   KAccel* kaccel = kaccelCurrent();
00457   bool bInsertRequired = true;
00458   // Apply new shortcut to all existing KAccel objects
00459   for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
00460     // Check whether shortcut has already been plugged into
00461     //  the current kaccel object.
00462     if( d->m_kaccelList[i] == kaccel )
00463       bInsertRequired = false;
00464     if( bChanged )
00465       updateKAccelShortcut( d->m_kaccelList[i] );
00466   }
00467 
00468   // Only insert action into KAccel if it has a valid name,
00469   if( kaccel && bInsertRequired && qstrcmp( name(), "unnamed" ) )
00470     insertKAccel( kaccel );
00471 
00472   if( bChanged ) {
00473 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
00474     if ( d->m_kaccel )
00475       d->m_kaccel->setShortcut( name(), cut );
00476 #endif
00477       int len = containerCount();
00478       for( int i = 0; i < len; ++i )
00479           updateShortcut( i );
00480   }
00481   return true;
00482 }
00483 
00484 bool KAction::updateKAccelShortcut( KAccel* kaccel )
00485 {
00486   // Check if action is permitted
00487   if (kapp && !kapp->authorizeKAction(name()))
00488     return false;
00489 
00490   bool b = true;
00491 
00492   if ( !kaccel->actions().actionPtr( name() ) ) {
00493     if(!d->m_cut.isNull() ) {
00494       kdDebug(129) << "Inserting " << name() << ", " << d->text() << ", " << d->plainText() << endl;
00495       b = kaccel->insert( name(), d->plainText(), QString::null,
00496           d->m_cut,
00497           this, SLOT(slotActivated()),
00498           isShortcutConfigurable(), isEnabled() );
00499       if( !b )
00500         kdWarning(129) << "KAction::updateKAccelShortcut: KAccel::insert() failed." << endl;
00501     }
00502   }
00503   else
00504     b = kaccel->setShortcut( name(), d->m_cut );
00505 
00506   return b;
00507 }
00508 
00509 void KAction::insertKAccel( KAccel* kaccel )
00510 {
00511   //kdDebug(129) << "KAction::insertKAccel( " << kaccel << " ): this = " << this << endl;
00512   if ( !kaccel->actions().actionPtr( name() ) ) {
00513     if( updateKAccelShortcut( kaccel ) ) {
00514       d->m_kaccelList.append( kaccel );
00515       connect( kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
00516     }
00517     else
00518       kdWarning(129) << "KAction::insertKAccel: KAccel::insert() failed." << endl;
00519   }
00520   else
00521     kdWarning(129) << "KAction::insertKAccel( kaccel = " << kaccel << " ): KAccel object already contains an action name \"" << name() << "\"" << endl; // -- ellis
00522 }
00523 
00524 void KAction::removeKAccel( KAccel* kaccel )
00525 {
00526   //kdDebug(129) << "KAction::removeKAccel( " << i << " ): this = " << this << endl;
00527   for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
00528     if( d->m_kaccelList[i] == kaccel ) {
00529       kaccel->remove( name() );
00530       d->m_kaccelList.remove( d->m_kaccelList.at( i ) );
00531       disconnect( kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
00532       break;
00533     }
00534   }
00535 }
00536 
00537 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
00538 void KAction::setAccel( int keyQt )
00539 {
00540   setShortcut( KShortcut(keyQt) );
00541 }
00542 #endif
00543 
00544 void KAction::updateShortcut( int i )
00545 {
00546   int id = itemId( i );
00547 
00548   QWidget* w = container( i );
00549   if ( w->inherits( "QPopupMenu" ) ) {
00550     QPopupMenu* menu = static_cast<QPopupMenu*>(w);
00551     updateShortcut( menu, id );
00552   }
00553   else if ( w->inherits( "QMenuBar" ) )
00554     static_cast<QMenuBar*>(w)->setAccel( d->m_cut.keyCodeQt(), id );
00555 }
00556 
00557 void KAction::updateShortcut( QPopupMenu* menu, int id )
00558 {
00559   //kdDebug(129) << "KAction::updateShortcut(): this = " << this << " d->m_kaccelList.count() = " << d->m_kaccelList.count() << endl;
00560   // If the action has a KAccel object,
00561   //  show the string representation of its shortcut.
00562   if ( d->m_kaccel || d->m_kaccelList.count() ) {
00563     QString s = menu->text( id );
00564     int i = s.find( '\t' );
00565     if ( i >= 0 )
00566       s.replace( i+1, s.length()-i, d->m_cut.seq(0).toString() );
00567     else
00568       s += "\t" + d->m_cut.seq(0).toString();
00569 
00570     menu->changeItem( id, s );
00571   }
00572   // Otherwise insert the shortcut itself into the popup menu.
00573   else {
00574     // This is a fall-hack in case the KAction is missing a proper parent collection.
00575     //  It should be removed eventually. --ellis
00576     menu->setAccel( d->m_cut.keyCodeQt(), id );
00577     kdWarning(129) << "KAction::updateShortcut(): name = \"" << name() << "\", cut = " << d->m_cut.toStringInternal() << "; No KAccel, probably missing a parent collection." << endl;
00578   }
00579 }
00580 
00581 const KShortcut& KAction::shortcut() const
00582 {
00583   return d->m_cut;
00584 }
00585 
00586 const KShortcut& KAction::shortcutDefault() const
00587 {
00588   return d->m_cutDefault;
00589 }
00590 
00591 QString KAction::shortcutText() const
00592 {
00593   return d->m_cut.toStringInternal();
00594 }
00595 
00596 void KAction::setShortcutText( const QString& s )
00597 {
00598   setShortcut( KShortcut(s) );
00599 }
00600 
00601 int KAction::accel() const
00602 {
00603   return d->m_cut.keyCodeQt();
00604 }
00605 
00606 void KAction::setGroup( const QString& grp )
00607 {
00608   d->m_group = grp;
00609 
00610   int len = containerCount();
00611   for( int i = 0; i < len; ++i )
00612     updateGroup( i );
00613 }
00614 
00615 void KAction::updateGroup( int )
00616 {
00617   // DO SOMETHING
00618 }
00619 
00620 QString KAction::group() const
00621 {
00622   return d->m_group;
00623 }
00624 
00625 bool KAction::isEnabled() const
00626 {
00627   return d->isEnabled();
00628 }
00629 
00630 bool KAction::isShortcutConfigurable() const
00631 {
00632   return d->m_configurable;
00633 }
00634 
00635 void KAction::setToolTip( const QString& tt )
00636 {
00637   d->setToolTip( tt );
00638 
00639   int len = containerCount();
00640   for( int i = 0; i < len; ++i )
00641     updateToolTip( i );
00642 }
00643 
00644 void KAction::updateToolTip( int i )
00645 {
00646   QWidget *w = container( i );
00647 
00648   if ( w->inherits( "KToolBar" ) )
00649     QToolTip::add( static_cast<KToolBar*>(w)->getWidget( itemId( i ) ), d->toolTip() );
00650 }
00651 
00652 QString KAction::toolTip() const
00653 {
00654   return d->toolTip();
00655 }
00656 
00657 int KAction::plug( QWidget *w, int index )
00658 {
00659   //kdDebug(129) << "KAction::plug( " << w << ", " << index << " )" << endl;
00660   if (w == 0) {
00661     kdWarning(129) << "KAction::plug called with 0 argument\n";
00662     return -1;
00663   }
00664 
00665 #ifndef NDEBUG
00666   KAccel* kaccel = kaccelCurrent();
00667   // If there is a shortcut, but no KAccel available
00668   if( !d->m_cut.isNull() && kaccel == 0 ) {
00669     kdWarning(129) << "KAction::plug(): has no KAccel object; this = " << this << " name = " << name() << " parentCollection = " << m_parentCollection << endl; // ellis
00670     kdDebug(129) << kdBacktrace() << endl;
00671   }
00672 #endif
00673 
00674   // Check if action is permitted
00675   if (kapp && !kapp->authorizeKAction(name()))
00676     return -1;
00677 
00678   plugShortcut();
00679 
00680   if ( w->inherits("QPopupMenu") )
00681   {
00682     QPopupMenu* menu = static_cast<QPopupMenu*>( w );
00683     int id;
00684     // Don't insert shortcut into menu if it's already in a KAccel object.
00685     int keyQt = (d->m_kaccelList.count() || d->m_kaccel) ? 0 : d->m_cut.keyCodeQt();
00686 
00687     if ( d->hasIcon() )
00688     {
00689         KInstance *instance;
00690         if ( m_parentCollection )
00691           instance = m_parentCollection->instance();
00692         else
00693           instance = KGlobal::instance();
00694         id = menu->insertItem( d->iconSet( KIcon::Small, 0, instance ), d->text(), this,//dsweet
00695                                  SLOT( slotActivated() ), keyQt,
00696                                  -1, index );
00697     }
00698     else
00699         id = menu->insertItem( d->text(), this,
00700                                SLOT( slotActivated() ),  //dsweet
00701                                keyQt, -1, index );
00702 
00703     // If the shortcut is already in a KAccel object, then
00704     //  we need to set the menu item's shortcut text.
00705     if ( d->m_kaccelList.count() || d->m_kaccel )
00706         updateShortcut( menu, id );
00707 
00708     // call setItemEnabled only if the item really should be disabled,
00709     // because that method is slow and the item is per default enabled
00710     if ( !d->isEnabled() )
00711         menu->setItemEnabled( id, false );
00712 
00713     if ( !d->whatsThis().isEmpty() )
00714         menu->setWhatsThis( id, whatsThisWithIcon() );
00715 
00716     addContainer( menu, id );
00717     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00718 
00719     if ( m_parentCollection )
00720       m_parentCollection->connectHighlight( menu, this );
00721 
00722     return d->m_containers.count() - 1;
00723   }
00724   else if ( w->inherits( "KToolBar" ) )
00725   {
00726     KToolBar *bar = static_cast<KToolBar *>( w );
00727 
00728     int id_ = getToolButtonID();
00729     KInstance *instance;
00730     if ( m_parentCollection )
00731       instance = m_parentCollection->instance();
00732     else
00733       instance = KGlobal::instance();
00734 
00735     if ( icon().isEmpty() ) // old code using QIconSet directly
00736     {
00737       bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00738                          SLOT( slotActivated() ),
00739                          d->isEnabled(), d->plainText(), index );
00740     }
00741     else
00742       bar->insertButton( d->iconName(), id_, SIGNAL( clicked() ), this,
00743                          SLOT( slotActivated() ),
00744                          d->isEnabled(), d->plainText(), index, instance );
00745 
00746     bar->getButton( id_ )->setName( QCString("toolbutton_")+name() );
00747 
00748     if ( !d->whatsThis().isEmpty() )
00749         QWhatsThis::add( bar->getButton(id_), whatsThisWithIcon() );
00750 
00751     if ( !d->toolTip().isEmpty() )
00752       QToolTip::add( bar->getButton(id_), d->toolTip() );
00753 
00754     addContainer( bar, id_ );
00755 
00756     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00757 
00758     if ( m_parentCollection )
00759       m_parentCollection->connectHighlight( bar, this );
00760 
00761     return containerCount() - 1;
00762   }
00763 
00764   return -1;
00765 }
00766 
00767 void KAction::unplug( QWidget *w )
00768 {
00769   int i = findContainer( w );
00770   if ( i == -1 )
00771     return;
00772   int id = itemId( i );
00773 
00774   if ( w->inherits( "QPopupMenu" ) )
00775   {
00776     QPopupMenu *menu = static_cast<QPopupMenu *>( w );
00777     menu->removeItem( id );
00778   }
00779   else if ( w->inherits( "KToolBar" ) )
00780   {
00781     KToolBar *bar = static_cast<KToolBar *>( w );
00782     bar->removeItem( id );
00783   }
00784   else if ( w->inherits( "QMenuBar" ) )
00785   {
00786     QMenuBar *bar = static_cast<QMenuBar *>( w );
00787     bar->removeItem( id );
00788   }
00789 
00790   removeContainer( i );
00791   if ( m_parentCollection )
00792     m_parentCollection->disconnectHighlight( w, this );
00793 }
00794 
00795 void KAction::plugAccel(KAccel *kacc, bool configurable)
00796 {
00797   kdWarning(129) << "KAction::plugAccel(): call to deprecated action." << endl;
00798   kdDebug(129) << kdBacktrace() << endl;
00799   //kdDebug(129) << "KAction::plugAccel( kacc = " << kacc << " ): name \"" << name() << "\"" << endl;
00800   if ( d->m_kaccel )
00801     unplugAccel();
00802 
00803   // If the parent collection's accel ptr isn't set yet
00804   //if ( m_parentCollection && !m_parentCollection->accel() )
00805   //  m_parentCollection->setAccel( kacc );
00806 
00807   // We can only plug this action into the given KAccel object
00808   //  if it does not already contain an action with the same name.
00809   if ( !kacc->actions().actionPtr(name()) )
00810   {
00811     d->m_kaccel = kacc;
00812     d->m_kaccel->insert(name(), d->plainText(), QString::null,
00813         KShortcut(d->m_cut),
00814         this, SLOT(slotActivated()),
00815         configurable, isEnabled());
00816     connect(d->m_kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()));
00817     //connect(d->m_kaccel, SIGNAL(keycodeChanged()), this, SLOT(slotKeycodeChanged()));
00818   }
00819   else
00820     kdWarning(129) << "KAction::plugAccel( kacc = " << kacc << " ): KAccel object already contains an action name \"" << name() << "\"" << endl; // -- ellis
00821 }
00822 
00823 void KAction::unplugAccel()
00824 {
00825   //kdDebug(129) << "KAction::unplugAccel() " << this << " " << name() << endl;
00826   if ( d->m_kaccel )
00827   {
00828     d->m_kaccel->remove(name());
00829     d->m_kaccel = 0;
00830   }
00831 }
00832 
00833 void KAction::plugMainWindowAccel( QWidget *w )
00834 {
00835   // Note: topLevelWidget() stops too early, we can't use it.
00836   QWidget * tl = w;
00837   QWidget * n;
00838   while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
00839     tl = n;
00840 
00841   KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
00842   if (mw)
00843     plugAccel( mw->accel() );
00844   else
00845     kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << tl << endl;
00846 }
00847 
00848 void KAction::setEnabled(bool enable)
00849 {
00850   //kdDebug(129) << "KAction::setEnabled( " << enable << " ): this = " << this << " d->m_kaccelList.count() = " << d->m_kaccelList.count() << endl;
00851   if ( enable == d->isEnabled() )
00852     return;
00853 
00854 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
00855   if (d->m_kaccel)
00856     d->m_kaccel->setEnabled(name(), enable);
00857 #endif
00858 
00859   for ( uint i = 0; i < d->m_kaccelList.count(); i++ )
00860     d->m_kaccelList[i]->setEnabled( name(), enable );
00861 
00862   d->setEnabled( enable );
00863 
00864   int len = containerCount();
00865   for( int i = 0; i < len; ++i )
00866     updateEnabled( i );
00867 
00868   emit enabled( d->isEnabled() );
00869 }
00870 
00871 void KAction::updateEnabled( int i )
00872 {
00873     QWidget *w = container( i );
00874 
00875     if ( w->inherits("QPopupMenu") )
00876       static_cast<QPopupMenu*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
00877     else if ( w->inherits("QMenuBar") )
00878       static_cast<QMenuBar*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
00879     else if ( w->inherits( "KToolBar" ) )
00880       static_cast<KToolBar*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
00881 }
00882 
00883 void KAction::setShortcutConfigurable( bool b )
00884 {
00885     d->m_configurable = b;
00886 }
00887 
00888 void KAction::setText( const QString& text )
00889 {
00890 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
00891   if (d->m_kaccel) {
00892     KAccelAction* pAction = d->m_kaccel->actions().actionPtr(name());
00893     if (pAction)
00894       pAction->setLabel( text );
00895   }
00896 #endif
00897 
00898   for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
00899     KAccelAction* pAction = d->m_kaccelList[i]->actions().actionPtr(name());
00900     if (pAction)
00901       pAction->setLabel( text );
00902   }
00903 
00904   d->setText( text );
00905 
00906   int len = containerCount();
00907   for( int i = 0; i < len; ++i )
00908     updateText( i );
00909 }
00910 
00911 void KAction::updateText( int i )
00912 {
00913   QWidget *w = container( i );
00914 
00915   if ( w->inherits( "QPopupMenu" ) ) {
00916     int id = itemId( i );
00917     static_cast<QPopupMenu*>(w)->changeItem( id, d->text() );
00918     updateShortcut( static_cast<QPopupMenu*>(w), id );
00919   }
00920   else if ( w->inherits( "QMenuBar" ) )
00921     static_cast<QMenuBar*>(w)->changeItem( itemId( i ), d->text() );
00922   else if ( w->inherits( "KToolBar" ) )
00923   {
00924     QWidget *button = static_cast<KToolBar *>(w)->getWidget( itemId( i ) );
00925     if ( button->inherits( "KToolBarButton" ) )
00926       static_cast<KToolBarButton *>(button)->setText( d->plainText() );
00927   }
00928 }
00929 
00930 QString KAction::text() const
00931 {
00932   return d->text();
00933 }
00934 
00935 QString KAction::plainText() const
00936 {
00937   return d->plainText( );
00938 }
00939 
00940 void KAction::setIcon( const QString &icon )
00941 {
00942   d->setIconName( icon );
00943 
00944   // now handle any toolbars
00945   int len = containerCount();
00946   for ( int i = 0; i < len; ++i )
00947     updateIcon( i );
00948 }
00949 
00950 void KAction::updateIcon( int id )
00951 {
00952   QWidget* w = container( id );
00953 
00954   if ( w->inherits( "QPopupMenu" ) ) {
00955     int itemId_ = itemId( id );
00956     static_cast<QPopupMenu*>(w)->changeItem( itemId_, d->iconSet( KIcon::Small ), d->text() );
00957     updateShortcut( static_cast<QPopupMenu*>(w), itemId_ );
00958   }
00959   else if ( w->inherits( "QMenuBar" ) )
00960     static_cast<QMenuBar*>(w)->changeItem( itemId( id ), d->iconSet( KIcon::Small ), d->text() );
00961   else if ( w->inherits( "KToolBar" ) )
00962     static_cast<KToolBar *>(w)->setButtonIcon( itemId( id ), d->iconName() );
00963 }
00964 
00965 QString KAction::icon() const
00966 {
00967   return d->iconName( );
00968 }
00969 
00970 void KAction::setIconSet( const QIconSet &iconset )
00971 {
00972   d->setIconSet( iconset );
00973 
00974   int len = containerCount();
00975   for( int i = 0; i < len; ++i )
00976     updateIconSet( i );
00977 }
00978 
00979 
00980 void KAction::updateIconSet( int id )
00981 {
00982   QWidget *w = container( id );
00983 
00984   if ( w->inherits( "QPopupMenu" ) )
00985   {
00986     int itemId_ = itemId( id );
00987     static_cast<QPopupMenu*>(w)->changeItem( itemId_, d->iconSet(), d->text() );
00988     updateShortcut( static_cast<QPopupMenu*>(w), itemId_ );
00989   }
00990   else if ( w->inherits( "QMenuBar" ) )
00991     static_cast<QMenuBar*>(w)->changeItem( itemId( id ), d->iconSet(), d->text() );
00992   else if ( w->inherits( "KToolBar" ) )
00993   {
00994     if ( icon().isEmpty() && d->hasIconSet() ) // only if there is no named icon ( scales better )
00995       static_cast<KToolBar *>(w)->setButtonIconSet( itemId( id ), d->iconSet() );
00996     else
00997       static_cast<KToolBar *>(w)->setButtonIconSet( itemId( id ), d->iconSet( KIcon::Small ) );
00998   }
00999 }
01000 
01001 QIconSet KAction::iconSet( KIcon::Group group, int size ) const
01002 {
01003     return d->iconSet( group, size );
01004 }
01005 
01006 bool KAction::hasIcon() const
01007 {
01008   return d->hasIcon();
01009 }
01010 
01011 void KAction::setWhatsThis( const QString& text )
01012 {
01013   d->setWhatsThis(  text );
01014 
01015   int len = containerCount();
01016   for( int i = 0; i < len; ++i )
01017     updateWhatsThis( i );
01018 }
01019 
01020 void KAction::updateWhatsThis( int i )
01021 {
01022   QPopupMenu* pm = popupMenu( i );
01023   if ( pm )
01024   {
01025     pm->setWhatsThis( itemId( i ), d->whatsThis() );
01026     return;
01027   }
01028 
01029   KToolBar *tb = toolBar( i );
01030   if ( tb )
01031   {
01032     QWidget *w = tb->getButton( itemId( i ) );
01033     QWhatsThis::remove( w );
01034     QWhatsThis::add( w, d->whatsThis() );
01035     return;
01036   }
01037 }
01038 
01039 QString KAction::whatsThis() const
01040 {
01041   return d->whatsThis();
01042 }
01043 
01044 QString KAction::whatsThisWithIcon() const
01045 {
01046     QString text = whatsThis();
01047     if (!d->iconName().isEmpty())
01048       return QString::fromLatin1("<img source=\"small|%1\"> %2").arg(d->iconName() ).arg(text);
01049     return text;
01050 }
01051 
01052 QWidget* KAction::container( int index ) const
01053 {
01054   assert( index < containerCount() );
01055   return d->m_containers[ index ].m_container;
01056 }
01057 
01058 KToolBar* KAction::toolBar( int index ) const
01059 {
01060     return dynamic_cast<KToolBar *>( d->m_containers[ index ].m_container );
01061 }
01062 
01063 QPopupMenu* KAction::popupMenu( int index ) const
01064 {
01065     return dynamic_cast<QPopupMenu *>( d->m_containers[ index ].m_container );
01066 }
01067 
01068 QWidget* KAction::representative( int index ) const
01069 {
01070   return d->m_containers[ index ].m_representative;
01071 }
01072 
01073 int KAction::itemId( int index ) const
01074 {
01075   return d->m_containers[ index ].m_id;
01076 }
01077 
01078 int KAction::containerCount() const
01079 {
01080   return d->m_containers.count();
01081 }
01082 
01083 uint KAction::kaccelCount() const
01084 {
01085   return d->m_kaccelList.count();
01086 }
01087 
01088 void KAction::addContainer( QWidget* c, int id )
01089 {
01090   KActionPrivate::Container p;
01091   p.m_container = c;
01092   p.m_id = id;
01093   d->m_containers.append( p );
01094 }
01095 
01096 void KAction::addContainer( QWidget* c, QWidget* w )
01097 {
01098   KActionPrivate::Container p;
01099   p.m_container = c;
01100   p.m_representative = w;
01101   d->m_containers.append( p );
01102 }
01103 
01104 void KAction::activate()
01105 {
01106   slotActivated();
01107 }
01108 
01109 void KAction::slotActivated()
01110 {
01111   emit activated();
01112 }
01113 
01114 void KAction::slotDestroyed()
01115 {
01116   kdDebug(129) << "KAction::slotDestroyed(): this = " << this << ", name = \"" << name() << "\", sender = " << sender() << endl;
01117   const QObject* o = sender();
01118 
01119 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
01120   if ( o == d->m_kaccel )
01121   {
01122     d->m_kaccel = 0;
01123     return;
01124   }
01125 #endif
01126 
01127   for( uint i = 0; i < d->m_kaccelList.count(); i++ )
01128   {
01129     if ( o == d->m_kaccelList[i] )
01130     {
01131       disconnect( d->m_kaccelList[i], SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
01132       d->m_kaccelList.remove( d->m_kaccelList.at( i ) );
01133       return;
01134     }
01135   }
01136 
01137   int i;
01138   do
01139   {
01140     i = findContainer( static_cast<const QWidget*>( o ) );
01141     if ( i != -1 )
01142       removeContainer( i );
01143   } while ( i != -1 );
01144 }
01145 
01146 int KAction::findContainer( const QWidget* widget ) const
01147 {
01148   int pos = 0;
01149   QValueList<KActionPrivate::Container>::ConstIterator it = d->m_containers.begin();
01150   while( it != d->m_containers.end() )
01151   {
01152     if ( (*it).m_representative == widget || (*it).m_container == widget )
01153       return pos;
01154     ++it;
01155     ++pos;
01156   }
01157 
01158   return -1;
01159 }
01160 
01161 void KAction::removeContainer( int index )
01162 {
01163   int i = 0;
01164   QValueList<KActionPrivate::Container>::Iterator it = d->m_containers.begin();
01165   while( it != d->m_containers.end() )
01166   {
01167     if ( i == index )
01168     {
01169       d->m_containers.remove( it );
01170       return;
01171     }
01172     ++it;
01173     ++i;
01174   }
01175 }
01176 
01177 // FIXME: Remove this (ellis)
01178 void KAction::slotKeycodeChanged()
01179 {
01180   kdDebug(129) << "KAction::slotKeycodeChanged()" << endl; // -- ellis
01181   KAccelAction* pAction = d->m_kaccel->actions().actionPtr(name());
01182   if( pAction )
01183     setShortcut(pAction->shortcut());
01184 }
01185 
01186 KActionCollection *KAction::parentCollection() const
01187 {
01188     return m_parentCollection;
01189 }
01190 
01191 void KAction::unplugAll()
01192 {
01193   while ( containerCount() != 0 )
01194     unplug( container( 0 ) );
01195 }
01196 
01197 class KToggleAction::KToggleActionPrivate
01198 {
01199 public:
01200   KToggleActionPrivate()
01201   {
01202     m_checked = false;
01203   }
01204 
01205   bool m_checked;
01206   QString m_exclusiveGroup;
01207 };
01208 
01209 KToggleAction::KToggleAction( const QString& text, const KShortcut& cut,
01210                               QObject* parent,
01211                               const char* name )
01212     : KAction( text, cut, parent, name )
01213 {
01214   d = new KToggleActionPrivate;
01215 }
01216 
01217 KToggleAction::KToggleAction( const QString& text, const KShortcut& cut,
01218                               const QObject* receiver, const char* slot,
01219                               QObject* parent, const char* name )
01220   : KAction( text, cut, receiver, slot, parent, name )
01221 {
01222   d = new KToggleActionPrivate;
01223 }
01224 
01225 KToggleAction::KToggleAction( const QString& text, const QIconSet& pix,
01226                               const KShortcut& cut,
01227                               QObject* parent, const char* name )
01228   : KAction( text, pix, cut, parent, name )
01229 {
01230   d = new KToggleActionPrivate;
01231 }
01232 
01233 KToggleAction::KToggleAction( const QString& text, const QString& pix,
01234                               const KShortcut& cut,
01235                               QObject* parent, const char* name )
01236  : KAction( text, pix, cut, parent, name )
01237 {
01238   d = new KToggleActionPrivate;
01239 }
01240 
01241 KToggleAction::KToggleAction( const QString& text, const QIconSet& pix,
01242                               const KShortcut& cut,
01243                               const QObject* receiver,
01244                               const char* slot, QObject* parent,
01245                               const char* name )
01246   : KAction( text, pix, cut, receiver, slot, parent, name )
01247 {
01248   d = new KToggleActionPrivate;
01249 }
01250 
01251 KToggleAction::KToggleAction( const QString& text, const QString& pix,
01252                               const KShortcut& cut,
01253                               const QObject* receiver,
01254                               const char* slot, QObject* parent,
01255                               const char* name )
01256   : KAction( text, pix, cut, receiver, slot, parent, name )
01257 {
01258   d = new KToggleActionPrivate;
01259 }
01260 
01261 KToggleAction::KToggleAction( QObject* parent, const char* name )
01262     : KAction( parent, name )
01263 {
01264   d = new KToggleActionPrivate;
01265 }
01266 
01267 KToggleAction::~KToggleAction()
01268 {
01269     delete d;
01270 }
01271 
01272 int KToggleAction::plug( QWidget* widget, int index )
01273 {
01274   if ( !widget->inherits("QPopupMenu") && !widget->inherits("KToolBar") )
01275   {
01276     kdWarning() << "Can not plug KToggleAction in " << widget->className() << endl;
01277     return -1;
01278   }
01279   if (kapp && !kapp->authorizeKAction(name()))
01280     return -1;
01281 
01282   int _index = KAction::plug( widget, index );
01283   if ( _index == -1 )
01284     return _index;
01285 
01286   if ( widget->inherits("QPopupMenu") )
01287   {
01288     int id = itemId( _index );
01289 
01290     static_cast<QPopupMenu*>(widget)->setItemChecked( id, d->m_checked );
01291   } else if ( widget->inherits( "KToolBar" ) ) {
01292     KToolBar *bar = static_cast<KToolBar *>( widget );
01293 
01294     bar->setToggle( itemId( _index ), true );
01295     bar->setButton( itemId( _index ), isChecked() );
01296   }
01297 
01298   return _index;
01299 }
01300 
01301 void KToggleAction::setChecked( bool c )
01302 {
01303   if ( c == d->m_checked )
01304     return;
01305   //kdDebug(129) << "KToggleAction::setChecked(" << c << ") " << this << " " << name() << endl;
01306 
01307   d->m_checked = c;
01308 
01309   int len = containerCount();
01310 
01311   for( int i = 0; i < len; ++i )
01312     updateChecked( i );
01313 
01314   if ( c && parent() && !exclusiveGroup().isEmpty() ) {
01315     const QObjectList *list = parent()->children();
01316     if ( list ) {
01317       QObjectListIt it( *list );
01318       for( ; it.current(); ++it ) {
01319         if ( it.current()->inherits( "KToggleAction" ) && it.current() != this &&
01320             static_cast<KToggleAction*>(it.current())->exclusiveGroup() == exclusiveGroup() ) {
01321       KToggleAction *a = static_cast<KToggleAction*>(it.current());
01322       if( a->isChecked() ) {
01323         a->setChecked( false );
01324         emit a->toggled( false );
01325       }
01326         }
01327       }
01328     }
01329   }
01330 }
01331 
01332 void KToggleAction::updateChecked( int id )
01333 {
01334   QWidget *w = container( id );
01335 
01336   if ( w->inherits( "QPopupMenu" ) )
01337     static_cast<QPopupMenu*>(w)->setItemChecked( itemId( id ), d->m_checked );
01338   else if ( w->inherits( "QMenuBar" ) )
01339     static_cast<QMenuBar*>(w)->setItemChecked( itemId( id ), d->m_checked );
01340   else if ( w->inherits( "KToolBar" ) )
01341   {
01342     QWidget* r = static_cast<KToolBar*>( w )->getButton( itemId( id ) );
01343     if ( r && r->inherits( "KToolBarButton" ) )
01344       static_cast<KToolBar*>( w )->setButton( itemId( id ), d->m_checked );
01345   }
01346 }
01347 
01348 void KToggleAction::slotActivated()
01349 {
01350   setChecked( !isChecked() );
01351   emit activated();
01352   emit toggled( isChecked() );
01353 }
01354 
01355 bool KToggleAction::isChecked() const
01356 {
01357   return d->m_checked;
01358 }
01359 
01360 void KToggleAction::setExclusiveGroup( const QString& name )
01361 {
01362   d->m_exclusiveGroup = name;
01363 }
01364 
01365 QString KToggleAction::exclusiveGroup() const
01366 {
01367   return d->m_exclusiveGroup;
01368 }
01369 
01370 
01371 KRadioAction::KRadioAction( const QString& text, const KShortcut& cut,
01372                             QObject* parent, const char* name )
01373 : KToggleAction( text, cut, parent, name )
01374 {
01375 }
01376 
01377 KRadioAction::KRadioAction( const QString& text, const KShortcut& cut,
01378                             const QObject* receiver, const char* slot,
01379                             QObject* parent, const char* name )
01380 : KToggleAction( text, cut, receiver, slot, parent, name )
01381 {
01382 }
01383 
01384 KRadioAction::KRadioAction( const QString& text, const QIconSet& pix,
01385                             const KShortcut& cut,
01386                             QObject* parent, const char* name )
01387 : KToggleAction( text, pix, cut, parent, name )
01388 {
01389 }
01390 
01391 KRadioAction::KRadioAction( const QString& text, const QString& pix,
01392                             const KShortcut& cut,
01393                             QObject* parent, const char* name )
01394 : KToggleAction( text, pix, cut, parent, name )
01395 {
01396 }
01397 
01398 KRadioAction::KRadioAction( const QString& text, const QIconSet& pix,
01399                             const KShortcut& cut,
01400                             const QObject* receiver, const char* slot,
01401                             QObject* parent, const char* name )
01402 : KToggleAction( text, pix, cut, receiver, slot, parent, name )
01403 {
01404 }
01405 
01406 KRadioAction::KRadioAction( const QString& text, const QString& pix,
01407                             const KShortcut& cut,
01408                             const QObject* receiver, const char* slot,
01409                             QObject* parent, const char* name )
01410 : KToggleAction( text, pix, cut, receiver, slot, parent, name )
01411 {
01412 }
01413 
01414 KRadioAction::KRadioAction( QObject* parent, const char* name )
01415 : KToggleAction( parent, name )
01416 {
01417 }
01418 
01419 void KRadioAction::slotActivated()
01420 {
01421   if ( isChecked() )
01422   {
01423     const QObject *senderObj = sender();
01424 
01425     if ( !senderObj || !senderObj->inherits( "KToolBarButton" ) )
01426       return;
01427 
01428     const_cast<KToolBarButton *>( static_cast<const KToolBarButton *>( senderObj ) )->on( true );
01429 
01430     return;
01431   }
01432 
01433   KToggleAction::slotActivated();
01434 }
01435 
01436 class KSelectAction::KSelectActionPrivate
01437 {
01438 public:
01439   KSelectActionPrivate()
01440   {
01441     m_edit = false;
01442     m_menuAccelsEnabled = true;
01443     m_menu = 0;
01444     m_current = -1;
01445     m_comboWidth = -1;
01446   }
01447   bool m_edit;
01448   bool m_menuAccelsEnabled;
01449   QPopupMenu *m_menu;
01450   int m_current;
01451   int m_comboWidth;
01452   QStringList m_list;
01453 
01454   QString makeMenuText( const QString &_text )
01455   {
01456       if ( m_menuAccelsEnabled )
01457         return _text;
01458       QString text = _text;
01459       uint i = 0;
01460       while ( i < text.length() ) {
01461           if ( text[ i ] == '&' ) {
01462               text.insert( i, '&' );
01463               i += 2;
01464           }
01465           else
01466               ++i;
01467       }
01468       return text;
01469   }
01470 };
01471 
01472 KSelectAction::KSelectAction( const QString& text, const KShortcut& cut,
01473                               QObject* parent, const char* name )
01474   : KAction( text, cut, parent, name )
01475 {
01476   d = new KSelectActionPrivate;
01477 }
01478 
01479 KSelectAction::KSelectAction( const QString& text, const KShortcut& cut,
01480                               const QObject* receiver, const char* slot,
01481                               QObject* parent, const char* name )
01482   : KAction( text, cut, receiver, slot, parent, name )
01483 {
01484   d = new KSelectActionPrivate;
01485 }
01486 
01487 KSelectAction::KSelectAction( const QString& text, const QIconSet& pix,
01488                               const KShortcut& cut,
01489                               QObject* parent, const char* name )
01490   : KAction( text, pix, cut, parent, name )
01491 {
01492   d = new KSelectActionPrivate;
01493 }
01494 
01495 KSelectAction::KSelectAction( const QString& text, const QString& pix,
01496                               const KShortcut& cut,
01497                               QObject* parent, const char* name )
01498   : KAction( text, pix, cut, parent, name )
01499 {
01500   d = new KSelectActionPrivate;
01501 }
01502 
01503 KSelectAction::KSelectAction( const QString& text, const QIconSet& pix,
01504                               const KShortcut& cut,
01505                               const QObject* receiver,
01506                               const char* slot, QObject* parent,
01507                               const char* name )
01508   : KAction( text, pix, cut, receiver, slot, parent, name )
01509 {
01510   d = new KSelectActionPrivate;
01511 }
01512 
01513 KSelectAction::KSelectAction( const QString& text, const QString& pix,
01514                               const KShortcut& cut,
01515                               const QObject* receiver,
01516                               const char* slot, QObject* parent,
01517                               const char* name )
01518   : KAction( text, pix, cut, receiver, slot, parent, name )
01519 {
01520   d = new KSelectActionPrivate;
01521 }
01522 
01523 KSelectAction::KSelectAction( QObject* parent, const char* name )
01524   : KAction( parent, name )
01525 {
01526   d = new KSelectActionPrivate;
01527 }
01528 
01529 KSelectAction::~KSelectAction()
01530 {
01531   assert(d);
01532   delete d->m_menu;
01533   delete d; d = 0;
01534 }
01535 
01536 void KSelectAction::setCurrentItem( int id )
01537 {
01538     if ( id >= (int)d->m_list.count() ) {
01539         Q_ASSERT(id < (int)d->m_list.count());
01540         return;
01541     }
01542 
01543     if ( d->m_menu )
01544     {
01545         if ( d->m_current >= 0 )
01546             d->m_menu->setItemChecked( d->m_current, false );
01547         if ( id >= 0 )
01548             d->m_menu->setItemChecked( id, true );
01549     }
01550 
01551     d->m_current = id;
01552 
01553     int len = containerCount();
01554 
01555     for( int i = 0; i < len; ++i )
01556         updateCurrentItem( i );
01557 
01558     //    emit KAction::activated();
01559     //    emit activated( currentItem() );
01560     //    emit activated( currentText() );
01561 }
01562 
01563 void KSelectAction::setComboWidth( int width )
01564 {
01565   if ( width < 0 )
01566     return;
01567 
01568   d->m_comboWidth=width;
01569 
01570   int len = containerCount();
01571 
01572   for( int i = 0; i < len; ++i )
01573     updateComboWidth( i );
01574 
01575 }
01576 QPopupMenu* KSelectAction::popupMenu() const
01577 {
01578     kdDebug(129) << "KAction::popupMenu()" << endl; // remove -- ellis
01579   if ( !d->m_menu )
01580   {
01581     d->m_menu = new KPopupMenu(0L, "KSelectAction::popupMenu()");
01582     QStringList::ConstIterator it = d->m_list.begin();
01583     int id = 0;
01584     for( ; it != d->m_list.end(); ++it ) {
01585       if (!((*it).isEmpty())) {
01586         d->m_menu->insertItem( d->makeMenuText( *it ), this, SLOT( slotActivated( int ) ), 0, id++ );
01587       } else {
01588         d->m_menu->insertSeparator();
01589       }
01590     }
01591     if ( d->m_current >= 0 )
01592       d->m_menu->setItemChecked( d->m_current, true );
01593   }
01594 
01595   return d->m_menu;
01596 }
01597 
01598 void KSelectAction::changeItem( int index, const QString& text )
01599 {
01600   if ( index < 0 || index >= (int)d->m_list.count() )
01601   {
01602     kdWarning() << "KSelectAction::changeItem Index out of scope" << endl;
01603     return;
01604   }
01605 
01606   d->m_list[ index ] = text;
01607 
01608   if ( d->m_menu )
01609     d->m_menu->changeItem( index, d->makeMenuText( text ) );
01610 
01611   int len = containerCount();
01612   for( int i = 0; i < len; ++i )
01613     changeItem( i, index, text );
01614 }
01615 
01616 void KSelectAction::changeItem( int id, int index, const QString& text)
01617 {
01618   if ( index < 0 )
01619         return;
01620 
01621   QWidget* w = container( id );
01622   if ( w->inherits( "KToolBar" ) )
01623   {
01624      QWidget* r = (static_cast<KToolBar*>( w ))->getWidget( itemId( id ) );
01625      if ( r->inherits( "QComboBox" ) )
01626      {
01627         QComboBox *b = static_cast<QComboBox*>( r );
01628         b->changeItem(text, index );
01629      }
01630   }
01631 }
01632 
01633 void KSelectAction::setItems( const QStringList &lst )
01634 {
01635     kdDebug(129) << "KAction::setItems()" << endl; // remove -- ellis
01636   d->m_list = lst;
01637   d->m_current = -1;
01638 
01639   if ( d->m_menu )
01640   {
01641     d->m_menu->clear();
01642     QStringList::ConstIterator it = d->m_list.begin();
01643     int id = 0;
01644     for( ; it != d->m_list.end(); ++it )
01645       if (!((*it).isEmpty())) {
01646         d->m_menu->insertItem( d->makeMenuText( *it ), this, SLOT( slotActivated( int ) ), 0, id++ );
01647       } else {
01648         d->m_menu->insertSeparator();
01649       }
01650   }
01651 
01652   int len = containerCount();
01653   for( int i = 0; i < len; ++i )
01654     updateItems( i );
01655 
01656   // Disable if empty and not editable
01657   setEnabled ( lst.count() > 0 || d->m_edit );
01658 }
01659 
01660 QStringList KSelectAction::items() const
01661 {
01662   return d->m_list;
01663 }
01664 
01665 QString KSelectAction::currentText() const
01666 {
01667   if ( currentItem() < 0 )
01668     return QString::null;
01669 
01670   return d->m_list[ currentItem() ];
01671 }
01672 
01673 int KSelectAction::currentItem() const
01674 {
01675   return d->m_current;
01676 }
01677 
01678 void KSelectAction::updateCurrentItem( int id )
01679 {
01680   if ( d->m_current < 0 )
01681         return;
01682 
01683   QWidget* w = container( id );
01684   if ( w->inherits( "KToolBar" ) ) {
01685     QWidget* r = static_cast<KToolBar*>( w )->getWidget( itemId( id ) );
01686     if ( r->inherits( "QComboBox" ) ) {
01687       QComboBox *b = static_cast<QComboBox*>( r );
01688       b->setCurrentItem( d->m_current );
01689     }
01690   }
01691 }
01692 
01693 int KSelectAction::comboWidth() const
01694 {
01695   return d->m_comboWidth;
01696 }
01697 
01698 void KSelectAction::updateComboWidth( int id )
01699 {
01700   QWidget* w = container( id );
01701   if ( w->inherits( "KToolBar" ) ) {
01702     QWidget* r = static_cast<KToolBar*>( w )->getWidget( itemId( id ) );
01703     if ( r->inherits( "QComboBox" ) ) {
01704       QComboBox *cb = static_cast<QComboBox*>( r );
01705       cb->setMinimumWidth( d->m_comboWidth );
01706       cb->setMaximumWidth( d->m_comboWidth );
01707     }
01708   }
01709 }
01710 
01711 void KSelectAction::updateItems( int id )
01712 {
01713     kdDebug(129) << "KAction::updateItems( " << id << ", lst )" << endl; // remove -- ellis
01714   QWidget* w = container( id );
01715   if ( w->inherits( "KToolBar" ) ) {
01716     QWidget* r = static_cast<KToolBar*>( w )->getWidget( itemId( id ) );
01717     if ( r->inherits( "QComboBox" ) ) {
01718       QComboBox *cb = static_cast<QComboBox*>( r );
01719       cb->clear();
01720       QStringList lst = comboItems();
01721       QStringList::ConstIterator it = lst.begin();
01722       for( ; it != lst.end(); ++it )
01723         cb->insertItem( *it );
01724       // Ok, this currently doesn't work due to a bug in QComboBox
01725       // (the sizehint is cached for ever and never recalculated)
01726       // Bug reported (against Qt 2.3.1).
01727       cb->setMinimumWidth( cb->sizeHint().width() );
01728     }
01729    }
01730 }
01731 
01732 int KSelectAction::plug( QWidget *widget, int index )
01733 {
01734   if (kapp && !kapp->authorizeKAction(name()))
01735     return -1;
01736   kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
01737   if ( widget->inherits("QPopupMenu") )
01738   {
01739     // Create the PopupMenu and store it in m_menu
01740     (void)popupMenu();
01741 
01742     QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
01743     int id;
01744     if ( hasIconSet() )
01745       id = menu->insertItem( iconSet(), text(), d->m_menu, -1, index );
01746     else
01747       id = menu->insertItem( text(), d->m_menu, -1, index );
01748 
01749     if ( !isEnabled() )
01750         menu->setItemEnabled( id, false );
01751 
01752     QString wth = whatsThis();
01753     if ( !wth.isEmpty() )
01754         menu->setWhatsThis( id, wth );
01755 
01756     addContainer( menu, id );
01757     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
01758 
01759     return containerCount() - 1;
01760   }
01761   else if ( widget->inherits("KToolBar") )
01762   {
01763     KToolBar* bar = static_cast<KToolBar*>( widget );
01764     int id_ = KAction::getToolButtonID();
01765     bar->insertCombo( comboItems(), id_, isEditable(),
01766                       SIGNAL( activated( const QString & ) ), this,
01767                       SLOT( slotActivated( const QString & ) ), isEnabled(),
01768                       toolTip(), -1, index );
01769 
01770     QComboBox *cb = bar->getCombo( id_ );
01771     if ( cb )
01772     {
01773       if (!isEditable()) cb->setFocusPolicy(QWidget::NoFocus);
01774       cb->setMinimumWidth( cb->sizeHint().width() );
01775       if ( d->m_comboWidth > 0 )
01776       {
01777         cb->setMinimumWidth( d->m_comboWidth );
01778         cb->setMaximumWidth( d->m_comboWidth );
01779       }
01780       cb->setInsertionPolicy( QComboBox::NoInsertion );
01781     }
01782 
01783     addContainer( bar, id_ );
01784 
01785     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
01786 
01787     updateCurrentItem( containerCount() - 1 );
01788 
01789     return containerCount() - 1;
01790   }
01791 
01792   kdWarning() << "Can not plug KAction in " << widget->className() << endl;
01793   return -1;
01794 }
01795 
01796 QStringList KSelectAction::comboItems() const
01797 {
01798   if( d->m_menuAccelsEnabled ) {
01799     QStringList lst;
01800     QStringList::ConstIterator it = d->m_list.begin();
01801     for( ; it != d->m_list.end(); ++it )
01802     {
01803       QString item = *it;
01804       int i = item.find( '&' );
01805       if ( i > -1 )
01806         item = item.remove( i, 1 );
01807       lst.append( item );
01808     }
01809     return lst;
01810   }
01811   else
01812     return d->m_list;
01813 }
01814 
01815 void KSelectAction::clear()
01816 {
01817   if ( d->m_menu )
01818     d->m_menu->clear();
01819 
01820   int len = containerCount();
01821   for( int i = 0; i < len; ++i )
01822     updateClear( i );
01823 }
01824 
01825 void KSelectAction::updateClear( int id )
01826 {
01827   QWidget* w = container( id );
01828   if ( w->inherits( "KToolBar" ) ) {
01829     QWidget* r = static_cast<KToolBar*>( w )->getWidget( itemId( id ) );
01830     if ( r->inherits( "QComboBox" ) ) {
01831       QComboBox *b = static_cast<QComboBox*>( r );
01832       b->clear();
01833     }
01834   }
01835 }
01836 
01837 void KSelectAction::slotActivated( int id )
01838 {
01839   if ( d->m_current == id )
01840     return;
01841 
01842   setCurrentItem( id );
01843   // Delay this. Especially useful when the slot connected to activated() will re-create
01844   // the menu, e.g. in the recent files action. This prevents a crash.
01845   QTimer::singleShot( 0, this, SLOT( slotActivated() ) );
01846 }
01847 
01848 void KSelectAction::slotActivated( const QString &text )
01849 {
01850   if ( isEditable() )
01851   {
01852     QStringList lst = items();
01853     if(lst.contains(text)==0)
01854     {
01855       lst.append( text );
01856       setItems( lst );
01857     }
01858   }
01859 
01860   int i = items().findIndex( text );
01861   if ( i > -1 )
01862       setCurrentItem( i );
01863   else
01864       setCurrentItem( comboItems().findIndex( text ) );
01865   // Delay this. Especially useful when the slot connected to activated() will re-create
01866   // the menu, e.g. in the recent files action. This prevents a crash.
01867   QTimer::singleShot( 0, this, SLOT( slotActivated() ) );
01868 }
01869 
01870 void KSelectAction::slotActivated()
01871 {
01872   KAction::slotActivated();
01873   kdDebug(129) << "KSelectAction::slotActivated currentItem=" << currentItem() << " currentText=" << currentText() << endl;
01874   emit activated( currentItem() );
01875   emit activated( currentText() );
01876 }
01877 
01878 void KSelectAction::setEditable( bool edit )
01879 {
01880   d->m_edit = edit;
01881 }
01882 
01883 bool KSelectAction::isEditable() const
01884 {
01885   return d->m_edit;
01886 }
01887 
01888 void KSelectAction::setRemoveAmpersandsInCombo( bool b )
01889 {
01890   setMenuAccelsEnabled( b );
01891 }
01892 
01893 bool KSelectAction::removeAmpersandsInCombo() const
01894 {
01895   return menuAccelsEnabled( );
01896 }
01897 
01898 void KSelectAction::setMenuAccelsEnabled( bool b )
01899 {
01900   d->m_menuAccelsEnabled = b;
01901 }
01902 
01903 bool KSelectAction::menuAccelsEnabled() const
01904 {
01905   return d->m_menuAccelsEnabled;
01906 }
01907 
01908 class KListAction::KListActionPrivate
01909 {
01910 public:
01911   KListActionPrivate()
01912   {
01913     m_current = 0;
01914   }
01915   int m_current;
01916 };
01917 
01918 KListAction::KListAction( const QString& text, const KShortcut& cut,
01919                           QObject* parent, const char* name )
01920   : KSelectAction( text, cut, parent, name )
01921 {
01922   d = new KListActionPrivate;
01923 }
01924 
01925 KListAction::KListAction( const QString& text, const KShortcut& cut,
01926                           const QObject* receiver, const char* slot,
01927                           QObject* parent, const char* name )
01928   : KSelectAction( text, cut, parent, name )
01929 {
01930   d = new KListActionPrivate;
01931   if ( receiver )
01932     connect( this, SIGNAL( activated( int ) ), receiver, slot );
01933 }
01934 
01935 KListAction::KListAction( const QString& text, const QIconSet& pix,
01936                           const KShortcut& cut,
01937                           QObject* parent, const char* name )
01938   : KSelectAction( text, pix, cut, parent, name )
01939 {
01940   d = new KListActionPrivate;
01941 }
01942 
01943 KListAction::KListAction( const QString& text, const QString& pix,
01944                           const KShortcut& cut,
01945                           QObject* parent, const char* name )
01946   : KSelectAction( text, pix, cut, parent, name )
01947 {
01948   d = new KListActionPrivate;
01949 }
01950 
01951 KListAction::KListAction( const QString& text, const QIconSet& pix,
01952                           const KShortcut& cut, const QObject* receiver,
01953                           const char* slot, QObject* parent,
01954                           const char* name )
01955   : KSelectAction( text, pix, cut, parent, name )
01956 {
01957   d = new KListActionPrivate;
01958   if ( receiver )
01959     connect( this, SIGNAL( activated( int ) ), receiver, slot );
01960 }
01961 
01962 KListAction::KListAction( const QString& text, const QString& pix,
01963                           const KShortcut& cut, const QObject* receiver,
01964                           const char* slot, QObject* parent,
01965                           const char* name )
01966   : KSelectAction( text, pix, cut, parent, name )
01967 {
01968   d = new KListActionPrivate;
01969   if ( receiver )
01970     connect( this, SIGNAL( activated( int ) ), receiver, slot );
01971 }
01972 
01973 KListAction::KListAction( QObject* parent, const char* name )
01974   : KSelectAction( parent, name )
01975 {
01976   d = new KListActionPrivate;
01977 }
01978 
01979 KListAction::~KListAction()
01980 {
01981   delete d; d = 0;
01982 }
01983 
01984 void KListAction::setCurrentItem( int index )
01985 {
01986   KSelectAction::setCurrentItem( index );
01987   d->m_current = index;
01988 
01989   //  emit KAction::activated();
01990   //  emit activated( currentItem() );
01991   // emit activated( currentText() );
01992 }
01993 
01994 QString KListAction::currentText() const
01995 {
01996   if ( currentItem() < 0 )
01997       return QString::null;
01998 
01999   return items()[ currentItem() ];
02000 }
02001 
02002 int KListAction::currentItem() const
02003 {
02004   return d->m_current;
02005 }
02006 
02007 class KRecentFilesAction::KRecentFilesActionPrivate
02008 {
02009 public:
02010   KRecentFilesActionPrivate()
02011   {
02012     m_maxItems = 0;
02013   }
02014   uint m_maxItems;
02015 };
02016 
02017 KRecentFilesAction::KRecentFilesAction( const QString& text,
02018                                         const KShortcut& cut,
02019                                         QObject* parent, const char* name,
02020                                         uint maxItems )
02021   : KListAction( text, cut, parent, name)
02022 {
02023   d = new KRecentFilesActionPrivate;
02024   d->m_maxItems = maxItems;
02025 
02026   init();
02027 }
02028 
02029 KRecentFilesAction::KRecentFilesAction( const QString& text,
02030                                         const KShortcut& cut,
02031                                         const QObject* receiver,
02032                                         const char* slot,
02033                                         QObject* parent, const char* name,
02034                                         uint maxItems )
02035   : KListAction( text, cut, parent, name)
02036 {
02037   d = new KRecentFilesActionPrivate;
02038   d->m_maxItems = maxItems;
02039 
02040   init();
02041 
02042   if ( receiver )
02043     connect( this,     SIGNAL(urlSelected(const KURL&)),
02044              receiver, slot );
02045 }
02046 
02047 KRecentFilesAction::KRecentFilesAction( const QString& text,
02048                                         const QIconSet& pix,
02049                                         const KShortcut& cut,
02050                                         QObject* parent, const char* name,
02051                                         uint maxItems )
02052   : KListAction( text, pix, cut, parent, name)
02053 {
02054   d = new KRecentFilesActionPrivate;
02055   d->m_maxItems = maxItems;
02056 
02057   init();
02058 }
02059 
02060 KRecentFilesAction::KRecentFilesAction( const QString& text,
02061                                         const QString& pix,
02062                                         const KShortcut& cut,
02063                                         QObject* parent, const char* name,
02064                                         uint maxItems )
02065   : KListAction( text, pix, cut, parent, name)
02066 {
02067   d = new KRecentFilesActionPrivate;
02068   d->m_maxItems = maxItems;
02069 
02070   init();
02071 }
02072 
02073 KRecentFilesAction::KRecentFilesAction( const QString& text,
02074                                         const QIconSet& pix,
02075                                         const KShortcut& cut,
02076                                         const QObject* receiver,
02077                                         const char* slot,
02078                                         QObject* parent, const char* name,
02079                                         uint maxItems )
02080   : KListAction( text, pix, cut, parent, name)
02081 {
02082   d = new KRecentFilesActionPrivate;
02083   d->m_maxItems = maxItems;
02084 
02085   init();
02086 
02087   if ( receiver )
02088     connect( this,     SIGNAL(urlSelected(const KURL&)),
02089              receiver, slot );
02090 }
02091 
02092 KRecentFilesAction::KRecentFilesAction( const QString& text,
02093                                         const QString& pix,
02094                                         const KShortcut& cut,
02095                                         const QObject* receiver,
02096                                         const char* slot,
02097                                         QObject* parent, const char* name,
02098                                         uint maxItems )
02099   : KListAction( text, pix, cut, parent, name)
02100 {
02101   d = new KRecentFilesActionPrivate;
02102   d->m_maxItems = maxItems;
02103 
02104   init();
02105 
02106   if ( receiver )
02107     connect( this,     SIGNAL(urlSelected(const KURL&)),
02108              receiver, slot );
02109 }
02110 
02111 KRecentFilesAction::KRecentFilesAction( QObject* parent, const char* name,
02112                                         uint maxItems )
02113   : KListAction( parent, name )
02114 {
02115   d = new KRecentFilesActionPrivate;
02116   d->m_maxItems = maxItems;
02117 
02118   init();
02119 }
02120 
02121 void KRecentFilesAction::init()
02122 {
02123   connect( this, SIGNAL( activated( const QString& ) ),
02124            this, SLOT( itemSelected( const QString& ) ) );
02125 
02126   setMenuAccelsEnabled( false );
02127 }
02128 
02129 KRecentFilesAction::~KRecentFilesAction()
02130 {
02131   delete d; d = 0;
02132 }
02133 
02134 uint KRecentFilesAction::maxItems() const
02135 {
02136     return d->m_maxItems;
02137 }
02138 
02139 void KRecentFilesAction::setMaxItems( uint maxItems )
02140 {
02141     QStringList lst = items();
02142     uint oldCount   = lst.count();
02143 
02144     // set new maxItems
02145     d->m_maxItems = maxItems;
02146 
02147     // remove all items that are too much
02148     while( lst.count() > maxItems )
02149     {
02150         // remove last item
02151         lst.remove( lst.last() );
02152     }
02153 
02154     // set new list if changed
02155     if( lst.count() != oldCount )
02156         setItems( lst );
02157 }
02158 
02159 void KRecentFilesAction::addURL( const KURL& url )
02160 {
02161     QString     file = url.prettyURL();
02162     QStringList lst = items();
02163 
02164     // remove file if already in list
02165     lst.remove( file );
02166 
02167     // remove las item if already maxitems in list
02168     if( lst.count() == d->m_maxItems )
02169     {
02170         // remove last item
02171         lst.remove( lst.last() );
02172     }
02173 
02174     // add file to list
02175     lst.prepend( file );
02176     setItems( lst );
02177 }
02178 
02179 void KRecentFilesAction::removeURL( const KURL& url )
02180 {
02181     QStringList lst = items();
02182     QString     file = url.prettyURL();
02183 
02184     // remove url
02185     if( lst.count() > 0 )
02186     {
02187         lst.remove( file );
02188         setItems( lst );
02189     }
02190 }
02191 
02192 void KRecentFilesAction::clearURLList()
02193 {
02194     clear();
02195 }
02196 
02197 void KRecentFilesAction::loadEntries( KConfig* config, QString groupname)
02198 {
02199     QString     key;
02200     QString     value;
02201     QString     oldGroup;
02202     QStringList lst;
02203 
02204     oldGroup = config->group();
02205 
02206     if (groupname.isEmpty())
02207       groupname = "RecentFiles";
02208     config->setGroup( groupname );
02209 
02210     // read file list
02211     for( unsigned int i = 1 ; i <= d->m_maxItems ; i++ )
02212     {
02213         key = QString( "File%1" ).arg( i );
02214         value = config->readEntry( key, QString::null );
02215 
02216         if (!value.isNull())
02217             lst.append( value );
02218     }
02219 
02220     // set file
02221     setItems( lst );
02222 
02223     config->setGroup( oldGroup );
02224 }
02225 
02226 void KRecentFilesAction::saveEntries( KConfig* config, QString groupname )
02227 {
02228     QString     key;
02229     QString     value;
02230     QString     oldGroup;
02231     QStringList lst = items();
02232 
02233     oldGroup = config->group();
02234 
02235     if (groupname.isEmpty())
02236       groupname = "RecentFiles";
02237     config->deleteGroup( groupname, true );
02238     config->setGroup( groupname );
02239 
02240     // write file list
02241     for( unsigned int i = 1 ; i <= lst.count() ; i++ )
02242     {
02243         key = QString( "File%1" ).arg( i );
02244         value = lst[ i - 1 ];
02245         config->writeEntry( key, value );
02246     }
02247 
02248     config->setGroup( oldGroup );
02249 }
02250 
02251 void KRecentFilesAction::itemSelected( const QString& text )
02252 {
02253     emit urlSelected( KURL( text ) );
02254 }
02255 
02256 class KFontAction::KFontActionPrivate
02257 {
02258 public:
02259   KFontActionPrivate()
02260   {
02261   }
02262   QStringList m_fonts;
02263 };
02264 
02265 KFontAction::KFontAction( const QString& text,
02266                           const KShortcut& cut, QObject* parent,
02267                           const char* name )
02268   : KSelectAction( text, cut, parent, name )
02269 {
02270     d = new KFontActionPrivate;
02271     get_fonts( d->m_fonts );
02272     KSelectAction::setItems( d->m_fonts );
02273     setEditable( true );
02274 }
02275 
02276 KFontAction::KFontAction( const QString& text, const KShortcut& cut,
02277                           const QObject* receiver, const char* slot,
02278                           QObject* parent, const char* name )
02279     : KSelectAction( text, cut, receiver, slot, parent, name )
02280 {
02281     d = new KFontActionPrivate;
02282     get_fonts( d->m_fonts );
02283     KSelectAction::setItems( d->m_fonts );
02284     setEditable( true );
02285 }
02286 
02287 KFontAction::KFontAction( const QString& text, const QIconSet& pix,
02288                           const KShortcut& cut,
02289                           QObject* parent, const char* name )
02290     : KSelectAction( text, pix, cut, parent, name )
02291 {
02292     d = new KFontActionPrivate;
02293     get_fonts( d->m_fonts );
02294     KSelectAction::setItems( d->m_fonts );
02295     setEditable( true );
02296 }
02297 
02298 KFontAction::KFontAction( const QString& text, const QString& pix,
02299                           const KShortcut& cut,
02300                           QObject* parent, const char* name )
02301     : KSelectAction( text, pix, cut, parent, name )
02302 {
02303     d = new KFontActionPrivate;
02304     get_fonts( d->m_fonts );
02305     KSelectAction::setItems( d->m_fonts );
02306     setEditable( true );
02307 }
02308 
02309 KFontAction::KFontAction( const QString& text, const QIconSet& pix,
02310                           const KShortcut& cut,
02311                           const QObject* receiver, const char* slot,
02312                           QObject* parent, const char* name )
02313     : KSelectAction( text, pix, cut, receiver, slot, parent, name )
02314 {
02315     d = new KFontActionPrivate;
02316     get_fonts( d->m_fonts );
02317     KSelectAction::setItems( d->m_fonts );
02318     setEditable( true );
02319 }
02320 
02321 KFontAction::KFontAction( const QString& text, const QString& pix,
02322                           const KShortcut& cut,
02323                           const QObject* receiver, const char* slot,
02324                           QObject* parent, const char* name )
02325     : KSelectAction( text, pix, cut, receiver, slot, parent, name )
02326 {
02327     d = new KFontActionPrivate;
02328     get_fonts( d->m_fonts );
02329     KSelectAction::setItems( d->m_fonts );
02330     setEditable( true );
02331 }
02332 
02333 
02334 KFontAction::KFontAction( QObject* parent, const char* name )
02335   : KSelectAction( parent, name )
02336 {
02337     d = new KFontActionPrivate;
02338     get_fonts( d->m_fonts );
02339     KSelectAction::setItems( d->m_fonts );
02340     setEditable( true );
02341 }
02342 
02343 KFontAction::~KFontAction()
02344 {
02345     delete d;
02346     d = 0;
02347 }
02348 
02349 /*
02350  * Maintenance note: Keep in sync with KFontCombo::setCurrentFont()
02351  */
02352 void KFontAction::setFont( const QString &family )
02353 {
02354     QString lowerName = family.lower();
02355     int i = 0;
02356     for ( QStringList::Iterator it = d->m_fonts.begin(); it != d->m_fonts.end(); ++it, ++i )
02357     {
02358        if ((*it).lower() == lowerName)
02359        {
02360           setCurrentItem(i);
02361           return;
02362        }
02363     }
02364     i = lowerName.find(" [");
02365     if (i>-1)
02366     {
02367        lowerName = lowerName.left(i);
02368        i = 0;
02369        for ( QStringList::Iterator it = d->m_fonts.begin(); it != d->m_fonts.end(); ++it, ++i )
02370        {
02371           if ((*it).lower() == lowerName)
02372           {
02373              setCurrentItem(i);
02374              return;
02375           }
02376        }
02377     }
02378 
02379     lowerName += " [";
02380     i = 0;
02381     for ( QStringList::Iterator it = d->m_fonts.begin(); it != d->m_fonts.end(); ++it, ++i )
02382     {
02383        if ((*it).lower().startsWith(lowerName))
02384        {
02385           setCurrentItem(i);
02386           return;
02387        }
02388     }
02389     kdDebug(129) << "Font not found " << family.lower() << endl;
02390 }
02391 
02392 int KFontAction::plug( QWidget *w, int index )
02393 {
02394   if (kapp && !kapp->authorizeKAction(name()))
02395     return -1;
02396   if ( w->inherits("KToolBar") )
02397   {
02398     KToolBar* bar = static_cast<KToolBar*>( w );
02399     int id_ = KAction::getToolButtonID();
02400     KFontCombo *cb = new KFontCombo( items(), bar );
02401     connect( cb, SIGNAL( activated( const QString & ) ),
02402              SLOT( slotActivated( const QString & ) ) );
02403     cb->setEnabled( isEnabled() );
02404     bar->insertWidget( id_, comboWidth(), cb, index );
02405     cb->setMinimumWidth( cb->sizeHint().width() );
02406 
02407     addContainer( bar, id_ );
02408 
02409     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02410 
02411     updateCurrentItem( containerCount() - 1 );
02412 
02413     return containerCount() - 1;
02414   }
02415   else return KSelectAction::plug( w, index );
02416 }
02417 
02418 class KFontSizeAction::KFontSizeActionPrivate
02419 {
02420 public:
02421   KFontSizeActionPrivate()
02422   {
02423   }
02424 };
02425 
02426 KFontSizeAction::KFontSizeAction( const QString& text,
02427                                   const KShortcut& cut,
02428                                   QObject* parent, const char* name )
02429   : KSelectAction( text, cut, parent, name )
02430 {
02431   init();
02432 }
02433 
02434 KFontSizeAction::KFontSizeAction( const QString& text,
02435                                   const KShortcut& cut,
02436                                   const QObject* receiver, const char* slot,
02437                                   QObject* parent, const char* name )
02438   : KSelectAction( text, cut, receiver, slot, parent, name )
02439 {
02440   init();
02441 }
02442 
02443 KFontSizeAction::KFontSizeAction( const QString& text, const QIconSet& pix,
02444                                   const KShortcut& cut,
02445                                   QObject* parent, const char* name )
02446   : KSelectAction( text, pix, cut, parent, name )
02447 {
02448   init();
02449 }
02450 
02451 KFontSizeAction::KFontSizeAction( const QString& text, const QString& pix,
02452                                   const KShortcut& cut,
02453                                   QObject* parent, const char* name )
02454   : KSelectAction( text, pix, cut, parent, name )
02455 {
02456   init();
02457 }
02458 
02459 KFontSizeAction::KFontSizeAction( const QString& text, const QIconSet& pix,
02460                                   const KShortcut& cut,
02461                                   const QObject* receiver,
02462                                   const char* slot, QObject* parent,
02463                                   const char* name )
02464     : KSelectAction( text, pix, cut, receiver, slot, parent, name )
02465 {
02466   init();
02467 }
02468 
02469 KFontSizeAction::KFontSizeAction( const QString& text, const QString& pix,
02470                                   const KShortcut& cut,
02471                                   const QObject* receiver,
02472                                   const char* slot, QObject* parent,
02473                                   const char* name )
02474   : KSelectAction( text, pix, cut, receiver, slot, parent, name )
02475 {
02476   init();
02477 }
02478 
02479 KFontSizeAction::KFontSizeAction( QObject* parent, const char* name )
02480   : KSelectAction( parent, name )
02481 {
02482   init();
02483 }
02484 
02485 KFontSizeAction::~KFontSizeAction()
02486 {
02487     delete d;
02488     d = 0;
02489 }
02490 
02491 void KFontSizeAction::init()
02492 {
02493     d = new KFontSizeActionPrivate;
02494 
02495     setEditable( true );
02496     QValueList<int> sizes = get_standard_font_sizes();
02497     QStringList lst;
02498     for ( QValueList<int>::Iterator it = sizes.begin(); it != sizes.end(); ++it )
02499         lst.append( QString::number( *it ) );
02500 
02501     setItems( lst );
02502 }
02503 
02504 void KFontSizeAction::setFontSize( int size )
02505 {
02506     if ( size == fontSize() ) {
02507         setCurrentItem( items().findIndex( QString::number( size ) ) );
02508         return;
02509     }
02510 
02511     if ( size < 1 ) {
02512         kdWarning() << "KFontSizeAction: Size " << size << " is out of range" << endl;
02513         return;
02514     }
02515 
02516     int index = items().findIndex( QString::number( size ) );
02517     if ( index == -1 ) {
02518         // Insert at the correct position in the list (to keep sorting)
02519         QValueList<int> lst;
02520         // Convert to list of ints
02521         QStringList itemsList = items();
02522         for (QStringList::Iterator it = itemsList.begin() ; it != itemsList.end() ; ++it)
02523             lst.append( (*it).toInt() );
02524         // New size
02525         lst.append( size );
02526         // Sort the list
02527         qHeapSort( lst );
02528         // Convert back to string list
02529         QStringList strLst;
02530         for (QValueList<int>::Iterator it = lst.begin() ; it != lst.end() ; ++it)
02531             strLst.append( QString::number(*it) );
02532         KSelectAction::setItems( strLst );
02533         // Find new current item
02534         index = lst.findIndex( size );
02535         setCurrentItem( index );
02536     }
02537     else
02538         setCurrentItem( index );
02539 
02540 
02541     //emit KAction::activated();
02542     //emit activated( index );
02543     //emit activated( QString::number( size ) );
02544     //emit fontSizeChanged( size );
02545 }
02546 
02547 int KFontSizeAction::fontSize() const
02548 {
02549   return currentText().toInt();
02550 }
02551 
02552 void KFontSizeAction::slotActivated( int index )
02553 {
02554   KSelectAction::slotActivated( index );
02555 
02556   emit fontSizeChanged( items()[ index ].toInt() );
02557 }
02558 
02559 void KFontSizeAction::slotActivated( const QString& size )
02560 {
02561   setFontSize( size.toInt() ); // insert sorted first
02562   KSelectAction::slotActivated( size );
02563   emit fontSizeChanged( size.toInt() );
02564 }
02565 
02566 class KActionMenu::KActionMenuPrivate
02567 {
02568 public:
02569   KActionMenuPrivate()
02570   {
02571     m_popup = new KPopupMenu(0L,"KActionMenu::KActionMenuPrivate");
02572     m_delayed = true;
02573     m_stickyMenu = true;
02574   }
02575   ~KActionMenuPrivate()
02576   {
02577     delete m_popup; m_popup = 0;
02578   }
02579   KPopupMenu *m_popup;
02580   bool m_delayed;
02581   bool m_stickyMenu;
02582 };
02583 
02584 KActionMenu::KActionMenu( QObject* parent, const char* name )
02585   : KAction( parent, name )
02586 {
02587   d = new KActionMenuPrivate;
02588   setShortcutConfigurable( false );
02589 }
02590 
02591 KActionMenu::KActionMenu( const QString& text, QObject* parent,
02592                           const char* name )
02593   : KAction( text, 0, parent, name )
02594 {
02595   d = new KActionMenuPrivate;
02596   setShortcutConfigurable( false );
02597 }
02598 
02599 KActionMenu::KActionMenu( const QString& text, const QIconSet& icon,
02600                           QObject* parent, const char* name )
02601   : KAction( text, icon, 0, parent, name )
02602 {
02603   d = new KActionMenuPrivate;
02604   setShortcutConfigurable( false );
02605 }
02606 
02607 KActionMenu::KActionMenu( const QString& text, const QString& icon,
02608                           QObject* parent, const char* name )
02609   : KAction( text, icon, 0, parent, name )
02610 {
02611   d = new KActionMenuPrivate;
02612   setShortcutConfigurable( false );
02613 }
02614 
02615 KActionMenu::~KActionMenu()
02616 {
02617     unplugAll();
02618     kdDebug(129) << "KActionMenu::~KActionMenu()" << endl; // ellis
02619     delete d; d = 0;
02620 }
02621 
02622 void KActionMenu::popup( const QPoint& global )
02623 {
02624   popupMenu()->popup( global );
02625 }
02626 
02627 KPopupMenu* KActionMenu::popupMenu() const
02628 {
02629   return d->m_popup;
02630 }
02631 
02632 void KActionMenu::insert( KAction* cmd, int index )
02633 {
02634   if ( cmd )
02635     cmd->plug( d->m_popup, index );
02636 }
02637 
02638 void KActionMenu::remove( KAction* cmd )
02639 {
02640   if ( cmd )
02641     cmd->unplug( d->m_popup );
02642 }
02643 
02644 bool KActionMenu::delayed() const {
02645     return d->m_delayed;
02646 }
02647 
02648 void KActionMenu::setDelayed(bool _delayed) {
02649     d->m_delayed = _delayed;
02650 }
02651 
02652 bool KActionMenu::stickyMenu() const {
02653     return d->m_stickyMenu;
02654 }
02655 
02656 void KActionMenu::setStickyMenu(bool sticky) {
02657     d->m_stickyMenu = sticky;
02658 }
02659 
02660 int KActionMenu::plug( QWidget* widget, int index )
02661 {
02662   if (kapp && !kapp->authorizeKAction(name()))
02663     return -1;
02664   kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
02665   if ( widget->inherits("QPopupMenu") )
02666   {
02667     QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
02668     int id;
02669     if ( hasIconSet() )
02670       id = menu->insertItem( iconSet(), text(), d->m_popup, -1, index );
02671     else
02672       id = menu->insertItem( text(), d->m_popup, -1, index );
02673 
02674     if ( !isEnabled() )
02675       menu->setItemEnabled( id, false );
02676 
02677     addContainer( menu, id );
02678     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02679 
02680     return containerCount() - 1;
02681   }
02682   else if ( widget->inherits( "KToolBar" ) )
02683   {
02684     KToolBar *bar = static_cast<KToolBar *>( widget );
02685 
02686     int id_ = KAction::getToolButtonID();
02687 
02688     if ( icon().isEmpty() && !iconSet().isNull() )
02689       bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
02690                          SLOT( slotActivated() ), isEnabled(), plainText(),
02691                          index );
02692     else
02693     {
02694       KInstance *instance;
02695 
02696       if ( m_parentCollection )
02697         instance = m_parentCollection->instance();
02698       else
02699         instance = KGlobal::instance();
02700 
02701       bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
02702                          SLOT( slotActivated() ), isEnabled(), plainText(),
02703                          index, instance );
02704     }
02705 
02706     addContainer( bar, id_ );
02707 
02708     if (!whatsThis().isEmpty())
02709       QWhatsThis::add( bar->getButton(id_), whatsThis() );
02710 
02711     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02712     if (delayed()) {
02713         bar->setDelayedPopup( id_, popupMenu(), stickyMenu() );
02714     } else {
02715         bar->getButton(id_)->setPopup(popupMenu(), stickyMenu() );
02716     }
02717 
02718     return containerCount() - 1;
02719   }
02720   else if ( widget->inherits( "QMenuBar" ) )
02721   {
02722     QMenuBar *bar = static_cast<QMenuBar *>( widget );
02723 
02724     int id;
02725 
02726     id = bar->insertItem( text(), popupMenu(), -1, index );
02727 
02728     if ( !isEnabled() )
02729         bar->setItemEnabled( id, false );
02730 
02731     addContainer( bar, id );
02732     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02733 
02734     return containerCount() - 1;
02735   }
02736 
02737   return -1;
02738 }
02739 
02741 
02742 KToolBarPopupAction::KToolBarPopupAction( const QString& text,
02743                                           const QString& icon,
02744                                           const KShortcut& cut,
02745                                           QObject* parent, const char* name )
02746   : KAction( text, icon, cut, parent, name )
02747 {
02748   m_popup = 0;
02749   m_delayed = true;
02750   m_stickyMenu = true;
02751 }
02752 
02753 KToolBarPopupAction::KToolBarPopupAction( const QString& text,
02754                                           const QString& icon,
02755                                           const KShortcut& cut,
02756                                           const QObject* receiver,
02757                                           const char* slot, QObject* parent,
02758                                           const char* name )
02759   : KAction( text, icon, cut, receiver, slot, parent, name )
02760 {
02761   m_popup = 0;
02762   m_delayed = true;
02763   m_stickyMenu = true;
02764 }
02765 
02766 KToolBarPopupAction::KToolBarPopupAction( const KGuiItem& item,
02767                                           const KShortcut& cut,
02768                                           const QObject* receiver,
02769                                           const char* slot, KActionCollection* parent,
02770                                           const char* name )
02771   : KAction( item, cut, receiver, slot, parent, name )
02772 {
02773   m_popup = 0;
02774   m_delayed = true;
02775   m_stickyMenu = true;
02776 }
02777 
02778 KToolBarPopupAction::~KToolBarPopupAction()
02779 {
02780   if ( m_popup )
02781     delete m_popup;
02782 }
02783 
02784 bool KToolBarPopupAction::delayed() const {
02785     return m_delayed;
02786 }
02787 
02788 void KToolBarPopupAction::setDelayed(bool delayed) {
02789     m_delayed = delayed;
02790 }
02791 
02792 bool KToolBarPopupAction::stickyMenu() const {
02793     return m_stickyMenu;
02794 }
02795 
02796 void KToolBarPopupAction::setStickyMenu(bool sticky) {
02797     m_stickyMenu = sticky;
02798 }
02799 
02800 int KToolBarPopupAction::plug( QWidget *widget, int index )
02801 {
02802   if (kapp && !kapp->authorizeKAction(name()))
02803     return -1;
02804   // This is very related to KActionMenu::plug.
02805   // In fact this class could be an interesting base class for KActionMenu
02806   if ( widget->inherits( "KToolBar" ) )
02807   {
02808     KToolBar *bar = (KToolBar *)widget;
02809 
02810     int id_ = KAction::getToolButtonID();
02811 
02812     KInstance * instance;
02813     if ( m_parentCollection )
02814         instance = m_parentCollection->instance();
02815     else
02816         instance = KGlobal::instance();
02817 
02818     bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
02819                        SLOT( slotActivated() ), isEnabled(), plainText(),
02820                        index, instance );
02821 
02822     addContainer( bar, id_ );
02823 
02824     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02825 
02826     if (delayed()) {
02827         bar->setDelayedPopup( id_, popupMenu(), stickyMenu() );
02828     } else {
02829         bar->getButton(id_)->setPopup(popupMenu(), stickyMenu());
02830     }
02831 
02832     if ( !whatsThis().isEmpty() )
02833         QWhatsThis::add( bar->getButton( id_ ), whatsThisWithIcon() );
02834 
02835     return containerCount() - 1;
02836   }
02837 
02838   return KAction::plug( widget, index );
02839 }
02840 
02841 KPopupMenu *KToolBarPopupAction::popupMenu() const
02842 {
02843     if ( !m_popup ) {
02844         KToolBarPopupAction *that = const_cast<KToolBarPopupAction*>(this);
02845         that->m_popup = new KPopupMenu;
02846     }
02847     return m_popup;
02848 }
02849 
02851 
02852 KToggleToolBarAction::KToggleToolBarAction( const char* toolBarName,
02853          const QString& text, KActionCollection* parent, const char* name )
02854   : KToggleAction( text, KShortcut(), parent, name )
02855   , m_toolBarName( toolBarName )
02856   , m_toolBar( 0L )
02857 {
02858 }
02859 
02860 KToggleToolBarAction::KToggleToolBarAction( KToolBar *toolBar, const QString &text,
02861                                             KActionCollection *parent, const char *name )
02862   : KToggleAction( text, KShortcut(), parent, name )
02863   , m_toolBarName( 0 ), m_toolBar( toolBar )
02864 {
02865 }
02866 
02867 KToggleToolBarAction::~KToggleToolBarAction()
02868 {
02869 }
02870 
02871 int KToggleToolBarAction::plug( QWidget* w, int index )
02872 {
02873   if (kapp && !kapp->authorizeKAction(name()))
02874       return -1;
02875 
02876   if ( !m_toolBar ) {
02877     // Note: topLevelWidget() stops too early, we can't use it.
02878     QWidget * tl = w;
02879     QWidget * n;
02880     while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
02881       tl = n;
02882 
02883     KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
02884 
02885     if ( mw )
02886         m_toolBar = mw->toolBar( m_toolBarName );
02887   }
02888 
02889   if( m_toolBar ) {
02890     setChecked( m_toolBar->isVisible() );
02891     connect( m_toolBar, SIGNAL(visibilityChanged(bool)), this, SLOT(setChecked(bool)) );
02892     // Also emit toggled when the toolbar's visibility changes (see comment in header)
02893     connect( m_toolBar, SIGNAL(visibilityChanged(bool)), this, SIGNAL(toggled(bool)) );
02894   } else {
02895     setEnabled( false );
02896   }
02897 
02898   return KToggleAction::plug( w, index );
02899 }
02900 
02901 void KToggleToolBarAction::setChecked( bool c )
02902 {
02903   if( m_toolBar && c != m_toolBar->isVisible() ) {
02904     if( c ) {
02905       m_toolBar->show();
02906     } else {
02907       m_toolBar->hide();
02908     }
02909     QMainWindow* mw = m_toolBar->mainWindow();
02910     if ( mw && mw->inherits( "KMainWindow" ) )
02911       static_cast<KMainWindow *>( mw )->setSettingsDirty();
02912   }
02913   KToggleAction::setChecked( c );
02914 }
02915 
02917 
02918 KWidgetAction::KWidgetAction( QWidget* widget,
02919     const QString& text, const KShortcut& cut,
02920     const QObject* receiver, const char* slot,
02921     KActionCollection* parent, const char* name )
02922   : KAction( text, cut, receiver, slot, parent, name )
02923   , m_widget( widget )
02924   , m_autoSized( false )
02925 {
02926 }
02927 
02928 KWidgetAction::~KWidgetAction()
02929 {
02930 }
02931 
02932 void KWidgetAction::setAutoSized( bool autoSized )
02933 {
02934   if( m_autoSized == autoSized )
02935     return;
02936 
02937   m_autoSized = autoSized;
02938 
02939   if( !m_widget || !isPlugged() )
02940     return;
02941 
02942   KToolBar* toolBar = (KToolBar*)m_widget->parent();
02943   int i = findContainer( toolBar );
02944   if ( i == -1 )
02945     return;
02946   int id = itemId( i );
02947 
02948   toolBar->setItemAutoSized( id, m_autoSized );
02949 }
02950 
02951 int KWidgetAction::plug( QWidget* w, int index )
02952 {
02953   if (kapp && !kapp->authorizeKAction(name()))
02954       return -1;
02955 
02956   if ( !w->inherits( "KToolBar" ) ) {
02957     kdError() << "KWidgetAction::plug: KWidgetAction must be plugged into KToolBar." << endl;
02958     return -1;
02959   }
02960   if ( !m_widget ) {
02961     kdError() << "KWidgetAction::plug: Widget was deleted or null!" << endl;
02962     return -1;
02963   }
02964 
02965   KToolBar* toolBar = static_cast<KToolBar*>( w );
02966 
02967   int id = KAction::getToolButtonID();
02968 
02969   m_widget->reparent( toolBar, QPoint() );
02970   toolBar->insertWidget( id, 0, m_widget, index );
02971   toolBar->setItemAutoSized( id, m_autoSized );
02972 
02973   addContainer( toolBar, id );
02974 
02975   connect( toolBar, SIGNAL( toolbarDestroyed() ), this, SLOT( slotToolbarDestroyed() ) );
02976   connect( toolBar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
02977 
02978   return containerCount() - 1;
02979 }
02980 
02981 void KWidgetAction::unplug( QWidget *w )
02982 {
02983   // ### shouldn't this method check if w == m_widget->parent() ? (Simon)
02984   if( !m_widget || !isPlugged() )
02985     return;
02986 
02987   KToolBar* toolBar = (KToolBar*)m_widget->parent();
02988   disconnect( toolBar, SIGNAL( toolbarDestroyed() ), this, SLOT( slotToolbarDestroyed() ) );
02989 
02990   m_widget->reparent( 0L, QPoint(), false /*showIt*/ );
02991 
02992   KAction::unplug( w );
02993 }
02994 
02995 void KWidgetAction::slotToolbarDestroyed()
02996 {
02997   Q_ASSERT( m_widget );
02998   Q_ASSERT( isPlugged() );
02999   if( !m_widget || !isPlugged() )
03000     return;
03001 
03002   // Don't let a toolbar being destroyed, delete my widget.
03003   m_widget->reparent( 0L, QPoint(), false /*showIt*/ );
03004 }
03005 
03007 
03008 KActionSeparator::KActionSeparator( QObject *parent, const char *name )
03009   : KAction( parent, name )
03010 {
03011 }
03012 
03013 KActionSeparator::~KActionSeparator()
03014 {
03015 }
03016 
03017 int KActionSeparator::plug( QWidget *widget, int index )
03018 {
03019   if ( widget->inherits("QPopupMenu") )
03020   {
03021     QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
03022 
03023     int id = menu->insertSeparator( index );
03024 
03025     addContainer( menu, id );
03026     connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03027 
03028     return containerCount() - 1;
03029   }
03030   else if ( widget->inherits( "QMenuBar" ) )
03031   {
03032     QMenuBar *menuBar = static_cast<QMenuBar *>( widget );
03033 
03034     int id = menuBar->insertSeparator( index );
03035 
03036     addContainer( menuBar, id );
03037 
03038     connect( menuBar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03039 
03040     return containerCount() - 1;
03041   }
03042   else if ( widget->inherits( "KToolBar" ) )
03043   {
03044     KToolBar *toolBar = static_cast<KToolBar *>( widget );
03045 
03046     int id = toolBar->insertSeparator( index );
03047 
03048     addContainer( toolBar, id );
03049 
03050     connect( toolBar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03051 
03052     return containerCount() - 1;
03053   }
03054 
03055   return -1;
03056 }
03057 
03058 class KActionCollection::KActionCollectionPrivate
03059 {
03060 public:
03061   KActionCollectionPrivate()
03062   {
03063     m_instance = 0;
03064     //m_bOneKAccelOnly = false;
03065     //m_iWidgetCurrent = 0;
03066     m_bAutoConnectShortcuts = true;
03067     m_widget = 0;
03068     m_kaccel = m_builderKAccel = 0;
03069     m_dctHighlightContainers.setAutoDelete( true );
03070     m_highlight = false;
03071     m_currentHighlightAction = 0;
03072     m_statusCleared = true;
03073   }
03074 
03075   KInstance *m_instance;
03076   QString m_sXMLFile;
03077   bool m_bAutoConnectShortcuts;
03078   //bool m_bOneKAccelOnly;
03079   //int m_iWidgetCurrent;
03080   //QValueList<QWidget*> m_widgetList;
03081   //QValueList<KAccel*> m_kaccelList;
03082   QValueList<KActionCollection*> m_docList;
03083   QWidget *m_widget;
03084   KAccel *m_kaccel;
03085   KAccel *m_builderKAccel;
03086 
03087   QAsciiDict<KAction> m_actionDict;
03088   QPtrDict< QPtrList<KAction> > m_dctHighlightContainers;
03089   bool m_highlight;
03090   KAction *m_currentHighlightAction;
03091   bool m_statusCleared;
03092 };
03093 
03094 KActionCollection::KActionCollection( QWidget *parent, const char *name,
03095                                       KInstance *instance )
03096   : QObject( parent, name )
03097 {
03098   kdDebug(129) << "KActionCollection::KActionCollection( " << parent << ", " << name << " ): this = " << this << endl; // ellis
03099   d = new KActionCollectionPrivate;
03100   if( parent )
03101     setWidget( parent );
03102   //d->m_bOneKAccelOnly = (d->m_kaccelList.count() > 0);
03103   setInstance( instance );
03104 }
03105 
03106 
03107 KActionCollection::KActionCollection( QWidget *watch, QObject* parent, const char *name,
03108                                       KInstance *instance )
03109   : QObject( parent, name )
03110 {
03111   kdDebug(129) << "KActionCollection::KActionCollection( " << watch << ", " << parent << ", " << name << " ): this = " << this << endl; //ellis
03112   d = new KActionCollectionPrivate;
03113   if( watch )
03114     setWidget( watch );
03115   //d->m_bOneKAccelOnly = (d->m_kaccelList.count() > 0);
03116   setInstance( instance );
03117 }
03118 
03119 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
03120 KActionCollection::KActionCollection( QObject *parent, const char *name,
03121                                       KInstance *instance )
03122   : QObject( parent, name )
03123 {
03124   kdWarning(129) << "KActionCollection::KActionCollection( QObject *parent, const char *name, KInstance *instance )" << endl; //ellis
03125   kdDebug(129) << kdBacktrace() << endl;
03126   d = new KActionCollectionPrivate;
03127   QWidget* w = dynamic_cast<QWidget*>( parent );
03128   if( w )
03129     setWidget( w );
03130   //d->m_bOneKAccelOnly = (d->m_kaccelList.count() > 0);
03131   setInstance( instance );
03132 }
03133 
03134 KActionCollection::KActionCollection( const KActionCollection &copy )
03135     : QObject()
03136 {
03137   kdWarning(129) << "KActionCollection::KActionCollection( const KActionCollection & ): function is severely deprecated." << endl;
03138   d = new KActionCollectionPrivate;
03139   *this = copy;
03140 }
03141 #endif
03142 
03143 KActionCollection::~KActionCollection()
03144 {
03145   kdDebug(129) << "KActionCollection::~KActionCollection(): this = " << this << endl;
03146   for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
03147     KAction* pAction = it.current();
03148     if ( pAction->m_parentCollection == this )
03149       pAction->m_parentCollection = 0L;
03150   }
03151 
03152   delete d->m_kaccel;
03153   delete d->m_builderKAccel;
03154   delete d; d = 0;
03155 }
03156 
03157 void KActionCollection::setWidget( QWidget* w )
03158 {
03159   //if ( d->m_actionDict.count() > 0 ) {
03160   //  kdError(129) << "KActionCollection::setWidget(): must be called before any actions are added to collection!" << endl;
03161   //  kdDebug(129) << kdBacktrace() << endl;
03162   //}
03163   //else
03164   if ( !d->m_widget ) {
03165     d->m_widget = w;
03166     d->m_kaccel = new KAccel( w, this, "KActionCollection-KAccel" );
03167   }
03168   else if ( d->m_widget != w )
03169     kdWarning(129) << "KActionCollection::setWidget(): tried to change widget from " << d->m_widget << " to " << w << endl;
03170 }
03171 
03172 void KActionCollection::setAutoConnectShortcuts( bool b )
03173 {
03174   d->m_bAutoConnectShortcuts = b;
03175 }
03176 
03177 bool KActionCollection::isAutoConnectShortcuts()
03178 {
03179   return d->m_bAutoConnectShortcuts;
03180 }
03181 
03182 bool KActionCollection::addDocCollection( KActionCollection* pDoc )
03183 {
03184     d->m_docList.append( pDoc );
03185     return true;
03186 }
03187 
03188 void KActionCollection::beginXMLPlug( QWidget *widget )
03189 {
03190     kdDebug(129) << "KActionCollection::beginXMLPlug( buildWidget = " << widget << " ): this = " <<  this << " d->m_builderKAccel = " << d->m_builderKAccel << endl;
03191 
03192     if( widget && !d->m_builderKAccel ) {
03193             d->m_builderKAccel = new KAccel( widget, this, "KActionCollection-BuilderKAccel" );
03194     }
03195 }
03196 
03197 void KActionCollection::endXMLPlug()
03198 {
03199     kdDebug(129) << "KActionCollection::endXMLPlug(): this = " <<  this << endl;
03200     //s_kaccelXML = 0;
03201 }
03202 
03203 void KActionCollection::prepareXMLUnplug()
03204 {
03205     kdDebug(129) << "KActionCollection::prepareXMLUnplug(): this = " <<  this << endl;
03206     unplugShortcuts( d->m_kaccel );
03207 
03208     if( d->m_builderKAccel ) {
03209         unplugShortcuts( d->m_builderKAccel );
03210         delete d->m_builderKAccel;
03211         d->m_builderKAccel = 0;
03212     }
03213 }
03214 
03215 void KActionCollection::unplugShortcuts( KAccel* kaccel )
03216 {
03217   for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
03218     KAction* pAction = it.current();
03219     pAction->removeKAccel( kaccel );
03220   }
03221 
03222   for( uint i = 0; i < d->m_docList.count(); i++ )
03223     d->m_docList[i]->unplugShortcuts( kaccel );
03224 }
03225 
03226 /*void KActionCollection::addWidget( QWidget* w )
03227 {
03228   if( !d->m_bOneKAccelOnly ) {
03229     kdDebug(129) << "KActionCollection::addWidget( " << w << " ): this = " << this << endl;
03230     for( uint i = 0; i < d->m_widgetList.count(); i++ ) {
03231       if( d->m_widgetList[i] == w ) {
03232         d->m_iWidgetCurrent = i;
03233         return;
03234       }
03235   }
03236     d->m_iWidgetCurrent = d->m_widgetList.count();
03237     d->m_widgetList.append( w );
03238     d->m_kaccelList.append( new KAccel( w, this, "KActionCollection-KAccel" ) );
03239   }
03240 }
03241 
03242 void KActionCollection::removeWidget( QWidget* w )
03243 {
03244   if( !d->m_bOneKAccelOnly ) {
03245     kdDebug(129) << "KActionCollection::removeWidget( " << w << " ): this = " << this << endl;
03246     for( uint i = 0; i < d->m_widgetList.count(); i++ ) {
03247       if( d->m_widgetList[i] == w ) {
03248         // Remove KAccel object from children.
03249         KAccel* pKAccel = d->m_kaccelList[i];
03250         for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
03251           KAction* pAction = it.current();
03252           if ( pAction->m_parentCollection == this ) {
03253             pAction->removeKAccel( pKAccel );
03254           }
03255         }
03256         delete pKAccel;
03257 
03258         d->m_widgetList.remove( d->m_widgetList.at( i ) );
03259         d->m_kaccelList.remove( d->m_kaccelList.at( i ) );
03260 
03261         if( d->m_iWidgetCurrent == (int)i )
03262           d->m_iWidgetCurrent = -1;
03263         else if( d->m_iWidgetCurrent > (int)i )
03264           d->m_iWidgetCurrent--;
03265         return;
03266       }
03267     }
03268     kdWarning(129) << "KActionCollection::removeWidget( " << w << " ): widget not in list." << endl;
03269   }
03270 }
03271 
03272 bool KActionCollection::ownsKAccel() const
03273 {
03274   return d->m_bOneKAccelOnly;
03275 }
03276 
03277 uint KActionCollection::widgetCount() const
03278 {
03279   return d->m_widgetList.count();
03280 }
03281 
03282 const KAccel* KActionCollection::widgetKAccel( uint i ) const
03283 {
03284   return d->m_kaccelList[i];
03285 }*/
03286 
03287 KAccel* KActionCollection::kaccel()
03288 {
03289   //if( d->m_kaccelList.count() > 0 )
03290   //  return d->m_kaccelList[d->m_iWidgetCurrent];
03291   //else
03292   //  return 0;
03293   return d->m_kaccel;
03294 }
03295 
03296 const KAccel* KActionCollection::kaccel() const
03297 {
03298   //if( d->m_kaccelList.count() > 0 )
03299   //  return d->m_kaccelList[d->m_iWidgetCurrent];
03300   //else
03301   //  return 0;
03302   return d->m_kaccel;
03303 }
03304 
03305 /*void KActionCollection::findMainWindow( QWidget *w )
03306 {
03307   // Note: topLevelWidget() stops too early, we can't use it.
03308   QWidget * tl = w;
03309   while ( tl->parentWidget() ) // lookup parent and store
03310     tl = tl->parentWidget();
03311 
03312   KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
03313   if (mw)
03314     d->m_mainwindow = mw;
03315   else
03316     kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << tl << endl;
03317 }*/
03318 
03319 void KActionCollection::_insert( KAction* action )
03320 {
03321   char unnamed_name[100];
03322   const char *name = action->name();
03323   if( qstrcmp( name, "unnamed" ) == 0 )
03324   {
03325      sprintf(unnamed_name, "unnamed-%p", (void *)action);
03326      name = unnamed_name;
03327   }
03328   KAction *a = d->m_actionDict[ name ];
03329   if ( a == action )
03330       return;
03331 
03332   d->m_actionDict.insert( name, action );
03333 
03334   emit inserted( action );
03335 }
03336 
03337 void KActionCollection::_remove( KAction* action )
03338 {
03339   delete _take( action );
03340 }
03341 
03342 KAction* KActionCollection::_take( KAction* action )
03343 {
03344   char unnamed_name[100];
03345   const char *name = action->name();
03346   if( qstrcmp( name, "unnamed" ) == 0 )
03347   {
03348      sprintf(unnamed_name, "unnamed-%p", (void *) action);
03349      name = unnamed_name;
03350   }
03351 
03352   KAction *a = d->m_actionDict.take( name );
03353   if ( !a || a != action )
03354       return 0;
03355 
03356   emit removed( action );
03357   return a;
03358 }
03359 
03360 void KActionCollection::_clear()
03361 {
03362   QAsciiDictIterator<KAction> it( d->m_actionDict );
03363   while ( it.current() )
03364     _remove( it.current() );
03365 }
03366 
03367 void KActionCollection::insert( KAction* action )   { _insert( action ); }
03368 void KActionCollection::remove( KAction* action )   { _remove( action ); }
03369 KAction* KActionCollection::take( KAction* action ) { return _take( action ); }
03370 void KActionCollection::clear()                     { _clear(); }
03371 KAccel* KActionCollection::accel()                  { return kaccel(); }
03372 const KAccel* KActionCollection::accel() const      { return kaccel(); }
03373 KAccel* KActionCollection::builderKAccel() const    { return d->m_builderKAccel; }
03374 
03375 KAction* KActionCollection::action( const char* name, const char* classname ) const
03376 {
03377   KAction* pAction = 0;
03378 
03379   if ( !classname && name )
03380     pAction = d->m_actionDict[ name ];
03381 
03382   else {
03383     QAsciiDictIterator<KAction> it( d->m_actionDict );
03384     for( ; it.current(); ++it )
03385     {
03386       if ( ( !name || strcmp( it.current()->name(), name ) == 0 ) &&
03387           ( !classname || strcmp( it.current()->className(), classname ) == 0 ) ) {
03388         pAction = it.current();
03389         break;
03390       }
03391     }
03392   }
03393 
03394   if( !pAction ) {
03395     for( uint i = 0; i < d->m_docList.count() && !pAction; i++ )
03396       pAction = d->m_docList[i]->action( name, classname );
03397   }
03398 
03399   return pAction;
03400 }
03401 
03402 KAction* KActionCollection::action( int index ) const
03403 {
03404   QAsciiDictIterator<KAction> it( d->m_actionDict );
03405   it += index;
03406   return it.current();
03407 //  return d->m_actions.at( index );
03408 }
03409 
03410 bool KActionCollection::readShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig )
03411 {
03412   return KActionShortcutList(this).readSettings( sConfigGroup, pConfig );
03413 }
03414 
03415 bool KActionCollection::writeShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig ) const
03416 {
03417   return KActionShortcutList((KActionCollection*)this).writeSettings( sConfigGroup, pConfig );
03418 }
03419 
03420 uint KActionCollection::count() const
03421 {
03422   return d->m_actionDict.count();
03423 }
03424 
03425 QStringList KActionCollection::groups() const
03426 {
03427   QStringList lst;
03428 
03429   QAsciiDictIterator<KAction> it( d->m_actionDict );
03430   for( ; it.current(); ++it )
03431     if ( !it.current()->group().isEmpty() && !lst.contains( it.current()->group() ) )
03432       lst.append( it.current()->group() );
03433 
03434   return lst;
03435 }
03436 
03437 KActionPtrList KActionCollection::actions( const QString& group ) const
03438 {
03439   KActionPtrList lst;
03440 
03441   QAsciiDictIterator<KAction> it( d->m_actionDict );
03442   for( ; it.current(); ++it )
03443     if ( it.current()->group() == group )
03444       lst.append( it.current() );
03445     else if ( it.current()->group().isEmpty() && group.isEmpty() )
03446       lst.append( it.current() );
03447 
03448   return lst;
03449 }
03450 
03451 KActionPtrList KActionCollection::actions() const
03452 {
03453   KActionPtrList lst;
03454 
03455   QAsciiDictIterator<KAction> it( d->m_actionDict );
03456   for( ; it.current(); ++it )
03457     lst.append( it.current() );
03458 
03459   return lst;
03460 }
03461 
03462 void KActionCollection::setInstance( KInstance *instance )
03463 {
03464   if ( instance )
03465     d->m_instance = instance;
03466   else
03467     d->m_instance = KGlobal::instance();
03468 }
03469 
03470 KInstance *KActionCollection::instance() const
03471 {
03472   return d->m_instance;
03473 }
03474 
03475 void KActionCollection::setXMLFile( const QString& sXMLFile )
03476 {
03477   d->m_sXMLFile = sXMLFile;
03478 }
03479 
03480 const QString& KActionCollection::xmlFile() const
03481 {
03482   return d->m_sXMLFile;
03483 }
03484 
03485 void KActionCollection::setHighlightingEnabled( bool enable )
03486 {
03487   d->m_highlight = enable;
03488 }
03489 
03490 bool KActionCollection::highlightingEnabled() const
03491 {
03492   return d->m_highlight;
03493 }
03494 
03495 void KActionCollection::connectHighlight( QWidget *container, KAction *action )
03496 {
03497   if ( !d->m_highlight )
03498     return;
03499 
03500   QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
03501 
03502   if ( !actionList )
03503   {
03504     actionList = new QPtrList<KAction>;
03505 
03506     if ( container->inherits( "QPopupMenu" ) )
03507     {
03508       connect( container, SIGNAL( highlighted( int ) ),
03509                this, SLOT( slotMenuItemHighlighted( int ) ) );
03510       connect( container, SIGNAL( aboutToHide() ),
03511                this, SLOT( slotMenuAboutToHide() ) );
03512     }
03513     else if ( container->inherits( "KToolBar" ) )
03514     {
03515       connect( container, SIGNAL( highlighted( int, bool ) ),
03516                this, SLOT( slotToolBarButtonHighlighted( int, bool ) ) );
03517     }
03518 
03519     connect( container, SIGNAL( destroyed() ),
03520              this, SLOT( slotDestroyed() ) );
03521 
03522     d->m_dctHighlightContainers.insert( container, actionList );
03523   }
03524 
03525   actionList->append( action );
03526 }
03527 
03528 void KActionCollection::disconnectHighlight( QWidget *container, KAction *action )
03529 {
03530   if ( !d->m_highlight )
03531     return;
03532 
03533   QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
03534 
03535   if ( !actionList )
03536     return;
03537 
03538   actionList->removeRef( action );
03539 
03540   if ( actionList->count() == 0 )
03541     d->m_dctHighlightContainers.remove( container );
03542 }
03543 
03544 void KActionCollection::slotMenuItemHighlighted( int id )
03545 {
03546   if ( !d->m_highlight )
03547     return;
03548 
03549   if ( d->m_currentHighlightAction )
03550     emit actionHighlighted( d->m_currentHighlightAction, false );
03551 
03552   QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
03553 
03554   d->m_currentHighlightAction = findAction( container, id );
03555 
03556   if ( !d->m_currentHighlightAction )
03557   {
03558       if ( !d->m_statusCleared )
03559           emit clearStatusText();
03560       d->m_statusCleared = true;
03561       return;
03562   }
03563 
03564   d->m_statusCleared = false;
03565   emit actionHighlighted( d->m_currentHighlightAction );
03566   emit actionHighlighted( d->m_currentHighlightAction, true );
03567   emit actionStatusText( d->m_currentHighlightAction->toolTip() );
03568 }
03569 
03570 void KActionCollection::slotMenuAboutToHide()
03571 {
03572     if ( d->m_currentHighlightAction )
03573         emit actionHighlighted( d->m_currentHighlightAction, false );
03574     d->m_currentHighlightAction = 0;
03575 
03576     if ( !d->m_statusCleared )
03577         emit clearStatusText();
03578     d->m_statusCleared = true;
03579 }
03580 
03581 void KActionCollection::slotToolBarButtonHighlighted( int id, bool highlight )
03582 {
03583   if ( !d->m_highlight )
03584     return;
03585 
03586   QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
03587 
03588   KAction *action = findAction( container, id );
03589 
03590   if ( !action )
03591   {
03592       d->m_currentHighlightAction = 0;
03593       // use tooltip groups for toolbar status text stuff instead (Simon)
03594 //      emit clearStatusText();
03595       return;
03596   }
03597 
03598   emit actionHighlighted( action, highlight );
03599 
03600   if ( highlight )
03601     d->m_currentHighlightAction = action;
03602   else
03603   {
03604     d->m_currentHighlightAction = 0;
03605 //    emit clearStatusText();
03606   }
03607 }
03608 
03609 void KActionCollection::slotDestroyed()
03610 {
03611     d->m_dctHighlightContainers.remove( reinterpret_cast<void *>( const_cast<QObject *>(sender()) ) );
03612 }
03613 
03614 KAction *KActionCollection::findAction( QWidget *container, int id )
03615 {
03616   QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ reinterpret_cast<void *>( container ) ];
03617 
03618   if ( !actionList )
03619     return 0;
03620 
03621   QPtrListIterator<KAction> it( *actionList );
03622   for (; it.current(); ++it )
03623     if ( it.current()->isPlugged( container, id ) )
03624       return it.current();
03625 
03626   return 0;
03627 }
03628 
03629 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
03630 KActionCollection KActionCollection::operator+(const KActionCollection &c ) const
03631 {
03632   kdWarning(129) << "KActionCollection::operator+(): function is severely deprecated." << endl;
03633   KActionCollection ret( *this );
03634 
03635   QValueList<KAction *> actions = c.actions();
03636   QValueList<KAction *>::ConstIterator it = actions.begin();
03637   QValueList<KAction *>::ConstIterator end = actions.end();
03638   for (; it != end; ++it )
03639     ret.insert( *it );
03640 
03641   return ret;
03642 }
03643 
03644 KActionCollection &KActionCollection::operator=( const KActionCollection &copy )
03645 {
03646   kdWarning(129) << "KActionCollection::operator=(): function is severely deprecated." << endl;
03647   //d->m_bOneKAccelOnly = copy.d->m_bOneKAccelOnly;
03648   //d->m_iWidgetCurrent = copy.d->m_iWidgetCurrent;
03649   //d->m_widgetList = copy.d->m_widgetList;
03650   //d->m_kaccelList = copy.d->m_kaccelList;
03651   d->m_widget = copy.d->m_widget;
03652   d->m_kaccel = copy.d->m_kaccel;
03653   d->m_actionDict = copy.d->m_actionDict;
03654   setInstance( copy.instance() );
03655   return *this;
03656 }
03657 
03658 KActionCollection &KActionCollection::operator+=( const KActionCollection &c )
03659 {
03660   kdWarning(129) << "KActionCollection::operator+=(): function is severely deprecated." << endl;
03661   QAsciiDictIterator<KAction> it(c.d->m_actionDict);
03662   for ( ; it.current(); ++it )
03663     insert( it.current() );
03664 
03665   return *this;
03666 }
03667 #endif
03668 
03669 //---------------------------------------------------------------------
03670 // KActionShortcutList
03671 //---------------------------------------------------------------------
03672 
03673 KActionShortcutList::KActionShortcutList( KActionCollection* pColl )
03674 : m_actions( *pColl )
03675     { }
03676 KActionShortcutList::~KActionShortcutList()
03677     { }
03678 uint KActionShortcutList::count() const
03679     { return m_actions.count(); }
03680 QString KActionShortcutList::name( uint i ) const
03681     { return m_actions.action(i)->name(); }
03682 QString KActionShortcutList::label( uint i ) const
03683     { return m_actions.action(i)->text(); }
03684 QString KActionShortcutList::whatsThis( uint i ) const
03685     { return m_actions.action(i)->whatsThis(); }
03686 const KShortcut& KActionShortcutList::shortcut( uint i ) const
03687     { return m_actions.action(i)->shortcut(); }
03688 const KShortcut& KActionShortcutList::shortcutDefault( uint i ) const
03689     { return m_actions.action(i)->shortcutDefault(); }
03690 bool KActionShortcutList::isConfigurable( uint i ) const
03691     { return m_actions.action(i)->isShortcutConfigurable(); }
03692 bool KActionShortcutList::setShortcut( uint i, const KShortcut& cut )
03693     { return m_actions.action(i)->setShortcut( cut ); }
03694 const KInstance* KActionShortcutList::instance() const
03695     { return m_actions.instance(); }
03696 QVariant KActionShortcutList::getOther( Other, uint ) const
03697     { return QVariant(); }
03698 bool KActionShortcutList::setOther( Other, uint, QVariant )
03699     { return false; }
03700 
03701 bool KActionShortcutList::save() const
03702 {
03703     kdDebug(129) << "KActionShortcutList::save(): xmlFile = " << m_actions.xmlFile() << endl;
03704 
03705     if( m_actions.xmlFile().isEmpty() )
03706         return writeSettings();
03707 
03708     QString tagActionProp = QString::fromLatin1("ActionProperties");
03709     QString tagAction     = QString::fromLatin1("Action");
03710     QString attrName      = QString::fromLatin1("name");
03711     QString attrShortcut  = QString::fromLatin1("shortcut");
03712     QString attrAccel     = QString::fromLatin1("accel"); // Depricated attribute
03713 
03714     // Read XML file
03715     QString sXml( KXMLGUIFactory::readConfigFile( m_actions.xmlFile(), false, instance() ) );
03716     QDomDocument doc;
03717     doc.setContent( sXml );
03718 
03719     // Process XML data
03720 
03721     // first, lets see if we have existing properties
03722     QDomElement elem;
03723     QDomElement it = doc.documentElement();
03724     // KXMLGUIFactory::removeDOMComments( it ); <-- What was this for? --ellis
03725     it = it.firstChild().toElement();
03726     for( ; !it.isNull(); it = it.nextSibling().toElement() ) {
03727         if( it.tagName() == tagActionProp ) {
03728             elem = it;
03729             break;
03730         }
03731     }
03732 
03733     // if there was none, create one
03734     if( elem.isNull() ) {
03735         elem = doc.createElement( tagActionProp );
03736         doc.documentElement().appendChild( elem );
03737     }
03738 
03739     // now, iterate through our actions
03740     uint nSize = count();
03741     for( uint i = 0; i < nSize; i++ ) {
03742         const QString& sName = name(i);
03743 
03744         bool bSameAsDefault = (shortcut(i) == shortcutDefault(i));
03745         //kdDebug(129) << "name = " << sName << " shortcut = " << shortcut(i).toStringInternal() << " def = " << shortcutDefault(i).toStringInternal() << endl;
03746 
03747         // now see if this element already exists
03748         QDomElement act_elem;
03749         for( it = elem.firstChild().toElement(); !it.isNull(); it = it.nextSibling().toElement() ) {
03750             if( it.attribute( attrName ) == sName ) {
03751                 act_elem = it;
03752                 break;
03753             }
03754         }
03755 
03756         // nope, create a new one
03757         if( act_elem.isNull() ) {
03758             if( bSameAsDefault )
03759                 continue;
03760             //kdDebug(129) << "\tnode doesn't exist." << endl;
03761             act_elem = doc.createElement( tagAction );
03762             act_elem.setAttribute( attrName, sName );
03763         }
03764 
03765         act_elem.removeAttribute( attrAccel );
03766         if( bSameAsDefault ) {
03767             act_elem.removeAttribute( attrShortcut );
03768             //kdDebug(129) << "act_elem.attributes().count() = " << act_elem.attributes().count() << endl;
03769             if( act_elem.attributes().count() == 1 )
03770                 elem.removeChild( act_elem );
03771         } else {
03772             act_elem.setAttribute( attrShortcut, shortcut(i).toStringInternal() );
03773             elem.appendChild( act_elem );
03774         }
03775     }
03776 
03777     // Write back to XML file
03778     return KXMLGUIFactory::saveConfigFile( doc, m_actions.xmlFile(), instance() );
03779 }
03780 
03781 //---------------------------------------------------------------------
03782 // KActionPtrShortcutList
03783 //---------------------------------------------------------------------
03784 
03785 KActionPtrShortcutList::KActionPtrShortcutList( KActionPtrList& list )
03786 : m_actions( list )
03787     { }
03788 KActionPtrShortcutList::~KActionPtrShortcutList()
03789     { }
03790 uint KActionPtrShortcutList::count() const
03791     { return m_actions.count(); }
03792 QString KActionPtrShortcutList::name( uint i ) const
03793     { return m_actions[i]->name(); }
03794 QString KActionPtrShortcutList::label( uint i ) const
03795     { return m_actions[i]->text(); }
03796 QString KActionPtrShortcutList::whatsThis( uint i ) const
03797     { return m_actions[i]->whatsThis(); }
03798 const KShortcut& KActionPtrShortcutList::shortcut( uint i ) const
03799     { return m_actions[i]->shortcut(); }
03800 const KShortcut& KActionPtrShortcutList::shortcutDefault( uint i ) const
03801     { return m_actions[i]->shortcutDefault(); }
03802 bool KActionPtrShortcutList::isConfigurable( uint i ) const
03803     { return m_actions[i]->isShortcutConfigurable(); }
03804 bool KActionPtrShortcutList::setShortcut( uint i, const KShortcut& cut )
03805     { return m_actions[i]->setShortcut( cut ); }
03806 QVariant KActionPtrShortcutList::getOther( Other, uint ) const
03807     { return QVariant(); }
03808 bool KActionPtrShortcutList::setOther( Other, uint, QVariant )
03809     { return false; }
03810 bool KActionPtrShortcutList::save() const
03811     { return false; }
03812 
03813 void KActionShortcutList::virtual_hook( int id, void* data )
03814 { KShortcutList::virtual_hook( id, data ); }
03815 
03816 void KActionPtrShortcutList::virtual_hook( int id, void* data )
03817 { KShortcutList::virtual_hook( id, data ); }
03818 
03819 void KAction::virtual_hook( int, void* )
03820 { /*BASE::virtual_hook( id, data );*/ }
03821 
03822 void KToggleAction::virtual_hook( int id, void* data )
03823 { KAction::virtual_hook( id, data ); }
03824 
03825 void KRadioAction::virtual_hook( int id, void* data )
03826 { KToggleAction::virtual_hook( id, data ); }
03827 
03828 void KSelectAction::virtual_hook( int id, void* data )
03829 { KAction::virtual_hook( id, data ); }
03830 
03831 void KListAction::virtual_hook( int id, void* data )
03832 { KSelectAction::virtual_hook( id, data ); }
03833 
03834 void KRecentFilesAction::virtual_hook( int id, void* data )
03835 { KListAction::virtual_hook( id, data ); }
03836 
03837 void KFontAction::virtual_hook( int id, void* data )
03838 { KSelectAction::virtual_hook( id, data ); }
03839 
03840 void KFontSizeAction::virtual_hook( int id, void* data )
03841 { KSelectAction::virtual_hook( id, data ); }
03842 
03843 void KActionMenu::virtual_hook( int id, void* data )
03844 { KAction::virtual_hook( id, data ); }
03845 
03846 void KToolBarPopupAction::virtual_hook( int id, void* data )
03847 { KAction::virtual_hook( id, data ); }
03848 
03849 void KToggleToolBarAction::virtual_hook( int id, void* data )
03850 { KToggleAction::virtual_hook( id, data ); }
03851 
03852 void KWidgetAction::virtual_hook( int id, void* data )
03853 { KAction::virtual_hook( id, data ); }
03854 
03855 void KActionSeparator::virtual_hook( int id, void* data )
03856 { KAction::virtual_hook( id, data ); }
03857 
03858 void KActionCollection::virtual_hook( int, void* )
03859 { /*BASE::virtual_hook( id, data );*/ }
03860 
03861 /* vim: et sw=2 ts=2
03862  */
03863 
03864 #include "kaction.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:57 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001