00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #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('-') )
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
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
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;
00271 #ifndef KDE_NO_COMPAT
00272 if (d->m_kaccel)
00273 unplugAccel();
00274 #endif
00275
00276
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
00284
00285
00286
00287
00288
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
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
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
00418 bool KAction::initShortcut( const KShortcut& cut )
00419 {
00420 d->m_cut = cut;
00421
00422
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
00435 void KAction::plugShortcut()
00436 {
00437 KAccel* kaccel = kaccelCurrent();
00438
00439
00440 if( kaccel && qstrcmp( name(), "unnamed" ) != 0 ) {
00441
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
00459 for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
00460
00461
00462 if( d->m_kaccelList[i] == kaccel )
00463 bInsertRequired = false;
00464 if( bChanged )
00465 updateKAccelShortcut( d->m_kaccelList[i] );
00466 }
00467
00468
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
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
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;
00518 }
00519
00520 void KAction::removeKAccel( KAccel* kaccel )
00521 {
00522
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
00556
00557
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
00569 else {
00570
00571
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
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
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
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;
00666 kdDebug(129) << kdBacktrace() << endl;
00667 }
00668 #endif
00669
00670
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
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,
00691 SLOT( slotActivated() ), keyQt,
00692 -1, index );
00693 }
00694 else
00695 id = menu->insertItem( d->text(), this,
00696 SLOT( slotActivated() ),
00697 keyQt, -1, index );
00698
00699
00700
00701 if ( d->m_kaccelList.count() || d->m_kaccel )
00702 updateShortcut( menu, id );
00703
00704
00705
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() )
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
00796 if ( d->m_kaccel )
00797 unplugAccel();
00798
00799
00800
00801
00802
00803
00804
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
00814 }
00815 else
00816 kdWarning(129) << "KAction::plugAccel( kacc = " << kacc << " ): KAccel object already contains an action name \"" << name() << "\"" << endl;
00817 }
00818
00819 void KAction::unplugAccel()
00820 {
00821
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
00832 QWidget * tl = w;
00833 QWidget * n;
00834 while ( !tl->isDialog() && ( n = tl->parentWidget() ) )
00835 tl = n;
00836
00837 KMainWindow * mw = dynamic_cast<KMainWindow *>(tl);
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
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
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() )
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
01174 void KAction::slotKeycodeChanged()
01175 {
01176 kdDebug(129) << "KAction::slotKeycodeChanged()" << endl;
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
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
01555
01556
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;
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;
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
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;
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
01721
01722
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;
01733 if ( widget->inherits("QPopupMenu") )
01734 {
01735
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
01840
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
01862
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
01986
01987
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
02141 d->m_maxItems = maxItems;
02142
02143
02144 while( lst.count() > maxItems )
02145 {
02146
02147 lst.remove( lst.last() );
02148 }
02149
02150
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
02161 lst.remove( file );
02162
02163
02164 if( lst.count() == d->m_maxItems )
02165 {
02166
02167 lst.remove( lst.last() );
02168 }
02169
02170
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
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
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
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
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
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
02515 QValueList<int> lst;
02516
02517 QStringList itemsList = items();
02518 for (QStringList::Iterator it = itemsList.begin() ; it != itemsList.end() ; ++it)
02519 lst.append( (*it).toInt() );
02520
02521 lst.append( size );
02522
02523 qHeapSort( lst );
02524
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
02530 index = lst.findIndex( size );
02531 setCurrentItem( index );
02532 }
02533 else
02534 setCurrentItem( index );
02535
02536
02537
02538
02539
02540
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() );
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;
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;
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
02801
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
02874 QWidget * tl = w;
02875 QWidget * n;
02876 while ( !tl->isDialog() && ( n = tl->parentWidget() ) )
02877 tl = n;
02878
02879 KMainWindow * mw = dynamic_cast<KMainWindow *>(tl);
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
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
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 );
02987
02988 KAction::unplug( w );
02989 }
02990
02991 void KWidgetAction::slotToolbarDestroyed()
02992 {
02993 if( !m_widget || !isPlugged() )
02994 return;
02995
02996
02997 m_widget->reparent( 0L, QPoint(), false );
02998 }
02999
03001
03002 KActionSeparator::KActionSeparator( QObject *parent, const char *name )
03003 : KAction( parent, name )
03004 {
03005 }
03006
03007 KActionSeparator::~KActionSeparator()
03008 {
03009 }
03010
03011 int KActionSeparator::plug( QWidget *widget, int index )
03012 {
03013 if ( widget->inherits("QPopupMenu") )
03014 {
03015 QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
03016
03017 int id = menu->insertSeparator( index );
03018
03019 addContainer( menu, id );
03020 connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03021
03022 return containerCount() - 1;
03023 }
03024 else if ( widget->inherits( "QMenuBar" ) )
03025 {
03026 QMenuBar *menuBar = static_cast<QMenuBar *>( widget );
03027
03028 int id = menuBar->insertSeparator( index );
03029
03030 addContainer( menuBar, id );
03031
03032 connect( menuBar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03033
03034 return containerCount() - 1;
03035 }
03036 else if ( widget->inherits( "KToolBar" ) )
03037 {
03038 KToolBar *toolBar = static_cast<KToolBar *>( widget );
03039
03040 int id = toolBar->insertSeparator( index );
03041
03042 addContainer( toolBar, id );
03043
03044 connect( toolBar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
03045
03046 return containerCount() - 1;
03047 }
03048
03049 return -1;
03050 }
03051
03052 class KActionCollection::KActionCollectionPrivate
03053 {
03054 public:
03055 KActionCollectionPrivate()
03056 {
03057 m_instance = 0;
03058
03059
03060 m_bAutoConnectShortcuts = true;
03061 m_widget = 0;
03062 m_kaccel = m_builderKAccel = 0;
03063 m_dctHighlightContainers.setAutoDelete( true );
03064 m_highlight = false;
03065 m_currentHighlightAction = 0;
03066 m_statusCleared = true;
03067 }
03068
03069 KInstance *m_instance;
03070 QString m_sXMLFile;
03071 bool m_bAutoConnectShortcuts;
03072
03073
03074
03075
03076 QValueList<KActionCollection*> m_docList;
03077 QWidget *m_widget;
03078 KAccel *m_kaccel;
03079 KAccel *m_builderKAccel;
03080
03081 QAsciiDict<KAction> m_actionDict;
03082 QPtrDict< QPtrList<KAction> > m_dctHighlightContainers;
03083 bool m_highlight;
03084 KAction *m_currentHighlightAction;
03085 bool m_statusCleared;
03086 };
03087
03088 KActionCollection::KActionCollection( QWidget *parent, const char *name,
03089 KInstance *instance )
03090 : QObject( parent, name )
03091 {
03092 kdDebug(129) << "KActionCollection::KActionCollection( " << parent << ", " << name << " ): this = " << this << endl;
03093 d = new KActionCollectionPrivate;
03094 if( parent )
03095 setWidget( parent );
03096
03097 setInstance( instance );
03098 }
03099
03100
03101 KActionCollection::KActionCollection( QWidget *watch, QObject* parent, const char *name,
03102 KInstance *instance )
03103 : QObject( parent, name )
03104 {
03105 kdDebug(129) << "KActionCollection::KActionCollection( " << watch << ", " << parent << ", " << name << " ): this = " << this << endl;
03106 d = new KActionCollectionPrivate;
03107 if( watch )
03108 setWidget( watch );
03109
03110 setInstance( instance );
03111 }
03112
03113 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
03114 KActionCollection::KActionCollection( QObject *parent, const char *name,
03115 KInstance *instance )
03116 : QObject( parent, name )
03117 {
03118 kdWarning(129) << "KActionCollection::KActionCollection( QObject *parent, const char *name, KInstance *instance )" << endl;
03119 kdDebug(129) << kdBacktrace() << endl;
03120 d = new KActionCollectionPrivate;
03121 QWidget* w = dynamic_cast<QWidget*>( parent );
03122 if( w )
03123 setWidget( w );
03124
03125 setInstance( instance );
03126 }
03127
03128 KActionCollection::KActionCollection( const KActionCollection © )
03129 : QObject()
03130 {
03131 kdWarning(129) << "KActionCollection::KActionCollection( const KActionCollection & ): function is severely deprecated." << endl;
03132 d = new KActionCollectionPrivate;
03133 *this = copy;
03134 }
03135 #endif
03136
03137 KActionCollection::~KActionCollection()
03138 {
03139 kdDebug(129) << "KActionCollection::~KActionCollection(): this = " << this << endl;
03140 for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
03141 KAction* pAction = it.current();
03142 if ( pAction->m_parentCollection == this )
03143 pAction->m_parentCollection = 0L;
03144 }
03145
03146 delete d->m_kaccel;
03147 delete d->m_builderKAccel;
03148 delete d; d = 0;
03149 }
03150
03151 void KActionCollection::setWidget( QWidget* w )
03152 {
03153
03154
03155
03156
03157
03158 if ( !d->m_widget ) {
03159 d->m_widget = w;
03160 d->m_kaccel = new KAccel( w, this, "KActionCollection-KAccel" );
03161 }
03162 else if ( d->m_widget != w )
03163 kdWarning(129) << "KActionCollection::setWidget(): tried to change widget from " << d->m_widget << " to " << w << endl;
03164 }
03165
03166 void KActionCollection::setAutoConnectShortcuts( bool b )
03167 {
03168 d->m_bAutoConnectShortcuts = b;
03169 }
03170
03171 bool KActionCollection::isAutoConnectShortcuts()
03172 {
03173 return d->m_bAutoConnectShortcuts;
03174 }
03175
03176 bool KActionCollection::addDocCollection( KActionCollection* pDoc )
03177 {
03178 d->m_docList.append( pDoc );
03179 return true;
03180 }
03181
03182 void KActionCollection::beginXMLPlug( QWidget *widget )
03183 {
03184 kdDebug(129) << "KActionCollection::beginXMLPlug( buildWidget = " << widget << " ): this = " << this << " d->m_builderKAccel = " << d->m_builderKAccel << endl;
03185
03186 if( widget && !d->m_builderKAccel ) {
03187 d->m_builderKAccel = new KAccel( widget, this, "KActionCollection-BuilderKAccel" );
03188 }
03189 }
03190
03191 void KActionCollection::endXMLPlug()
03192 {
03193 kdDebug(129) << "KActionCollection::endXMLPlug(): this = " << this << endl;
03194
03195 }
03196
03197 void KActionCollection::prepareXMLUnplug()
03198 {
03199 kdDebug(129) << "KActionCollection::prepareXMLUnplug(): this = " << this << endl;
03200 unplugShortcuts( d->m_kaccel );
03201
03202 if( d->m_builderKAccel ) {
03203 unplugShortcuts( d->m_builderKAccel );
03204 delete d->m_builderKAccel;
03205 d->m_builderKAccel = 0;
03206 }
03207 }
03208
03209 void KActionCollection::unplugShortcuts( KAccel* kaccel )
03210 {
03211 for ( QAsciiDictIterator<KAction> it( d->m_actionDict ); it.current(); ++it ) {
03212 KAction* pAction = it.current();
03213 pAction->removeKAccel( kaccel );
03214 }
03215
03216 for( uint i = 0; i < d->m_docList.count(); i++ )
03217 d->m_docList[i]->unplugShortcuts( kaccel );
03218 }
03219
03220
03221
03222
03223
03224
03225
03226
03227
03228
03229
03230
03231
03232
03233
03234
03235
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245
03246
03247
03248
03249
03250
03251
03252
03253
03254
03255
03256
03257
03258
03259
03260
03261
03262
03263
03264
03265
03266
03267
03268
03269
03270
03271
03272
03273
03274
03275
03276
03277
03278
03279
03280
03281 KAccel* KActionCollection::kaccel()
03282 {
03283
03284
03285
03286
03287 return d->m_kaccel;
03288 }
03289
03290 const KAccel* KActionCollection::kaccel() const
03291 {
03292
03293
03294
03295
03296 return d->m_kaccel;
03297 }
03298
03299
03300
03301
03302
03303
03304
03305
03306
03307
03308
03309
03310
03311
03312
03313 void KActionCollection::_insert( KAction* action )
03314 {
03315 char unnamed_name[100];
03316 const char *name = action->name();
03317 if( qstrcmp( name, "unnamed" ) == 0 )
03318 {
03319 sprintf(unnamed_name, "unnamed-%p", (void *)action);
03320 name = unnamed_name;
03321 }
03322 KAction *a = d->m_actionDict[ name ];
03323 if ( a == action )
03324 return;
03325
03326 d->m_actionDict.insert( name, action );
03327
03328 emit inserted( action );
03329 }
03330
03331 void KActionCollection::_remove( KAction* action )
03332 {
03333 delete _take( action );
03334 }
03335
03336 KAction* KActionCollection::_take( KAction* action )
03337 {
03338 char unnamed_name[100];
03339 const char *name = action->name();
03340 if( qstrcmp( name, "unnamed" ) == 0 )
03341 {
03342 sprintf(unnamed_name, "unnamed-%p", (void *) action);
03343 name = unnamed_name;
03344 }
03345
03346 KAction *a = d->m_actionDict.take( name );
03347 if ( !a || a != action )
03348 return 0;
03349
03350 emit removed( action );
03351 return a;
03352 }
03353
03354 void KActionCollection::_clear()
03355 {
03356 QAsciiDictIterator<KAction> it( d->m_actionDict );
03357 while ( it.current() )
03358 _remove( it.current() );
03359 }
03360
03361 void KActionCollection::insert( KAction* action ) { _insert( action ); }
03362 void KActionCollection::remove( KAction* action ) { _remove( action ); }
03363 KAction* KActionCollection::take( KAction* action ) { return _take( action ); }
03364 void KActionCollection::clear() { _clear(); }
03365 KAccel* KActionCollection::accel() { return kaccel(); }
03366 const KAccel* KActionCollection::accel() const { return kaccel(); }
03367 KAccel* KActionCollection::builderKAccel() const { return d->m_builderKAccel; }
03368
03369 KAction* KActionCollection::action( const char* name, const char* classname ) const
03370 {
03371 KAction* pAction = 0;
03372
03373 if ( !classname && name )
03374 pAction = d->m_actionDict[ name ];
03375
03376 else {
03377 QAsciiDictIterator<KAction> it( d->m_actionDict );
03378 for( ; it.current(); ++it )
03379 {
03380 if ( ( !name || strcmp( it.current()->name(), name ) == 0 ) &&
03381 ( !classname || strcmp( it.current()->className(), classname ) == 0 ) ) {
03382 pAction = it.current();
03383 break;
03384 }
03385 }
03386 }
03387
03388 if( !pAction ) {
03389 for( uint i = 0; i < d->m_docList.count() && !pAction; i++ )
03390 pAction = d->m_docList[i]->action( name, classname );
03391 }
03392
03393 return pAction;
03394 }
03395
03396 KAction* KActionCollection::action( int index ) const
03397 {
03398 QAsciiDictIterator<KAction> it( d->m_actionDict );
03399 it += index;
03400 return it.current();
03401
03402 }
03403
03404 bool KActionCollection::readShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig )
03405 {
03406 return KActionShortcutList(this).readSettings( sConfigGroup, pConfig );
03407 }
03408
03409 bool KActionCollection::writeShortcutSettings( const QString& sConfigGroup, KConfigBase* pConfig ) const
03410 {
03411 return KActionShortcutList((KActionCollection*)this).writeSettings( sConfigGroup, pConfig );
03412 }
03413
03414 uint KActionCollection::count() const
03415 {
03416 return d->m_actionDict.count();
03417 }
03418
03419 QStringList KActionCollection::groups() const
03420 {
03421 QStringList lst;
03422
03423 QAsciiDictIterator<KAction> it( d->m_actionDict );
03424 for( ; it.current(); ++it )
03425 if ( !it.current()->group().isEmpty() && !lst.contains( it.current()->group() ) )
03426 lst.append( it.current()->group() );
03427
03428 return lst;
03429 }
03430
03431 KActionPtrList KActionCollection::actions( const QString& group ) const
03432 {
03433 KActionPtrList lst;
03434
03435 QAsciiDictIterator<KAction> it( d->m_actionDict );
03436 for( ; it.current(); ++it )
03437 if ( it.current()->group() == group )
03438 lst.append( it.current() );
03439 else if ( it.current()->group().isEmpty() && group.isEmpty() )
03440 lst.append( it.current() );
03441
03442 return lst;
03443 }
03444
03445 KActionPtrList KActionCollection::actions() const
03446 {
03447 KActionPtrList lst;
03448
03449 QAsciiDictIterator<KAction> it( d->m_actionDict );
03450 for( ; it.current(); ++it )
03451 lst.append( it.current() );
03452
03453 return lst;
03454 }
03455
03456 void KActionCollection::setInstance( KInstance *instance )
03457 {
03458 if ( instance )
03459 d->m_instance = instance;
03460 else
03461 d->m_instance = KGlobal::instance();
03462 }
03463
03464 KInstance *KActionCollection::instance() const
03465 {
03466 return d->m_instance;
03467 }
03468
03469 void KActionCollection::setXMLFile( const QString& sXMLFile )
03470 {
03471 d->m_sXMLFile = sXMLFile;
03472 }
03473
03474 const QString& KActionCollection::xmlFile() const
03475 {
03476 return d->m_sXMLFile;
03477 }
03478
03479 void KActionCollection::setHighlightingEnabled( bool enable )
03480 {
03481 d->m_highlight = enable;
03482 }
03483
03484 bool KActionCollection::highlightingEnabled() const
03485 {
03486 return d->m_highlight;
03487 }
03488
03489 void KActionCollection::connectHighlight( QWidget *container, KAction *action )
03490 {
03491 if ( !d->m_highlight )
03492 return;
03493
03494 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
03495
03496 if ( !actionList )
03497 {
03498 actionList = new QPtrList<KAction>;
03499
03500 if ( container->inherits( "QPopupMenu" ) )
03501 {
03502 connect( container, SIGNAL( highlighted( int ) ),
03503 this, SLOT( slotMenuItemHighlighted( int ) ) );
03504 connect( container, SIGNAL( aboutToHide() ),
03505 this, SLOT( slotMenuAboutToHide() ) );
03506 }
03507 else if ( container->inherits( "KToolBar" ) )
03508 {
03509 connect( container, SIGNAL( highlighted( int, bool ) ),
03510 this, SLOT( slotToolBarButtonHighlighted( int, bool ) ) );
03511 }
03512
03513 connect( container, SIGNAL( destroyed() ),
03514 this, SLOT( slotDestroyed() ) );
03515
03516 d->m_dctHighlightContainers.insert( container, actionList );
03517 }
03518
03519 actionList->append( action );
03520 }
03521
03522 void KActionCollection::disconnectHighlight( QWidget *container, KAction *action )
03523 {
03524 if ( !d->m_highlight )
03525 return;
03526
03527 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ container ];
03528
03529 if ( !actionList )
03530 return;
03531
03532 actionList->removeRef( action );
03533
03534 if ( actionList->count() == 0 )
03535 d->m_dctHighlightContainers.remove( container );
03536 }
03537
03538 void KActionCollection::slotMenuItemHighlighted( int id )
03539 {
03540 if ( !d->m_highlight )
03541 return;
03542
03543 if ( d->m_currentHighlightAction )
03544 emit actionHighlighted( d->m_currentHighlightAction, false );
03545
03546 QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
03547
03548 d->m_currentHighlightAction = findAction( container, id );
03549
03550 if ( !d->m_currentHighlightAction )
03551 {
03552 if ( !d->m_statusCleared )
03553 emit clearStatusText();
03554 d->m_statusCleared = true;
03555 return;
03556 }
03557
03558 d->m_statusCleared = false;
03559 emit actionHighlighted( d->m_currentHighlightAction );
03560 emit actionHighlighted( d->m_currentHighlightAction, true );
03561 emit actionStatusText( d->m_currentHighlightAction->toolTip() );
03562 }
03563
03564 void KActionCollection::slotMenuAboutToHide()
03565 {
03566 if ( d->m_currentHighlightAction )
03567 emit actionHighlighted( d->m_currentHighlightAction, false );
03568 d->m_currentHighlightAction = 0;
03569
03570 if ( !d->m_statusCleared )
03571 emit clearStatusText();
03572 d->m_statusCleared = true;
03573 }
03574
03575 void KActionCollection::slotToolBarButtonHighlighted( int id, bool highlight )
03576 {
03577 if ( !d->m_highlight )
03578 return;
03579
03580 QWidget *container = static_cast<QWidget *>( const_cast<QObject *>( sender() ) );
03581
03582 KAction *action = findAction( container, id );
03583
03584 if ( !action )
03585 {
03586 d->m_currentHighlightAction = 0;
03587
03588
03589 return;
03590 }
03591
03592 emit actionHighlighted( action, highlight );
03593
03594 if ( highlight )
03595 d->m_currentHighlightAction = action;
03596 else
03597 {
03598 d->m_currentHighlightAction = 0;
03599
03600 }
03601 }
03602
03603 void KActionCollection::slotDestroyed()
03604 {
03605 d->m_dctHighlightContainers.remove( reinterpret_cast<void *>( const_cast<QObject *>(sender()) ) );
03606 }
03607
03608 KAction *KActionCollection::findAction( QWidget *container, int id )
03609 {
03610 QPtrList<KAction> *actionList = d->m_dctHighlightContainers[ reinterpret_cast<void *>( container ) ];
03611
03612 if ( !actionList )
03613 return 0;
03614
03615 QPtrListIterator<KAction> it( *actionList );
03616 for (; it.current(); ++it )
03617 if ( it.current()->isPlugged( container, id ) )
03618 return it.current();
03619
03620 return 0;
03621 }
03622
03623 #if KDE_VERSION < KDE_MAKE_VERSION( 4,0,0 )
03624 KActionCollection KActionCollection::operator+(const KActionCollection &c ) const
03625 {
03626 kdWarning(129) << "KActionCollection::operator+(): function is severely deprecated." << endl;
03627 KActionCollection ret( *this );
03628
03629 QValueList<KAction *> actions = c.actions();
03630 QValueList<KAction *>::ConstIterator it = actions.begin();
03631 QValueList<KAction *>::ConstIterator end = actions.end();
03632 for (; it != end; ++it )
03633 ret.insert( *it );
03634
03635 return ret;
03636 }
03637
03638 KActionCollection &KActionCollection::operator=( const KActionCollection © )
03639 {
03640 kdWarning(129) << "KActionCollection::operator=(): function is severely deprecated." << endl;
03641
03642
03643
03644
03645 d->m_widget = copy.d->m_widget;
03646 d->m_kaccel = copy.d->m_kaccel;
03647 d->m_actionDict = copy.d->m_actionDict;
03648 setInstance( copy.instance() );
03649 return *this;
03650 }
03651
03652 KActionCollection &KActionCollection::operator+=( const KActionCollection &c )
03653 {
03654 kdWarning(129) << "KActionCollection::operator+=(): function is severely deprecated." << endl;
03655 QAsciiDictIterator<KAction> it(c.d->m_actionDict);
03656 for ( ; it.current(); ++it )
03657 insert( it.current() );
03658
03659 return *this;
03660 }
03661 #endif
03662
03663
03664
03665
03666
03667 KActionShortcutList::KActionShortcutList( KActionCollection* pColl )
03668 : m_actions( *pColl )
03669 { }
03670 KActionShortcutList::~KActionShortcutList()
03671 { }
03672 uint KActionShortcutList::count() const
03673 { return m_actions.count(); }
03674 QString KActionShortcutList::name( uint i ) const
03675 { return m_actions.action(i)->name(); }
03676 QString KActionShortcutList::label( uint i ) const
03677 { return m_actions.action(i)->text(); }
03678 QString KActionShortcutList::whatsThis( uint i ) const
03679 { return m_actions.action(i)->whatsThis(); }
03680 const KShortcut& KActionShortcutList::shortcut( uint i ) const
03681 { return m_actions.action(i)->shortcut(); }
03682 const KShortcut& KActionShortcutList::shortcutDefault( uint i ) const
03683 { return m_actions.action(i)->shortcutDefault(); }
03684 bool KActionShortcutList::isConfigurable( uint i ) const
03685 { return m_actions.action(i)->isShortcutConfigurable(); }
03686 bool KActionShortcutList::setShortcut( uint i, const KShortcut& cut )
03687 { return m_actions.action(i)->setShortcut( cut ); }
03688 const KInstance* KActionShortcutList::instance() const
03689 { return m_actions.instance(); }
03690 QVariant KActionShortcutList::getOther( Other, uint ) const
03691 { return QVariant(); }
03692 bool KActionShortcutList::setOther( Other, uint, QVariant )
03693 { return false; }
03694
03695 bool KActionShortcutList::save() const
03696 {
03697 kdDebug(129) << "KActionShortcutList::save(): xmlFile = " << m_actions.xmlFile() << endl;
03698
03699 if( m_actions.xmlFile().isEmpty() )
03700 return writeSettings();
03701
03702 QString tagActionProp = QString::fromLatin1("ActionProperties");
03703 QString tagAction = QString::fromLatin1("Action");
03704 QString attrName = QString::fromLatin1("name");
03705 QString attrShortcut = QString::fromLatin1("shortcut");
03706 QString attrAccel = QString::fromLatin1("accel");
03707
03708
03709 QString sXml( KXMLGUIFactory::readConfigFile( m_actions.xmlFile(), false, instance() ) );
03710 QDomDocument doc;
03711 doc.setContent( sXml );
03712
03713
03714
03715
03716 QDomElement elem;
03717 QDomElement it = doc.documentElement();
03718
03719 it = it.firstChild().toElement();
03720 for( ; !it.isNull(); it = it.nextSibling().toElement() ) {
03721 if( it.tagName() == tagActionProp ) {
03722 elem = it;
03723 break;
03724 }
03725 }
03726
03727
03728 if( elem.isNull() ) {
03729 elem = doc.createElement( tagActionProp );
03730 doc.documentElement().appendChild( elem );
03731 }
03732
03733
03734 uint nSize = count();
03735 for( uint i = 0; i < nSize; i++ ) {
03736 const QString& sName = name(i);
03737
03738 bool bSameAsDefault = (shortcut(i) == shortcutDefault(i));
03739
03740
03741
03742 QDomElement act_elem;
03743 for( it = elem.firstChild().toElement(); !it.isNull(); it = it.nextSibling().toElement() ) {
03744 if( it.attribute( attrName ) == sName ) {
03745 act_elem = it;
03746 break;
03747 }
03748 }
03749
03750
03751 if( act_elem.isNull() ) {
03752 if( bSameAsDefault )
03753 continue;
03754
03755 act_elem = doc.createElement( tagAction );
03756 act_elem.setAttribute( attrName, sName );
03757 }
03758
03759 act_elem.removeAttribute( attrAccel );
03760 if( bSameAsDefault ) {
03761 act_elem.removeAttribute( attrShortcut );
03762
03763 if( act_elem.attributes().count() == 1 )
03764 elem.removeChild( act_elem );
03765 } else {
03766 act_elem.setAttribute( attrShortcut, shortcut(i).toStringInternal() );
03767 elem.appendChild( act_elem );
03768 }
03769 }
03770
03771
03772 return KXMLGUIFactory::saveConfigFile( doc, m_actions.xmlFile(), instance() );
03773 }
03774
03775
03776
03777
03778
03779 KActionPtrShortcutList::KActionPtrShortcutList( KActionPtrList& list )
03780 : m_actions( list )
03781 { }
03782 KActionPtrShortcutList::~KActionPtrShortcutList()
03783 { }
03784 uint KActionPtrShortcutList::count() const
03785 { return m_actions.count(); }
03786 QString KActionPtrShortcutList::name( uint i ) const
03787 { return m_actions[i]->name(); }
03788 QString KActionPtrShortcutList::label( uint i ) const
03789 { return m_actions[i]->text(); }
03790 QString KActionPtrShortcutList::whatsThis( uint i ) const
03791 { return m_actions[i]->whatsThis(); }
03792 const KShortcut& KActionPtrShortcutList::shortcut( uint i ) const
03793 { return m_actions[i]->shortcut(); }
03794 const KShortcut& KActionPtrShortcutList::shortcutDefault( uint i ) const
03795 { return m_actions[i]->shortcutDefault(); }
03796 bool KActionPtrShortcutList::isConfigurable( uint i ) const
03797 { return m_actions[i]->isShortcutConfigurable(); }
03798 bool KActionPtrShortcutList::setShortcut( uint i, const KShortcut& cut )
03799 { return m_actions[i]->setShortcut( cut ); }
03800 QVariant KActionPtrShortcutList::getOther( Other, uint ) const
03801 { return QVariant(); }
03802 bool KActionPtrShortcutList::setOther( Other, uint, QVariant )
03803 { return false; }
03804 bool KActionPtrShortcutList::save() const
03805 { return false; }
03806
03807 void KActionShortcutList::virtual_hook( int id, void* data )
03808 { KShortcutList::virtual_hook( id, data ); }
03809
03810 void KActionPtrShortcutList::virtual_hook( int id, void* data )
03811 { KShortcutList::virtual_hook( id, data ); }
03812
03813 void KAction::virtual_hook( int, void* )
03814 { }
03815
03816 void KToggleAction::virtual_hook( int id, void* data )
03817 { KAction::virtual_hook( id, data ); }
03818
03819 void KRadioAction::virtual_hook( int id, void* data )
03820 { KToggleAction::virtual_hook( id, data ); }
03821
03822 void KSelectAction::virtual_hook( int id, void* data )
03823 { KAction::virtual_hook( id, data ); }
03824
03825 void KListAction::virtual_hook( int id, void* data )
03826 { KSelectAction::virtual_hook( id, data ); }
03827
03828 void KRecentFilesAction::virtual_hook( int id, void* data )
03829 { KListAction::virtual_hook( id, data ); }
03830
03831 void KFontAction::virtual_hook( int id, void* data )
03832 { KSelectAction::virtual_hook( id, data ); }
03833
03834 void KFontSizeAction::virtual_hook( int id, void* data )
03835 { KSelectAction::virtual_hook( id, data ); }
03836
03837 void KActionMenu::virtual_hook( int id, void* data )
03838 { KAction::virtual_hook( id, data ); }
03839
03840 void KToolBarPopupAction::virtual_hook( int id, void* data )
03841 { KAction::virtual_hook( id, data ); }
03842
03843 void KToggleToolBarAction::virtual_hook( int id, void* data )
03844 { KToggleAction::virtual_hook( id, data ); }
03845
03846 void KWidgetAction::virtual_hook( int id, void* data )
03847 { KAction::virtual_hook( id, data ); }
03848
03849 void KActionSeparator::virtual_hook( int id, void* data )
03850 { KAction::virtual_hook( id, data ); }
03851
03852 void KActionCollection::virtual_hook( int, void* )
03853 { }
03854
03855
03856
03857
03858 #include "kaction.moc"