20 #include "kactionselector.h" 23 #include <kiconloader.h> 26 #include <tqapplication.h> 27 #include <tqlistbox.h> 28 #include <tqtoolbutton.h> 32 #include <tqwhatsthis.h> 34 class KActionSelectorPrivate {
36 TQListBox *availableListBox, *selectedListBox;
37 TQToolButton *btnAdd, *btnRemove, *btnUp, *btnDown;
38 TQLabel *lAvailable, *lSelected;
39 bool moveOnDoubleClick, keyboardEnabled;
41 TQString addIcon, removeIcon, upIcon, downIcon;
43 bool showUpDownButtons;
48 KActionSelector::KActionSelector( TQWidget *parent,
const char *name )
49 : TQWidget( parent, name )
51 d =
new KActionSelectorPrivate();
52 d->moveOnDoubleClick =
true;
53 d->keyboardEnabled =
true;
54 d->iconSize = SmallIcon;
55 d->addIcon = TQApplication::reverseLayout()?
"back" :
"forward";
56 d->removeIcon = TQApplication::reverseLayout()?
"forward" :
"back";
59 d->availableInsertionPolicy = Sorted;
60 d->selectedInsertionPolicy = BelowCurrent;
61 d->showUpDownButtons =
true;
65 TQHBoxLayout *lo =
new TQHBoxLayout(
this );
68 TQVBoxLayout *loAv =
new TQVBoxLayout( lo );
69 d->lAvailable =
new TQLabel( i18n(
"&Available:"),
this );
70 loAv->addWidget( d->lAvailable );
71 d->availableListBox =
new TQListBox(
this );
72 loAv->addWidget( d->availableListBox );
73 d->lAvailable->setBuddy( d->availableListBox );
75 TQVBoxLayout *loHBtns =
new TQVBoxLayout( lo );
76 loHBtns->addStretch( 1 );
77 d->btnAdd =
new TQToolButton(
this );
78 loHBtns->addWidget( d->btnAdd );
79 d->btnRemove =
new TQToolButton(
this );
80 loHBtns->addWidget( d->btnRemove );
81 loHBtns->addStretch( 1 );
83 TQVBoxLayout *loS =
new TQVBoxLayout( lo );
84 d->lSelected =
new TQLabel( i18n(
"&Selected:"),
this );
85 loS->addWidget( d->lSelected );
86 d->selectedListBox =
new TQListBox(
this );
87 loS->addWidget( d->selectedListBox );
88 d->lSelected->setBuddy( d->selectedListBox );
90 TQVBoxLayout *loVBtns =
new TQVBoxLayout( lo );
91 loVBtns->addStretch( 1 );
92 d->btnUp =
new TQToolButton(
this );
93 d->btnUp->setAutoRepeat(
true );
94 loVBtns->addWidget( d->btnUp );
95 d->btnDown =
new TQToolButton(
this );
96 d->btnDown->setAutoRepeat(
true );
97 loVBtns->addWidget( d->btnDown );
98 loVBtns->addStretch( 1 );
102 connect( d->btnAdd, TQT_SIGNAL(clicked()),
this, TQT_SLOT(buttonAddClicked()) );
103 connect( d->btnRemove, TQT_SIGNAL(clicked()),
this, TQT_SLOT(buttonRemoveClicked()) );
104 connect( d->btnUp, TQT_SIGNAL(clicked()),
this, TQT_SLOT(buttonUpClicked()) );
105 connect( d->btnDown, TQT_SIGNAL(clicked()),
this, TQT_SLOT(buttonDownClicked()) );
106 connect( d->availableListBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)),
107 this, TQT_SLOT(itemDoubleClicked(TQListBoxItem*)) );
108 connect( d->selectedListBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)),
109 this, TQT_SLOT(itemDoubleClicked(TQListBoxItem*)) );
110 connect( d->availableListBox, TQT_SIGNAL(currentChanged(TQListBoxItem*)),
111 this, TQT_SLOT(slotCurrentChanged(TQListBoxItem *)) );
112 connect( d->selectedListBox, TQT_SIGNAL(currentChanged(TQListBoxItem*)),
113 this, TQT_SLOT(slotCurrentChanged(TQListBoxItem *)) );
115 d->availableListBox->installEventFilter(
this );
116 d->selectedListBox->installEventFilter(
this );
119 KActionSelector::~KActionSelector()
130 return d->availableListBox;
135 return d->selectedListBox;
144 d->btnAdd->setIconSet( SmallIconSet( icon, d->iconSize ) );
147 d->removeIcon = icon;
148 d->btnRemove->setIconSet( SmallIconSet( icon, d->iconSize ) );
152 d->btnUp->setIconSet( SmallIconSet( icon, d->iconSize ) );
156 d->btnDown->setIconSet( SmallIconSet( icon, d->iconSize ) );
159 kdDebug(13001)<<
"KActionSelector::setButtonIcon: DAINBREAD!"<<
endl;
168 d->btnAdd->setIconSet( iconset );
171 d->btnRemove->setIconSet( iconset );
174 d->btnUp->setIconSet( iconset );
177 d->btnDown->setIconSet( iconset );
180 kdDebug(13001)<<
"KActionSelector::setButtonIconSet: DAINBREAD!"<<
endl;
189 d->btnAdd->setTextLabel( tip );
192 d->btnRemove->setTextLabel( tip );
195 d->btnUp->setTextLabel( tip );
198 d->btnDown->setTextLabel( tip );
201 kdDebug(13001)<<
"KActionSelector::setButtonToolTip: DAINBREAD!"<<
endl;
210 TQWhatsThis::add( d->btnAdd, text );
213 TQWhatsThis::add( d->btnRemove, text );
216 TQWhatsThis::add( d->btnUp, text );
219 TQWhatsThis::add( d->btnDown, text );
222 kdDebug(13001)<<
"KActionSelector::setButtonWhatsThis: DAINBREAD!"<<
endl;
228 d->btnAdd->setEnabled( d->availableListBox->currentItem() > -1 );
229 d->btnRemove->setEnabled( d->selectedListBox->currentItem() > -1 );
230 d->btnUp->setEnabled( d->selectedListBox->currentItem() > 0 );
231 d->btnDown->setEnabled( d->selectedListBox->currentItem() > -1 &&
232 d->selectedListBox->currentItem() < (int)d->selectedListBox->count() - 1 );
241 return d->moveOnDoubleClick;
246 d->moveOnDoubleClick = b;
251 return d->keyboardEnabled;
256 d->keyboardEnabled = b;
261 return d->lAvailable->text();
266 d->lAvailable->setText( text );
271 return d->lSelected->text();
276 d->lSelected->setText( text );
293 return d->availableInsertionPolicy;
298 d->availableInsertionPolicy = p;
303 return d->selectedInsertionPolicy;
308 d->selectedInsertionPolicy = p;
313 return d->showUpDownButtons;
318 d->showUpDownButtons = show;
345 if ( ! d->keyboardEnabled )
return;
346 if ( (e->state() & TQt::ControlButton) )
354 buttonRemoveClicked();
371 if ( d->keyboardEnabled && e->type() == TQEvent::KeyPress )
373 if ( (((TQKeyEvent*)e)->state() & TQt::ControlButton) )
375 switch ( ((TQKeyEvent*)e)->key() )
381 buttonRemoveClicked();
390 return TQWidget::eventFilter( o, e );
395 else if ( o->inherits( TQLISTBOX_OBJECT_NAME_STRING ) )
397 switch ( ((TQKeyEvent*)e)->key() )
401 TQListBox *lb = (TQListBox*)o;
402 int index = lb->currentItem();
403 if ( index < 0 )
break;
404 moveItem( lb->item( index ) );
409 return TQWidget::eventFilter( o, e );
416 void KActionSelector::buttonAddClicked()
419 TQListBoxItem *item = d->availableListBox->firstItem();
421 if ( item->isSelected() ) {
422 d->availableListBox->takeItem( item );
423 d->selectedListBox->insertItem( item, insertionIndex( d->selectedListBox, d->selectedInsertionPolicy ) );
424 d->selectedListBox->setCurrentItem( item );
426 item = d->availableListBox->firstItem();
430 if ( d->selectedInsertionPolicy == Sorted )
431 d->selectedListBox->sort();
432 d->selectedListBox->setFocus();
435 void KActionSelector::buttonRemoveClicked()
438 TQListBoxItem *item = d->selectedListBox->firstItem();
440 if ( item->isSelected() ) {
441 d->selectedListBox->takeItem( item );
442 d->availableListBox->insertItem( item, insertionIndex( d->availableListBox, d->availableInsertionPolicy ) );
443 d->availableListBox->setCurrentItem( item );
444 emit removed( item );
445 item = d->selectedListBox->firstItem();
449 if ( d->availableInsertionPolicy == Sorted )
450 d->availableListBox->sort();
451 d->availableListBox->setFocus();
454 void KActionSelector::buttonUpClicked()
456 int c = d->selectedListBox->currentItem();
458 TQListBoxItem *item = d->selectedListBox->item( c );
459 d->selectedListBox->takeItem( item );
460 d->selectedListBox->insertItem( item, c-1 );
461 d->selectedListBox->setCurrentItem( item );
462 emit movedUp( item );
465 void KActionSelector::buttonDownClicked()
467 int c = d->selectedListBox->currentItem();
468 if ( c < 0 || c ==
int( d->selectedListBox->count() ) - 1 )
return;
469 TQListBoxItem *item = d->selectedListBox->item( c );
470 d->selectedListBox->takeItem( item );
471 d->selectedListBox->insertItem( item, c+1 );
472 d->selectedListBox->setCurrentItem( item );
473 emit movedDown( item );
476 void KActionSelector::itemDoubleClicked( TQListBoxItem *item )
478 if ( d->moveOnDoubleClick )
486 void KActionSelector::loadIcons()
488 d->btnAdd->setIconSet( SmallIconSet( d->addIcon, d->iconSize ) );
489 d->btnRemove->setIconSet( SmallIconSet( d->removeIcon, d->iconSize ) );
490 d->btnUp->setIconSet( SmallIconSet( d->upIcon, d->iconSize ) );
491 d->btnDown->setIconSet( SmallIconSet( d->downIcon, d->iconSize ) );
494 void KActionSelector::moveItem( TQListBoxItem *item )
496 TQListBox *lbFrom = item->listBox();
498 if ( lbFrom == d->availableListBox )
499 lbTo = d->selectedListBox;
500 else if ( lbFrom == d->selectedListBox )
501 lbTo = d->availableListBox;
506 d->availableInsertionPolicy : d->selectedInsertionPolicy;
508 lbFrom->takeItem( item );
509 lbTo->insertItem( item, insertionIndex( lbTo, p ) );
511 lbTo->setCurrentItem( item );
515 if ( lbTo == d->selectedListBox )
518 emit removed( item );
521 int KActionSelector::insertionIndex( TQListBox *lb,
InsertionPolicy policy )
527 index = lb->currentItem();
528 if ( index > -1 ) index += 1;
540 #include "kactionselector.moc" void setButtonsEnabled()
Sets the enabled state of all moving buttons to reflect the current options.
bool moveOnDoubleClick() const
void setAvailableInsertionPolicy(InsertionPolicy policy)
Sets the insertion policy for the available listbox.
InsertionPolicy
This enum defines policies for where to insert moved items in a listbox.
void setButtonTooltip(const TQString &tip, MoveButton button)
Sets the tooltip for the button button to tip.
void polish()
Emitted when an item is moved to the "selected" listbox.
void setSelectedLabel(const TQString &text)
Sets the label for the selected items listbox to text.
void keyPressEvent(TQKeyEvent *)
Reimplamented for internal reasons.
kdbgstream kdDebug(int area=0)
void setAvailableLabel(const TQString &text)
Sets the label for the available items listbox to text.
TQString selectedLabel() const
bool eventFilter(TQObject *, TQEvent *)
Reimplemented for internal reasons.
TQListBox * selectedListBox() const
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
bool keyboardEnabled() const
void setMoveOnDoubleClick(bool enable)
Sets moveOnDoubleClick to enable.
void setKeyboardEnabled(bool enable)
Sets the keyboard enabled depending on enable.
ButtonIconSize
This enum identifies the icon sizes, used for the move buttons.
void setSelectedInsertionPolicy(InsertionPolicy policy)
Sets the insertion policy for the selected listbox.
void setShowUpDownButtons(bool show)
Sets wheather the Up and Down buttons should be displayed according to show.
InsertionPolicy selectedInsertionPolicy() const
InsertionPolicy availableInsertionPolicy() const
TQString availableLabel() const
TQListBox * availableListBox() const
void setButtonIconSet(const TQIconSet &iconset, MoveButton button)
Sets the iconset for button button to iconset.
ButtonIconSize buttonIconSize() const
bool showUpDownButtons() const
void setButtonIconSize(ButtonIconSize size)
Sets the button icon size.
kndbgstream & endl(kndbgstream &s)
void setButtonWhatsThis(const TQString &text, MoveButton button)
Sets the whatsthis help for button button to text.
MoveButton
This enum indentifies the moving buttons.
void setButtonIcon(const TQString &icon, MoveButton button)
Sets the pixmap of the button button to icon.