kaction.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:56:08 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001