summaryrefslogtreecommitdiffabout
path: root/microkde/kdeui/kaction.cpp
Unidiff
Diffstat (limited to 'microkde/kdeui/kaction.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdeui/kaction.cpp1215
1 files changed, 1215 insertions, 0 deletions
diff --git a/microkde/kdeui/kaction.cpp b/microkde/kdeui/kaction.cpp
new file mode 100644
index 0000000..77d36a5
--- a/dev/null
+++ b/microkde/kdeui/kaction.cpp
@@ -0,0 +1,1215 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
3 (C) 1999 Simon Hausmann <hausmann@kde.org>
4 (C) 2000 Nicolas Hadacek <haadcek@kde.org>
5 (C) 2000 Kurt Granroth <granroth@kde.org>
6 (C) 2000 Michael Koch <koch@kde.org>
7 (C) 2001 Holger Freyther <freyther@kde.org>
8 (C) 2002 Ellis Whitehead <ellis@kde.org>
9 (C) 2002 Joseph Wenninger <jowenn@kde.org>
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public
13 License version 2 as published by the Free Software Foundation.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU Library General Public License
21 along with this library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
24*/
25
26#include "kaction.h"
27
28#include <assert.h>
29
30#include <qtooltip.h>
31//US#include <qwhatsthis.h>
32//US#include <kaccel.h>
33//US#include <kaccelbase.h>
34#include <kapplication.h>
35#include <kdebug.h>
36#include <kguiitem.h>
37//US#include <kmainwindow.h>
38//US#include <kmenubar.h>
39//US#include <kpopupmenu.h>
40#include <ktoolbar.h>
41#include <ktoolbarbutton.h>
42
43//US added this includefiles
44#include <qmenubar.h>
45#include <qtoolbar.h>
46#include <qpopupmenu.h>
47#include <qiconset.h>
48
49/**
50* How it works.
51* KActionCollection is an organizing container for KActions.
52* KActionCollection keeps track of the information necessary to handle
53* configuration and shortcuts.
54*
55* Focus Widget pointer:
56* This is the widget which is the focus for action shortcuts.
57* It is set either by passing a QWidget* to the KActionCollection constructor
58* or by calling setWidget() if the widget wasn't known when the object was
59* initially constructed (as in KXMLGUIClient and KParts::PartBase)
60*
61* Shortcuts:
62* An action's shortcut will not not be connected unless a focus widget has
63* been specified in KActionCollection.
64*
65* XML Filename:
66* This is used to save user-modified settings back to the *ui.rc file.
67* It is set by KXMLGUIFactory.
68*/
69
70int KAction::getToolButtonID()
71{
72 static int toolbutton_no = -2;
73 return toolbutton_no--;
74}
75
76//---------------------------------------------------------------------
77// KAction::KActionPrivate
78//---------------------------------------------------------------------
79
80class KAction::KActionPrivate : public KGuiItem
81{
82public:
83 KActionPrivate() : KGuiItem()
84 {
85 m_kaccel = 0;
86 m_configurable = true;
87 }
88
89 KAccel *m_kaccel;
90 QValueList<KAccel*> m_kaccelList;
91
92 QString m_groupText;
93 QString m_group;
94
95 KShortcut m_cut;
96 KShortcut m_cutDefault;
97
98 bool m_configurable;
99
100 struct Container
101 {
102 Container() { m_container = 0; m_representative = 0; m_id = 0; }
103 Container( const Container& s ) { m_container = s.m_container;
104 m_id = s.m_id; m_representative = s.m_representative; }
105 QWidget* m_container;
106 int m_id;
107 QWidget* m_representative;
108 };
109
110 QValueList<Container> m_containers;
111};
112
113//---------------------------------------------------------------------
114// KAction
115//---------------------------------------------------------------------
116KAction::KAction( const QString& text, const KShortcut& cut,
117 const QObject* receiver, const char* slot,
118 KActionCollection* parent, const char* name )
119: QObject( parent, name )
120{
121 initPrivate( text, cut, receiver, slot );
122}
123
124KAction::KAction( const QString& text, const QString& sIconName, const KShortcut& cut,
125 const QObject* receiver, const char* slot,
126 KActionCollection* parent, const char* name )
127: QObject( parent, name )
128{
129 initPrivate( text, cut, receiver, slot );
130 d->setIconName( sIconName );
131}
132
133KAction::KAction( const QString& text, const QIconSet& pix, const KShortcut& cut,
134 const QObject* receiver, const char* slot,
135 KActionCollection* parent, const char* name )
136: QObject( parent, name )
137{
138 initPrivate( text, cut, receiver, slot );
139 d->setIconSet( pix );
140}
141KAction::KAction( const KGuiItem& item, const KShortcut& cut,
142 const QObject* receiver, const char* slot,
143 KActionCollection* parent, const char* name )
144: QObject( parent, name )
145{
146 initPrivate( item.text(), cut, receiver, slot );
147 if( item.hasIconSet() )
148 setIcon( item.iconName() );
149 setToolTip( item.toolTip() );
150 setWhatsThis( item.whatsThis() );
151}
152
153// KDE 4: remove
154KAction::KAction( const QString& text, const KShortcut& cut,
155 QObject* parent, const char* name )
156 : QObject( parent, name )
157{
158 initPrivate( text, cut, 0, 0 );
159}
160KAction::KAction( const QString& text, const KShortcut& cut,
161 const QObject* receiver,
162 const char* slot, QObject* parent, const char* name )
163 : QObject( parent, name )
164{
165 initPrivate( text, cut, receiver, slot );
166}
167KAction::KAction( const QString& text, const QIconSet& pix,
168 const KShortcut& cut,
169 QObject* parent, const char* name )
170 : QObject( parent, name )
171{
172 initPrivate( text, cut, 0, 0 );
173 setIconSet( pix );
174}
175
176KAction::KAction( const QString& text, const QString& pix,
177 const KShortcut& cut,
178 QObject* parent, const char* name )
179: QObject( parent, name )
180{
181 initPrivate( text, cut, 0, 0 );
182 d->setIconName( pix );
183}
184
185KAction::KAction( const QString& text, const QIconSet& pix,
186 const KShortcut& cut,
187 const QObject* receiver, const char* slot, QObject* parent,
188 const char* name )
189 : QObject( parent, name )
190{
191 initPrivate( text, cut, receiver, slot );
192 setIconSet( pix );
193}
194
195KAction::KAction( const QString& text, const QString& pix,
196 const KShortcut& cut,
197 const QObject* receiver, const char* slot, QObject* parent,
198 const char* name )
199 : QObject( parent, name )
200{
201 initPrivate( text, cut, receiver, slot );
202 d->setIconName(pix);
203}
204
205KAction::KAction( QObject* parent, const char* name )
206 : QObject( parent, name )
207{
208
209 initPrivate( QString::null, KShortcut(), 0, 0 );
210}
211// KDE 4: remove end
212
213KAction::~KAction()
214{
215 kdDebug(129) << "KAction::~KAction( this = \"" << name() << "\" )" << endl; // -- ellis
216#ifndef KDE_NO_COMPAT
217 if (d->m_kaccel)
218 unplugAccel();
219#endif
220
221 // If actionCollection hasn't already been destructed,
222 if ( m_parentCollection ) {
223 m_parentCollection->take( this );
224 for( uint i = 0; i < d->m_kaccelList.count(); i++ )
225//US d->m_kaccelList[i]->remove( name() );
226 qDebug("KAction::KAction~ ...1 has top be fixed");
227 }
228
229 // Do not call unplugAll from here, as tempting as it sounds.
230 // KAction is designed around the idea that you need to plug
231 // _and_ to unplug it "manually". Unplugging leads to an important
232 // slowdown when e.g. closing the window, in which case we simply
233 // want to destroy everything asap, not to remove actions one by one
234 // from the GUI.
235
236 delete d; d = 0;
237}
238
239void KAction::initPrivate( const QString& text, const KShortcut& cut,
240 const QObject* receiver, const char* slot )
241{
242 d = new KActionPrivate;
243
244 d->m_cutDefault = cut;
245
246//US m_parentCollection = dynamic_cast<KActionCollection *>( parent() );
247 m_parentCollection = (KActionCollection *)( parent() );
248 kdDebug(129) << "KAction::initPrivate(): this = " << this << " name = \"" << name() << "\" cut = " << cut.toStringInternal() << " m_parentCollection = " << m_parentCollection << endl;
249 if ( m_parentCollection )
250 m_parentCollection->insert( this );
251
252 if ( receiver && slot )
253 connect( this, SIGNAL( activated() ), receiver, slot );
254
255 if( !cut.isNull() && qstrcmp( name(), "unnamed" ) == 0 )
256 kdWarning(129) << "KAction::initPrivate(): trying to assign a shortcut (" << cut.toStringInternal() << ") to an unnamed action." << endl;
257 d->setText( text );
258 initShortcut( cut );
259
260}
261
262bool KAction::isPlugged() const
263{
264 return (containerCount() > 0) || d->m_kaccel;
265}
266
267bool KAction::isPlugged( const QWidget *container ) const
268{
269 return findContainer( container ) > -1;
270}
271
272bool KAction::isPlugged( const QWidget *container, int id ) const
273{
274 int i = findContainer( container );
275 return ( i > -1 && itemId( i ) == id );
276}
277
278bool KAction::isPlugged( const QWidget *container, const QWidget *_representative ) const
279{
280 int i = findContainer( container );
281 return ( i > -1 && representative( i ) == _representative );
282}
283
284
285/*
286Three actionCollection conditions:
287 1) Scope is known on creation and KAccel object is created (e.g. KMainWindow)
288 2) Scope is unknown and no KAccel object is available (e.g. KXMLGUIClient)
289 a) addClient() will be called on object
290 b) we just want to add the actions to another KXMLGUIClient object
291
292The question is how to do we incorporate #2b into the XMLGUI framework?
293
294
295We have a KCommandHistory object with undo and redo actions in a passed actionCollection
296We have a KoDoc object which holds a KCommandHistory object and the actionCollection
297We have two KoView objects which both point to the same KoDoc object
298Undo and Redo should be available in both KoView objects, and
299 calling the undo->setEnabled() should affect both KoViews
300
301When addClient is called, it needs to be able to find the undo and redo actions
302When it calls plug() on them, they need to be inserted into the KAccel object of the appropriate KoView
303
304In this case, the actionCollection belongs to KoDoc and we need to let it know that its shortcuts
305have the same scope as the KoView actionCollection
306
307KXMLGUIClient::addSubActionCollection
308
309Document:
310 create document actions
311
312View
313 create view actions
314 add document actionCollection as sub-collection
315
316A parentCollection is created
317Scenario 1: parentCollection has a focus widget set (e.g. via KMainWindow)
318 A KAccel object is created in the parentCollection
319 A KAction is created with parent=parentCollection
320 The shortcut is inserted into this actionCollection
321 Scenario 1a: xml isn't used
322 done
323 Scenario 1b: KXMLGUIBuilder::addClient() called
324 setWidget is called -- ignore
325 shortcuts are set
326Scenario 2: parentCollection has no focus widget (e.g., KParts)
327 A KAction is created with parent=parentCollection
328 Scenario 2a: xml isn't used
329 no shortcuts
330 Scenario 2b: KXMLGUIBuilder::addClient() called
331 setWidget is called
332 shortcuts are inserted into current KAccel
333 shortcuts are set in all other KAccels, if the action is present in the other KAccels
334*/
335
336/*
337shortcut may be set:
338 - on construction
339 - on plug
340 - on reading XML
341 - on plugAccel (deprecated)
342
343On Construction: [via initShortcut()]
344 insert into KAccel of m_parentCollection,
345 if kaccel() && isAutoConnectShortcuts() exists
346
347On Plug: [via plug() -> plugShortcut()]
348 insert into KAccel of m_parentCollection, if exists and not already inserted into
349
350On Read XML: [via setShortcut()]
351 set in all current KAccels
352 insert into KAccel of m_parentCollection, if exists and not already inserted into
353*/
354
355KAccel* KAction::kaccelCurrent()
356{
357 if( m_parentCollection && m_parentCollection->builderKAccel() )
358 return m_parentCollection->builderKAccel();
359 else if( m_parentCollection && m_parentCollection->kaccel() )
360 return m_parentCollection->kaccel();
361 else
362 return 0L;
363}
364
365// Only to be called from initPrivate()
366bool KAction::initShortcut( const KShortcut& cut )
367{
368 d->m_cut = cut;
369
370 // Only insert action into KAccel if it has a valid name,
371 if( qstrcmp( name(), "unnamed" ) != 0 &&
372 m_parentCollection &&
373 m_parentCollection->isAutoConnectShortcuts() &&
374 m_parentCollection->kaccel() )
375 {
376 insertKAccel( m_parentCollection->kaccel() );
377 return true;
378 }
379 return false;
380 }
381
382// Only to be called from plug()
383void KAction::plugShortcut()
384{
385 KAccel* kaccel = kaccelCurrent();
386
387 //kdDebug(129) << "KAction::plugShortcut(): this = " << this << " kaccel() = " << (m_parentCollection ? m_parentCollection->kaccel() : 0) << endl;
388 if( kaccel && qstrcmp( name(), "unnamed" ) != 0 ) {
389 // Check if already plugged into current KAccel object
390 for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
391 if( d->m_kaccelList[i] == kaccel )
392 return;
393 }
394
395 insertKAccel( kaccel );
396 }
397}
398
399bool KAction::setShortcut( const KShortcut& cut )
400{
401 qDebug("KAction::setShortcut~ ...1 has top be fixed");
402/*US
403 bool bChanged = (d->m_cut != cut);
404 d->m_cut = cut;
405
406 KAccel* kaccel = kaccelCurrent();
407 bool bInsertRequired = true;
408 // Apply new shortcut to all existing KAccel objects
409 for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
410 // Check whether shortcut has already been plugged into
411 // the current kaccel object.
412 if( d->m_kaccelList[i] == kaccel )
413 bInsertRequired = false;
414 if( bChanged )
415 updateKAccelShortcut( d->m_kaccelList[i] );
416 }
417
418 // Only insert action into KAccel if it has a valid name,
419 if( kaccel && bInsertRequired && qstrcmp( name(), "unnamed" ) )
420 insertKAccel( kaccel );
421
422 if( bChanged ) {
423 // KDE 4: remove
424 if ( d->m_kaccel )
425 d->m_kaccel->setShortcut( name(), cut );
426 // KDE 4: remove end
427 int len = containerCount();
428 for( int i = 0; i < len; ++i )
429 updateShortcut( i );
430 }
431*/
432
433 return true;
434}
435
436bool KAction::updateKAccelShortcut( KAccel* kaccel )
437{
438 qDebug("KAction::updateKAccelShortcut~ ...1 has top be fixed");
439 // Check if action is permitted
440/*US
441 if (kapp && !kapp->authorizeKAction(name()))
442 return false;
443
444 bool b = true;
445
446 if ( !kaccel->actions().actionPtr( name() ) ) {
447 if(!d->m_cut.isNull() ) {
448 kdDebug(129) << "Inserting " << name() << ", " << d->text() << ", " << d->plainText() << endl;
449 b = kaccel->insert( name(), d->plainText(), QString::null,
450 d->m_cut,
451 this, SLOT(slotActivated()),
452 isShortcutConfigurable(), isEnabled() );
453 }
454 }
455 else
456 b = kaccel->setShortcut( name(), d->m_cut );
457
458 return b;
459*/
460 return true;
461}
462
463void KAction::insertKAccel( KAccel* kaccel )
464{
465 qDebug("KAction::updateKAccelShortcut~ ...1 has top be fixed");
466/*US
467 //kdDebug(129) << "KAction::insertKAccel( " << kaccel << " ): this = " << this << endl;
468 if ( !kaccel->actions().actionPtr( name() ) ) {
469 if( updateKAccelShortcut( kaccel ) ) {
470 d->m_kaccelList.append( kaccel );
471 connect( kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
472 }
473 }
474 else
475 kdWarning(129) << "KAction::insertKAccel( kaccel = " << kaccel << " ): KAccel object already contains an action name \"" << name() << "\"" << endl; // -- ellis
476*/
477}
478
479void KAction::removeKAccel( KAccel* kaccel )
480{
481 qDebug("KAction::removeKAccel~ ...1 has top be fixed");
482/*US
483 //kdDebug(129) << "KAction::removeKAccel( " << i << " ): this = " << this << endl;
484 for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
485 if( d->m_kaccelList[i] == kaccel ) {
486 kaccel->remove( name() );
487 d->m_kaccelList.remove( d->m_kaccelList.at( i ) );
488 disconnect( kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
489 break;
490 }
491 }
492*/
493}
494
495// KDE 4: remove
496void KAction::setAccel( int keyQt )
497{
498 setShortcut( KShortcut(keyQt) );
499}
500// KDE 4: remove end
501
502void KAction::updateShortcut( int i )
503{
504 int id = itemId( i );
505
506 QWidget* w = container( i );
507 if ( w->inherits( "QPopupMenu" ) ) {
508 QPopupMenu* menu = static_cast<QPopupMenu*>(w);
509 updateShortcut( menu, id );
510 }
511 else if ( w->inherits( "QMenuBar" ) )
512//US static_cast<QMenuBar*>(w)->setAccel( d->m_cut.keyCodeQt(), id );
513//US (QMenuBar*)(w)->setAccel( d->m_cut.keyCodeQt(), id );
514 qDebug("KAction::updateShortcut( int i ) ...1 has top be fixed");
515
516}
517
518void KAction::updateShortcut( QPopupMenu* menu, int id )
519{
520/*US
521 //kdDebug(129) << "KAction::updateShortcut(): this = " << this << " d->m_kaccelList.count() = " << d->m_kaccelList.count() << endl;
522 // If the action has a KAccel object,
523 // show the string representation of its shortcut.
524 if ( d->m_kaccel || d->m_kaccelList.count() ) {
525 QString s = menu->text( id );
526 int i = s.find( '\t' );
527 if ( i >= 0 )
528 s.replace( i+1, s.length()-i, d->m_cut.seq(0).toString() );
529 else
530 s += "\t" + d->m_cut.seq(0).toString();
531
532 menu->changeItem( id, s );
533 }
534 // Otherwise insert the shortcut itself into the popup menu.
535 else {
536 // This is a fall-hack in case the KAction is missing a proper parent collection.
537 // It should be removed eventually. --ellis
538 menu->setAccel( d->m_cut.keyCodeQt(), id );
539 kdWarning(129) << "KAction::updateShortcut(): name = \"" << name() << "\", cut = " << d->m_cut.toStringInternal() << "; No KAccel, probably missing a parent collection." << endl;
540 }
541*/
542 qDebug("KAction::updateShortcut( QPopupMenu* menu, int id ) ...1 has top be fixed");
543
544}
545
546const KShortcut& KAction::shortcut() const
547{
548 return d->m_cut;
549}
550
551const KShortcut& KAction::shortcutDefault() const
552{
553 return d->m_cutDefault;
554}
555
556QString KAction::shortcutText() const
557{
558 return d->m_cut.toStringInternal();
559}
560
561void KAction::setShortcutText( const QString& s )
562{
563 setShortcut( KShortcut(s) );
564}
565
566int KAction::accel() const
567{
568 qDebug("KAction::accel() ...1 has top be fixed");
569//US return d->m_cut.keyCodeQt();
570 return 0;
571}
572
573void KAction::setGroup( const QString& grp )
574{
575 d->m_group = grp;
576
577 int len = containerCount();
578 for( int i = 0; i < len; ++i )
579 updateGroup( i );
580}
581
582void KAction::updateGroup( int )
583{
584 // DO SOMETHING
585}
586
587QString KAction::group() const
588{
589 return d->m_group;
590}
591
592bool KAction::isEnabled() const
593{
594 return d->isEnabled();
595}
596
597bool KAction::isShortcutConfigurable() const
598{
599 return d->m_configurable;
600}
601
602void KAction::setToolTip( const QString& tt )
603{
604 qDebug("KAction::setToolTip ...1 has top be fixed");
605 d->setToolTip( tt );
606
607 int len = containerCount();
608 for( int i = 0; i < len; ++i )
609 updateToolTip( i );
610}
611
612void KAction::updateToolTip( int i )
613{
614 qDebug("KAction::updateToolTip ...1 has top be fixed");
615 QWidget *w = container( i );
616
617 if ( w->inherits( "KToolBar" ) )
618 QToolTip::add( static_cast<KToolBar*>(w)->getWidget( itemId( i ) ), d->toolTip() );
619 else if ( w->inherits( "QToolBar" ) )
620 QToolTip::add( static_cast<KToolBar*>(w)->getWidget( itemId( i ) ), d->toolTip() );
621}
622
623QString KAction::toolTip() const
624{
625 return d->toolTip();
626}
627
628int KAction::plug( QWidget *w, int index )
629{
630 //kdDebug(129) << "KAction::plug( " << w << ", " << index << " )" << endl;
631 if (w == 0) {
632 kdWarning(129) << "KAction::plug called with 0 argument\n";
633 return -1;
634 }
635
636#ifndef NDEBUG
637 KAccel* kaccel = kaccelCurrent();
638 // If there is a shortcut, but no KAccel available
639 if( !d->m_cut.isNull() && kaccel == 0 ) {
640 kdWarning(129) << "KAction::plug(): has no KAccel object; this = " << this << " name = " << name() << " parentCollection = " << m_parentCollection << endl; // ellis
641//US kdDebug(129) << kdBacktrace() << endl;
642 }
643#endif
644
645 // Check if action is permitted
646//US if (kapp && !kapp->authorizeKAction(name()))
647//US return -1;
648
649 plugShortcut();
650
651 if ( w->inherits("QPopupMenu") )
652 {
653 QPopupMenu* menu = static_cast<QPopupMenu*>( w );
654 int id;
655 // Don't insert shortcut into menu if it's already in a KAccel object.
656 //qDebug("KAction::plug warning: real shortcuts not available yet. ");
657//US int keyQt = (d->m_kaccelList.count() || d->m_kaccel) ? 0 : d->m_cut.keyCodeQt();
658 int keyQt = 0;
659
660 if ( d->hasIcon() )
661 {
662/*US
663 KInstance *instance;
664 if ( m_parentCollection )
665 instance = m_parentCollection->instance();
666 else
667 instance = KGlobal::instance();
668*/
669 id = menu->insertItem( d->iconSet( KIcon::Small, 0/*US , instance */), d->text(), this,//dsweet
670 SLOT( slotActivated() ), keyQt,
671 -1, index );
672 }
673 else
674 id = menu->insertItem( d->text(), this,
675 SLOT( slotActivated() ), //dsweet
676 keyQt, -1, index );
677
678 // If the shortcut is already in a KAccel object, then
679 // we need to set the menu item's shortcut text.
680/*US if ( d->m_kaccelList.count() || d->m_kaccel )
681 updateShortcut( menu, id );
682*/
683 // call setItemEnabled only if the item really should be disabled,
684 // because that method is slow and the item is per default enabled
685 if ( !d->isEnabled() )
686 menu->setItemEnabled( id, false );
687
688 if ( !d->whatsThis().isEmpty() )
689 menu->setWhatsThis( id, whatsThisWithIcon() );
690
691 addContainer( menu, id );
692 connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
693
694 if ( m_parentCollection )
695 m_parentCollection->connectHighlight( menu, this );
696
697 return d->m_containers.count() - 1;
698 }
699 else if ( w->inherits( "KToolBar" ) )
700 {
701 KToolBar *bar = static_cast<KToolBar *>( w );
702
703 int id_ = getToolButtonID();
704/*US
705 KInstance *instance;
706 if ( m_parentCollection )
707 instance = m_parentCollection->instance();
708 else
709 instance = KGlobal::instance();
710*/
711 if ( icon().isEmpty() && !iconSet().pixmap().isNull() ) // old code using QIconSet directly
712 {
713 bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
714 SLOT( slotActivated() ),
715 d->isEnabled(), d->plainText(), index );
716 }
717 else
718 {
719 QString icon = d->iconName();
720 if ( icon.isEmpty() )
721 icon = "unknown";
722 bar->insertButton( icon, id_, SIGNAL( clicked() ), this,
723 SLOT( slotActivated() ),
724 d->isEnabled(), d->plainText(), index/*US, instance*/ );
725 }
726 bar->getButton( id_ )->setName( QCString("toolbutton_")+name() );
727
728//US if ( !d->whatsThis().isEmpty() )
729//US QWhatsThis::add( bar->getButton(id_), whatsThisWithIcon() );
730 if ( !d->toolTip().isEmpty() )
731 QToolTip::add( bar->getButton(id_), d->toolTip() );
732
733 addContainer( bar, id_ );
734
735 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
736
737 if ( m_parentCollection )
738 m_parentCollection->connectHighlight( bar, this );
739
740 return containerCount() - 1;
741
742 }
743
744 return -1;
745}
746
747void KAction::unplug( QWidget *w )
748{
749 int i = findContainer( w );
750 if ( i == -1 )
751 return;
752 int id = itemId( i );
753
754 if ( w->inherits( "QPopupMenu" ) )
755 {
756 QPopupMenu *menu = static_cast<QPopupMenu *>( w );
757 menu->removeItem( id );
758 }
759 else if ( w->inherits( "KToolBar" ) )
760 {
761 KToolBar *bar = static_cast<KToolBar *>( w );
762 bar->removeItemDelayed( id );
763 }
764 else if ( w->inherits( "QMenuBar" ) )
765 {
766 QMenuBar *bar = static_cast<QMenuBar *>( w );
767 bar->removeItem( id );
768 }
769
770 removeContainer( i );
771
772 if ( m_parentCollection )
773 m_parentCollection->disconnectHighlight( w, this );
774}
775
776void KAction::plugAccel(KAccel *kacc, bool configurable)
777{
778 qDebug("KAction::plugAccel ...1 has top be fixed");
779/*US
780 kdWarning(129) << "KAction::plugAccel(): call to deprecated action." << endl;
781 kdDebug(129) << kdBacktrace() << endl;
782 //kdDebug(129) << "KAction::plugAccel( kacc = " << kacc << " ): name \"" << name() << "\"" << endl;
783 if ( d->m_kaccel )
784 unplugAccel();
785
786 // If the parent collection's accel ptr isn't set yet
787 //if ( m_parentCollection && !m_parentCollection->accel() )
788 // m_parentCollection->setAccel( kacc );
789
790 // We can only plug this action into the given KAccel object
791 // if it does not already contain an action with the same name.
792 if ( !kacc->actions().actionPtr(name()) )
793 {
794 d->m_kaccel = kacc;
795 d->m_kaccel->insert(name(), d->plainText(), QString::null,
796 KShortcut(d->m_cut),
797 this, SLOT(slotActivated()),
798 configurable, isEnabled());
799 connect(d->m_kaccel, SIGNAL(destroyed()), this, SLOT(slotDestroyed()));
800 //connect(d->m_kaccel, SIGNAL(keycodeChanged()), this, SLOT(slotKeycodeChanged()));
801 }
802 else
803 kdWarning(129) << "KAction::plugAccel( kacc = " << kacc << " ): KAccel object already contains an action name \"" << name() << "\"" << endl; // -- ellis
804*/
805}
806
807void KAction::unplugAccel()
808{
809 qDebug("KAction::unplugAccel ...1 has top be fixed");
810/*US
811 //kdDebug(129) << "KAction::unplugAccel() " << this << " " << name() << endl;
812 if ( d->m_kaccel )
813 {
814 d->m_kaccel->remove(name());
815 d->m_kaccel = 0;
816 }
817*/
818}
819
820void KAction::plugMainWindowAccel( QWidget *w )
821{
822 qDebug("KAction::plugMainWindowAccel ...1 has top be fixed");
823/*US
824 // Note: topLevelWidget() stops too early, we can't use it.
825 QWidget * tl = w;
826 QWidget * n;
827 while ( !tl->isDialog() && ( n = tl->parentWidget() ) ) // lookup parent and store
828 tl = n;
829
830 KMainWindow * mw = dynamic_cast<KMainWindow *>(tl); // try to see if it's a kmainwindow
831 if (mw)
832 plugAccel( mw->accel() );
833 else
834 kdDebug(129) << "KAction::plugMainWindowAccel: Toplevel widget isn't a KMainWindow, can't plug accel. " << tl << endl;
835*/
836}
837
838void KAction::setEnabled(bool enable)
839{
840 //kdDebug(129) << "KAction::setEnabled( " << enable << " ): this = " << this << " d->m_kaccelList.count() = " << d->m_kaccelList.count() << endl;
841 if ( enable == d->isEnabled() )
842 return;
843
844 // KDE 4: remove
845//US if (d->m_kaccel)
846//US d->m_kaccel->setEnabled(name(), enable);
847 // KDE 4: remove end
848
849//US for ( uint i = 0; i < d->m_kaccelList.count(); i++ )
850//US d->m_kaccelList[i]->setEnabled( name(), enable );
851
852 d->setEnabled( enable );
853
854 int len = containerCount();
855 for( int i = 0; i < len; ++i )
856 updateEnabled( i );
857
858 emit enabled( d->isEnabled() );
859}
860
861void KAction::updateEnabled( int i )
862{
863 QWidget *w = container( i );
864
865 if ( w->inherits("QPopupMenu") )
866 static_cast<QPopupMenu*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
867 else if ( w->inherits("QMenuBar") )
868 static_cast<QMenuBar*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
869 else if ( w->inherits( "KToolBar" ) )
870 {
871 static_cast<KToolBar*>(w)->setItemEnabled( itemId( i ), d->isEnabled() );
872 }
873}
874
875void KAction::setShortcutConfigurable( bool b )
876{
877 d->m_configurable = b;
878}
879
880void KAction::setText( const QString& text )
881{
882/*US
883 // KDE 4: remove
884 if (d->m_kaccel) {
885 KAccelAction* pAction = d->m_kaccel->actions().actionPtr(name());
886 if (pAction)
887 pAction->setLabel( text );
888 }
889 // KDE 4: remove end
890
891 for( uint i = 0; i < d->m_kaccelList.count(); i++ ) {
892 KAccelAction* pAction = d->m_kaccelList[i]->actions().actionPtr(name());
893 if (pAction)
894 pAction->setLabel( text );
895 }
896*/
897 d->setText( text );
898
899 int len = containerCount();
900 for( int i = 0; i < len; ++i )
901 updateText( i );
902
903}
904
905void KAction::updateText( int i )
906{
907 QWidget *w = container( i );
908
909 if ( w->inherits( "QPopupMenu" ) ) {
910 int id = itemId( i );
911 static_cast<QPopupMenu*>(w)->changeItem( id, d->text() );
912 updateShortcut( static_cast<QPopupMenu*>(w), id );
913 }
914 else if ( w->inherits( "QMenuBar" ) )
915 static_cast<QMenuBar*>(w)->changeItem( itemId( i ), d->text() );
916 else if ( w->inherits( "KToolBar" ) )
917 {
918 qDebug("KAction::updateText ...3 has top be fixed");
919 QWidget *button = static_cast<KToolBar *>(w)->getWidget( itemId( i ) );
920 if ( button->inherits( "KToolBarButton" ) )
921 static_cast<KToolBarButton *>(button)->setText( d->plainText() );
922
923 }
924}
925
926QString KAction::text() const
927{
928 return d->text();
929}
930
931QString KAction::plainText() const
932{
933 return d->plainText( );
934}
935
936void KAction::setIcon( const QString &icon )
937{
938 d->setIconName( icon );
939
940 // now handle any toolbars
941 int len = containerCount();
942 for ( int i = 0; i < len; ++i )
943 updateIcon( i );
944}
945
946void KAction::updateIcon( int id )
947{
948 QWidget* w = container( id );
949
950 if ( w->inherits( "QPopupMenu" ) ) {
951 int itemId_ = itemId( id );
952 static_cast<QPopupMenu*>(w)->changeItem( itemId_, d->iconSet( KIcon::Small ), d->text() );
953 updateShortcut( static_cast<QPopupMenu*>(w), itemId_ );
954 }
955 else if ( w->inherits( "QMenuBar" ) )
956 static_cast<QMenuBar*>(w)->changeItem( itemId( id ), d->iconSet( KIcon::Small ), d->text() );
957 else if ( w->inherits( "KToolBar" ) )
958 static_cast<KToolBar *>(w)->setButtonIcon( itemId( id ), d->iconName() );
959 else if ( w->inherits( "QToolBar" ) )
960 {
961 qDebug("KAction::updateIcon has top be fixed");
962//US static_cast<QToolBar *>(w)->setButtonIcon( itemId( id ), d->iconName() );
963 }
964}
965
966QString KAction::icon() const
967{
968 return d->iconName( );
969}
970
971void KAction::setIconSet( const QIconSet &iconset )
972{
973 d->setIconSet( iconset );
974
975 int len = containerCount();
976 for( int i = 0; i < len; ++i )
977 updateIconSet( i );
978}
979
980
981void KAction::updateIconSet( int id )
982{
983 QWidget *w = container( id );
984
985 if ( w->inherits( "QPopupMenu" ) )
986 {
987 int itemId_ = itemId( id );
988 static_cast<QPopupMenu*>(w)->changeItem( itemId_, d->iconSet(), d->text() );
989 updateShortcut( static_cast<QPopupMenu*>(w), itemId_ );
990 }
991 else if ( w->inherits( "QMenuBar" ) )
992 static_cast<QMenuBar*>(w)->changeItem( itemId( id ), d->iconSet(), d->text() );
993 else if ( w->inherits( "KToolBar" ) )
994 {
995 if ( icon().isEmpty() && d->hasIconSet() ) // only if there is no named icon ( scales better )
996 static_cast<KToolBar *>(w)->setButtonIconSet( itemId( id ), d->iconSet() );
997 else
998 static_cast<KToolBar *>(w)->setButtonIconSet( itemId( id ), d->iconSet( KIcon::Small ) );
999 }
1000}
1001
1002QIconSet KAction::iconSet( KIcon::Group group, int size ) const
1003{
1004 return d->iconSet( group, size );
1005}
1006
1007bool KAction::hasIcon() const
1008{
1009 return d->hasIcon();
1010}
1011
1012
1013void KAction::setWhatsThis( const QString& text )
1014{
1015 d->setWhatsThis( text );
1016
1017 int len = containerCount();
1018 for( int i = 0; i < len; ++i )
1019 updateWhatsThis( i );
1020}
1021
1022void KAction::updateWhatsThis( int i )
1023{
1024 qDebug("KAction::updateWhatsThis ...1 has top be fixed");
1025 QPopupMenu* pm = popupMenu( i );
1026 if ( pm )
1027 {
1028 pm->setWhatsThis( itemId( i ), d->whatsThis() );
1029 return;
1030 }
1031
1032 KToolBar *tb = toolBar( i );
1033 if ( tb )
1034 {
1035 QWidget *w = tb->getButton( itemId( i ) );
1036//US QWhatsThis::remove( w );
1037//US QWhatsThis::add( w, d->whatsThis() );
1038 return;
1039 }
1040}
1041
1042QString KAction::whatsThis() const
1043{
1044 return d->whatsThis();
1045}
1046
1047QString KAction::whatsThisWithIcon() const
1048{
1049 QString text = whatsThis();
1050 if (!d->iconName().isEmpty())
1051 return QString::fromLatin1("<img source=\"small|%1\"> %2").arg(d->iconName() ).arg(text);
1052 return text;
1053}
1054
1055QWidget* KAction::container( int index ) const
1056{
1057 assert( index < containerCount() );
1058 return d->m_containers[ index ].m_container;
1059}
1060
1061KToolBar* KAction::toolBar( int index ) const
1062{
1063//US return dynamic_cast<KToolBar *>( d->m_containers[ index ].m_container );
1064 return (KToolBar *)( d->m_containers[ index ].m_container );
1065}
1066
1067QPopupMenu* KAction::popupMenu( int index ) const
1068{
1069//US return dynamic_cast<QPopupMenu *>( d->m_containers[ index ].m_container );
1070 return (QPopupMenu *)( d->m_containers[ index ].m_container );
1071}
1072
1073QWidget* KAction::representative( int index ) const
1074{
1075 return d->m_containers[ index ].m_representative;
1076}
1077
1078int KAction::itemId( int index ) const
1079{
1080 return d->m_containers[ index ].m_id;
1081}
1082
1083int KAction::containerCount() const
1084{
1085 return d->m_containers.count();
1086}
1087
1088uint KAction::kaccelCount() const
1089{
1090 return d->m_kaccelList.count();
1091}
1092
1093void KAction::addContainer( QWidget* c, int id )
1094{
1095 KActionPrivate::Container p;
1096 p.m_container = c;
1097 p.m_id = id;
1098 d->m_containers.append( p );
1099}
1100
1101void KAction::addContainer( QWidget* c, QWidget* w )
1102{
1103 KActionPrivate::Container p;
1104 p.m_container = c;
1105 p.m_representative = w;
1106 d->m_containers.append( p );
1107}
1108
1109void KAction::activate()
1110{
1111 slotActivated();
1112}
1113
1114void KAction::slotActivated()
1115{
1116 emit activated();
1117}
1118
1119void KAction::slotDestroyed()
1120{
1121 kdDebug(129) << "KAction::slotDestroyed(): this = " << this << ", name = \"" << name() << "\", sender = " << sender() << endl;
1122 const QObject* o = sender();
1123
1124/*
1125
1126
1127 // KDE 4: remove
1128 if ( o == d->m_kaccel )
1129 {
1130 d->m_kaccel = 0;
1131 return;
1132 }
1133 // KDE 4: remove end
1134
1135 for( uint i = 0; i < d->m_kaccelList.count(); i++ )
1136 {
1137 if ( o == d->m_kaccelList[i] )
1138 {
1139 disconnect( d->m_kaccelList[i], SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
1140 d->m_kaccelList.remove( d->m_kaccelList.at( i ) );
1141 return;
1142 }
1143 }
1144*/
1145 int i;
1146 do
1147 {
1148 i = findContainer( static_cast<const QWidget*>( o ) );
1149 if ( i != -1 )
1150 removeContainer( i );
1151 } while ( i != -1 );
1152
1153}
1154
1155int KAction::findContainer( const QWidget* widget ) const
1156{
1157 int pos = 0;
1158 QValueList<KActionPrivate::Container>::ConstIterator it = d->m_containers.begin();
1159 while( it != d->m_containers.end() )
1160 {
1161 if ( (*it).m_representative == widget || (*it).m_container == widget )
1162 return pos;
1163 ++it;
1164 ++pos;
1165 }
1166
1167 return -1;
1168}
1169
1170void KAction::removeContainer( int index )
1171{
1172 int i = 0;
1173 QValueList<KActionPrivate::Container>::Iterator it = d->m_containers.begin();
1174 while( it != d->m_containers.end() )
1175 {
1176 if ( i == index )
1177 {
1178 d->m_containers.remove( it );
1179 return;
1180 }
1181 ++it;
1182 ++i;
1183 }
1184}
1185
1186// FIXME: Remove this (ellis)
1187void KAction::slotKeycodeChanged()
1188{
1189 qDebug("KAction::slotKeycodeChanged() ...44 has top be fixed");
1190/*US
1191 kdDebug(129) << "KAction::slotKeycodeChanged()" << endl; // -- ellis
1192 KAccelAction* pAction = d->m_kaccel->actions().actionPtr(name());
1193 if( pAction )
1194 setShortcut(pAction->shortcut());
1195*/
1196}
1197
1198KActionCollection *KAction::parentCollection() const
1199{
1200 return m_parentCollection;
1201}
1202
1203void KAction::unplugAll()
1204{
1205 while ( containerCount() != 0 )
1206 unplug( container( 0 ) );
1207}
1208
1209void KAction::virtual_hook( int, void* )
1210{ /*BASE::virtual_hook( id, data );*/ }
1211
1212/* vim: et sw=2 ts=2
1213 */
1214
1215//US #include "kaction.moc"